Advertisement
Guest User

Untitled

a guest
Apr 7th, 2014
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 8.62 KB | None | 0 0
  1. #include <GL/glut.h>
  2. #include <math.h>
  3. #include <stdio.h>
  4. #include <stdbool.h>
  5. #include <string.h>
  6.  
  7. //------------------
  8. //  VARIABLES
  9. //------------------
  10.  
  11. //angle of rotation for camera direction
  12. float angle = 0.0f;
  13.  
  14. //the key states, these variables will be 0
  15. //when no key is being pressed
  16. float deltaAngle = 0.0f;
  17. float deltaMove = 0;
  18. int xOrigin = -1;
  19.  
  20. //XZ position of camera
  21. float x = 0.0f, z = 5.0f;
  22.  
  23. //actual vector representing the cameras direction
  24. float lx = 0.0f, lz = -1.0f;
  25.  
  26. float red = 1.0f;
  27. float blue = 0.5f;
  28. float green = 0.5f;
  29.  
  30. //load textures
  31. GLuint tableTopTexture;
  32. GLuint tableLegTexture;
  33.  
  34. //------------------
  35. //  TEXTURES
  36. //------------------
  37.  
  38. GLuint LoadTexture(const char * filename){
  39.     GLuint texture;
  40.  
  41.     int width, height;
  42.  
  43.     unsigned char * data;
  44.  
  45.     FILE * file;
  46.  
  47.     file = fopen(filename, "rb");
  48.  
  49.     if(file == NULL)
  50.         return 0;
  51.     width = 512;
  52.     height = 1024;
  53.     data = (unsigned char *) malloc (width * height * 3);
  54.    
  55.     fread(data, width * height * 3, 1, file);
  56.     fclose(file);
  57.  
  58.     for(int i = 0; i < width * height; ++i){
  59.         int index = i * 3;
  60.         unsigned char B,R;
  61.         B = data[index];
  62.         R = data[index + 2];
  63.  
  64.         data[index] = R;
  65.         data[index + 2] = B;
  66.     }
  67.  
  68.     glGenTextures(1, &texture);
  69.     glBindTexture(GL_TEXTURE_2D, texture);
  70.     glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
  71.     glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
  72.    
  73.     glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  74.     glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  75.     glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  76.     gluBuild2DMipmaps(GL_TEXTURE_2D, 3,width, height, GL_RGB, GL_UNSIGNED_BYTE, data);
  77.  
  78.     return texture;
  79. }
  80.  
  81. void loadTextures(){
  82.     tableTopTexture = LoadTexture("table_top.bmp");
  83.     tableLegTexture = LoadTexture("table_leg.bmp");
  84. }
  85.  
  86. //------------------
  87. //  DRAWING
  88. //------------------
  89.  
  90. void drawCube(float scale_x, float scale_y, float scale_z, float position_x, float position_y, float position_z) {
  91.    
  92.     glPushMatrix();
  93.         glTranslatef(position_x,position_y,position_z);
  94.         glScalef(scale_x,scale_y,scale_z);
  95.         glBegin(GL_QUADS);
  96.             //top face
  97.             glColor3f(0,green,0);
  98.             glVertex3f(1.0,1.0,-1.0);
  99.             glVertex3f(-1.0,1.0,-1.0);
  100.             glVertex3f(-1.0,1.0,1.0);
  101.             glVertex3f(1.0,1.0,1.0);
  102.  
  103.             //bottom face
  104.             glColor3f(1.0f,0.5f,0.0f);
  105.             glVertex3f(1.0,1.0,1.0);
  106.             glVertex3f(-1.0,1.0,1.0);
  107.             glVertex3f(-1.0,-1.0,1.0);
  108.             glVertex3f(1.0,-1.0,1.0);
  109.  
  110.             //front face
  111.             glColor3f(1.0f,0.0f,0.0f);
  112.             glVertex3f(1.0,1.0,1.0);
  113.             glVertex3f(-1.0,1.0,1.0);
  114.             glVertex3f(-1.0,-1.0,1.0);
  115.             glVertex3f(1.0,-1.0,1.0);
  116.        
  117.             //back face
  118.             glColor3f(1.0f,1.0f,0.0f);
  119.             glVertex3f(1.0,-1.0,-1.0);
  120.             glVertex3f(-1.0,-1.0,-1.0);
  121.             glVertex3f(-1.0,1.0,-1.0);
  122.             glVertex3f(1.0,1.0,-1.0);
  123.  
  124.             //left face
  125.             glColor3f(0.0f,0.0f,1.0f);
  126.             glVertex3f(-1.0,1.0,1.0);
  127.             glVertex3f(-1.0,1.0,-1.0);
  128.             glVertex3f(-1.0,-1.0,-1.0);
  129.             glVertex3f(-1.0,-1.0,1.0);
  130.  
  131.             //right
  132.             glColor3f(1.0f,0.0f,1.0f);
  133.             glVertex3f(1.0,1.0,-1.0);
  134.             glVertex3f(1.0,1.0,1.0);
  135.             glVertex3f(1.0,-1.0,1.0);
  136.             glVertex3f(1.0,-1.0,-1.0);
  137.         glEnd();
  138.     glPopMatrix();
  139. }
  140.  
  141. void drawTable(){
  142.    
  143.     //texture stuff
  144.     glEnable(GL_TEXTURE_2D);
  145.     glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
  146.    
  147.    
  148.  
  149.     //draw legs
  150.     glBindTexture(GL_TEXTURE_2D, tableLegTexture);
  151.     glTexCoord2f(0.0,0.0);
  152.     drawCube(0.050,0.5,0.060,0,0,0);
  153.     glTexCoord2f(0.0,1.0);
  154.     drawCube(0.050,0.5,0.060,3,0,0);
  155.     glTexCoord2f(1.0,1.0);
  156.     drawCube(0.050,0.5,0.060,3,0,-1);
  157.     glTexCoord2f(1.0,0.0);
  158.     drawCube(0.050,0.5,0.060,0,0,-1);
  159.  
  160.     //draw table top
  161.     glBindTexture(GL_TEXTURE_2D, tableTopTexture);
  162.     glTexCoord2f(0.0,0.0);
  163.     glTexCoord2f(1.0,0.0);
  164.     glTexCoord2f(1.0,1.0);
  165.     glTexCoord2f(0.0,1.0);
  166.     drawCube(2,0.050,1,1.5,0.5,-0.5);
  167.  
  168.     glDisable(GL_TEXTURE_2D);
  169. }
  170.  
  171. void drawLight(){
  172.     //draw base
  173.     drawCube(0.30,0.02,0.30,0,0,0);
  174.  
  175.     //draw center pole
  176.     drawCube(0.02,1,0.02,0,1,0);
  177.  
  178.     //draw light
  179.     drawCube(0.04,0.50,0.04,0,1.3,0);
  180.  
  181.     //draw shade
  182.     drawCube(0.20,0.65,0.005,0,1.3,-0.10);
  183.     drawCube(0.005,0.65,0.10,0.20,1.3,0);
  184.     drawCube(0.005,0.65,0.10,-0.20,1.3,0);
  185.     drawCube(0.20,0.65,0.005,0,1.3,0.10);
  186. }
  187.  
  188. void drawChair(){
  189.     //draw legs
  190.     drawCube(0.025,0.70,0.020,0,0.35,0);
  191.     drawCube(0.025,0.35,0.019,0.50,0,0);
  192.     drawCube(0.025,0.70,0.020,0,0.35,-0.45);
  193.     drawCube(0.025,0.35,0.019,0.50,0,-0.45);
  194.  
  195.     //draw seat
  196.     drawCube(0.27,0.02,0.26,0.27,0.33,-0.22);
  197.  
  198.     //draw back support
  199.     drawCube(0.025,0.030,0.21,0,1,-0.22);
  200.     drawCube(0.025,0.030,0.21,0,0.75,-0.22);
  201. }
  202.  
  203. void drawWalls(){
  204.     glColor3f(1,0,0);
  205.     glPushMatrix();
  206.     glBegin(GL_QUADS);
  207.         //floor
  208.         glVertex3f(-1,-1,-1);
  209.         glVertex3f(1,-1,-1);
  210.         glVertex3f(1,-1,1);
  211.         glVertex3f(-1,-1,1);
  212.  
  213.         //ceiling
  214.         glVertex3f(-1,1,-1);
  215.         glVertex3f(1,1,-1);
  216.         glVertex3f(1,1,1);
  217.         glVertex3f(-1,1,1);
  218.  
  219.         glColor3f(0,1,0);
  220.  
  221.         //walls
  222.         glVertex3f(-1,-1,1);
  223.         glVertex3f(1,-1,1);
  224.         glVertex3f(1,1,1);
  225.         glVertex3f(-1,1,1);
  226.  
  227.         glVertex3f(-1,-1,-1);
  228.         glVertex3f(1,-1,-1);
  229.         glVertex3f(1,1,-1);
  230.         glVertex3f(-1,1,-1);
  231.  
  232.         glVertex3f(1,1,1);
  233.         glVertex3f(1,-1,1);
  234.         glVertex3f(1,-1,-1);
  235.         glVertex3f(1,1,-1);
  236.  
  237.         glVertex3f(-1,1,1);
  238.         glVertex3f(-1,-1,1);
  239.         glVertex3f(-1,-1,-1);
  240.         glVertex3f(-1,1,-1);
  241.     glEnd();
  242.     glPopMatrix();
  243. }
  244.  
  245. //------------------
  246. //  MOUSE
  247. //------------------
  248. void mouseButton(int button, int state, int x, int y){
  249.     //only start motion if the left button is pressed
  250.     if(button == GLUT_LEFT_BUTTON) {
  251.         angle += deltaAngle;
  252.         xOrigin = -1;  
  253.     }
  254.     else {
  255.         xOrigin = x;
  256.     }
  257. }
  258.  
  259. void mouseMove(int x, int y){
  260.     //this will only be true when the left button is down
  261.     if(xOrigin >= 0){
  262.         //update deltaAngle
  263.         deltaAngle = (x - xOrigin) * 0.001f;
  264.  
  265.         //update cameras direction
  266.         lx = sin(angle + deltaAngle);
  267.         lz = -cos(angle + deltaAngle);
  268.     }
  269. }
  270.  
  271. void computePos(float deltaMove){
  272.     x += deltaMove * lx * 0.1f;
  273.     z += deltaMove * lz * 0.1f;
  274. }
  275.  
  276. void computeDir(float deltaAngle){
  277.     angle += deltaAngle;
  278.     lx = sin(angle);
  279.     lz = -cos(angle);
  280. }
  281.  
  282.  
  283. //------------------
  284. //  KEYBOARD
  285. //------------------
  286.  
  287. void pressKey(int key, int xx, int yy){
  288.     switch(key){
  289.         case GLUT_KEY_LEFT :
  290.             deltaAngle = -0.01f;
  291.             break; 
  292.         case GLUT_KEY_RIGHT :
  293.             deltaAngle = 0.01f;
  294.             break; 
  295.         case GLUT_KEY_UP :
  296.             deltaMove = 0.5f;
  297.             break; 
  298.         case GLUT_KEY_DOWN :
  299.             deltaMove = -0.5f;
  300.             break;     
  301.     }
  302. }
  303.  
  304. void releaseKey(int key, int x, int y){
  305.     switch(key){
  306.         case GLUT_KEY_LEFT :
  307.         case GLUT_KEY_RIGHT :
  308.             deltaAngle = 0.0f;
  309.             break;
  310.         case GLUT_KEY_UP :
  311.         case GLUT_KEY_DOWN :
  312.             deltaMove = 0;
  313.             break;     
  314.     }
  315. }
  316.  
  317. //------------------
  318. //  RENDERING
  319. //------------------
  320. void renderScene(void){
  321.    
  322.     if(deltaMove)
  323.         computePos(deltaMove);
  324.     if(deltaAngle)
  325.         computeDir(deltaAngle);
  326.  
  327.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  328.     glLoadIdentity();
  329.  
  330.     gluLookAt(
  331.         x,1.0f,z,
  332.         x+lx,1.0f, z+lz,
  333.         0.0f,1.0f,0.0f
  334.     );
  335.  
  336.     //drawLight();
  337.  
  338.     //add table to scene
  339.  
  340.     glPushMatrix();
  341.         glTranslatef(0,0,0);
  342.         drawTable();
  343.     glPopMatrix();
  344.     //add chairs to scene
  345.     //glPushMatrix();
  346.     //  glTranslatef(1,0,1);
  347.     //  glRotatef(90,0,1,0);
  348.     //  drawChair();
  349.     //glPopMatrix();
  350.    
  351.     //glPushMatrix();
  352.     //  glTranslatef(2.5,0,1);
  353.     //  glRotatef(90,0,1,0);
  354.     //  drawChair();   
  355.     //glPopMatrix();
  356.  
  357.     //glPushMatrix();
  358.     //  glTranslatef(1,0,-2);
  359.     //  glRotatef(270,0,1,0);
  360.     //  drawChair();
  361.     //glPopMatrix();
  362.    
  363.     //glPushMatrix();
  364.     //  glTranslatef(2.5,0,-2);
  365.     //  glRotatef(270,0,1,0);
  366.     //  drawChair();   
  367.     //glPopMatrix();
  368.  
  369.     glutSwapBuffers();
  370. }
  371.  
  372.  
  373. void changeSize(int w, int h){
  374.     //prevent divide by 0 when window is too short
  375.     if(h == 0)
  376.         h==1;
  377.     float ratio = 1.0 * w/h;
  378.  
  379.     //Use projection matrix
  380.     glMatrixMode(GL_PROJECTION);
  381.    
  382.     //reset matrix
  383.     glLoadIdentity();
  384.  
  385.     //set viewport to be entire window
  386.     glViewport(0,0,w,h);
  387.  
  388.     gluPerspective(100,ratio,0.01,500);
  389.  
  390.     //get back to model view
  391.     glMatrixMode(GL_MODELVIEW);
  392. }
  393.  
  394. //------------------
  395. //  MAIN
  396. //------------------
  397. int main (int argc, char **argv){
  398.  
  399.     //init glut and create window
  400.  
  401.     glutInit(&argc, argv);
  402.     glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
  403.     glutInitWindowPosition(100,100);
  404.     glutInitWindowSize(800,600);
  405.     glutCreateWindow("Assignment 1");
  406.  
  407.     //register callbacks
  408.     loadTextures();
  409.     glutDisplayFunc(renderScene);
  410.     glutReshapeFunc(changeSize);
  411.     glutIdleFunc(renderScene);
  412.  
  413.     glutIgnoreKeyRepeat(1);
  414.     glutSpecialFunc(pressKey);
  415.     glutSpecialUpFunc(releaseKey);
  416.  
  417.     //mouse functions
  418.     glutMouseFunc(mouseButton);
  419.     glutMotionFunc(mouseMove);
  420.  
  421.     //OpenGL init
  422.     glEnable(GL_DEPTH_TEST);
  423.  
  424.     //enter GLUT event processing cycle
  425.     glutMainLoop();
  426.  
  427.     return 1;
  428. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement