Android OpenGL可以在模拟器上工作,但不能在手机上工作
我有以下渲染器代码:Android OpenGL可以在模拟器上工作,但不能在手机上工作,android,opengl-es,surfaceview,glulookat,Android,Opengl Es,Surfaceview,Glulookat,我有以下渲染器代码: package hello.project; 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; public cla
package hello.project;
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;
public class HelloOpenGLES10Renderer implements Renderer {
private Square square;
private Square2 square2;// the square
private Context context;
private Context context2;
public static int w,h;
/** Constructor to set the handed over context */
public HelloOpenGLES10Renderer(Context context) {
this.square = new Square();
this.square2 = new Square2();
this.context=context;
}
public void onDrawFrame(GL10 gl) {
/*Square.loadGLTexture(gl, this.context,Square.getSex());
Square2.loadGLTexture(gl, this.context,Square2.getHair());
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glLoadIdentity();
GLU.gluPerspective(gl, 45.0f, (float)w / (float)h, 0.1f, 100.0f);
GLU.gluLookAt(gl, 0, 1, 5, 0f, 0f, 0f, 0f, 1.0f, 0.0f);
gl.glColor4f(1.0f, 1.0f, 1.0f, 2.0f);
square.draw(gl);
square2.draw(gl);*/
// clear Screen and Depth Buffer
Square.loadGLTexture(gl, this.context,Square.getSex());
Square2.loadGLTexture(gl, this.context,Square2.getHair());
gl.glColor4f(1.0f, 1.0f, 1.0f, 2.0f);
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
// Reset the Modelview Matrix
gl.glLoadIdentity();
// GLU.gluLookAt(gl, 0, 0, 0, 0, 0, 0, 0, 0, 0);
// Drawing
gl.glTranslatef(0.0f, 0.0f, -5.0f); // move 5 units INTO the screen
square.draw(gl);
square2.draw(gl);
}
public void onSurfaceChanged(GL10 gl, int width, int height) {
if(height == 0) { //Prevent A Divide By Zero By
height = 1; //Making Height Equal One
}
w=width;
h=height;
gl.glViewport(0, 0, width, height); //Reset The Current Viewport
gl.glMatrixMode(GL10.GL_PROJECTION); //Select The Projection Matrix
gl.glLoadIdentity(); //Reset The Projection Matrix
//Calculate The Aspect Ratio Of The Window
GLU.gluPerspective(gl, 45.0f, (float)width / (float)height, 0.1f, 100.0f);
gl.glMatrixMode(GL10.GL_MODELVIEW); //Select The Modelview Matrix
gl.glLoadIdentity(); //Reset The Modelview Matrix
}
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
// Load the texture for the square
gl.glEnable(GL10.GL_BLEND);
gl.glEnable(GL10.GL_TEXTURE_2D);
gl.glShadeModel(GL10.GL_FLAT); //Enable Smooth Shading
//gl.glEnable(GL10.GL_ALPHA_TEST);
gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
//gl.glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
Square.loadGLTexture(gl, this.context,Square.getSex());
// gl.glAlphaFunc(GL10.GL_GREATER, 0.5f);
gl.glDisable(GL10.GL_DEPTH_TEST);
gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f); //Black Background
gl.glClearDepthf(1.0f); //Depth Buffer Setup
gl.glDepthFunc(GL10.GL_NEVER); //The Type Of Depth Testing To Do
//Really Nice Perspective Calculations
gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST);
}
}
如果我使用onDrawFrame
中的当前代码,它非常适合模拟器,但是在手机上SurfaceView
是空白的,我什么也得不到。
我在一篇帖子上发现我应该使用GLU.gluLookAt();而不是gl.glLoadIdentity();
如果我这样做(在onDrawFrame中注释的代码),它会工作,我会得到背景,但我的纹理不会加载,无论是在手机上还是在模拟器上。我必须做什么才能加载它们和应用程序才能工作
编辑1:我评论了GLU.GLU透视图(gl,45.0f,(float)w/(float)h,0.1f,100.0f)代码>行,现在它显示我的纹理,但程序强制在几秒钟后关闭
我得到下一个LogCat错误:
06-29 14:28:07.126: E/Adreno200-EGL(4825): eglLockWindowSurface: failed to map the memory for fd=54 offs=7385088
06-29 14:28:07.126: E/Adreno200-EGL(4825): egliSwapWindowSurface: oglSwapBuffer failed
06-29 14:28:07.126: E/EglHelper(4825): eglSwapBuffers glGetError = %d1285
06-29 14:28:07.126: E/Adreno200-EGL(4825): eglLockWindowSurface: failed to map the memory for fd=54 offs=7385088
06-29 14:28:07.276: W/dalvikvm(4825): threadid=9: thread exiting with uncaught exception (group=0x4001d5a0)
06-29 14:28:07.276: E/AndroidRuntime(4825): FATAL EXCEPTION: GLThread 10
06-29 14:28:07.276: E/AndroidRuntime(4825): java.lang.RuntimeException: eglSwapBuffers failed: EGL_BAD_ALLOC
06-29 14:28:07.276: E/AndroidRuntime(4825): at android.opengl.GLSurfaceView$EglHelper.throwEglException(GLSurfaceView.java:1084)
06-29 14:28:07.276: E/AndroidRuntime(4825): at android.opengl.GLSurfaceView$EglHelper.swap(GLSurfaceView.java:1042)
06-29 14:28:07.276: E/AndroidRuntime(4825): at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1368)
06-29 14:28:07.276: E/AndroidRuntime(4825): at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1122)
06-29 14:28:09.268: I/Process(4825): Sending signal. PID: 4825 SIG: 9
有什么想法可以解释这意味着什么吗?
主要是由于没有正确处理glSurfaceView
而导致的错误
必须将当前活动生命周期通知glSurfaceView
。请看这里:
另外,您的纹理有多大,如何确保其格式正确?我的onDrawFrame()中有这两个函数:
Square.loadGLTexture(gl, this.context,Square.getSex());
Square2.loadGLTexture(gl, this.context,Square2.getHair());
他们不断地在每一帧上重新加载,直到内存被填满并崩溃。
更改为:
if (Project.ifDraw){
Square.loadGLTexture(gl, this.context,Square.getSex());
Square2.loadGLTexture(gl, this.context,Square2.getHair());
Project.ifDraw=false;
}
在我的专题课上:
public static boolean ifDraw=true;
OnClickListeners实现了:
mGLView.invalidate();
ifDraw=true;
也许你在onDrawFrame()中的loadGLTexture()调用试图(重新)加载每一帧的纹理,这样你的内存就满了?是的,我也注意到了,我在那里实现了一个布尔值,现在一切都好了,我正要回答我自己的问题,但你做到了:D纹理的格式正确。问题是在onDraw中加载纹理导致它使用所有内存并崩溃