简体   繁体   English

OpenGL:纹理加载但未应用

[英]OpenGL: Texture loading but not applied

UPDATE 更新


I've stopped using glutSolidCube and started applying the texture manually. 我已经停止使用glutSolidCube并开始手动应用纹理。 Still no avail. 仍然无济于事。 Any ideas, if not, can you point me to an example in which a texture is successfully applied to a cube? 如果没有想法,您能指出一个将纹理成功应用于多维数据集的示例吗?

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

I'm making a simple voxel based game with OpenGL + Glut and I'm having trouble with loading a texture and applying it to a cube. 我正在用OpenGL + Glut制作一个基于体素的简单游戏,但在加载纹理并将其应用于立方体时遇到了麻烦。 The texture is created here: 在这里创建纹理:

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

And applied in the draw method here: 并应用于这里的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);

But when I run the application, I get a solid white cube still. 但是,当我运行该应用程序时,我仍然得到一个纯白色的立方体。


(source: snag.gy ) (来源: snag.gy

Am I missing something very obvious? 我是否缺少一些显而易见的东西?

glutSolidCube does not include uv coordinates, therefore there is no way for OpenGL to know where the texture should be applied. glutSolidCube不包含uv坐标,因此OpenGL无法知道应在何处应用纹理。 I believe only glutSolidTeapot can be textured in this way? 我相信只有glutSolidTeapot可以通过这种方式进行纹理处理吗? Not sure though. 虽然不确定。

Anyway, there isn't any way to easily texture glutSolidCube short of autogenerating uv coordinates. 无论如何,除了自动生成uv坐标外,没有任何方法可以轻松使glutSolidCube贴图。 I'm not sure how to go about doing this, but this link might help. 我不确定如何执行此操作,但是此链接可能会有所帮助。 If you don't want to deal with autogenerating coordinates, than just manually draw your cubes and specify uvs there. 如果您不想处理自动生成的坐标,则只需手动绘制多维数据集并在其中指定uvs。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM