简体   繁体   中英

OpenGL: Texture loading but not applied

UPDATE


I've stopped using glutSolidCube and started applying the texture manually. 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. 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:

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 )

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. I believe only glutSolidTeapot can be textured in this way? Not sure though.

Anyway, there isn't any way to easily texture glutSolidCube short of autogenerating uv coordinates. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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