PDA

View Full Version : opengl texturing issues



riche606
08-17-2011, 02:46 PM
im trying to learn opengl and i want to start texturing but i got stuck, ive created a level with 2 rooms and a corridor, so i want diferent textures for different parts (i.e a texture for the floor, texture for the walls and so on) but after looking at the nehe tutorial im still stuck, where do i need to put it in my code, thanks

#include <windows.h>
#include <gl\gl.h>
#include <gl\glut.h>
#include <gl\glu.h> /* GLU extention library */

void init(void);
void display(void);
void keyboard(unsigned char, int, int);
void resize(int, int);


int is_depth; /* depth testing flag */

int main (int argc, char **argv)
{
glutInit(&amp;argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(600, 600);
glutInitWindowPosition(40, 40);
glutCreateWindow("The Cube World");
init();
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);

/* this time we're going to keep the aspect ratio
constant by trapping the window resizes */
glutReshapeFunc(resize);

glutMainLoop();
return 0;
}

void init(void)
{
glClearColor(0.0, 0.0, 0.0, 0.0);
glEnable(GL_DEPTH_TEST);
is_depth = 1;
glMatrixMode(GL_MODELVIEW);
}

void display(void)
{
if (is_depth)
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
else
glClear(GL_COLOR_BUFFER_BIT);

glBegin(GL_QUADS);

//floor
glColor3f(0.2f, 0.2f, 0.2f);
glVertex3f(-50.0, 0.0, -25.0);
glColor3f(0.4f, 0.4f, 0.4f);
glVertex3f(-50.0, 0.0, 25.0);
glColor3f(0.6f, 0.6f, 0.6f);
glVertex3f(50.0, 0.0, 25.0);
glColor3f(0.8f, 0.8f, 0.8f);
glVertex3f(50.0, 0.0, -25.0);

//left wall
glColor3f(0.2f, 0.2f, 0.2f);
glVertex3f(-50.0, 0.0, -25.0);
glColor3f(0.4f, 0.4f, 0.4f);
glVertex3f(-50.0, 00.0, 25.0);
glColor3f(0.6f, 0.6f, 0.6f);
glVertex3f(-50.0, 25.0, 25.0);
glColor3f(0.8f, 0.8f, 0.8f);
glVertex3f(-50.0, 25.0, -25.0);

//right wall
glColor3f(0.2f, 0.2f, 0.2f);
glVertex3f(50.0, 0.0, -25.0);
glColor3f(0.4f, 0.4f, 0.4f);
glVertex3f(50.0, 00.0, 25.0);
glColor3f(0.6f, 0.6f, 0.6f);
glVertex3f(50.0, 25.0, 25.0);
glColor3f(0.8f, 0.8f, 0.8f);
glVertex3f(50.0, 25.0, -25.0);

//roof
glColor3f(0.2f, 0.2f, 0.2f);
glVertex3f(-50.0, 25.0, -25.0);
glColor3f(0.4f, 0.4f, 0.4f);
glVertex3f(-50.0, 25.0, 25.0);
glColor3f(0.6f, 0.6f, 0.6f);
glVertex3f(50.0, 25.0, 25.0);
glColor3f(0.8f, 0.8f, 0.8f);
glVertex3f(50.0, 25.0, -25.0);

//right panel
glColor3f(0.2f, 0.2f, 0.2f);
glVertex3f(10.0, 0.0, -25.0);
glColor3f(0.4f, 0.4f, 0.4f);
glVertex3f(10.0, 15.0, -25.0);
glColor3f(0.6f, 0.6f, 0.6f);
glVertex3f(50.0, 15.0, -25.0);
glColor3f(0.8f, 0.8f, 0.8f);
glVertex3f(50.0, 0.0, -25.0);

//left panel
glColor3f(0.2f, 0.2f, 0.2f);
glVertex3f(-10.0, 0.0, -25.0);
glColor3f(0.4f, 0.4f, 0.4f);
glVertex3f(-10.0, 15.0, -25.0);
glColor3f(0.6f, 0.6f, 0.6f);
glVertex3f(-50.0, 15.0, -25.0);
glColor3f(0.8f, 0.8f, 0.8f);
glVertex3f(-50.0, 0.0, -25.0);

//top panel
glColor3f(0.2f, 0.2f, 0.2f);
//bottom right
glVertex3f(50.0, 15.0, -25.0);
glColor3f(0.4f, 0.4f, 0.4f);
//top right
glVertex3f(50.0, 25.0, -25.0);
glColor3f(0.6f, 0.6f, 0.6f);
//top left
glVertex3f(-50.0, 25.0, -25.0);
glColor3f(0.8f, 0.8f, 0.8f);
//bottom left
glVertex3f(-50.0, 15.0, -25.0);

// corridor floor
glColor3f(0.2f, 0.2f, 0.2f);
glVertex3f(-10.0, 0.0, -25.0);
glColor3f(0.4f, 0.4f, 0.4f);
glVertex3f(-10.0, 0.0, -275.0);
glColor3f(0.6f, 0.6f, 0.6f);
glVertex3f(10.0, 0.0, -275.0);
glColor3f(0.8f, 0.8f, 0.8f);
glVertex3f(10.0, 0.0, -25.0);

// corridor left wall
glColor3f(0.2f, 0.2f, 0.2f);
glVertex3f(-10.0, 0.0, -25.0);
glColor3f(0.4f, 0.4f, 0.4f);
glVertex3f(-10.0, 00.0, -275.0);
glColor3f(0.6f, 0.6f, 0.6f);
glVertex3f(-10.0, 15.0, -275.0);
glColor3f(0.8f, 0.8f, 0.8f);
glVertex3f(-10.0, 15.0, -25.0);

// corridor right wall
glColor3f(0.2f, 0.2f, 0.2f);
glVertex3f(10.0, 0.0, -25.0);
glColor3f(0.4f, 0.4f, 0.4f);
glVertex3f(10.0, 00.0, -275.0);
glColor3f(0.6f, 0.6f, 0.6f);
glVertex3f(10.0, 15.0, -275.0);
glColor3f(0.8f, 0.8f, 0.8f);
glVertex3f(10.0, 15.0, -25.0);

//corridor roof
glColor3f(0.2f, 0.2f, 0.2f);
glVertex3f(-10.0, 15.0, -25.0);
glColor3f(0.4f, 0.4f, 0.4f);
glVertex3f(-10.0, 15.0, -275.0);
glColor3f(0.6f, 0.6f, 0.6f);
glVertex3f(10.0, 15.0, -275.0);
glColor3f(0.8f, 0.8f, 0.8f);
glVertex3f(10.0, 15.0, -25.0);

//right panel room 2
glColor3f(0.2f, 0.2f, 0.2f);
glVertex3f(10.0, 0.0, -275.0);
glColor3f(0.4f, 0.4f, 0.4f);
glVertex3f(10.0, 15.0, -275.0);
glColor3f(0.6f, 0.6f, 0.6f);
glVertex3f(50.0, 15.0, -275.0);
glColor3f(0.8f, 0.8f, 0.8f);
glVertex3f(50.0, 0.0, -275.0);

//left panel room 2
glColor3f(0.2f, 0.2f, 0.2f);
glVertex3f(-10.0, 0.0, -275.0);
glColor3f(0.4f, 0.4f, 0.4f);
glVertex3f(-10.0, 15.0, -275.0);
glColor3f(0.6f, 0.6f, 0.6f);
glVertex3f(-50.0, 15.0, -275.0);
glColor3f(0.8f, 0.8f, 0.8f);
glVertex3f(-50.0, 0.0, -275.0);

//top panel room 2
glColor3f(0.2f, 0.2f, 0.2f);
glVertex3f(50.0, 15.0, -275.0);
glColor3f(0.4f, 0.4f, 0.4f);
glVertex3f(50.0, 25.0, -275.0);
glColor3f(0.6f, 0.6f, 0.6f);
glVertex3f(-50.0, 25.0, -275.0);
glColor3f(0.8f, 0.8f, 0.8f);
glVertex3f(-50.0, 15.0, -275.0);

//right wall room 2
glColor3f(0.2f, 0.2f, 0.2f);
glVertex3f(50.0, 0.0, -275.0);
glColor3f(0.4f, 0.4f, 0.4f);
glVertex3f(50.0, 00.0, -325.0);
glColor3f(0.6f, 0.6f, 0.6f);
glVertex3f(50.0, 25.0, -325.0);
glColor3f(0.8f, 0.8f, 0.8f);
glVertex3f(50.0, 25.0, -275.0);

//left wall room 2
glColor3f(0.2f, 0.2f, 0.2f);
glVertex3f(-50.0, 0.0, -275.0);
glColor3f(0.4f, 0.4f, 0.4f);
glVertex3f(-50.0, 00.0, -325.0);
glColor3f(0.6f, 0.6f, 0.6f);
glVertex3f(-50.0, 25.0, -325.0);
glColor3f(0.8f, 0.8f, 0.8f);
glVertex3f(-50.0, 25.0, -275.0);

//roof room 2
glColor3f(0.2f, 0.2f, 0.2f);
glVertex3f(-50.0, 25.0, -275.0);
glColor3f(0.4f, 0.4f, 0.4f);
glVertex3f(-50.0, 25.0, -325.0);
glColor3f(0.6f, 0.6f, 0.6f);
glVertex3f(50.0, 25.0, -325.0);
glColor3f(0.8f, 0.8f, 0.8f);
glVertex3f(50.0, 25.0, -275.0);

//back wall room 2
glColor3f(0.2f, 0.2f, 0.2f);
glVertex3f(50.0, 0.0, -325.0);
glColor3f(0.4f, 0.4f, 0.4f);
glVertex3f(50.0, 25.0, -325.0);
glColor3f(0.6f, 0.6f, 0.6f);
glVertex3f(-50.0, 25.0, -325.0);
glColor3f(0.8f, 0.8f, 0.8f);
glVertex3f(-50.0, 0.0, -325.0);

//floor room 2
glColor3f(0.2f, 0.2f, 0.2f);
glVertex3f(-50.0, 0.0, -275.0);
glColor3f(0.4f, 0.4f, 0.4f);
glVertex3f(-50.0, 0.0, -325.0);
glColor3f(0.6f, 0.6f, 0.6f);
glVertex3f(50.0, 0.0, -325.0);
glColor3f(0.8f, 0.8f, 0.8f);
glVertex3f(50.0, 0.0, -275.0);

glEnd();


glutSwapBuffers();
}

void keyboard(unsigned char key, int x, int y)
{

switch (key)
{
case 'a':
case 'A':
glTranslatef(5.0, 0.0, 0.0);
break;
case 'd':
case 'D':
glTranslatef(-5.0, 0.0, 0.0);
break;
case 'w':
case 'W':
glTranslatef(0.0, 0.0, 5.0);
break;
case 's':
case 'S':
glTranslatef(0.0, 0.0, -5.0);
break;
case 't':
case 'T':
if (is_depth)
{
is_depth = 0;
glDisable(GL_DEPTH_TEST);
}
else
{
is_depth = 1;
glEnable(GL_DEPTH_TEST);
}
}
display();
}

void resize(int width, int height)
{
if (height == 0) height = 1;

glMatrixMode(GL_PROJECTION);
glLoadIdentity();

/* note we divide our width by our height to get the aspect ratio */
gluPerspective(45.0, width / height, 1.0, 400.0);

/* set initial position */
glTranslatef(0.0, -5.0, -150.0);

glMatrixMode(GL_MODELVIEW);
}

BionicBytes
08-18-2011, 03:39 AM
By inserting texture coordinates and binding the texture you need.
For example:

//floor
glColor3f(0.2f, 0.2f, 0.2f);
glVertex3f(-50.0, 0.0, -25.0);

becomes...


//floor
glBindtexture (GL_TEXTURE_2D, floortexture);
//glColor3f(0.2f, 0.2f, 0.2f);//don't need colours when texturing
glTexCoord2f (0,0); //top left corner
glVertex3f(-50.0, 0.0, -25.0);
......

..so you need to inset the tex coord for each corner of the floor or wall that the vertex represents