Load texture to a cube

Problems with Canvas, OpenGL, etc...

Load texture to a cube

Postby demonofnight » Wed May 18, 2011 6:44 pm

Hi. i'm trying to load some image to my 3d cube.

i'm using this code to load
Code: Select all
Bitmap bmp = BitmapFactory.decodeResource(theResource,R.raw.q);
       ByteBuffer bb = extract(bmp);
       int width = bmp.getWidth();
       int height = bmp.getHeight();
      int[] textures = new int[1];
      gl.glGenTextures(1, textures, 0);
       mTextureId = textures[0];
      
      // ...and bind it to our array
      gl.glBindTexture(GL10.GL_TEXTURE_2D, mTextureId);

      // 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);

      // Different possible texture parameters, e.g. GL10.GL_CLAMP_TO_EDGE
      gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_S,
            GL10.GL_CLAMP_TO_EDGE);
      gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_T,
            GL10.GL_REPEAT);
      gl.glTexImage2D(GL10.GL_TEXTURE_2D, 0, GL10.GL_RGBA,width, height, 0, GL10.GL_RGBA, GL10.GL_UNSIGNED_BYTE, bb);
      // Use the Android GLUtils to specify a two-dimensional texture image
      // from our bitmap
      GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0,bmp, 0);


and this one to draw the cube

Code: Select all
   
   gl.glVertexPointer(3, gl.GL_FIXED, 0, mVertexBuffer);
       gl.glTexCoordPointer(2, GL10.GL_FIXED, 0, mTextureBuffer);

       setCubeTexture(gl);
        gl.glDrawElements(gl.GL_TRIANGLES, 36, gl.GL_UNSIGNED_BYTE, mIndexBuffer);


but my cube is gray, i cannot load the texture properly.


Someone can help me?
demonofnight
Freshman
Freshman
 
Posts: 7
Joined: Wed May 11, 2011 8:28 pm

Top

Re: Load texture to a cube

Postby Sweep88 » Wed May 18, 2011 9:44 pm

So First: If your Result is not the expected Result enable GL Error checking in your GLSurfaceView:
setDebugFlags(DEBUG_CHECK_GL_ERROR);

Maybe your Bitmaps dimensions does not match a power of 2 and your Device doesn't support NPOT Textures, the GL Error Check will show.

And as a Tip: Dont Upload your Bitmap this way:
Code: Select all
gl.glTexImage2D(GL10.GL_TEXTURE_2D, 0, GL10.GL_RGBA,width, height, 0, GL10.GL_RGBA, GL10.GL_UNSIGNED_BYTE, bb);

use GLUtils.texImage2D() to Upload a Bitmap.

And don't forget to enable Texturing by glEnable(GL_TEXTURE_2D).

// Edit:
I see, your texImage2D calls are redundant, use only the GLUtils.texImage2D Call! It internally calls glTexImage2D() and every call of glTexImage2D allocates new Memory on your GPU, if you have to update your textures at a later time only use texSubImage2D calls!
Sweep88
Freshman
Freshman
 
Posts: 5
Joined: Tue Apr 12, 2011 3:23 pm
Location: Germany

Re: Load texture to a cube

Postby demonofnight » Thu May 19, 2011 2:37 pm

still, it just draw some gray cube
demonofnight
Freshman
Freshman
 
Posts: 7
Joined: Wed May 11, 2011 8:28 pm

Top

Return to Android 2D/3D Graphics - OpenGL Problems

Who is online

Users browsing this forum: No registered users and 3 guests