Advertisement
NathanSimbahan

OpenGL rendering problem

Feb 15th, 2012
655
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.30 KB | None | 0 0
  1. #ifndef __linux__
  2.    #include <windows.h>
  3. #endif
  4. #include <GL/gl.h>
  5. #include <GL/glu.h>
  6. #define GLUT_DISABLE_ATEXIT_HACK
  7. #include <GL/glut.h>
  8. #include <time.h>
  9. #include <math.h>
  10. #include <stdio.h>
  11.  
  12.  
  13. int cameraposx = 0;
  14. int cameraposy = 0;
  15. int cameraposz = 3;
  16. int rotateX = 0;
  17. int rotateY = 0;
  18. int rotateZ = 0;
  19. GLuint texture;
  20. float rotationamount = 0;
  21. int lastx = 0, lasty = 0;
  22. void drawCube1(){ // Simple hollow cube
  23.      glColor3f(1,1,1);
  24.      
  25.      glBegin(GL_LINE_STRIP); // lower square
  26.                            glVertex3f(0,0,0);
  27.                            glVertex3f(1,0,0);
  28.                            glVertex3f(1,0,-1);
  29.                            glVertex3f(0,0,-1);
  30.                            glVertex3f(0,0,0);
  31.      glEnd();
  32.      
  33.      glBegin(GL_LINE_STRIP); // Upper square
  34.                            glVertex3f(0,1,0);
  35.                            glVertex3f(1,1,0);
  36.                            glVertex3f(1,1,-1);
  37.                            glVertex3f(0,1,-1);
  38.                            glVertex3f(0,1,0);
  39.      glEnd();
  40.      
  41.      glBegin(GL_LINES);
  42.                         glVertex3f(0,0,0);
  43.                         glVertex3f(0,1,0);
  44.                            
  45.                            glVertex3f(1,0,0);
  46.                            glVertex3f(1,1,0);
  47.                            
  48.                            glVertex3f(1,0,-1);
  49.                            glVertex3f(1,1,-1);
  50.                            
  51.                            glVertex3f(0,0,-1);
  52.                            glVertex3f(0,1,-1);
  53.      glEnd();
  54.  
  55. }
  56. //
  57. //void readtexture()
  58. //{
  59. //GLuint *TextureImage[1];
  60. //
  61. //if (LoadBitmap(TextureImage[0], "a.bmp"))
  62. //{
  63. //                                     printf("a.bmp!\n");
  64. //{
  65. //glGenTextures(1, &texture[0]);
  66. //glBindTexture(GL_TEXTURE_2D, texture[0]);
  67. //glTexImage2D(GL_TEXTURE_2D,
  68. //           0,
  69. //           3,
  70. //           TextureImage[0]->sizeX,
  71. //           TextureImage[0]->sizeY,
  72. //           0,
  73. //           GL_RGB,
  74. //           GL_UNSIGNED_BYTE,
  75. //           TextureImage[0]->data);
  76. //glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
  77. //}
  78. //}
  79.  
  80. void Init()
  81. {
  82.    //  readtexture();
  83.     glEnable(GL_TEXTURE_2D);
  84.     glShadeModel(GL_SMOOTH);                            // Enable Smooth Shading
  85.     glClearColor(0.0f, 0.0f, 0.0f, 0.5f);               // Black Background
  86.     glClearDepth(1.0f);                                 // Depth Buffer Setup
  87.     glEnable(GL_DEPTH_TEST);                            // Depth Buffer Setup
  88.     glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);  // Really Nice Perspective Calculations
  89.     glEnable(GL_LIGHTING);
  90.     glEnable(GL_LIGHT0);
  91.     glEnable(GL_NORMALIZE);
  92.  }
  93.  
  94.  
  95.  
  96. void drawNiceCube(){
  97.     glBegin(GL_QUADS);                                  // Draw A Quad
  98.         glColor3f(0.0f,1.0f,0.0f);                      // Set The Color To Green
  99.         glVertex3f( 1.0f, 1.0f,-1.0f);                  // Top Right Of The Quad (Top)
  100.         glVertex3f(-1.0f, 1.0f,-1.0f);                  // Top Left Of The Quad (Top)
  101.         glVertex3f(-1.0f, 1.0f, 1.0f);                  // Bottom Left Of The Quad (Top)
  102.         glVertex3f( 1.0f, 1.0f, 1.0f);                  // Bottom Right Of The Quad (Top)
  103.         glColor3f(1.0f,0.5f,0.0f);                      // Set The Color To Orange
  104.         glVertex3f( 1.0f,-1.0f, 1.0f);                  // Top Right Of The Quad (Bottom)
  105.         glVertex3f(-1.0f,-1.0f, 1.0f);                  // Top Left Of The Quad (Bottom)
  106.         glVertex3f(-1.0f,-1.0f,-1.0f);                  // Bottom Left Of The Quad (Bottom)
  107.         glVertex3f( 1.0f,-1.0f,-1.0f);                  // Bottom Right Of The Quad (Bottom)
  108.         glColor3f(1.0f,0.0f,0.0f);                      // Set The Color To Red
  109.         glVertex3f( 1.0f, 1.0f, 1.0f);                  // Top Right Of The Quad (Front)
  110.         glVertex3f(-1.0f, 1.0f, 1.0f);                  // Top Left Of The Quad (Front)
  111.         glVertex3f(-1.0f,-1.0f, 1.0f);                  // Bottom Left Of The Quad (Front)
  112.         glVertex3f( 1.0f,-1.0f, 1.0f);                  // Bottom Right Of The Quad (Front)
  113.         glColor3f(1.0f,1.0f,0.0f);                      // Set The Color To Yellow
  114.         glVertex3f( 1.0f,-1.0f,-1.0f);                  // Top Right Of The Quad (Back)
  115.         glVertex3f(-1.0f,-1.0f,-1.0f);                  // Top Left Of The Quad (Back)
  116.         glVertex3f(-1.0f, 1.0f,-1.0f);                  // Bottom Left Of The Quad (Back)
  117.         glVertex3f( 1.0f, 1.0f,-1.0f);                  // Bottom Right Of The Quad (Back)
  118.         glColor3f(0.0f,0.0f,1.0f);                      // Set The Color To Blue
  119.         glVertex3f(-1.0f, 1.0f, 1.0f);                  // Top Right Of The Quad (Left)
  120.         glVertex3f(-1.0f, 1.0f,-1.0f);                  // Top Left Of The Quad (Left)
  121.         glVertex3f(-1.0f,-1.0f,-1.0f);                  // Bottom Left Of The Quad (Left)
  122.         glVertex3f(-1.0f,-1.0f, 1.0f);                  // Bottom Right Of The Quad (Left)
  123.         glColor3f(1.0f,0.0f,1.0f);                      // Set The Color To Violet
  124.         glVertex3f( 1.0f, 1.0f,-1.0f);                  // Top Right Of The Quad (Right)
  125.         glVertex3f( 1.0f, 1.0f, 1.0f);                  // Top Left Of The Quad (Right)
  126.         glVertex3f( 1.0f,-1.0f, 1.0f);                  // Bottom Left Of The Quad (Right)
  127.         glVertex3f( 1.0f,-1.0f,-1.0f);                  // Bottom Right Of The Quad (Right)
  128.     glEnd();                     
  129.      
  130. }
  131. void drawCube2(){ // Lit cube
  132.      
  133.      glColor3f(1,1,0);    
  134.      glBegin(GL_QUADS); // bottom
  135.              glVertex3f(0,0,0);
  136.              glVertex3f(1,0,0);
  137.              glVertex3f(1,0,-1);
  138.              glVertex3f(0,0,-1);
  139.      glEnd();
  140.      
  141.      glColor3f(1,1,1);
  142.      glBegin(GL_QUADS); // top
  143.              glVertex3f(0,1,0);
  144.              glVertex3f(1,1,0);
  145.              glVertex3f(1,1,-1);
  146.              glVertex3f(0,1,-1);
  147.      glEnd();
  148.      
  149.      glColor3f(1,0,1);
  150.      glBegin(GL_QUADS); // front
  151.              glVertex3f(0,0,0);
  152.              glVertex3f(1,0,0);
  153.              glVertex3f(1,1,0);
  154.              glVertex3f(0,1,0);
  155.              
  156.      glEnd();
  157.      
  158.      glColor3f(1,0,.5);
  159.      glBegin(GL_QUADS); // back
  160.              glVertex3f(1,0,-1);
  161.              glVertex3f(0,0,-1);
  162.              glVertex3f(0,1,-1);
  163.              glVertex3f(1,1,-1);
  164.      glEnd();
  165.      
  166.      glColor3f(1,0,.5);
  167.      glBegin(GL_QUADS); // left
  168.              glVertex3f(0,0,0);
  169.              glVertex3f(0,0,-1);
  170.              glVertex3f(0,1,-1);
  171.              glVertex3f(0,1,0);
  172.      glEnd();
  173.      
  174.      glColor3f(.5,1,.5);
  175.      glBegin(GL_QUADS); // right
  176.              glVertex3f(1,0,0);
  177.              glVertex3f(1,0,-1);
  178.              glVertex3f(1,1,-1);
  179.              glVertex3f(1,1,0);
  180.      glEnd();
  181.  
  182.  
  183. //       glColor3f(1,1,0);
  184. //       glBegin(GL_QUAD_STRIP);
  185. //                              glVertex3f(0, 0, 0);          
  186. //                              glVertex3f(1, 0, 0);
  187. //                              glVertex3f(0, 0, 1);
  188. //                              glVertex3f(1, 0, 1);
  189. //                              
  190. //                              glVertex3f(0, 1, 1);
  191. //                              glVertex3f(1, 1, 1);
  192. //                              glVertex3f(0, 1, 0);
  193. //                              glVertex3f(1, 1, 0);
  194. //       glEnd();
  195. //      
  196. //       glColor3f(1,0,0);
  197. //       glBegin(GL_QUAD_STRIP);
  198. //                              glVertex3f(1, 1, 1);          
  199. //                              glVertex3f(1, 0, 1);
  200. //                              glVertex3f(1, 1, 0);
  201. //                              glVertex3f(1, 0, 0);
  202. //                              
  203. //                              glVertex3f(0, 1, 0);
  204. //                              glVertex3f(0, 0, 0);
  205. //                              glVertex3f(0, 1, 1);
  206. //                              glVertex3f(0, 0, 1);
  207. //       glEnd();
  208.        
  209. }
  210.  
  211. void drawCube3() // Textured cube
  212. {
  213.            
  214. }
  215.  
  216. //keeps track of location of primitive and rotation of it
  217. void lookat()
  218. {
  219.     // glLoadIdentity();
  220.      
  221. }
  222.  
  223. void drawScene() // Draw the actual scene
  224. {
  225.     // glLineWidth(10);
  226.      glColor3ub(255,0,0);
  227.      glBegin(GL_LINES);
  228.          glVertex3f(0,20,0);
  229.          glVertex3f(0,-20,0);
  230.      glEnd();
  231.    
  232.      glColor3ub(0,0,255);
  233.      glBegin(GL_LINES);
  234.          glVertex3f(20,0,0);
  235.          glVertex3f(-20,0,0);
  236.      glEnd();
  237.      
  238.      glColor3ub(0,255,0);
  239.      glBegin(GL_LINES);
  240.          glVertex3f(0,0,20);
  241.          glVertex3f(0,0,-20);
  242.      glEnd();
  243. }
  244.  
  245. void draw(void) // Reshape function and idle rendering function
  246. {
  247.     GLfloat lightColor0[] = {0.5f, 0.5f, 0.5f, 1.0f}; //Color (0.5, 0.5, 0.5)
  248.     GLfloat lightPos0[] = {4.0f, 0.0f, 8.0f, 1.0f}; //Positioned at (4, 0, 8)
  249.     glLightfv(GL_LIGHT0, GL_DIFFUSE, lightColor0);
  250.     glLightfv(GL_LIGHT0, GL_POSITION, lightPos0);
  251.    
  252.     // Init();
  253.     glViewport (0, 0, 640, 480);
  254.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  255.     glMatrixMode(GL_PROJECTION);
  256.     glLoadIdentity ();
  257.     gluPerspective(45, 640/480.0, .5, 100);
  258.    
  259.     glMatrixMode(GL_MODELVIEW); //select the modelview matrix.
  260.     glLoadIdentity ();
  261.     gluLookAt(0,0,5,
  262.     0,0,0,
  263.     0,1,0);    
  264.        
  265.      
  266.     glRotatef(rotateX,1,0,0);
  267.     glRotatef(rotateY,0,1,0);
  268.     glRotatef(rotateZ,0,0,1);
  269.     drawScene();
  270.        
  271.         //rotate this cube around. :P
  272.     glPushMatrix();
  273.                       // glRotatef(rotationamount,0,0,1);
  274.                       // drawCube2();
  275.     glPopMatrix();
  276.     glPushMatrix();
  277.                        glTranslatef(-2,1,0);
  278.                       //  glutSolidCube(1);  
  279.                        drawNiceCube();
  280.     glPopMatrix();
  281.        
  282.     glPushMatrix();
  283.                    glColor3f(1.0f,0.0f,0.0f);
  284.                    glutWireCube(1);  
  285.          
  286.     glPopMatrix();
  287.    
  288.     glutSwapBuffers();
  289. }
  290.  
  291. void processKeyboard(unsigned char key, int x, int y) // Receive keyboard inputs
  292. {
  293.     switch (key){
  294.     case 'w':
  295.          rotateX -= 30;
  296.         break;
  297.     case 's' :
  298. rotateY -= 30;
  299.         break;
  300.     case 'p' :
  301.              rotateX += 30;
  302.         break;
  303.     case 'z' :
  304.  rotateZ -= 30;
  305.         break;
  306.     case 'm' :
  307.  
  308.  rotateZ += 30;
  309.         break;
  310.     case 'l' :
  311.  
  312. rotateY += 30;
  313.         break;
  314.     default: break;
  315.     }
  316. }
  317.  
  318. void processMouse(int button, int state, int x, int y)
  319. {  
  320.      
  321.      if(state == GLUT_DOWN && button == GLUT_LEFT){
  322.               //do computations
  323.               rotationamount =  x-lastx ;
  324.               lastx = x;
  325.               lasty = y;        
  326.      }
  327.      
  328.      
  329. }
  330.  
  331. main(int argc, char **argv) {
  332.  
  333.     //Initialization functions
  334.     glutInit(&argc, argv);
  335.     glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
  336. Init();
  337.     glutInitWindowPosition(100,100); //Set window position
  338.     glutInitWindowSize(640,480); //Set window size
  339.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  340.    
  341.     glutCreateWindow("Lighting and Textures"); //Create the window
  342.        
  343.     glutDisplayFunc(draw); // Call reshape function
  344.  
  345.     glutIdleFunc(draw); // The idle rendering function
  346.     glutKeyboardFunc(processKeyboard);
  347.     glutMouseFunc(processMouse);
  348.  
  349.     glutMainLoop();
  350. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement