PDA

View Full Version : Texture Mapping



genorade
07-05-2010, 08:39 AM
hello everybody :)

i hope my english is not too bad to understand me :).
I just startet programming with OpenGL. Today i tried to texture a cube. Ive got 6 texture images which i want to put on my cube.

The problem is that always the same texture map is used?


// Texturierung einschalten
glEnable(GL_TEXTURE_2D);

// Texturobjekt aktivieren
glBindTexture(GL_TEXTURE_2D, TEXNUMBER_01);


glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);

glBegin(GL_QUADS);
// Front Face
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f); // Bottom Left Of The Texture and Quad
glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f); // Bottom Right Of The Texture and Quad
glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 1.0f); // Top Right Of The Texture and Quad
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f); // Top Left Of The Texture and Quad
glBindTexture(GL_TEXTURE_2D, TEXNUMBER_04);
// Back Face
glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f); // Bottom Right Of The Texture and Quad
glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f); // Top Right Of The Texture and Quad
glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f); // Top Left Of The Texture and Quad
glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f); // Bottom Left Of The Texture and Quad
// Top Face
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f); // Top Left Of The Texture and Quad
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, 1.0f, 1.0f); // Bottom Left Of The Texture and Quad
glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, 1.0f, 1.0f); // Bottom Right Of The Texture and Quad
glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f); // Top Right Of The Texture and Quad
// Bottom Face
glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, -1.0f, -1.0f); // Top Right Of The Texture and Quad
glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, -1.0f, -1.0f); // Top Left Of The Texture and Quad
glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f); // Bottom Left Of The Texture and Quad
glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f); // Bottom Right Of The Texture and Quad
// Right face
glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f); // Bottom Right Of The Texture and Quad
glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f); // Top Right Of The Texture and Quad
glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 1.0f); // Top Left Of The Texture and Quad
glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f); // Bottom Left Of The Texture and Quad
// Left Face
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f); // Bottom Left Of The Texture and Quad
glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f); // Bottom Right Of The Texture and Quad
glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f); // Top Right Of The Texture and Quad
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f); // Top Left Of The Texture and Quad
glEnd();


After the first front face is drawn i tried to change the texture map with this piece of code:


glBindTexture(GL_TEXTURE_2D, TEXNUMBER_04);


Can anybody help?

:-)

carsten neumann
07-05-2010, 10:36 AM
The set of commands allowed between glBegin() and glEnd() is very limited, basically you are only able to specify vertex properties (glColor(), glNormal(), glTexCoord(), etc.), change material properties (glMaterial()), and call display lists.

If you need to change textures you need to render each face individually, i.e. surround each face with its own glBegin(GL_QUADS), glEnd() calls.
A more efficient way to render your cube would be to store all six texture images into one texture and adjust the tex coordinates for the faces accordingly (of course if you are concerned about performance you should not use immediate mode (i.e. glBegin()/glEnd()) in the first place ;) ).

overlay
07-05-2010, 10:49 AM
For reference, OpenGL 4.0 compatibility profile spec at page 30,
section "2.6.3 GL Commands within Begin/End" gives details about what commands are allowed between glBegin() and glEnd().

"
2.6.3 GL Commands within Begin / End

The only GL commands that are allowed within any Begin / End pairs are the
commands for specifying vertex coordinates, vertex colors, normal coordinates,
texture coordinates, generic vertex attributes, and fog coordinates (Vertex, Color,
SecondaryColor, Index, Normal, TexCoord and MultiTexCoord, VertexAttrib,
FogCoord), the ArrayElement command (see section 2.8), the EvalCoord and
EvalPoint commands (see section 5.1), commands for specifying lighting mate-
rial parameters (Material commands; see section 2.13.2), display list invocation
commands (CallList and CallLists; see section 5.5), and the EdgeFlag command.
Executing any other GL command between the execution of Begin and the corre-
sponding execution of End results in the error INVALID_OPERATION. Executing
Begin after Begin has already been executed but before an End is executed gen-
erates the INVALID_OPERATION error, as does executing End without a previous
corresponding Begin.
Execution of the commands EnableClientState, DisableClientState, Push-
ClientAttrib, PopClientAttrib, ColorPointer, FogCoordPointer, EdgeFlag-
Pointer, IndexPointer, NormalPointer, TexCoordPointer, SecondaryCol-
orPointer, VertexPointer, VertexAttribPointer, ClientActiveTexture, Inter-
leavedArrays, and PixelStore is not allowed within any Begin / End pair, but
an error may or may not be generated if such execution occurs. If an error is not
generated, GL operation is undefined. (These commands are described in sections
2.8, 3.7.1, and chapter 6.)
"



OpenGL 4.0 Compatibility Profile Specification with changes marked (updated March 11, 2010) http://www.opengl.org/registry/doc/glspec40.compatibility.20100311.withchanges.pdf

available from:
http://www.opengl.org/registry/

genorade
07-05-2010, 11:13 AM
oh ok, I understand the problem.
I changed it and it works now :-)

thanks...