halfo

Untitled

Mar 30th, 2015
254
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.81 KB | None | 0 0
  1. // #pragma comment (lib,"glut32.lib")
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <math.h>
  6.  
  7. #include <GL/glut.h>
  8. float lx=0.0f,lz=-1.0f;
  9. float x=0.0f,z=5.0f;
  10.  
  11. float angle = 0.0f;
  12.  
  13.  
  14. void change(int w, int h)
  15. {
  16.     float ratio = w * 1.0 / h;
  17.     glMatrixMode(GL_PROJECTION);
  18.     glLoadIdentity();
  19.     glViewport(0, 0, w, h);
  20.     gluPerspective(45.0f, ratio, 0.1f, 100.0f);
  21.     glMatrixMode(GL_MODELVIEW);
  22. }
  23.  
  24. void draw(void)
  25. {
  26.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  27.     glLoadIdentity();
  28.     gluLookAt( x, 1.0f, z,
  29.             x+lx, 1.0f, z+lz,
  30.             0.0f, 1.0f, 0.0f);
  31.  
  32.     // Draw ground
  33.     glColor3f(0.9f, 0.9f, 0.9f);
  34.     glBegin(GL_QUADS);
  35.     glVertex3f(-10.0f, 0.0f, -10.0f);
  36.     glVertex3f(-10.0f, 0.0f, 10.0f);
  37.     glVertex3f( 10.0f, 0.0f, 10.0f);
  38.     glVertex3f( 10.0f, 0.0f, -10.0f);
  39.     glEnd();
  40.  
  41.     glutSwapBuffers();
  42. }
  43.  
  44. void processNormalKeys(unsigned char key, int x, int y)
  45. {
  46.  
  47.     if (key == 27)
  48.         exit(0);
  49. }
  50.  
  51. void processSpecialKeys(int key, int xx, int yy)
  52. {
  53.  
  54.     float fraction = 0.1f;
  55.  
  56.     switch (key) {
  57.         case GLUT_KEY_LEFT :
  58.             angle -= 0.01f;
  59.             lx = sin(angle);
  60.             lz = -cos(angle);
  61.             break;
  62.         case GLUT_KEY_RIGHT :
  63.             angle += 0.01f;
  64.             lx = sin(angle);
  65.             lz = -cos(angle);
  66.             break;
  67.         case GLUT_KEY_UP :
  68.             x += lx * fraction;
  69.             z += lz * fraction;
  70.             break;
  71.         case GLUT_KEY_DOWN :
  72.             x -= lx * fraction;
  73.             z -= lz * fraction;
  74.             break;
  75.     }
  76. }
  77.  
  78. int main(int argc, char **argv)
  79. {
  80.     glutInit(&argc, argv);
  81.     glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
  82.     glutInitWindowPosition(100,100);
  83.     glutInitWindowSize(320,320);
  84.     glutCreateWindow("Lighthouse3D - GLUT Tutorial");
  85.     glutDisplayFunc(draw);
  86.     glutReshapeFunc(change);
  87.     glutIdleFunc(draw);
  88.     glutKeyboardFunc(processNormalKeys);
  89.     glutSpecialFunc(processSpecialKeys);
  90.     glEnable(GL_DEPTH_TEST);
  91.     glutMainLoop();
  92.  
  93.     return 1;
  94. }
Advertisement
Add Comment
Please, Sign In to add comment