0% found this document useful (0 votes)
63 views5 pages

Texture Mapping

This document defines functions for rendering a 3D kitchen scene using OpenGL and textures. It loads 13 texture files, sets up lighting and material properties, and defines functions for rendering the floor, walls, ceiling and handling user input. The main function initializes the GLUT window and rendering callbacks and enters the main loop.

Uploaded by

Dee Fatah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
63 views5 pages

Texture Mapping

This document defines functions for rendering a 3D kitchen scene using OpenGL and textures. It loads 13 texture files, sets up lighting and material properties, and defines functions for rendering the floor, walls, ceiling and handling user input. The main function initializes the GLUT window and rendering callbacks and enters the main loop.

Uploaded by

Dee Fatah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

#include <GL/glut.

h>
#include <SOIL/SOIL.h>
#include <math.h>

GLfloat lightPosition[] = { 10, 3, 0, 1 };


GLfloat lightDirection[] = { 0, 0, 10, 0 };
GLfloat diffuseLight[] = { 0, 1, 0, 1 };
GLfloat ambientLight[] = { 0.2, 0.2, 0.2, 1 };

float x = 1.0f, z = 5.0f;


float vX = 1.0f, vZ = 0.0f;
float angle = 1.5f;

int windowWidth = 1024;


int windowHeight = 768;

char* textureFiles[13] = { "floortexture.png", "tabletexture.png",


"chairtexture.png", "orange.png", "helptexture.png",
"fridgetexture.png", "oven.png", "yellow.png", "dishwasher.png",
"metallic.png", "cabinet.png", "wood.png", "cabinet2.png" };
GLuint texture[13];

void loadTexture() {
for (int i = 0; i < 13; i++) {
texture[i] = SOIL_load_OGL_texture(textureFiles[i], SOIL_LOAD_RGBA,
SOIL_CREATE_NEW_ID, 0);

glBindTexture(GL_TEXTURE_2D, texture[i]);

glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);


glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
}
}

void init() {
glEnable(GL_TEXTURE_2D);

loadTexture();

glShadeModel(GL_SMOOTH);
glEnable(GL_NORMALIZE);
glEnable(GL_AUTO_NORMAL);
glEnable(GL_DEPTH_TEST);
glEnable(GL_COLOR_MATERIAL);
glClearDepth(1);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glDepthFunc(GL_LEQUAL);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
glClearColor(0, 0, 0, 1);
}

void processSpecialKeys(int key, int xx, int yy) {

float moveFraction = 0.2f;


float angleFraction = 0.1f;

switch (key) {
case GLUT_KEY_LEFT:
angle -= angleFraction;
vX = sin(angle);
vZ = -cos(angle);
break;
case GLUT_KEY_RIGHT:
angle += angleFraction;
vX = sin(angle);
vZ = -cos(angle);
break;
case GLUT_KEY_UP:
x += vX * moveFraction;
z += vZ * moveFraction;
if (x < 1) {
x = 1;
}
if (z < 1) {
z = 1;
}
if (x > 9) {
x = 9;
}
if (z > 9) {
z = 9;
}
break;
case GLUT_KEY_DOWN:
x -= vX * moveFraction;
z -= vZ * moveFraction;
if (x < 1) {
x = 1;
}
if (z < 1) {
z = 1;
}
if (x > 9) {
x = 9;
}
if (z > 9) {
z = 9;
}
break;
}
}

void initLight() {
glLightfv(GL_LIGHT0, GL_AMBIENT, ambientLight);
glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuseLight);
glLightfv(GL_LIGHT0, GL_POSITION, lightPosition);
glLightf(GL_LIGHT0, GL_SPOT_CUTOFF, 180);
glLightf(GL_LIGHT0, GL_SPOT_EXPONENT, 128);
glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, lightDirection);
}

void renderWalls() {
// floor
glBindTexture(GL_TEXTURE_2D, texture[0]);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glBegin(GL_QUADS);

glTexCoord2f(0, 0);
glVertex3f(0.0f, 0.0f, 0.0f);

glTexCoord2f(0, 8);
glVertex3f(0.0f, 0.0f, 10.0f);

glTexCoord2f(8, 8);
glVertex3f(10.0f, 0.0f, 10.0f);

glTexCoord2f(8, 0);
glVertex3f(10.0f, 0.0f, 0.0f);

glEnd();

// walls
glBindTexture(GL_TEXTURE_2D, texture[3]);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

// first wall
glBegin(GL_QUADS);

glTexCoord2f(0, 0);
glVertex3f(0.0f, 0.0f, 0.0f);

glTexCoord2f(0, 1);
glVertex3f(0.0f, 3.0f, 0.0f);

glTexCoord2f(1, 1);
glVertex3f(10.0f, 3.0f, 0.0f);

glTexCoord2f(1, 0);
glVertex3f(10.0f, 0.0f, 0.0f);

glEnd();

// second wall
glBegin(GL_QUADS);

glTexCoord2f(0, 0);
glVertex3f(10.0f, 0.0f, 0.0f);

glTexCoord2f(0, 1);
glVertex3f(10.0f, 3.0f, 0.0f);

glTexCoord2f(1, 1);
glVertex3f(10.0f, 3.0f, 10.0f);

glTexCoord2f(1, 0);
glVertex3f(10.0f, 0.0f, 10.0f);

glEnd();

// third wall
glBegin(GL_QUADS);

glTexCoord2f(0, 0);
glVertex3f(10.0f, 0.0f, 10.0f);

glTexCoord2f(0, 1);
glVertex3f(10.0f, 3.0f, 10.0f);

glTexCoord2f(1, 1);
glVertex3f(0.0f, 3.0f, 10.0f);

glTexCoord2f(1, 0);
glVertex3f(0.0f, 0.0f, 10.0f);

glEnd();

// fourth wall
glBegin(GL_QUADS);

glTexCoord2f(0, 0);
glVertex3f(0.0f, 0.0f, 0.0f);

glTexCoord2f(0, 1);
glVertex3f(0.0f, 3.0f, 0.0f);

glTexCoord2f(1, 1);
glVertex3f(0.0f, 3.0f, 10.0f);

glTexCoord2f(1, 0);
glVertex3f(0.0f, 0.0f, 10.0f);

glEnd();

// ceiling
glBindTexture(GL_TEXTURE_2D, texture[7]);
glBegin(GL_QUADS);

glTexCoord2f(0, 0);
glVertex3f(0.0f, 3.0f, 0.0f);

glTexCoord2f(0, 1);
glVertex3f(10.0f, 3.0f, 0.0f);

glTexCoord2f(1, 1);
glVertex3f(10.0f, 3.0f, 10.0f);

glTexCoord2f(1, 0);
glVertex3f(0.0f, 3.0f, 10.0f);

glEnd();
}

void display() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();

gluLookAt(x, 1.5f, z, x + vX, 1.5f, z + vZ, 0.0f, 1.5f, 0.0f);


initLight();

renderWalls();

glutSwapBuffers();
}

void changeWindowSize(int width, int height) {


if (height == 0) {
height = 1;
}

float ratio = 1.0 * width / height;

windowWidth = width;
windowHeight = height;

glMatrixMode(GL_PROJECTION);
glLoadIdentity();

glViewport(0, 0, width, height);

gluPerspective(45.0f, ratio, 0.1f, 100.0f);

glMatrixMode(GL_MODELVIEW);
}

int main(int argc, char **argv) {


glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowPosition(0, 0);
glutInitWindowSize(windowWidth, windowHeight);
glutCreateWindow("Kitchen");

init();

glutDisplayFunc(display);
glutReshapeFunc(changeWindowSize);
glutIdleFunc(display);
glutSpecialFunc(processSpecialKeys);

glutMainLoop();

return 0;
}

You might also like