tags:

views:

119

answers:

2

hello i create a cube and want on one side an texture.

glEnable(GL_TEXTURE_2D);
    glBindTexture(GL_TEXTURE_2D, texture[0]);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, filterMode);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, filterMode);

        glBegin(GL_POLYGON);   //Vorderseite
    glColor4f(1.0f,0.0f,0.0f,1.0f); //ROT
    glVertex3f(-fSeitenL/2.0f,-fSeitenL/2.0f,+fSeitenL/2.0f);
    glColor4f(1.0f,1.0f,0.0f,1.0f); //GELB
    glVertex3f(+fSeitenL/2.0f,-fSeitenL/2.0f,+fSeitenL/2.0f);
    glColor4f(1.0f,1.0f,1.0f,1.0f); //WEISS
    glVertex3f(+fSeitenL/2.0f,+fSeitenL/2.0f,+fSeitenL/2.0f);
    glColor4f(1.0f,0.0f,1.0f,1.0f); //MAGENTA
    glVertex3f(-fSeitenL/2.0f,+fSeitenL/2.0f,+fSeitenL/2.0f);
    glEnd();

    glDisable(GL_TEXTURE_2D);

but i can't see my texture, what did i wrong? thanks.

+4  A: 

You haven't supplied texture coordinates. You need to issue one call to glTexCoord (the 2f variant being the most commonly-used) that indicates a part of the texture that the vector maps to, before the corresponding glVertex call.

Otherwise, OpenGL has no idea how the texture should be pasted onto the polygons.

greyfade
+2  A: 

First of all this doesn't seem a cube but just a quad, a cube is made by 6 different quads.. (and you could use GL_QUADS instead that GL_POLYGON.

Second thing is that you are loading the texture but not mapping it to the vertices. You need to supply coordinates to map how the texture should fit onto the quad. You can do it by using

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

the example is taken from NEHE OpenGL guide and I really suggest you to take a look since it's quite well explained: http://nehe.gamedev.net

Check tutorial 6 about texture mapping: http://nehe.gamedev.net/data/lessons/lesson.asp?lesson=06

Jack