Reputation: 11
How can i add a texture to an object in Java Open GL (especially for AndAR)... What's wrong with my code ? I read a few examples but always the same, only a "Black Rectangle" or the texture is bound on the background... How can i bind it to my rectangle ?
Here is my Code:
int[] textureIDs = new int[1];
gl.glEnable(GL10.GL_TEXTURE_2D);
gl.glGenTextures(1, textureIDs, 0);
//load the textures into the graphics memory
Bitmap bm = BitmapFactory.decodeResource(CustomActivity.context.getResources(), R.drawable.icon);
gl.glBindTexture(GL10.GL_TEXTURE_2D, textureIDs[0]);
GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bm,0);
gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR);
gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);
GLUT.glutSolidBox(gl,200.0f,100.0f,10.0f);
Upvotes: 0
Views: 356
Reputation: 45322
For texturinh to have a useful effect, you will need texture coordianates which tell the GL which part of the texture is to be mapped to which parts of the primitives. Since you are using the fixed function pipeline, there are two options:
The GLUT objects never provide any texture coordinates. Which means that OpenGL will use the currently set texture coordinate for every vertex. That will result that just uni specific texture location is sampled over and over again - it doesn't have to be black, but your object will be evenly colored.
You might be tempted to go for option 2 then, automatic texture coordinate generation, which is controlled with the `glTexGen() family of functions. However, the available texture coordinate generation modes are all not suitable for texturing a cube.
SO the only real solution is to specify the cube vertex manually, and specifying useful texture coordinates. You never specified the mapping you wanted. The texture is a rectangular image, you could map it to each of the face, our you could want to have a different sub-rectangle of your image mapped to each side - and you have to tell the GL how to map it, it cannot guess that just because you draw 6 faces and have texturing enabled.
or the texture is bound on the background.
You need to disable texturing again when you want to draw untextured.
Upvotes: 1