gpt4 book ai didi

c++ - 旧版 OpenGL 纹理立方体

转载 作者:行者123 更新时间:2023-11-30 01:35:35 25 4
gpt4 key购买 nike

我正在尝试在遗留 OpenGL 中为长方体制作纹理。无论我先渲染长方体的哪一侧,这一侧的纹理都不正确,而所有其他侧的纹理都正确。我尝试先渲染立方体的不同面。

这是绘制立方体的代码,包括它的纹理坐标:

void Objects::cuboidTextured(float width, float height, float depth, float xpos, float ypos, float zpos, float r, float g, float b){
glMatrixMode(GL_MODELVIEW);

glPushMatrix();
glTranslatef(xpos, ypos, zpos);

glColor3f(r, g, b);

glBegin(GL_QUADS);
glVertex3f( width, -height, depth );
glTexCoord2f(1,0);
glVertex3f( width, height, depth );
glTexCoord2f(1,1);
glVertex3f( -width, height, depth );
glTexCoord2f(0,1);
glVertex3f( -width, -height, depth );
glTexCoord2f(0,0);
glEnd();

glBegin(GL_QUADS);
glVertex3f( width, -height, -depth );
glTexCoord2f(1,0);
glVertex3f( width, height, -depth );
glTexCoord2f(1,1);
glVertex3f( width, height, depth );
glTexCoord2f(0,1);
glVertex3f( width, -height, depth );
glTexCoord2f(0,0);
glEnd();

glBegin(GL_QUADS);
glVertex3f( -width, -height, depth );
glTexCoord2f(1,0);
glVertex3f( -width, height, depth );
glTexCoord2f(1,1);
glVertex3f( -width, height, -depth );
glTexCoord2f(0,1);
glVertex3f( -width, -height, -depth );
glTexCoord2f(0,0);
glEnd();

glBegin(GL_QUADS);
glVertex3f( width, height, depth );
glTexCoord2f(1,0);
glVertex3f( width, height, -depth );
glTexCoord2f(1,1);
glVertex3f( -width, height, -depth );
glTexCoord2f(0,1);
glVertex3f( -width, height, depth );
glTexCoord2f(0,0);
glEnd();

glBegin(GL_QUADS);
glVertex3f( width, -height, -depth );
glTexCoord2f(0,1);
glVertex3f( width, -height, depth );
glTexCoord2f(0,0);
glVertex3f( -width, -height, depth );
glTexCoord2f(1,1);
glVertex3f( -width, -height, -depth );
glTexCoord2f(1,0);
glEnd();

glBegin(GL_QUADS);
glVertex3f( -width, -height, -depth);
glTexCoord2f(1,0);
glVertex3f( -width, height, -depth);
glTexCoord2f(1,1);
glVertex3f( width, height, -depth);
glTexCoord2f(0,1);
glVertex3f( width, -height, -depth);
glTexCoord2f(0,0);
glEnd();

glPopMatrix();
}

最佳答案

你在倒着做,在 glTexCoord() 之前调用 glVertex()

glVertex :

glVertex commands are used within glBegin/glEnd pairs to specify point, line, and polygon vertices. The current color, normal, texture coordinates, and fog coordinate are associated with the vertex when glVertex is called.

因此,通过 glTexCoord() 和 friend 设置所有顶点状态,然后调用 glVertex():

glBegin(GL_QUADS);
glTexCoord2f(1,0);
glVertex3f( width, -height, -depth );
glTexCoord2f(1,1);
glVertex3f( width, height, -depth );
glTexCoord2f(0,1);
glVertex3f( width, height, depth );
glTexCoord2f(0,0);
glVertex3f( width, -height, depth );
glEnd();

否则 glVertex() 将使用上次设置的任何随机纹理坐标。

关于c++ - 旧版 OpenGL 纹理立方体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53870903/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com