Daniel Bişar
Daniel Bişar

Reputation: 2763

Why does glColorPointer not color the triangle - and other weird things with opengl es

I am not very familiar with OpenGL but I tried to draw a simple triangle on the screen. Each corner colored in a different color. Something like a basic tutorial... The problem is, that

  1. the triangle just is white.
  2. the triangle is only visible in emulator not on my HTC Desire.

My GLView class:

public class GLView extends GLSurfaceView implements GLSurfaceView.Renderer {

private FloatBuffer vertexBuff;
private FloatBuffer colorBuff;

public static FloatBuffer makeFloatBuffer(float[] arr)
{
    ByteBuffer bb = ByteBuffer.allocateDirect(arr.length * 4);
    bb.order(ByteOrder.nativeOrder());
    FloatBuffer fb = bb.asFloatBuffer();
    fb.put(arr);
    fb.position(0);
    return fb;
}

public GLView(Context context) {
    super(context);
    setRenderer(this);
}

@Override
public void onDrawFrame(GL10 gl) {
    gl.glClearColor(0, 0, 0, 0);
    gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
    
    
    gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
    gl.glColorPointer(3, GL10.GL_FLOAT, 0, makeFloatBuffer(new float[] { 1, 0, 0,
                                                                         0, 1, 0,
                                                                         0, 0, 1 }));
    
    gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);       
    gl.glVertexPointer(3, GL10.GL_FLOAT, 0, makeFloatBuffer(new float[] { 0, 0, 0,
                                                                          0, 1, 0,
                                                                          1, 1, 0}));
    
    gl.glDrawArrays(GL10.GL_TRIANGLES, 0, 3);
}

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

@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
    
}
}

Any suggestions?

Upvotes: 0

Views: 5121

Answers (1)

harism
harism

Reputation: 6073

I'm guessing OpenGL state might be somewhat unspecified as you don't provide projection or model view matrices. But I'm guessing mostly as I'm not exactly familiar with OpenGL. Anyway I got your code to work by adding alpha values to color array

    gl.glColorPointer(4, GL10.GL_FLOAT, 0, makeFloatBuffer(new float[] { 1, 0, 0, 1,
                                                                         0, 1, 0, 1,
                                                                         0, 0, 1, 1 }));

And providing mentioned transformation matrices might do the trick on device.

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

        gl.glMatrixMode(GL10.GL_PROJECTION);
        gl.glLoadIdentity();
        GLU.gluOrtho2D(gl, -1.0f, 1.0f, -1.0f, 1.0f);
        gl.glMatrixMode(GL10.GL_MODELVIEW);
        gl.glLoadIdentity();
    }

Upvotes: 3

Related Questions