Question

So I'm making an app for Android with OpenGL ES 1.0. My problem is that objects which are drawn using GL_TRIANGLES do not show on Android 3.x nor 4.x but everything works perfectly on 2.x. Is there anything to be enabled or disabled before?

File with 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);
}

}

File with render(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();
 }
 }

I think it's enough. The above code is not a problem. It works (at least on Honeycomb).

Was it helpful?

Solution

I found a solution, the problem was in the model loader because for some reason File or FileInputStream is not null by default in 3.x+ (I optimized the class to use with other java projects).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top