PDA

View Full Version : Texture Mapping & Texture Coordinates



senem
09-26-2007, 12:25 PM
I have texture coordinate arrays and vertex arrays. I try to load them in CPP&opengl.
Object is ok geometrically. Texture is .tga file. But there are some problems with texture coordinates, because .tga(512x512) picture is strange on texture. I think there is a problem with texture coordinates.

Texture coordinate array dimension is more than vertex array.

I use following functions.

glTexCoord2f..
glVertex3fv..

Can texture coordinate array dimension be more than vertex? How will I use them? Should vertex array index and texture array index be equal to each other?

Lindley
09-26-2007, 08:38 PM
You can only have one set of texture coordinates per vertex. Anything else wouldn't make sense.

As for specifically which texture coordinates go with which vertices, that's something you'll have to decide based on your use-case.

sharp_pixel
09-27-2007, 01:45 AM
Since the texture lies on a plane, meshes need to be unfolded.

By doing so, the number of geometry faces is the same as the number of texture faces, but the number of texture vertices may be greater than the number of 3D vertices since the mesh is cut.

Since you have the correspondance between texture faces and 3D faces, you can render them using the indices of the texture vertices and of the 3D vertices.

senem
09-27-2007, 02:44 AM
I have texture coordinate array, texture index array, vertex coordinate array, vertex index array already.
I use same index to reach index arrays, and then I reach texture index array and vertex index array and then I reach texture coordinates and vertex coordinate arrays.

But object texture mapping is missing for some part of objects. Some part is OK.

How will I render for fallowing pseudo code and how will I use vertex index and vertex coordinate arrays, texture index and texture coordinate arrays together ?

Pseudo Code
TextureCoordinates [][]=....
TextureIndex=[]
VertexCoordinates[][]=...
VertexIndex=[]

for i..1t o N {
glTexCoord2f(TextureCoordinates[TextureIndex[i]][0],TextureCoordinates[TextureIndex[i]][1]);
glVertex3fv(VertexCoordinates[VertexIndex=[i]];
}

sharp_pixel
09-27-2007, 08:07 AM
You cannot do that.
You need to make the loop over the faces:

Pseudo code for immediate mode:

int va, vb, vc, tca, tcb, tcc;
for (int i = 0; i < nbFaces; ++i) {
getNthFace(i, &amp;va, &amp;vb, &amp;vc); // assuming faces are triangles
getNthTextureFace(i, &amp;tca, &amp;tcb, &amp;tcc); // idem

glTexCoord2f(TextureCoordinates[tca][0], TextureCoordinates[tca][1]);
glVertex3fv(&amp;VertexCoordinates[3*va]);

glTexCoord2f(TextureCoordinates[tcb][0], TextureCoordinates[tcb][1]);
glVertex3fv(&amp;VertexCoordinates[3*vb]);

glTexCoord2f(TextureCoordinates[tcc][0], TextureCoordinates[tcc][1]);
glVertex3fv(&amp;VertexCoordinates[3*vc]);
}

senem
09-27-2007, 10:30 AM
But, if I remove glTexCoord2f, if I don't use texture then object shape is rendered correctly with this code. Just texture doesn't run.

senem
09-28-2007, 02:32 AM
Following code runs correctly. Object is OK.
But, texture is not OK.

VertexCoordinates[][]=...
VertexIndex=[]

for i..1t o N {
glVertex3fv(VertexCoordinates[VertexIndex=[i]];
}

How will texture coordinated be used?

senem
09-28-2007, 02:43 AM
Is it about dimensions of .tga(512*512)?
Because some part of object are covered with the texture. But some part of object is blank.

Maybe object size is big for .tga ???

sharp_pixel
09-28-2007, 03:00 AM
Texture coordinates have nothing to do with the texture size.
Except for GL_TEXTURE_RECTANGLE, they are normalized, meaning they are between 0 and 1, independently of the texture resolution.
The texture could be 512x512, 128x1024, or anything else, the texture coordinates would stay between 0 and 1.

If your rendering work by using the vertex in the order they appear instead of using the faces, you are lucky they appear in the same order as they would by looping over the faces.

senem
09-28-2007, 08:09 AM
I use faces already.
I use vertex indexes and texture indexes to reach vertex and texture coordinates.
Vertex coordinates are OK. But texture is not OK
???

pudman
09-28-2007, 08:35 AM
senem, I'm not sure you are listening. Your code loops over vertices and *not* faces. They are two different things. Your code is not correct, that is why "if I don't use texture then object shape is rendered correctly with this code. Just texture doesn't run."

Change your code. Try sharp_pixel's example.

senem
09-28-2007, 12:39 PM
But I use vertex arrays to reach vertice coordinates.
It is triangle. Three vertex index on vertex array in order defines a face. So I think it should be correct.

If face is different thing, how will I get a face from vertice index array and vertice coordinate array?

Zengar
09-28-2007, 02:59 PM
Maybe you should post your whole code... We are not getting anywhere like this. What format is this model in for example (I don't see you mentioning it)?

senem
09-29-2007, 12:28 AM
I use 3ds max vrml 2.0 coordinates :
Rendering object is correct. But texture mapping runs for some part of object correctly, for some part of object, texture is missing. Some part of object is not covered with texture picture.
There is something wrong matching texture&vertex indexes and coordinates.

???

GLfloat Coordinate [][3] ={{5.09517,8.73457,0},xxx...,{9999,9999,9999}};
short coordIndex []= {6, 1, 0, -1, ...,9999};
short TextureIndex []= {6, 1, 0, -1,...,9999};
GLfloat TextureCoordinate [][2] = {{2,2},
{2,1.975},...,{9999,9999}};
Render :
glBegin (GL_TRIANGLES);
while(coordIndex[intCoordIndex]!=9999)
{
int vi=coordIndex[intCoordIndex];
int ni=normalIndex[intCoordIndex];
int ti=TextureIndex[intCoordIndex];
if (ni!=9999&ni>=0){ glNormal3fv(&Normal[ni][0]);};
if(vi>=0){
glTexCoord2f(TextureCoordinate[ti][0],TextureCoordinate[ti][1]);
glVertex3fv(&Coordinate[vi][0]);
}
++intCoordIndex;
};
glEnd ();
glEndList();

senem
09-29-2007, 06:09 AM
And this code runs for torus, sphere, box objects for texturing. I tried for all of them. Texture is ok for them.
Texture mapping doesn't run for teapot body. (It runs teapot funnel, teapot cover part, but it doesn't run for just teapot body.) But vertex side is correct for all teapot parts. Rendering object is OK. But texturing doesn't OK.
???

Zengar
09-29-2007, 09:15 AM
In this case you teapot model data is probably wrong. Could you try more objects?

senem
09-29-2007, 10:44 AM
I tried lots of objects.
Torus, sphere, box are OK. They are rendered with texture.
I am sure teapot coordinates are OK. Because I tried it much with different teapots.
But teopot and a head model aren't rendered with texture correctly.
Maybe more complicated objects aren't rendered correctly. If the object contains complicated polygons, then it is not rendered with texture.
Without texture, all of them are ok.
I cannot understand relationship between vertex and texture coordinates.
? ? ?

Tin Whisker
09-29-2007, 11:16 AM
I get the feeling senem isn't going to blink anytime soon. :D

Is there really nothing better to discuss than this? I don't know whether I should offer technical advice or youth guidance counseling.

senem
09-29-2007, 11:48 AM
Maybe it is because of texture parameter settings.
Because some parts are rendered with texture correctly. Some parts are marked with lines, some parts are blank.
It depends on object polygon style.

Y-tension
10-01-2007, 04:14 AM
Check for any openGL errors. It might give you a clue to any gl commands that are ignored during rendering.
Don't know if it helps.
Oh, and that glEndList() at the end of your code looks a bit suspicious...

Dat
10-03-2007, 07:50 AM
There's multi-pass rendering and multi-texture rendering.

I think what you want are some multi-pass rendering tutorials, because it doesn't even
look like you're setting up the blending modes correctly, if you're rendering in multiple
passes.