PDA

View Full Version : environment/cube mapping



nick2price
11-19-2008, 10:42 AM
I have loaded a 3ds max object into my program, and now i would like to try and draw a scene(road, trees etc). I am finding this environment and cube mapping quite difficult to understand. Does anyone have any user friendly examples or can point me to a good tutorial?
thanks

trinitrotoluene
11-19-2008, 10:55 AM
Check this on NVidia web site (http://developer.nvidia.com/object/cube_map_ogl_tutorial.html) for cube map texture. Advice: the developer web site of both major graphics cards company, AMD (http://developer.amd.com/documentation/presentations/Pages/default.aspx) and NVidia (http://developer.nvidia.com/page/home.html) have plenty tutorials of rendering techniques.

nick2price
11-19-2008, 12:26 PM
The problem is that them examples are really complicated for someone new like me to understand. What i am trying to do is like create a room. So i presume this would involve making a cube, placing the camera inside the cube, and mapping the sides of the walls with images. What would be really good if you could tell me what this is. I have seen loads of examples and they all use different names e.g texture mapping, mipmap, environment mapping...

trinitrotoluene
11-19-2008, 12:49 PM
Ok, you want simple 2d texturing not a cube map. Then see the texturing tutorials (http://nehe.gamedev.net/lesson.asp?index=02) at NeHe. After reading the tutorial, my advice is to try to map a custom made 2x2 pixels texture on a simple quad to start. Omitting the texture initialization, for a wall the drawing code should look like:



//Draw a wall of the room
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D,wall1TexId);
glBegin(GL_TRIANGLE_STRIP);
glTexCoord2f(0.0,0.0);
glVertex3fv(bottom_left);
glTexCoord2f(1.0,0.0);
glVertex3fv(bottom_right);
glTexCoord2f(0.0,1.0);
glVertex3fv(top_left);
glTexCoord2f(1.0,1.0);
glVertex3fv(top_right);
glEnd();

//draw next wall
glBindTexture(GL_TEXTURE_2D,wall2TexId);
...
...
glDisable(GL_TEXTURE_2D);


Edit: For texture initialization you need in general these functions, glGenTextures (http://www.opengl.org/sdk/docs/man/xhtml/glGenTextures.xml), glBindTexture (http://www.opengl.org/sdk/docs/man/xhtml/glBindTexture.xml), glTexImage2D (http://www.opengl.org/sdk/docs/man/xhtml/glTexImage2D.xml), glTexParameteri (http://www.opengl.org/sdk/docs/man/xhtml/glTexParameter.xml).

nick2price
11-19-2008, 04:41 PM
I have done somthing which i feel comfortable with. I am not sure if it is ok doing it like this, infact, i dont even know what it is called what i am doing (presume normal texture mapping). I am just getting different texture ids and mapping each one onto a side of a cube. I have done two sides so far and it works fine. Can someone just let me know if the code is ok or if i should be doing it differently.
cheers

Sorry, just noticed few little errors in my code, just egnore these. I have just found out somthing else, either my pictures need flipping 180 degrees anti clockwise or my box does. How would i do this?


void drawCube(GLuint textureId, GLuint textureId1) {
glBegin(GL_QUADS);

//Top face
glColor3f(1.0f, 1.0f, 0.0f);
glNormal3f(0.0, 1.0f, 0.0f);
glVertex3f(-BOX_SIZE / 2, BOX_SIZE / 2, -BOX_SIZE / 2);
glVertex3f(-BOX_SIZE / 2, BOX_SIZE / 2, BOX_SIZE / 2);
glVertex3f(BOX_SIZE / 2, BOX_SIZE / 2, BOX_SIZE / 2);
glVertex3f(BOX_SIZE / 2, BOX_SIZE / 2, -BOX_SIZE / 2);

//Bottom face
glColor3f(1.0f, 0.0f, 1.0f);
glNormal3f(0.0, -1.0f, 0.0f);
glVertex3f(-BOX_SIZE / 2, -BOX_SIZE / 2, -BOX_SIZE / 2);
glVertex3f(BOX_SIZE / 2, -BOX_SIZE / 2, -BOX_SIZE / 2);
glVertex3f(BOX_SIZE / 2, -BOX_SIZE / 2, BOX_SIZE / 2);
glVertex3f(-BOX_SIZE / 2, -BOX_SIZE / 2, BOX_SIZE / 2);

//Left face
glNormal3f(-1.0, 0.0f, 0.0f);
glColor3f(0.0f, 1.0f, 1.0f);
glVertex3f(-BOX_SIZE / 2, -BOX_SIZE / 2, -BOX_SIZE / 2);
glVertex3f(-BOX_SIZE / 2, -BOX_SIZE / 2, BOX_SIZE / 2);
glColor3f(0.0f, 0.0f, 1.0f);
glVertex3f(-BOX_SIZE / 2, BOX_SIZE / 2, BOX_SIZE / 2);
glVertex3f(-BOX_SIZE / 2, BOX_SIZE / 2, -BOX_SIZE / 2);

glEnd();


glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, textureId);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glColor3f(1.0f, 1.0f, 1.0f);
glBegin(GL_QUADS);

//Right face
glNormal3f(1.0, 0.0f, 0.0f);
glTexCoord2f(0.0f, 0.0f);
glVertex3f(BOX_SIZE / 2, -BOX_SIZE / 2, -BOX_SIZE / 2);
glTexCoord2f(1.0f, 0.0f);
glVertex3f(BOX_SIZE / 2, BOX_SIZE / 2, -BOX_SIZE / 2);
glTexCoord2f(1.0f, 1.0f);
glVertex3f(BOX_SIZE / 2, BOX_SIZE / 2, BOX_SIZE / 2);
glTexCoord2f(0.0f, 1.0f);
glVertex3f(BOX_SIZE / 2, -BOX_SIZE / 2, BOX_SIZE / 2);


glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, textureId);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glColor3f(1.0f, 1.0f, 1.0f);
glBegin(GL_QUADS);


//Back face
glNormal3f(0.0, 0.0f, -1.0f);
glTexCoord2f(0.0f, 0.0f);
glVertex3f(-BOX_SIZE / 2, -BOX_SIZE / 2, -BOX_SIZE / 2);
glTexCoord2f(1.0f, 0.0f);
glVertex3f(-BOX_SIZE / 2, BOX_SIZE / 2, -BOX_SIZE / 2);
glTexCoord2f(1.0f, 1.0f);
glVertex3f(BOX_SIZE / 2, BOX_SIZE / 2, -BOX_SIZE / 2);
glTexCoord2f(0.0f, 1.0f);
glVertex3f(BOX_SIZE / 2, -BOX_SIZE / 2, -BOX_SIZE / 2);

glEnd();
glDisable(GL_TEXTURE_2D);
}


GLuint _textureId; //The OpenGL id of the texture
GLuint _textureId1;

GLuint _displayListId; //The OpenGL id of the display list

void handleKeypress(unsigned char key, int x, int y) {
switch (key) {
case 27: //Escape key
exit(0);
}
}

void initRendering() {
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_NORMALIZE);
glEnable(GL_COLOR_MATERIAL);

_textureId = LoadBitmap("vtr.bmp");
_textureId = LoadBitmap("vtr.bmp");

//Set up a display list for drawing a cube
_displayListId = glGenLists(1); //Make room for the display list
glNewList(_displayListId, GL_COMPILE); //Begin the display list
drawCube(_textureId, _textureId1); //Add commands for drawing a cube to the display list
glEndList(); //End the display list
}

trinitrotoluene
11-19-2008, 05:16 PM
I have just found out somthing else, either my pictures need flipping 180 degrees anti clockwise or my box does. How would i do this?. The easiest solution is to change your texture coordinates of your vertex. Other solution are to flip the texture in memory (some texture loading api provide that. Otherwise do it yourself) or the less clever one :


glMatrixMode(GL_TEXTURE);
glLoadIdentity();
glTranslatef(0.5,0.5,0.0);
glRotatef(180.0,0.0,0.0,1.0);
glTranslatef(-0.5,-0.5,0.0);
glMatrixMode(GL_MODELVIEW);

nick2price
11-19-2008, 05:31 PM
Not liking it now, looks very fake. I am going to try somthing different now. So i have a cube displayed on my screen right infront of me. How would i put the camera or viewport inside of the cube?

trinitrotoluene
11-19-2008, 05:43 PM
How would i put the camera or viewport inside of the cube?

At the beginning of your rendering loop call gluLookAt (http://www.opengl.org/sdk/docs/man/xhtml/gluLookAt.xml).



glClear(....)
glLoadItentity();
gluLookAt(...);
...



Not liking it now, looks very fake. To make things look realistic you need a lot of artistic sense (make material look realistic) and a lot of math skill (make things lit realistic).