weggo
weggo

Reputation: 134

Texture mapping with openGL

I was texture mapping a primitive, a quad to be exact. I had a problem with the texture being somehow rotated 90 degrees to anticlockwise direction. I thought the problem would be with the loading code of the texture, but turned out it was actually a problem with the draw function. So this was the code which draw the picture erroneously:

glVertex2f(0.0f, 0.0f); glTexCoord2f(0.0f, 1.0f);
glVertex2f(0.5f, 0.0f); glTexCoord2f(1.0f, 1.0f);
glVertex2f(0.5f, 0.5f); glTexCoord2f(1.0f, 0.0f);
glVertex2f(0.0f, 0.5f); glTexCoord2f(0.0f, 0.0f);

and this one draw it just as I intended it to be drawn:

glTexCoord2f(0.0f, 1.0f); glVertex2f(0.0f, 0.0f);
glTexCoord2f(1.0f, 1.0f); glVertex2f(0.5f, 0.0f);
glTexCoord2f(1.0f, 0.0f); glVertex2f(0.5f, 0.5f);
glTexCoord2f(0.0f, 0.0f); glVertex2f(0.0f, 0.5f);

What causes this kind of behaviour? I really didn't think this would have such effects to the drawing.

Upvotes: 0

Views: 3388

Answers (4)

Erwald
Erwald

Reputation: 2216

Probably the best explanation there is online : Texture mapping - Tutorial

And also just to make sure, texture coordinates (texCoor) are as following :

And the order in which they are called matters!

  • (0,0) bottom left corner
  • (0,1) upper left corner
  • (1,0) bottom right corner
  • (1,1) upper right corner

Upvotes: 0

Nicol Bolas
Nicol Bolas

Reputation: 474276

I really didn't think this would have such effects to the drawing.

Think about it. What does glTexCoord do? It specifies the texture coordinate, correct? But the texture coordinate of what?

Yes, you know it specifies the texture coordinate of the next vertex, but OpenGL doesn't know that. All glTexCoord does is set the values you pass it into a piece of memory.

glVertex does something more. It sets the vertex position, but it also tells OpenGL, "Take all of the vertex values I've set so far and render a vertex with it." That's why you can't call glVertex outside of glBegin/glEnd, even though you can do that with glTexCoord, glColor, etc.

So when you do glTexCoord(...); glVertex(...), you're saying "set the current texture coordinate to X, then set the position to Y and render with these values." When you do glVertex(...); glTexCoord(...);, you're saying, "set the position to Y and render with the previously set values, then set the current texture coordinate to X."

It's a little late to be setting the texture coordinate after you've already told OpenGL to render a vertex.

Upvotes: 2

Fred
Fred

Reputation: 4954

OpenGL functions in a state-wise fashion. Many GL function calls serve to change the current state so that when you call some other functions, they can use the current state to do the proper operation.

In your situation, the glVertex2f() call uses the current texture state to define which part of the texture gets mapped on which vertex. In your first series of call, the first call to glVertex2f() would have no previous texture state, so it would probably default to (0.0f, 0.0f), although it could also be undefined behavior. The second call to glVertex2f would then use the state set by your first call to glTexCoord2f(), then the third call to glVertex2f() uses the state set by the second call to glTexCoord2(), and so on.

In the future, make sure to set the proper GL state before you call the functions which use those states, and you should be good to go.

Upvotes: 0

Nathan Monteleone
Nathan Monteleone

Reputation: 5470

The order in which you call glVertex and glTexCoord definitely matters! Whenever you specify vertex attributes like glTexCoord, glColor, etc.. they apply all future vertices that you draw, until you change one of those attributes again. So in the previous example, your first vertex was being drawn with some unspecified previous tex coord, the second vertex with tex coord (0.0, 1.0), etc..

Upvotes: 0

Related Questions