PDA

View Full Version : OpenGL room scene help



Nikiben
11-26-2009, 04:13 PM
Please, i'm trying to create a 3D table and a chair in a room, i want to implement the capabilities to switch camera too! please any help would be appreciated.
Here is what i have so far .
Thanks.



#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <GL/glut.h>

void myInit(int *, char **);
void mouseManager(int, int, int, int);
void displayFunc(void);
void idle(void);

GLfloat dx=0.0, dy=0.0, dz=0.0, xAngle = 0.0, yAngle = 0.0, zAngle = 0.0;
int spinning=0, res;

float v[4][3]= {{0.0,0.0,1.0},{0.0,0.9428,-0.3333},
{-0.8165,-0.4714,-0.3333},{0.8165,-0.4714,-0.3333}};

GLfloat ambient[] = {0.2, 0.2, 0.2, 1.0};
GLfloat diffuse[] = {0.9, 0.8, 0.0, 1.0};
GLfloat specular[] = {1.0, 1.0, 1.0, 1.0};

GLfloat global_ambient[] = {0.1, 0.1, 0.1, 1.0};

GLfloat light0_ambient[] = {1.0, 0.0, 0.0, 1.0};
GLfloat light0_diffuse[] = {1.0, 0.0, 0.0, 1.0};
GLfloat light0_specular[] = {1.0, 1.0, 1.0, 1.0};
GLfloat light0_position[] = {1.0, 2.0, 3.0, 1.0};


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

myInit(&amp;argc, argv);

glutDisplayFunc(displayFunc);
glutMouseFunc(mouseManager);
glutIdleFunc(idle);

glutMainLoop(); //infinite loop
}

void myInit(int *argc, char **argv){
glutInit(argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(640,480);
glutInitWindowPosition(100, 150);
glutCreateWindow(argv[0]);

glClearColor(1.0,1.0,1.0,0.0); // Set clear color to white

glLightfv(GL_LIGHT0, GL_AMBIENT, light0_ambient);
glLightfv(GL_LIGHT0, GL_DIFFUSE, light0_diffuse);
glLightfv(GL_LIGHT0, GL_SPECULAR, light0_specular);
glLightfv(GL_LIGHT0, GL_POSITION, light0_position);

glLightModelfv(GL_LIGHT_MODEL_AMBIENT, global_ambient);

glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, ambient);
glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, diffuse);
glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, specular);

glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 100);

glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glShadeModel(GL_FLAT);

glEnable(GL_DEPTH_TEST);
}


void displayFunc(void){
int i;

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(40.0, // field of view
640.0/480.0, // Aspect ratio
1.0, // Near Z
16.0 // Far Z
);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

gluLookAt(0.0, 0.0, 6.0, // Eye is at (0, 0, 6)
0.0, 0.0, 0.0, // center is at (0,0,0)
0.0, 1.0, 0.0); // up is Y direction

glRotatef(xAngle, 1.0, 0.0, 0.0);
glRotatef(yAngle, 0.0, 1.0, 0.0);
glRotatef(zAngle, 0.0, 0.0, 1.0);

glBegin(GL_QUADS); //begin the four sided shape
glColor3f(1.0, 0.0, 1.0);
glVertex3f(-0.9, -0.9, 0.0); //first corner at -0.5, -0.5
glColor3f(0.0, 0.0, 1.0);
glVertex3f(-0.7, 0.7, 0.0); //second corner at -0.5, 0.5
glColor3f(1.0, 1.0, 0.0);
glVertex3f(0.9, 0.9, 0.0); //third corner at 0.5, 0.5
glColor3f(0.0, 1.0, 1.0);
glVertex3f(0.7, -0.7, 0.0); //fourth corner at 0.5, -0.5
glEnd(); //end the shape we are currently working on
glutSwapBuffers();

}

void mouseManager(int button, int state, int x, int y){

switch(button){
case GLUT_LEFT_BUTTON:
if(state==GLUT_DOWN){
dx=0.2;
spinning=1;
}
else{
dx=0.0;
spinning=0;
}
break;

case GLUT_RIGHT_BUTTON:
if(state==GLUT_DOWN){
dy=0.2;
spinning=1;
}
else{
dy=0.0;
spinning=0;
}
break;

case GLUT_MIDDLE_BUTTON:
if(state==GLUT_DOWN){
dz=0.2;
spinning=1;
}
else{
dz=0.0;
spinning=0;
}
break;
defaut:
;
}

displayFunc();
}

void idle(void){

if(spinning){
xAngle+=dx;
yAngle+=dy;
zAngle+=dz;
};

glutPostRedisplay();
}