0

I'm working on an Android project where I need to display the camera preview using OpenGL. However I'm always getting a steady white picture on one device (Galaxy Nexus) and a black one on another device (HTC One X). The code where I suppose there is the error is listed below.
This first method loadGLTexture is called inside onSurfaceCreated and is followed by other gl initialization.

public void loadGLTexture(GL10 gl, Context context)
{

    // harcoded size
    // int width = 640;
    // int height = 480;

    // I'm dealing with RGB565 image, so 2 byte per pixels.
    image = new byte[640*480*2];

    // native method that take the new frame from the camera buffer
    Native.takeCurrentFrame(image); 

    // generate one texture pointer
    gl.glGenTextures(1, textures, 0);
    gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);

    // set filter parameters
    gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_NEAREST);
    gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);

    // allocate the buffer for glTexImage2D
    byte_buffer = ByteBuffer.allocateDirect(image.length * 2);
    byte_buffer.order(ByteOrder.nativeOrder());
    byte_buffer.put(image);
    byte_buffer.position(0);

    gl.glTexImage2D(GL10.GL_TEXTURE_2D, 0, GL10.GL_RGB, 640, 480, 0, GL10.GL_RGB, GL10.GL_UNSIGNED_SHORT_5_6_5, byte_buffer);
}

This second method draw is called inside onDrawFrame

public void draw(GL10 gl)
{
    // update the texture with new image from camera
    Native.takeCurrentFrame(image);
    byte_buffer.put(image);
    byte_buffer.position(0);

    gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);

    // Point to buffers
    gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
    gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);


    gl.glTexImage2D(GL10.GL_TEXTURE_2D, 0, GL10.GL_RGB, 640, 480, 0, GL10.GL_RGB, GL10.GL_UNSIGNED_SHORT_5_6_5, byte_buffer);
    // Set the face rotation
    gl.glFrontFace(GL10.GL_CW);

    // Point to vertex and coord buffer
    gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
    gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, textureBuffer);

    // Draw the vertices as triangle strip
    gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, vertices.length / 3);

    gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
    gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
}

Thank you for any help!

1 Answer 1

0

ok, I made a little stupid error, the texture was not power of two. Strange enough that, in this case, you don't get any error about the wrong resolution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.