Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <GL/glut.h>
- #include <math.h>
- #include <stdio.h>
- #include <stdbool.h>
- #include <string.h>
- //------------------
- // VARIABLES
- //------------------
- //angle of rotation for camera direction
- float angle = 0.0f;
- //the key states, these variables will be 0
- //when no key is being pressed
- float deltaAngle = 0.0f;
- float deltaMove = 0;
- int xOrigin = -1;
- //XZ position of camera
- float x = 0.0f, z = 5.0f;
- //actual vector representing the cameras direction
- float lx = 0.0f, lz = -1.0f;
- float red = 1.0f;
- float blue = 0.5f;
- float green = 0.5f;
- //load textures
- GLuint tableTopTexture;
- GLuint tableLegTexture;
- //------------------
- // TEXTURES
- //------------------
- GLuint LoadTexture(const char * filename){
- GLuint texture;
- int width, height;
- unsigned char * data;
- FILE * file;
- file = fopen(filename, "rb");
- if(file == NULL)
- return 0;
- width = 512;
- height = 1024;
- data = (unsigned char *) malloc (width * height * 3);
- fread(data, width * height * 3, 1, file);
- fclose(file);
- for(int i = 0; i < width * height; ++i){
- int index = i * 3;
- unsigned char B,R;
- B = data[index];
- R = data[index + 2];
- data[index] = R;
- data[index + 2] = B;
- }
- glGenTextures(1, &texture);
- glBindTexture(GL_TEXTURE_2D, texture);
- glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
- glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
- glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
- glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
- glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
- gluBuild2DMipmaps(GL_TEXTURE_2D, 3,width, height, GL_RGB, GL_UNSIGNED_BYTE, data);
- return texture;
- }
- void loadTextures(){
- tableTopTexture = LoadTexture("table_top.bmp");
- tableLegTexture = LoadTexture("table_leg.bmp");
- }
- //------------------
- // DRAWING
- //------------------
- void drawCube(float scale_x, float scale_y, float scale_z, float position_x, float position_y, float position_z) {
- glPushMatrix();
- glTranslatef(position_x,position_y,position_z);
- glScalef(scale_x,scale_y,scale_z);
- glBegin(GL_QUADS);
- //top face
- glColor3f(0,green,0);
- glVertex3f(1.0,1.0,-1.0);
- glVertex3f(-1.0,1.0,-1.0);
- glVertex3f(-1.0,1.0,1.0);
- glVertex3f(1.0,1.0,1.0);
- //bottom face
- glColor3f(1.0f,0.5f,0.0f);
- glVertex3f(1.0,1.0,1.0);
- glVertex3f(-1.0,1.0,1.0);
- glVertex3f(-1.0,-1.0,1.0);
- glVertex3f(1.0,-1.0,1.0);
- //front face
- glColor3f(1.0f,0.0f,0.0f);
- glVertex3f(1.0,1.0,1.0);
- glVertex3f(-1.0,1.0,1.0);
- glVertex3f(-1.0,-1.0,1.0);
- glVertex3f(1.0,-1.0,1.0);
- //back face
- glColor3f(1.0f,1.0f,0.0f);
- glVertex3f(1.0,-1.0,-1.0);
- glVertex3f(-1.0,-1.0,-1.0);
- glVertex3f(-1.0,1.0,-1.0);
- glVertex3f(1.0,1.0,-1.0);
- //left face
- glColor3f(0.0f,0.0f,1.0f);
- glVertex3f(-1.0,1.0,1.0);
- glVertex3f(-1.0,1.0,-1.0);
- glVertex3f(-1.0,-1.0,-1.0);
- glVertex3f(-1.0,-1.0,1.0);
- //right
- glColor3f(1.0f,0.0f,1.0f);
- glVertex3f(1.0,1.0,-1.0);
- glVertex3f(1.0,1.0,1.0);
- glVertex3f(1.0,-1.0,1.0);
- glVertex3f(1.0,-1.0,-1.0);
- glEnd();
- glPopMatrix();
- }
- void drawTable(){
- //texture stuff
- glEnable(GL_TEXTURE_2D);
- glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
- //draw legs
- glBindTexture(GL_TEXTURE_2D, tableLegTexture);
- glTexCoord2f(0.0,0.0);
- drawCube(0.050,0.5,0.060,0,0,0);
- glTexCoord2f(0.0,1.0);
- drawCube(0.050,0.5,0.060,3,0,0);
- glTexCoord2f(1.0,1.0);
- drawCube(0.050,0.5,0.060,3,0,-1);
- glTexCoord2f(1.0,0.0);
- drawCube(0.050,0.5,0.060,0,0,-1);
- //draw table top
- glBindTexture(GL_TEXTURE_2D, tableTopTexture);
- glTexCoord2f(0.0,0.0);
- glTexCoord2f(1.0,0.0);
- glTexCoord2f(1.0,1.0);
- glTexCoord2f(0.0,1.0);
- drawCube(2,0.050,1,1.5,0.5,-0.5);
- glDisable(GL_TEXTURE_2D);
- }
- void drawLight(){
- //draw base
- drawCube(0.30,0.02,0.30,0,0,0);
- //draw center pole
- drawCube(0.02,1,0.02,0,1,0);
- //draw light
- drawCube(0.04,0.50,0.04,0,1.3,0);
- //draw shade
- drawCube(0.20,0.65,0.005,0,1.3,-0.10);
- drawCube(0.005,0.65,0.10,0.20,1.3,0);
- drawCube(0.005,0.65,0.10,-0.20,1.3,0);
- drawCube(0.20,0.65,0.005,0,1.3,0.10);
- }
- void drawChair(){
- //draw legs
- drawCube(0.025,0.70,0.020,0,0.35,0);
- drawCube(0.025,0.35,0.019,0.50,0,0);
- drawCube(0.025,0.70,0.020,0,0.35,-0.45);
- drawCube(0.025,0.35,0.019,0.50,0,-0.45);
- //draw seat
- drawCube(0.27,0.02,0.26,0.27,0.33,-0.22);
- //draw back support
- drawCube(0.025,0.030,0.21,0,1,-0.22);
- drawCube(0.025,0.030,0.21,0,0.75,-0.22);
- }
- void drawWalls(){
- glColor3f(1,0,0);
- glPushMatrix();
- glBegin(GL_QUADS);
- //floor
- glVertex3f(-1,-1,-1);
- glVertex3f(1,-1,-1);
- glVertex3f(1,-1,1);
- glVertex3f(-1,-1,1);
- //ceiling
- glVertex3f(-1,1,-1);
- glVertex3f(1,1,-1);
- glVertex3f(1,1,1);
- glVertex3f(-1,1,1);
- glColor3f(0,1,0);
- //walls
- glVertex3f(-1,-1,1);
- glVertex3f(1,-1,1);
- glVertex3f(1,1,1);
- glVertex3f(-1,1,1);
- glVertex3f(-1,-1,-1);
- glVertex3f(1,-1,-1);
- glVertex3f(1,1,-1);
- glVertex3f(-1,1,-1);
- glVertex3f(1,1,1);
- glVertex3f(1,-1,1);
- glVertex3f(1,-1,-1);
- glVertex3f(1,1,-1);
- glVertex3f(-1,1,1);
- glVertex3f(-1,-1,1);
- glVertex3f(-1,-1,-1);
- glVertex3f(-1,1,-1);
- glEnd();
- glPopMatrix();
- }
- //------------------
- // MOUSE
- //------------------
- void mouseButton(int button, int state, int x, int y){
- //only start motion if the left button is pressed
- if(button == GLUT_LEFT_BUTTON) {
- angle += deltaAngle;
- xOrigin = -1;
- }
- else {
- xOrigin = x;
- }
- }
- void mouseMove(int x, int y){
- //this will only be true when the left button is down
- if(xOrigin >= 0){
- //update deltaAngle
- deltaAngle = (x - xOrigin) * 0.001f;
- //update cameras direction
- lx = sin(angle + deltaAngle);
- lz = -cos(angle + deltaAngle);
- }
- }
- void computePos(float deltaMove){
- x += deltaMove * lx * 0.1f;
- z += deltaMove * lz * 0.1f;
- }
- void computeDir(float deltaAngle){
- angle += deltaAngle;
- lx = sin(angle);
- lz = -cos(angle);
- }
- //------------------
- // KEYBOARD
- //------------------
- void pressKey(int key, int xx, int yy){
- switch(key){
- case GLUT_KEY_LEFT :
- deltaAngle = -0.01f;
- break;
- case GLUT_KEY_RIGHT :
- deltaAngle = 0.01f;
- break;
- case GLUT_KEY_UP :
- deltaMove = 0.5f;
- break;
- case GLUT_KEY_DOWN :
- deltaMove = -0.5f;
- break;
- }
- }
- void releaseKey(int key, int x, int y){
- switch(key){
- case GLUT_KEY_LEFT :
- case GLUT_KEY_RIGHT :
- deltaAngle = 0.0f;
- break;
- case GLUT_KEY_UP :
- case GLUT_KEY_DOWN :
- deltaMove = 0;
- break;
- }
- }
- //------------------
- // RENDERING
- //------------------
- void renderScene(void){
- if(deltaMove)
- computePos(deltaMove);
- if(deltaAngle)
- computeDir(deltaAngle);
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- glLoadIdentity();
- gluLookAt(
- x,1.0f,z,
- x+lx,1.0f, z+lz,
- 0.0f,1.0f,0.0f
- );
- //drawLight();
- //add table to scene
- glPushMatrix();
- glTranslatef(0,0,0);
- drawTable();
- glPopMatrix();
- //add chairs to scene
- //glPushMatrix();
- // glTranslatef(1,0,1);
- // glRotatef(90,0,1,0);
- // drawChair();
- //glPopMatrix();
- //glPushMatrix();
- // glTranslatef(2.5,0,1);
- // glRotatef(90,0,1,0);
- // drawChair();
- //glPopMatrix();
- //glPushMatrix();
- // glTranslatef(1,0,-2);
- // glRotatef(270,0,1,0);
- // drawChair();
- //glPopMatrix();
- //glPushMatrix();
- // glTranslatef(2.5,0,-2);
- // glRotatef(270,0,1,0);
- // drawChair();
- //glPopMatrix();
- glutSwapBuffers();
- }
- void changeSize(int w, int h){
- //prevent divide by 0 when window is too short
- if(h == 0)
- h==1;
- float ratio = 1.0 * w/h;
- //Use projection matrix
- glMatrixMode(GL_PROJECTION);
- //reset matrix
- glLoadIdentity();
- //set viewport to be entire window
- glViewport(0,0,w,h);
- gluPerspective(100,ratio,0.01,500);
- //get back to model view
- glMatrixMode(GL_MODELVIEW);
- }
- //------------------
- // MAIN
- //------------------
- int main (int argc, char **argv){
- //init glut and create window
- glutInit(&argc, argv);
- glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
- glutInitWindowPosition(100,100);
- glutInitWindowSize(800,600);
- glutCreateWindow("Assignment 1");
- //register callbacks
- loadTextures();
- glutDisplayFunc(renderScene);
- glutReshapeFunc(changeSize);
- glutIdleFunc(renderScene);
- glutIgnoreKeyRepeat(1);
- glutSpecialFunc(pressKey);
- glutSpecialUpFunc(releaseKey);
- //mouse functions
- glutMouseFunc(mouseButton);
- glutMotionFunc(mouseMove);
- //OpenGL init
- glEnable(GL_DEPTH_TEST);
- //enter GLUT event processing cycle
- glutMainLoop();
- return 1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement