OpenGL多边形渲染模式和纹理映射
早些时候问了一个关于我的项目的问题,但似乎我对 openGL 的了解比我现在知道的少得多,但我得到的答案都在丰富我的知识,所以提前感谢!
好的,目前我的汽车形状是使用 GL_LINE_LOOP 绘制的,如下所示。
我想要映射到车身上的图像如下所示。
我知道我的形状图片并不完美,但我试图将图片放入我的线条中这样它至少能给汽车建模留下一些印象。我尝试使用以下代码:
void drawBody(int textureindex)
{
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, textureLib[textureindex]);
glBegin(GL_POLYGON);
glTexCoord2f(0.0F, 0.0F);
glVertex2f(-1.0f, 0.0f);
glTexCoord2f(-0.97, 0.0);
glVertex2f(-1.97f, 0.0f);
//wheel arch begin.
glTexCoord2f(-0.93, 0.3);
glVertex2f(-1.93f, 0.3f);
glTexCoord2f(-0.95, 0.4);
glVertex2f(-1.95f, 0.4f);
glTexCoord2f(-1.2, 0.6);
glVertex2f(-2.2f, 0.6f);
glTexCoord2f(-1.6, 0.6);
glVertex2f(-2.6f, 0.6f);
glTexCoord2f(-1.82, 0.4);
glVertex2f(-2.82f, 0.4f);
glTexCoord2f(-1.8, 0.3);
glVertex2f(-2.8f, 0.3f);
glTexCoord2f(-1.78, 0.0);
glVertex2f(-2.78f, 0.0f);
//end wheel arch.
//Front of car.
glTexCoord2f(-2.8, 0.0);
glVertex2f(-3.8f, 0.0f);
glTexCoord2f(-2.7, 0.2);
glVertex2f(-3.7f, 0.2f);
glTexCoord2f(-2.8, 0.4);
glVertex2f(-3.8f, 0.4f);
glTexCoord2f(-1.8, 0.7);
glVertex2f(-2.8f, 0.7f);
glTexCoord2f(-1.4, 0.7);
glVertex2f(-2.4f, 0.7f);
//Windscreen.
glTexCoord2f(0.0, 1.0);
glVertex2f(-1.0f, 1.0f);
glTexCoord2f(1.5, 1.05);
glVertex2f(0.5f, 1.05f);
glTexCoord2f(3.3, 0.8);
glVertex2f(2.3f, 0.8f);
//rear bumper.
glTexCoord2f(3.15, 0.5);
glVertex2f(2.15f, 0.5f);
glTexCoord2f(3.15, 0.3);
glVertex2f(2.15f, 0.3f);
glTexCoord2f(3.35, 0.3);
glVertex2f(2.35f, 0.3f);
glTexCoord2f(3.0, 0.0);
glVertex2f(2.0f, 0.0f);
glTexCoord2f(2.83, 0.0);
glVertex2f(1.83f, 0.0f);
//wheel arch begin.
glTexCoord2f(2.85, 0.3);
glVertex2f(1.85f, 0.3f);
glTexCoord2f(2.86, 0.4);
glVertex2f(1.86f, 0.4f);
glTexCoord2f(2.6, 0.6);
glVertex2f(1.6f, 0.6f);
glTexCoord2f(2.2, 0.6);
glVertex2f(1.2f, 0.6f);
glTexCoord2f(2.0, 0.4);
glVertex2f(1.0f, 0.4f);
glTexCoord2f(2.0, 0.3);
glVertex2f(1.0f, 0.3f);
glTexCoord2f(2.05, 0.0);
glVertex2f(1.05f, 0.0f);
//end wheel arch.
glEnd();
glDisable(GL_TEXTURE_2D);
}
这导致了这个可怕的
两个问题。一是很明显我的纹理坐标确实是错误的,二是我不知道如何阻止多边形模式摆脱轮拱:/将不胜感激这两个方面的帮助:)
asked a question on my project earlier but it seems im learning more about how little i know of openGL than how much i do at the moment, but ever answer i get is feeding my knowledge so thanks in advance!
Ok so currently my car shape is drawn using GL_LINE_LOOP and looks like this.
The image i want to map onto the body of the car looks like this.
I know my shape the picture isnt perfect but im attempting to put that picture within my lines so that it gives at least some impression of the car im modelling. i have attempted this using the following code:
void drawBody(int textureindex)
{
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, textureLib[textureindex]);
glBegin(GL_POLYGON);
glTexCoord2f(0.0F, 0.0F);
glVertex2f(-1.0f, 0.0f);
glTexCoord2f(-0.97, 0.0);
glVertex2f(-1.97f, 0.0f);
//wheel arch begin.
glTexCoord2f(-0.93, 0.3);
glVertex2f(-1.93f, 0.3f);
glTexCoord2f(-0.95, 0.4);
glVertex2f(-1.95f, 0.4f);
glTexCoord2f(-1.2, 0.6);
glVertex2f(-2.2f, 0.6f);
glTexCoord2f(-1.6, 0.6);
glVertex2f(-2.6f, 0.6f);
glTexCoord2f(-1.82, 0.4);
glVertex2f(-2.82f, 0.4f);
glTexCoord2f(-1.8, 0.3);
glVertex2f(-2.8f, 0.3f);
glTexCoord2f(-1.78, 0.0);
glVertex2f(-2.78f, 0.0f);
//end wheel arch.
//Front of car.
glTexCoord2f(-2.8, 0.0);
glVertex2f(-3.8f, 0.0f);
glTexCoord2f(-2.7, 0.2);
glVertex2f(-3.7f, 0.2f);
glTexCoord2f(-2.8, 0.4);
glVertex2f(-3.8f, 0.4f);
glTexCoord2f(-1.8, 0.7);
glVertex2f(-2.8f, 0.7f);
glTexCoord2f(-1.4, 0.7);
glVertex2f(-2.4f, 0.7f);
//Windscreen.
glTexCoord2f(0.0, 1.0);
glVertex2f(-1.0f, 1.0f);
glTexCoord2f(1.5, 1.05);
glVertex2f(0.5f, 1.05f);
glTexCoord2f(3.3, 0.8);
glVertex2f(2.3f, 0.8f);
//rear bumper.
glTexCoord2f(3.15, 0.5);
glVertex2f(2.15f, 0.5f);
glTexCoord2f(3.15, 0.3);
glVertex2f(2.15f, 0.3f);
glTexCoord2f(3.35, 0.3);
glVertex2f(2.35f, 0.3f);
glTexCoord2f(3.0, 0.0);
glVertex2f(2.0f, 0.0f);
glTexCoord2f(2.83, 0.0);
glVertex2f(1.83f, 0.0f);
//wheel arch begin.
glTexCoord2f(2.85, 0.3);
glVertex2f(1.85f, 0.3f);
glTexCoord2f(2.86, 0.4);
glVertex2f(1.86f, 0.4f);
glTexCoord2f(2.6, 0.6);
glVertex2f(1.6f, 0.6f);
glTexCoord2f(2.2, 0.6);
glVertex2f(1.2f, 0.6f);
glTexCoord2f(2.0, 0.4);
glVertex2f(1.0f, 0.4f);
glTexCoord2f(2.0, 0.3);
glVertex2f(1.0f, 0.3f);
glTexCoord2f(2.05, 0.0);
glVertex2f(1.05f, 0.0f);
//end wheel arch.
glEnd();
glDisable(GL_TEXTURE_2D);
}
Which results in this awfullness
Two problems. one is cleary my coords for texturing are really wrong and secondly i dont know how to stop the polygon mode from getting rid of the wheel arches :/ help on these two fronts would be greatly appreciated :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
纹理坐标应在 [0, 1] 范围内。
轮拱问题是因为OpenGL只渲染凸多边形。使用曲面细分工具将凹多边形转换为三角形网格。
PS:对于OpenGL的立即模式,我建议您采用不同的编码风格,这样更容易阅读和维护:
Texture coordinates should be in the range [0, 1].
The wheel arches problem is because OpenGL only renders convex polygons. Use the tessellation facilities to convert concave polygons into triangle meshes.
P.S.: For OpenGL's immediate mode, I suggest you adopt a different coding style that is vastly easier to read and maintain: