2012-06-29 53 views
1

我對渲染器下面的代碼:Android的OpenGL的工作在模擬器,但沒有電話

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); 

    } 
} 

如果我使用從當前代碼它的作品完美的模擬器,但在手機上SurfaceView是空白,我什麼也得不到。 我發現我應該使用GLU.gluLookAt();而不是gl.glLoadIdentity(); 如果我這樣做(在onDrawFrame中註釋的代碼)它的工作原理,我得到的背景,但我的紋理不加載,無論是在手機或模擬器。我有什麼要做,所以我可以加載他們和應用程序工作?

編輯1:我評論GLU.gluPerspective(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 

任何ideeas什麼,這可能意味着?

+1

也許你loadGLTexture()的調用onDrawFrame()嘗試(重新)爲每一幀加載紋理,因此你的記憶變滿了? – Dirk

+0

是的,我也注意到了,而且我在那裏實現了一個布爾值,現在這一切都很好,我正在回答我自己的問題,但您先生,做到了:D –

回答

0

我在onDrawFrame有這個2()函數:

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; 
0

java.lang.RuntimeException: eglSwapBuffers failed: EGL_BAD_ALLOC由於未正確處理glSurfaceView而導致大部分錯誤。

您必須通知glSurfaceView有關當前活動的生命週期。看到這裏: http://developer.android.com/reference/android/opengl/GLSurfaceView.html

此外,你的紋理有多大,你如何確保它在正確的格式?

+0

貼圖格式正確。問題是在onDraw中加載紋理,導致它使用所有的內存和崩潰 –

相關問題