PDA

View Full Version : The texture is not shown on the object



jiaoyang_28
04-30-2012, 12:20 PM
Hi all,

I have an mesh object, and I want to texture it with an customer .rgb figure.

I loaded the figure with the following code:


glBindTexture( GL_TEXTURE_2D, texture );
image = read_texture("figure.rgb", &width, &height, &components);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexImage2D(GL_TEXTURE_2D, 0, components, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE,image);


Then while drawing the mesh object, I automatically compute the texture coordinates and draw the mesh as the following code:


glEnable( GL_TEXTURE_2D );
glActiveTexture(GL_TEXTURE0);

glTexGeni(GL_S, GL_TEXTURE_GEN_MODE,GL_NORMAL_MAP);
glTexGeni(GL_T, GL_TEXTURE_GEN_MODE,GL_NORMAL_MAP);
glEnable(GL_TEXTURE_GEN_S); //enable texture coordinate generation
glEnable(GL_TEXTURE_GEN_T);

glBindTexture(GL_TEXTURE_2D, texture);

glBegin(GL_TRIANGLES);
for( i = 0; i < current_gs-> street_light->ntri; i++)
{
for(j = 0; j < 3; j++)
{
//assign the normal of the vertices
//draw (x,y,z) of each vertices
}
}
glEnd();


However, the resulted figure is just black.

May I ask what did I do wrong? Is this the problem of my automatic texture coordinates generation? I tested this .rgb figure on a rectangle, it can shown correctly.

Any reply would be warmly welcomed.

Thank you very much.

BionicBytes
05-01-2012, 05:35 AM
glTexGeni(GL_S, GL_TEXTURE_GEN_MODE,GL_NORMAL_MAP);
glTexGeni(GL_T, GL_TEXTURE_GEN_MODE,GL_NORMAL_MAP);

GL_NORMAL_MAP is most often used for cube mapping. Is that what you were trying to do?
If you were, you need: glTexGeni(GL_R, GL_TEXTURE_GEN_MODE,GL_NORMAL_MAP);glEnable(GL_TEX TURE_GEN_R);

as cube mapping uses 3 texture coordinates. Finally, you will also need to enable cube mapping: glEnable (GL_TEXTURE_CUBE_MAP)

jiaoyang_28
05-01-2012, 10:46 AM
GL_NORMAL_MAP is most often used for cube mapping. Is that what you were trying to do?
If you were, you need: glTexGeni(GL_R, GL_TEXTURE_GEN_MODE,GL_NORMAL_MAP);glEnable(GL_TEX TURE_GEN_R);

as cube mapping uses 3 texture coordinates. Finally, you will also need to enable cube mapping: glEnable (GL_TEXTURE_CUBE_MAP)

What I want to do is texture wrapping. I have an 2D texture, and I want to wrap it onto the object.

Can I use the glTexGeni() here? Or the only thing I can do is to manually assign the texture coordinates?

The object does not have smooth surfaces, I really hope that I can automatically assign the texture coordinates for each vertex. Is there any methods doing this?

Thank you very much.

BionicBytes
05-02-2012, 03:23 AM
Smooth surfaces or not, texture mapping is texture mapping. It relys upon texture coordinates. Either you provde these or allow OpenGL to calculate them - based on some function. If the function does not make sense for the object being rendered, then the results look aweful (as you would expect).
The red Book (Opengl programming guide) 5th ed, version 2, pages 429+ discusses this.
Texture wrapping - you mean computing the texture coordinates automatically?
have you tried different texture generation modes?
gltexGeni (GL_S | GL_T, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR | GL_EYE_LINEAR | GL_SPHERE_MAP | GL_NORMAL_MAP | GL_REFLECTION_MAP)
Depending upon the geometry, one of the first three parameters above could be used with GL_OBJECT_LINEAR being the best default choice.With this command you also use:
gltexGenfv (GL_S | GL_T, GL_OBJECT_PLANE, someplane); where someplane could be someplane[0]={1.0, 0.0, 0.0, 0.0}
Try different plane equations to suit.
You may also need to use:
glTexParameteri (GL_TEXTURE2D, GL_TEXTURE_WRAP_S, GL_REPEAT)
glTexParameteri (GL_TEXTURE2D, GL_TEXTURE_WRAP_T, GL_REPEAT)
to get the best from the auto-gen texture coordinates.

Having said all that, this kind of thing is dead easy in a vertex shader and makes the fixed function thing look rubbish for being so rigid.

jiaoyang_28
05-02-2012, 09:59 AM
Thank you very much.

That do helps me understand how this thing works.