0

所以我正在使用 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 上)。

4

1 回答 1

0

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

于 2013-09-09T11:37:50.327 回答