OpenGL多边形渲染模式和纹理映射

发布于 2024-12-25 21:50:20 字数 2360 浏览 0 评论 0原文

早些时候问了一个关于我的项目的问题,但似乎我对 openGL 的了解比我现在知道的少得多,但我得到的答案都在丰富我的知识,所以提前感谢!

好的,目前我的汽车形状是使用 GL_LINE_LOOP 绘制的,如下所示。 car shape

我想要映射到车身上的图像如下所示。 汽车图片

我知道我的形状图片并不完美,但我试图将图片放入我的线条中这样它至少能给汽车建模留下一些印象。我尝试使用以下代码:

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);
}

这导致了这个可怕的

awful car

两个问题。一是很明显我的纹理坐标确实是错误的,二是我不知道如何阻止多边形模式摆脱轮拱:/将不胜感激这两个方面的帮助:)

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.
car shape

The image i want to map onto the body of the car looks like this.
car picture

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

awful car

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

二手情话 2025-01-01 21:50:20

纹理坐标应在 [0, 1] 范围内。

轮拱问题是因为OpenGL只渲染凸多边形。使用曲面细分工具将凹多边形转换为三角形网格。

PS:对于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);
}

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:

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);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文