gpt4 book ai didi

c++ - OpenGL:纹理加载但未应用

转载 作者:太空宇宙 更新时间:2023-11-04 04:44:54 25 4
gpt4 key购买 nike

更新


我已经停止使用 glutSolidCube 并开始手动应用纹理。还是没有用。有什么想法吗?如果没有,你能给我指出一个纹理成功应用于立方体的例子吗?

glRotatef(180, 0.0f, 1.0f, 0.0f);

glBindTexture(GL_TEXTURE_CUBE_MAP, texture);

glTranslatef(pos_x, 0, pos_z);

int size = 1;

// Begin Rending
glBegin(GL_QUADS);

// Face 1
glNormal3f( 0.0f, 0.0f, 1.0f);

glTexCoord2f(0.0f, 0.0f);
glVertex3f( size, size,-size);

glTexCoord2f(1.0f, 0.0f);
glVertex3f(-size, size,-size);

glTexCoord2f(1.0f, 1.0f);
glVertex3f(-size, size, size);

glTexCoord2f(0.0f, 1.0f);
glVertex3f( size, size, size);

// Face 2

glNormal3f( 0.0f, 0.0f,-1.0f);

glTexCoord2f(0.0f, 0.0f);
glVertex3f( size,-size, size);

glTexCoord2f(1.0f, 0.0f);
glVertex3f(-size,-size, size);

glTexCoord2f(1.0f, 1.0f);
glVertex3f(-size,-size,-size);

glTexCoord2f(0.0f, 1.0f);
glVertex3f( size,-size,-size);

// Face 3


glTexCoord2f(0.0f, 0.0f);
glVertex3f( size, size, size);

glTexCoord2f(1.0f, 0.0f);
glVertex3f(-size, size, size);

glTexCoord2f(1.0f, 1.0f);
glVertex3f(-size,-size, size);

glTexCoord2f(0.0f, 1.0f);
glVertex3f( size,-size, size);

// Face 4

glNormal3f( 0.0f,-1.0f, 0.0f);

glTexCoord2f(0.0f, 0.0f);
glVertex3f( size,-size,-size);

glTexCoord2f(1.0f, 0.0f);
glVertex3f(-size,-size,-size);

glTexCoord2f(1.0f, 1.0f);
glVertex3f(-size, size,-size);

glTexCoord2f(0.0f, 1.0f);
glVertex3f( size, size,-size);

// Face 5

glNormal3f( 1.0f, 0.0f, 0.0f);

glTexCoord2f(0.0f, 0.0f);
glVertex3f(-size, size, size);

glTexCoord2f(1.0f, 0.0f);
glVertex3f(-size, size,-size);

glTexCoord2f(1.0f, 1.0f);
glVertex3f(-size,-size,-size);

glTexCoord2f(0.0f, 1.0f);
glVertex3f(-size,-size, size);

// Face 6

glNormal3f( 1.0f, 0.0f, 0.0f);

glTexCoord2f(0.0f, 0.0f);
glVertex3f( size, size,-size);

glTexCoord2f(1.0f, 0.0f);
glVertex3f( size, size, size);

glTexCoord2f(1.0f, 1.0f);
glVertex3f( size,-size, size);

glTexCoord2f(0.0f, 1.0f);
glVertex3f( size,-size,-size);

glEnd();

我正在使用 OpenGL + Glut 制作一个简单的基于体素的游戏,但我在加载纹理并将其应用于立方体时遇到了问题。纹理创建于此:

texture = SOIL_load_OGL_cubemap (
"block.png",
"block.png",
"block.png",
"block.png",
"block.png",
"block.png",
SOIL_LOAD_RGB,
SOIL_CREATE_NEW_ID,
SOIL_FLAG_MIPMAPS
);

并在这里的draw方法中应用:

glEnable(GL_TEXTURE_GEN_S); //enable texture coordinate generation
glEnable(GL_TEXTURE_GEN_T);
glBindTexture(GL_TEXTURE_2D, texture);
glutSolidCube(2);
glDisable(GL_TEXTURE_GEN_S); //enable texture coordinate generation
glDisable(GL_TEXTURE_GEN_T);

但是当我运行应用程序时,我仍然得到一个纯白色的立方体。


(来源:snag.gy)

我是否漏掉了一些非常明显的东西?

最佳答案

glutSolidCube 不包括 uv 坐标,因此 OpenGL 无法知道应该在何处应用纹理。我相信只有 glutSolidTeapot 可以用这种方式制作纹理?虽然不确定。

无论如何,除了自动生成 uv 坐标之外,没有任何方法可以轻松地对 glutSolidCube 进行纹理处理。我不确定该怎么做,但是这 link可能有帮助。如果您不想处理自动生成的坐标,那么只需手动绘制立方体并在那里指定 uvs。

关于c++ - OpenGL:纹理加载但未应用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22233479/

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