我之前问了一个关于我项目的问题,但似乎我现在更多地了解了我对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);
}
导致这种糟糕的结果
两个问题。一个是清晰的我的纹理纹理真的是错的,其次我不知道如何阻止多边形模式摆脱轮拱:/这两个方面的帮助将非常感谢:)
答案 0 :(得分:4)
纹理坐标应在[0,1]范围内。
轮拱问题是因为OpenGL只渲染凸多边形。使用tessellation facilities将凹多边形转换为三角形网格。
P.S。:对于OpenGL的立即模式,我建议你采用一种更容易阅读和维护的不同编码风格:
void drawBody(int textureindex)
{
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, textureLib[textureindex]);
glBegin(GL_POLYGON);
glTexCoord2f( 0.0 , 0.0 ); glVertex2f(-1.0 , 0.0 );
glTexCoord2f(-0.97, 0.0 ); glVertex2f(-1.97, 0.0 );
// Wheel arch
glTexCoord2f(-0.93, 0.3 ); glVertex2f(-1.93, 0.3 );
glTexCoord2f(-0.95, 0.4 ); glVertex2f(-1.95, 0.4 );
glTexCoord2f(-1.2 , 0.6 ); glVertex2f(-2.2 , 0.6 );
glTexCoord2f(-1.6 , 0.6 ); glVertex2f(-2.6 , 0.6 );
glTexCoord2f(-1.82, 0.4 ); glVertex2f(-2.82, 0.4 );
glTexCoord2f(-1.8 , 0.3 ); glVertex2f(-2.8 , 0.3 );
glTexCoord2f(-1.78, 0.0 ); glVertex2f(-2.78, 0.0 );
// Front of car
glTexCoord2f(-2.8 , 0.0 ); glVertex2f(-3.8 , 0.0 );
glTexCoord2f(-2.7 , 0.2 ); glVertex2f(-3.7 , 0.2 );
glTexCoord2f(-2.8 , 0.4 ); glVertex2f(-3.8 , 0.4 );
glTexCoord2f(-1.8 , 0.7 ); glVertex2f(-2.8 , 0.7 );
glTexCoord2f(-1.4 , 0.7 ); glVertex2f(-2.4 , 0.7 );
// Windscreen
glTexCoord2f( 0.0 , 1.0 ); glVertex2f(-1.0 , 1.0 );
glTexCoord2f( 1.5 , 1.05); glVertex2f( 0.5 , 1.05);
glTexCoord2f( 3.3 , 0.8 ); glVertex2f( 2.3 , 0.8 );
// Rear bumper
glTexCoord2f( 3.15, 0.5 ); glVertex2f( 2.15, 0.5 );
glTexCoord2f( 3.15, 0.3 ); glVertex2f( 2.15, 0.3 );
glTexCoord2f( 3.35, 0.3 ); glVertex2f( 2.35, 0.3 );
glTexCoord2f( 3.0 , 0.0 ); glVertex2f( 2.0 , 0.0 );
glTexCoord2f( 2.83, 0.0 ); glVertex2f( 1.83, 0.0 );
// Wheel arch
glTexCoord2f( 2.85, 0.3 ); glVertex2f( 1.85, 0.3 );
glTexCoord2f( 2.86, 0.4 ); glVertex2f( 1.86, 0.4 );
glTexCoord2f( 2.6 , 0.6 ); glVertex2f( 1.6 , 0.6 );
glTexCoord2f( 2.2 , 0.6 ); glVertex2f( 1.2 , 0.6 );
glTexCoord2f( 2.0 , 0.4 ); glVertex2f( 1.0 , 0.4 );
glTexCoord2f( 2.0 , 0.3 ); glVertex2f( 1.0 , 0.3 );
glTexCoord2f( 2.05, 0.0 ); glVertex2f( 1.05, 0.0 );
glEnd();
glDisable(GL_TEXTURE_2D);
}