简体   繁体   English

使用openGL进行纹理映射

[英]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. 我有一个问题,纹理以某种方式旋转90度逆时针方向。 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. 我真的不认为这会对绘图产生这样的影响。

I really didn't think this would have such effects to the drawing. 我真的不认为这会对绘图产生这样的影响。

Think about it. 想一想。 What does glTexCoord do? glTexCoord做什么? 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. 是的, 知道它指定下一个顶点的纹理坐标,但OpenGL不知道。 All glTexCoord does is set the values you pass it into a piece of memory. 所有glTexCoord都会将您传递给它的值设置为一块内存。

glVertex does something more. glVertex做了更多的事情。 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." 它设置顶点位置,但它告诉OpenGL,“获取我到目前为止设置的所有顶点值并用它渲染一个顶点。” That's why you can't call glVertex outside of glBegin/glEnd , even though you can do that with glTexCoord , glColor , etc. 这就是为什么你不能在glBegin/glEnd之外调用glVertex ,即使你可以glTexCoordglColor做到这一点。

So when you do glTexCoord(...); glVertex(...) 所以当你做glTexCoord(...); glVertex(...) glTexCoord(...); glVertex(...) , you're saying "set the current texture coordinate to X, then set the position to Y and render with these values." glTexCoord(...); glVertex(...) ,你说“将当前纹理坐标设置为X,然后将位置设置为Y并使用这些值进行渲染。” When you do glVertex(...); glTexCoord(...); 当你做glVertex(...); glTexCoord(...); 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." ,你说,“将位置设置为Y并使用先前设置的值进行渲染,然后将当前纹理坐标设置为X.”

It's a little late to be setting the texture coordinate after you've already told OpenGL to render a vertex. 在您已经告诉OpenGL渲染顶点之后设置纹理坐标有点晚了。

The order in which you call glVertex and glTexCoord definitely matters! 你调用glVertex和glTexCoord的顺序绝对重要! 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. 每当您指定顶点属性(如glTexCoord,glColor等)时,它们将应用您绘制的所有未来顶点,直到您再次更改其中一个属性。 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.. 所以在前面的例子中,你的第一个顶点是用一些未指定的前一个tex coord绘制的,第二个顶点是tex coord(0.0,1.0)等。

OpenGL functions in a state-wise fashion. OpenGL以状态方式运行。 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. 许多GL函数调用用于更改当前状态,以便在调用其他函数时,它们可以使用当前状态来执行正确的操作。

In your situation, the glVertex2f() call uses the current texture state to define which part of the texture gets mapped on which vertex. 在您的情况下,glVertex2f()调用使用当前纹理状态来定义纹理的哪个部分映射到哪个顶点。 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. 在你的第一个调用系列中,第一次调用glVertex2f()将没有先前的纹理状态,因此它可能默认为(0.0f,0.0f),尽管它也可能是未定义的行为。 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. 然后第二次调用glVertex2f将使用第一次调用glTexCoord2f()时设置的状态,然后第三次调用glVertex2f()使用第二次调用glTexCoord2()时设置的状态,依此类推。

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. 将来,确保在调用使用这些状态的函数之前设置正确的GL状态,你应该好好去。

Probably the best explanation there is online : Texture mapping - Tutorial 可能最好的解释是在线: 纹理映射 - 教程

And also just to make sure, texture coordinates (texCoor) are as following : 而且为了确保,纹理坐标(texCoor)如下:

And the order in which they are called matters! 他们被称为重要的顺序!

  • (0,0) bottom left corner (0,0)左下角
  • (0,1) upper left corner (0,1)左上角
  • (1,0) bottom right corner (1,0)右下角
  • (1,1) upper right corner (1,1)右上角

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM