当我打开opengl调试标志以尝试调试纹理加载问题时,我收到错误。
在Android开发博客上,以下code snippet演示了如何打开调试
//constructor
public MyCustomSurfaceView(Context context) {
super(context);
// Turn on error-checking and logging
setDebugFlags(DEBUG_CHECK_GL_ERROR | DEBUG_LOG_GL_CALLS);
mRenderer = new ClearRenderer();
setRenderer(mRenderer);
}
我正试图在我的活动onCreate函数中直接使用以下相关方法打开调试:
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
glSurfaceView = new GLSurfaceView(this);
glSurfaceView.setDebugFlags(GLSurfaceView.DEBUG_CHECK_GL_ERROR | GLSurfaceView.DEBUG_LOG_GL_CALLS);
glSurfaceView.setRenderer(new GLRenderer(this));
setContentView(glSurfaceView);
}
当行glSurfaceView.setDebugFlags(GLSurfaceView.DEBUG_CHECK_GL_ERROR | GLSurfaceView.DEBUG_LOG_GL_CALLS)
更改为glSurfaceView.setDebugFlags(GLSurfaceView.DEBUG_LOG_GL_CALLS)
时,我的程序会运行。但是当我添加它时,我得到以下错误:
07-16 08:57:56.009: ERROR/AndroidRuntime(14959): FATAL EXCEPTION: GLThread 9
07-16 08:57:56.009: ERROR/AndroidRuntime(14959): android.opengl.GLException: invalid value
07-16 08:57:56.009: ERROR/AndroidRuntime(14959): at android.opengl.GLErrorWrapper.checkError(GLErrorWrapper.java:62)
07-16 08:57:56.009: ERROR/AndroidRuntime(14959): at android.opengl.GLErrorWrapper.glEnable(GLErrorWrapper.java:270)
07-16 08:57:56.009: ERROR/AndroidRuntime(14959): at android.opengl.GLLogWrapper.glEnable(GLLogWrapper.java:1552)
07-16 08:57:56.009: ERROR/AndroidRuntime(14959): at android.com.gl_tutorials.GLRenderer.onSurfaceCreated(GLRenderer.java:64)
07-16 08:57:56.009: ERROR/AndroidRuntime(14959): at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1317)
07-16 08:57:56.009: ERROR/AndroidRuntime(14959): at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1116)
调试数据如下所示:
07-16 08:57:55.979: VERBOSE/GLSurfaceView(14959): glGenTextures(1, [0], 0) returns {
07-16 08:57:55.979: VERBOSE/GLSurfaceView(14959): [0] = 1
07-16 08:57:55.979: VERBOSE/GLSurfaceView(14959): };
07-16 08:57:55.979: VERBOSE/GLSurfaceView(14959): glBindTexture(GL_TEXTURE_2D, 1);
07-16 08:57:55.979: VERBOSE/GLSurfaceView(14959): glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
07-16 08:57:55.979: VERBOSE/GLSurfaceView(14959): glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
07-16 08:57:55.979: VERBOSE/GLSurfaceView(14959): glEnable(GL_TEXTURE_2D);
当glSurfaceView.setDebugFlags(GLSurfaceView.DEBUG_CHECK_GL_ERROR | GLSurfaceView.DEBUG_LOG_GL_CALLS);不存在我的形状显示为绿色方块(它应该是纹理的)。当我添加该行时程序崩溃。我的完整代码如下。
基础活动:
package android.com.gl_tutorials;
import android.app.Activity;
import android.opengl.GLSurfaceView;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;
public class Run extends Activity {
/** the opengl view */
private GLSurfaceView glSurfaceView;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
glSurfaceView = new GLSurfaceView(this);
//the line of death:
glSurfaceView.setDebugFlags(GLSurfaceView.DEBUG_CHECK_GL_ERROR | GLSurfaceView.DEBUG_LOG_GL_CALLS);
glSurfaceView.setRenderer(new GLRenderer(this));
setContentView(glSurfaceView);
}
@Override
protected void onResume(){
super.onResume();
glSurfaceView.onResume();
}
@Override
protected void onPause(){
super.onPause();
glSurfaceView.onPause();
}
}
OpenGL渲染器:
package android.com.gl_tutorials;
import java.nio.FloatBuffer;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
import android.content.Context;
import android.opengl.GLSurfaceView.Renderer;
import android.opengl.GLU;
import android.util.Log;
public class GLRenderer implements Renderer {
private Square square;
private Context context;
public GLRenderer(Context context){
this.context=context;
square = new Square();
}
@Override
public void onDrawFrame(GL10 gl) {
//Log.v("Tutorial","onDrawFrame Triggered");
// TODO Auto-generated method stub
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
gl.glLoadIdentity();
gl.glTranslatef(0.0f, 0.0f, -5.0f);
gl.glTranslatef(1.0f, 1.0f, -1.0f);
square.draw(gl);
}
@Override
public void onSurfaceChanged(GL10 gl, int width, int height) {
Log.v("Tutorial","onSurfaceChanged Triggered");
// TODO Auto-generated method stub
if(height ==0) {
height=1;
}
gl.glViewport(0, 0, width, height);
gl.glMatrixMode(GL10.GL_PROJECTION);
gl.glLoadIdentity();
GLU.gluPerspective(gl, 45.0f, (float)width/(float)height, 0.1f, 100.0f);
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glLoadIdentity();
}
@Override
public void onSurfaceCreated(GL10 gl, EGLConfig arg1) {
Log.v("Tutorial","onSurfaceCreated Triggered");
//Load the texture for the square
square.loadGLTexture(gl, this.context);
gl.glEnable(GL10.GL_TEXTURE_2D); //Enable Texture Mapping
gl.glShadeModel(GL10.GL_SMOOTH); //Enable Smooth Shading
gl.glClearColor(0, 0, 0, 0); //Black Background
gl.glClearDepthf(1.0f); //Depth buffer setup
gl.glEnable(GL10.GL_DEPTH_TEST);
gl.glDepthFunc(GL10.GL_LEQUAL);
// TODO Auto-generated method stub
gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST); //Really nice perspective calculations, uneeded for what I'm doing
}
}
方形物体
package android.com.gl_tutorials;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import javax.microedition.khronos.opengles.GL10;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.opengl.GLUtils;
public class Square {
private FloatBuffer vertexBuffer;
private float vertices[] = { //x,y,z
-1.0f, -1.0f, 0.0f, //V1 - bottom left
-1.0f, 1f, 0.0f, // V2 - top left
1.0f, -1.0f, 0.0f, // V3 bottom right
1.0f, 1.0f, 0.0f //V4 top right
};
private FloatBuffer textureBuffer;
private float texture[] = {
//Mapping coordinates for the vertices
0.0f, 1.0f, //top left (V2)
0.0f, 0.0f, // bottom left (V1)
1.0f, 1.0f, // top right (V4)
1.0f, 0.0f //bottom right (V3)
};
public Square() {
ByteBuffer vertexByteBuffer = ByteBuffer.allocateDirect(vertices.length*4);
vertexByteBuffer.order(ByteOrder.nativeOrder());
vertexBuffer = vertexByteBuffer.asFloatBuffer();
vertexBuffer.put(vertices);
vertexBuffer.position(0);
ByteBuffer byteBuffer = ByteBuffer.allocateDirect(texture.length * 4);
byteBuffer.order(ByteOrder.nativeOrder());
textureBuffer = byteBuffer.asFloatBuffer();
textureBuffer.put(texture);
textureBuffer.position(0);
}
public void draw(GL10 gl) {
// bind the generated texture, make it active
gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);
// Point to our buffers
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
//Set the face rotation
gl.glFrontFace(GL10.GL_CW);
//gl.glColor4f(0.0f, 0.0f, 1.0f, 0.5f);
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, textureBuffer);
// Draw the vertices as triangle strip
gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, vertices.length/3);
//Disable the client state before leaving
gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
}
private int[] textures = new int[1];
public void loadGLTexture(GL10 gl, Context context){
//loading texture
Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.bg_bismarck);
//generate one texture pointer
gl.glGenTextures(1, textures, 0);
// ..and bind it to our array
gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]); //A bound texture is an active texture
// create nearest filtered texture
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_NEAREST); //This is where the scaling algorithms are
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_NEAREST); //This is where the scaling algorithms are
GLUtils.texImage2D(GL10.GL_TEXTURE_2D,0,bitmap,0);
bitmap.recycle();
}
}