2012-06-13 94 views
0

我有一个OpenGl ES 2.0的问题。我已经设计了一个非常简单的框架来帮助我加载OBJ文件,并吸引他们我的Android手机上,但我有2个错误,我一直没能解决:OpenGL ES 2.0没有绘制任何东西

  1. 我的应用程序不显示任何东西,直到我改变方向和活动被刮和重新加载。在重新加载之前,只能看到GLES20.glClear()(我已将其设置为灰色)。

  2. 有时,当我加载OBJ并执行定向技巧时,我的一半面孔缺失,并且看到间隙,但问题可能在于我使用的OBJ文件。


package com.ideas.opengl; 

import java.io.IOException; 
import javax.microedition.khronos.egl.EGLConfig; 
import javax.microedition.khronos.opengles.GL10; 

import android.opengl.GLES20; 
import android.opengl.GLSurfaceView.Renderer; 
import android.opengl.Matrix; 

public class DemoRenderer implements Renderer { 
    public static int mProgramHandle; 
    private int mMVPMatrixHandle; 
    private float[] mModelMatrix = new float[16]; 
    private float[] mViewMatrix = new float[16]; 
    private float[] mProjectionMatrix = new float[16]; 
    private float[] mMVPMatrix = new float[16]; 

    public Object banana; 

    private OpenGLDemoActivity activity; 

    private int angle; 

    public DemoRenderer(OpenGLDemoActivity activity) throws IOException { 
     this.activity = activity; 
    } 



    @Override 
    public void onSurfaceCreated(GL10 gl, EGLConfig config){ 
     GLES20.glClearColor(0.65f, 0.65f, 0.65f, 0.65f); 

     //GLES20.glEnable(GLES20.GL_CULL_FACE); 

     GLES20.glEnable(GLES20.GL_DEPTH_TEST); 

     GLES20.glEnable(GLES20.GL_TEXTURE_2D); 

     Shader shader = null; 
     try { 
      shader = new Shader(activity, "simple"); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     Vector position = new Vector(); 
     Vector speed = new Vector(); 
     Vector accel = new Vector(); 
     Model model = null; 
     try { 
      model = new Model("banana", activity); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

     banana = new Object(position, speed, accel, model); 

     mProgramHandle = shader.getProgramHandle(); 

     final float eyeX = 0.0f; 
     final float eyeY = 0.0f; 
     final float eyeZ = 3.5f; 

     final float lookX = 0.0f; 
     final float lookY = 0.0f; 
     final float lookZ = -5.0f; 

     final float upX = 0.0f; 
     final float upY = 1.0f; 
     final float upZ = 0.0f; 

     Matrix.setLookAtM(mViewMatrix, 0, eyeX, eyeY, eyeZ, lookX, lookY, lookZ, upX, upY, upZ); 

     mMVPMatrixHandle = GLES20.glGetUniformLocation(mProgramHandle, "u_MVPMatrix"); 
    } 

    @Override 
    public void onSurfaceChanged(GL10 gl, int width, int height) { 
     GLES20.glViewport(0, 0, width, height); 

     final float ratio = (float) width/height; 
     final float left = -ratio; 
     final float right = ratio; 
     final float bottom = -1.0f; 
     final float top = 1.0f; 
     final float near = 1.0f; 
     final float far = 10.0f; 

     Matrix.frustumM(mProjectionMatrix, 0, left, right, bottom, top, near, far); 

    } 


    @Override 
    public void onDrawFrame(GL10 gl) {  
     GLES20.glClear(GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT); 

     angle++; 

     Matrix.setIdentityM(mModelMatrix, 0); 
     Matrix.rotateM(mModelMatrix, 0, angle, 0.0f, 1.0f, 1.0f); 

     Matrix.multiplyMM(mMVPMatrix, 0, mViewMatrix, 0, mModelMatrix, 0); 

     Matrix.multiplyMM(mMVPMatrix, 0, mProjectionMatrix, 0, mMVPMatrix, 0); 

     GLES20.glUniformMatrix4fv(mMVPMatrixHandle, 1, false, mMVPMatrix, 0); 

     banana.draw(); 
    } 
} 

`

+1

发布您的代码。 – Tim

+0

没有人甚至对帮助...感兴趣吗? – dragostis

+0

我没有看到任何明显的错误。您是否在代码中使用glGetError?您应该阅读它的工作原理并使用它来检查错误。 – Tim

回答

1

尝试了一些glGetError(),并得到了错误,一个关于一个变量,它有它的价值不变,另一个是glGetAttributeLocation()返回-1如果该属性未在顶点着色器中使用。 Thaks寻求帮助!

1

与几何问题是可能是由于使用输入和输出相同的浮阵列中的矩阵乘法: Matrix.multiplyMM(mMVPMatrix, 0, mProjectionMatrix, 0, mMVPMatrix, 0); 按照multiplyMM()SPECS输入和输出阵列可以不具有重叠的元素,但使用相同的阵列开始mMVPMatrix用相同的索引0的一种方法来解决这个问题是使用另一个矩阵作为用于最后乘法结果的缓冲器,所以不是:

Matrix.multiplyMM(mMVPMatrix, 0, mProjectionMatrix, 0, mMVPMatrix, 0); 
GLES20.glUniformMatrix4fv(mMVPMatrixHandle, 1, false, mMVPMatrix, 0); 

使用:

Matrix.multiplyMM(mMatrixFinal, 0, mProjectionMatrix, 0, mMVPMatrix, 0); 
GLES20.glUniformMatrix4fv(mMVPMatrixHandle, 1, false, mMatrixFinal, 0); 
相关问题