OpenGL ES 1.0 GL_TRIANGLES在Android 2.3上运行,但在3.x / 4.x上运行

时间:2013-09-08 19:33:49

标签: android opengl-es geometry android-4.0-ice-cream-sandwich android-2.3-gingerbread

所以我正在使用OpenGL ES 1.0为Android制作应用程序。我的问题是使用GL_TRIANGLES绘制的对象不会在Android 3.x和4.x上显示,但一切都在2.x上完美运行。之前有什么要启用或禁用的吗?

使用GL_TRIANGLES(Model.java)的文件:

public class Model() {

private FloatBuffer vertexBuffer;
private ByteBuffer indexBuffer;
private FloatBuffer colorBuffer;
byte indices[] = {0, 1, 2};
float vertices[];
float colors[] = {1.0f, 1.0f, 1.0f, 1.0f};

public load(int resid, Context context) {
   ModelLoader.load(context, resid);
   vertices = ModelLoader.vertices();
   colors = ModelLoader.colors();
   ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length * 4);
   vbb.order(ByteOrder.nativeOrder());
   vertexBuffer = vbb.asFloatBuffer();
   vertexBuffer.put(vertices);
   vertexBuffer.position(0);
   ByteBuffer cbb = ByteBuffer.allocateDirect(colors.length * 4);
   cbb.order(ByteOrder.nativeOrder());
   colorBuffer = cbb.asFloatBuffer();
   colorBuffer.put(colors);
   colorBuffer.position(0);
   indexBuffer = ByteBuffer.allocateDirect(indices.length);
   indexBuffer.put(indices);
   indexBuffer.position(0);
}

public void draw(GL10 gl) {
   gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
   gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
   gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
   gl.glDrawArrays(GL10.GL_TRIANGLES, 0, vertices.length / 3);
   gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
   gl.glDisableClientState(GL10.GL_COLOR_ARRAY);
}

}

带渲染的文件(GLRender.java):

  public class GLRender extends GLSurfaceView.Renderer {

  public Model sample = new Model();
  public void onSurfaceChanged(GL10 gl, int w, int h) {
    if(h==0) h=1;
    float aspect = (float)w/h;
    gl.glViewport(0, 0, w, h);
    gl.glMatrixMode(GL10.GL_PROJECTION);
    gl.glLoadIdentity();
    GLU.gluPerspective(gl, 45, aspect, 0.1f, 100.0f);
    gl.glMatrixMode(GL10.GL_MODELVIEW);
    gl.glLoadIdentity();
}

public void onSurfaceCreated(GL10 gl, EGLConfig glc) {
    gl.glClearColor(0, 0, 0, 1.0f);
    gl.glClearDepthf(1.0f);
    gl.glEnable(GL10.GL_DEPTH_TEST);
    gl.glDisable(GL10.GL_CULL_FACE);
    gl.glDepthFunc(GL10.GL_LEQUAL);
    gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST);
    gl.glShadeModel(GL10.GL_SMOOTH);
    gl.glDisable(GL10.GL_DITHER);
            gl.glEnable(GL10.GL_LIGHTING);
    gl.glEnable(GL10.GL_LIGHT0);

    gl.glEnable(GL10.GL_COLOR_MATERIAL);
    gl.glEnable(GL10.GL_TEXTURE_2D);
    gl.glEnable(GL10.GL_BLEND);}

 public void onDrawFrame(GL10 gl) {
 gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
 gl.glLoadIdentity();                // Reset model-view matrix ( NEW )
 gl.glMatrixMode(GL10.GL_MODELVIEW);
 gl.glPushMatrix();
 sample.draw(gl);
 gl.glPopMatrix();
 }
 }

我认为这已经足够了。 上面的代码不是问题。它起作用(至少在Honeycomb上)。

1 个答案:

答案 0 :(得分:0)

我找到了一个解决方案,问题出在模型加载器中,因为出于某种原因,File或FileInputStream在3.x +中默认不为null(我优化了该类以用于其他java项目)。