tomi
tomi

Reputation: 535

glTexImage2D does not render the picture

Why does this not display me the graphic? All I get is a white square. The picture is 512x512.

public void loadGLTexture(GL10 gl) {  

      imageData = ByteBuffer.allocate(mPicture512.length).order(ByteOrder.nativeOrder());
    imageData.put(mPicture512);  
    imageData.position(0); 
    // generate one texture pointer 
    gl.glGenTextures(1, textures, 0);
    // ...and bind it to our array 
//Bitmap bitmap = getBitmap(mContext);

//bitmap.getPixels(pixels, offset, stride, x, y, width, height)

    // create nearest filtered texture
    gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR);
    gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);

    gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]); 
    gl.glTexImage2D(GL10.GL_TEXTURE_2D, 0, GL10.GL_RGBA, 512, 512, 0,GL10.GL_RGBA, GL10.GL_BYTE, imageData);

ps. I know I could this the following and it works, but since this is a performance critical app I do not want the overhead of first creating the bitmap.

GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bitmap, 0);

Upvotes: 0

Views: 3342

Answers (3)

tomi
tomi

Reputation: 535

The problem what that I was loading a bitmap PNG which has headers and not a raw picture.

Upvotes: 0

dragostis
dragostis

Reputation: 2662

Why don't you upload the image to OpenGL in a different manner?

BitmapFactory.Options options = new BitmapFactory.Options();
options.inScaled = false;
Bitmap bitmap = BitmapFactory.decodeStream(activity.getAssets().open(fileName));

GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bitmap, 0);

gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR);
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);

Upvotes: 1

Tim
Tim

Reputation: 35923

glTexParameter modifies a per-texture state. Your min/mag filter setup is called before you bind the texture, so it has no effect on your texture.

Therefore your texture is still using the default mipmapping for the min filter, and will not display correctly.

Upvotes: 2

Related Questions