我的代码
/**
* GLExample java */
import android.app.Activity;
import android.opengl.GLSurfaceView;
import android.os.Bundle;
/**
* @author pream
*
*/
public class GLExample extends Activity{
GLSurfaceView oursurface;
/* (non-Javadoc)
* @see android.app.Activity#onCreate(android.os.Bundle)
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
oursurface = new GLSurfaceView(this);
oursurface.setRenderer(new GLRendererEx());
setContentView(oursurface);
}
/* (non-Javadoc)
* @see android.app.Activity#onPause()
*/
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
oursurface.onPause();
}
/* (non-Javadoc)
* @see android.app.Activity#onResume()
*/
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
oursurface.onResume();
}
}
**
GLRendererEX *** /
package net.sourceforge.users.shafiul;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
import android.opengl.GLSurfaceView.Renderer;
import android.opengl.GLU;
public class GLRendererEx implements Renderer {
private GLTriangleEx tri;
public void GLRendererEx() {
tri = new GLTriangleEx();
}
public void onSurfaceCreated(GL10 gl, EGLConfig eglConfig) {
// TODO Auto-generated method stub
gl.glDisable(GL10.GL_DITHER); //boosting performances
gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_FASTEST);
gl.glClearColor(.8f, 0f, .2f, 1f);//red,green&blue,alpha for transparency
gl.glClearDepthf(1f);
//gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
}
public void onDrawFrame(GL10 gl) {
// TODO Auto-generated method stub
gl.glDisable(GL10.GL_DITHER);
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);//masking.handling colour buffer
//camera to look into
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glLoadIdentity();
GLU.gluLookAt(gl, 0, 0, -5, 0, 0, 0, 0, 2, 0);
//GLU.gluLookAt(gl, 0, 0, -23, 0, 0, 0, 0, 2, 0);
//GLU.gluLookAt(gl, 0, 0, -30, 0, 0, 0, 0, 2, 0);
tri.draw(gl);
}
public void onSurfaceChanged(GL10 gl, int width, int height) {
// TODO Auto-generated method stub
//viewport settings
gl.glViewport(0, 0, width, height);
float ratio = (float) width/height;
gl.glMatrixMode(GL10.GL_PROJECTION);
gl.glLoadIdentity();
gl.glFrustumf(-ratio, ratio, -1, 1, 1, 25);
}
}
/**
*
GLTraingle.java
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.nio.ShortBuffer;
import javax.microedition.khronos.opengles.GL10;
/**
* @author pream
*
*/
public class GLTriangleEx {
private float vertices[]={
0f , 1f , //p0
1f ,-1f, //p2
-1f,-1f //p3
};
private FloatBuffer vertBuff;
private short[] pIndex = {0,1,2};
private ShortBuffer pBuff;
public GLTriangleEx()
{
ByteBuffer bBuff = ByteBuffer.allocateDirect(vertices.length*4);//how much
bBuff.order(ByteOrder.nativeOrder());
vertBuff =bBuff.asFloatBuffer();
vertBuff.put(vertices);
vertBuff.position(0);
ByteBuffer pbBuff = ByteBuffer.allocateDirect(pIndex.length*2);
pbBuff.order(ByteOrder.nativeOrder());
pBuff = pbBuff.asShortBuffer();
pBuff.put(pIndex);
pBuff.position(0);
}
public void draw(GL10 gl) {
gl.glFrontFace(GL10.GL_CW); //clockwise
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glVertexPointer(2, GL10.GL_FLOAT, 0, vertBuff);//size-2d object,type,stride,buffr pointer.
gl.glDrawElements(GL10.GL_TRIANGLES, pIndex.length,GL10.GL_UNSIGNED_SHORT, pBuff);// mode,count,type,indices
gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
}
}
这是我的logcat ..请帮助我...
11-28 13:56:22.209: D/dalvikvm(855): GC_FOR_ALLOC freed 42K, 4% free 2549K/2640K, paused 74ms, total 78ms
11-28 13:56:22.219: I/dalvikvm-heap(855): Grow heap (frag case) to 3.451MB for 960016-byte allocation
11-28 13:56:22.309: D/dalvikvm(855): GC_FOR_ALLOC freed <1K, 3% free 3486K/3580K, paused 85ms, total 85ms
11-28 13:56:23.049: D/gralloc_goldfish(855): Emulator without GPU emulation detected.
11-28 13:56:50.079: D/dalvikvm(855): GC_FOR_ALLOC freed 943K, 25% free 3021K/4028K, paused 97ms, total 108ms
11-28 13:56:50.369: I/Choreographer(855): Skipped 30 frames! The application may be doing too much work on its main thread.
11-28 13:56:50.619: I/Choreographer(855): Skipped 149 frames! The application may be doing too much work on its main thread.
11-28 13:56:50.909: I/Choreographer(855): Skipped 63 frames! The application may be doing too much work on its main thread.
11-28 13:56:51.169: I/Choreographer(855): Skipped 69 frames! The application may be doing too much work on its main thread.
11-28 13:56:51.649: I/Choreographer(855): Skipped 49 frames! The application may be doing too much work on its main thread.
11-28 13:56:51.819: I/Choreographer(855): Skipped 48 frames! The application may be doing too much work on its main thread.
11-28 13:56:51.919: I/Choreographer(855): Skipped 49 frames! The application may be doing too much work on its main thread.
11-28 13:57:07.889: D/dalvikvm(855): GC_FOR_ALLOC freed 11K, 13% free 3506K/4028K, paused 149ms, total 168ms
11-28 13:57:08.059: D/dalvikvm(855): GC_FOR_ALLOC freed 444K, 15% free 3463K/4028K, paused 98ms, total 99ms
11-28 13:57:08.099: I/Choreographer(855): Skipped 298 frames! The application may be doing too much work on its main thread.
11-28 13:57:08.905: I/Choreographer(855): Skipped 43 frames! The application may be doing too much work on its main thread.
11-28 13:57:08.999: I/Choreographer(855): Skipped 49 frames! The application may be doing too much work on its main thread.
11-28 13:57:09.338: I/Choreographer(855): Skipped 44 frames! The application may be doing too much work on its main thread.
11-28 13:57:09.479: I/Choreographer(855): Skipped 49 frames! The application may be doing too much work on its main thread.
11-28 13:57:09.599: I/Choreographer(855): Skipped 48 frames! The application may be doing too much work on its main thread.
11-28 13:57:09.689: I/Choreographer(855): Skipped 49 frames! The application may be doing too much work on its main thread.
11-28 13:57:10.119: I/Choreographer(855): Skipped 48 frames! The application may be doing too much work on its main thread.
11-28 13:57:10.209: I/Choreographer(855): Skipped 47 frames! The application may be doing too much work on its main thread.
11-28 13:57:10.289: I/Choreographer(855): Skipped 48 frames! The application may be doing too much work on its main thread.
11-28 13:57:11.139: I/Choreographer(855): Skipped 100 frames! The application may be doing too much work on its main thread.
11-28 13:57:11.159: D/libEGL(855): Emulator without GPU support detected. Fallback to software renderer.
11-28 13:57:11.239: D/libEGL(855): loaded /system/lib/egl/libGLES_android.so
11-28 13:57:11.639: W/dalvikvm(855): threadid=11: thread exiting with uncaught exception (group=0x414c4700)
11-28 13:57:11.639: E/AndroidRuntime(855): FATAL EXCEPTION: GLThread 82
11-28 13:57:11.639: E/AndroidRuntime(855): java.lang.NullPointerException
11-28 13:57:11.639: E/AndroidRuntime(855): at net.sourceforge.users.shafiul.GLRendererEx.onDrawFrame(GLRendererEx.java:54)
11-28 13:57:11.639: E/AndroidRuntime(855): at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1523)
11-28 13:57:11.639: E/AndroidRuntime(855): at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1240)
11-28 13:57:13.439: I/Choreographer(855): Skipped 64 frames! The application may be doing too much work on its main thread.
11-28 13:57:16.929: I/Process(855): Sending signal. PID: 855 SIG: 9
答案 0 :(得分:1)
当您在onDrawFrame(GL10 gl)方法上调用tri
方法时,您的.draw()
对象似乎为null。没有太多其他可能性,这可能会让你在该行中为空。
我不知道您的应用应该做什么,但是从GLRendererEx类的构造函数中删除void
。构造函数无法返回void
。
这就是构造函数的样子:
public GLRendererEx() {
tri = new GLTriangleEx();
}
void关键字实际上阻止了要调用的构造函数,因为它未被识别为构造函数。 构造函数没有明确的返回类型(所以没有void
或其他)。