This week only. Pastebin PRO Accounts Christmas Special! Don't miss out!Want more features on Pastebin? Sign Up, it's FREE!

Camera UP and DOWN 89.95 degrees

By: Dresmor on Oct 31st, 2013  |  syntax: C  |  size: 7.95 KB  |  views: 10  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. /*
  2.     This program using Freeglut-MinGW-2.8.1-1 API
  3.     Move      : Q W E A S D
  4.     Rotate    : Hold right mouse button then move the mouse
  5.     Fullscreen: F11
  6. */
  7.  
  8. #include<GL/glut.h>
  9.  
  10. //math
  11.  
  12. #include<math.h>
  13.  
  14. #define math_pi    (atan(1.0)*4.0)
  15. #define math_pirad (math_pi/180.0)
  16.  
  17. #define math_rad(x)({__typeof__(x)d=(math_pirad);x*d;})
  18. #define math_deg(x)({__typeof__(x)d=(math_pirad);x/d;})
  19. #define math_abs(x)({\
  20.     __typeof__(x)_x=(x);\
  21.     __typeof__(x)_n=(-1);\
  22.     __typeof__(x)_z=(0);\
  23.     _x<_z?_x*_n:_x;\
  24. })
  25.  
  26. #define add3v(v,u) ({\
  27.     v[0]+=(u[0]);\
  28.     v[1]+=(u[1]);\
  29.     v[2]+=(u[2]);\
  30. })
  31.  
  32. #define pay3v(v,u) ({\
  33.     v[0]-=(u[0]);\
  34.     v[1]-=(u[1]);\
  35.     v[2]-=(u[2]);\
  36. })
  37.  
  38. /**************************************************************************************************/
  39.  
  40. GLfloat   light_ambient [4]={0.1f,0.1f,0.1f,1.0f};
  41. GLfloat   light_diffuse [4]={1.0f,1.0f,1.0f,1.0f};
  42. GLfloat   light_specular[4]={1.0f,1.0f,1.0f,1.0f};
  43. GLfloat   light_position[4]={2.0f,5.0f,5.0f,0.0f};
  44.  
  45. GLfloat   mat_ambient   [4]={0.7f,0.7f,0.7f,1.0f};
  46. GLfloat   mat_diffuse   [4]={0.8f,0.8f,0.8f,1.0f};
  47. GLfloat   mat_specular  [4]={1.0f,1.0f,1.0f,1.0f};
  48. GLfloat   high_shininess[1]={100.0f};
  49.  
  50. GLfloat   view_field       =60.0f;//fovy
  51. GLfloat   view_near        =0.1f;
  52. GLfloat   view_far         =5000.0f;
  53. GLint     view_port     [4]={0,0,640,480};
  54.  
  55. GLint     win_size      [2]={640,480};
  56. GLboolean win_fullscreen   =GL_FALSE;
  57.  
  58. GLint     display_fps      =1000/120;//I hate when the mouse delays
  59. GLboolean display_disabled =GL_FALSE;
  60.  
  61. GLboolean mos_dirch        =GL_FALSE;//mouse direct cahnged
  62. GLfloat   mos_slow         =5.0f;
  63. GLint     mos_x            =0;
  64. GLint     mos_y            =0;
  65. GLboolean mos_btns      [3]={GL_FALSE,GL_FALSE,GL_FALSE};
  66.  
  67. GLboolean key_states[256];
  68.  
  69. //camera
  70.  
  71. GLfloat   cam_pos       [3]={0.0f,10.0f,0.0f};
  72. GLfloat   cam_dir       [3]={1.0f,0.0f,0.0f};
  73. GLfloat   cam_rotri        =0.0f;
  74. GLfloat   cam_rotup        =0.0f;
  75. GLfloat   cam_rotuplim     =89.95f;
  76.  
  77. void cam_update(void){
  78.     cam_dir[1]=sinf(math_rad(cam_rotup));
  79.  
  80.     cam_dir[0]=cosf(math_rad(cam_rotri))*(1-math_abs(cam_dir[1]));
  81.     cam_dir[2]=sinf(math_rad(cam_rotri))*(1-math_abs(cam_dir[1]));
  82. };
  83.  
  84. void cam_display(void){
  85.     gluLookAt(
  86.         cam_pos[0],cam_pos[1],cam_pos[2],
  87.         cam_pos[0]+cam_dir[0],cam_pos[1]+cam_dir[1],cam_pos[2]+cam_dir[2],
  88.         0.0,1.0,0.0
  89.     );
  90. };
  91.  
  92. /**************************************************************************************************/
  93.  
  94. void event_mouse(int button,int state,int x,int y){
  95.     mos_btns[button]=!state;
  96.  
  97.     mos_x=x;
  98.     mos_y=y;
  99. };
  100.  
  101. void event_motion(int x,int y){
  102.     if(mos_dirch){
  103.         mos_dirch=GL_FALSE;
  104.         return;
  105.     };
  106.  
  107.     if(mos_btns[GLUT_RIGHT_BUTTON]){
  108.         cam_rotri-=((float)(mos_x-x))/mos_slow;
  109.         cam_rotup+=((float)(mos_y-y))/mos_slow;
  110.  
  111.         cam_rotup=math_abs(cam_rotup)>cam_rotuplim?cam_rotuplim*(cam_rotup/math_abs(cam_rotup)):cam_rotup;
  112.  
  113.         cam_update();
  114.  
  115.         mos_dirch=GL_TRUE;
  116.         glutWarpPointer(mos_x,mos_y);
  117.  
  118.         return;
  119.     };
  120.  
  121.     mos_x=x;
  122.     mos_y=y;
  123. };
  124.  
  125. void event_reshape(int w,int h){
  126.     GLfloat ar=(GLfloat)w/(GLfloat)h;
  127.  
  128.     glViewport(0,0,w,h);
  129.  
  130.     glGetIntegerv(GL_VIEWPORT,view_port);
  131.  
  132.     glMatrixMode(GL_PROJECTION);
  133.     glLoadIdentity();
  134.  
  135.     //glFrustum(-ar,ar,-1.0,1.0,view_near,view_far);
  136.     gluPerspective(view_field,ar,view_near,view_far);
  137.  
  138.     glMatrixMode(GL_MODELVIEW);
  139.     glLoadIdentity();
  140. };
  141.  
  142. void event_display(void){
  143.     if(display_disabled)return;
  144.  
  145.     glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
  146.  
  147.     if(key_states['w']){
  148.         add3v(cam_pos,cam_dir);
  149.     };
  150.     if(key_states['s']){
  151.         pay3v(cam_pos,cam_dir);
  152.     };
  153.     if(key_states['d']){
  154.         GLfloat dir[3]={-cam_dir[2]/(1-math_abs(cam_dir[1])),0,cam_dir[0]/(1-math_abs(cam_dir[1]))};
  155.         add3v(cam_pos,dir);
  156.     };
  157.     if(key_states['a']){
  158.         GLfloat dir[3]={-cam_dir[2]/(1-math_abs(cam_dir[1])),0,cam_dir[0]/(1-math_abs(cam_dir[1]))};
  159.         pay3v(cam_pos,dir);
  160.     };
  161.     if(key_states['e']){
  162.         GLfloat dir[3]={cam_dir[0]/(1-math_abs(cam_dir[1])),1,cam_dir[2]/(1-math_abs(cam_dir[1]))};
  163.         add3v(cam_pos,dir);
  164.     };
  165.     if(key_states['q']){
  166.         GLfloat dir[3]={cam_dir[0]/(1-math_abs(cam_dir[1])),1,cam_dir[2]/(1-math_abs(cam_dir[1]))};
  167.         pay3v(cam_pos,dir);
  168.     };
  169.  
  170.     unsigned long time=(unsigned long)glutGet(GLUT_ELAPSED_TIME);
  171.  
  172.     //glLoadIdentity();
  173.  
  174.     glPushMatrix();
  175.         cam_display();
  176.  
  177.         glPushMatrix();
  178.             glColor4f(1.0f,0.0f,0.0f,1.0f);
  179.             glTranslatef(0.0f,10.0f,0.0f);
  180.             glRotatef((float)(time%100000lu)/100000.0f*360.0f,1.0f,1.0f,1.0f);
  181.             glScalef(10.0f,10.0f,10.0f);
  182.             glutSolidIcosahedron();
  183.         glPopMatrix();
  184.  
  185.         float i=1.0f;
  186.         float l=100.0f;
  187.         for(;i<l;i+=1.0f){
  188.             glPushMatrix();
  189.                 glColor4f(1.0f,0.0f,0.0f,1.0f);
  190.                 glRotatef((float)(time%100000lu)/1000.0f*360.0f*(i/100),0.0f,1.0f,0.0f);
  191.                 glTranslatef(30.0f+i*2.5f,10.0f,0.0f);
  192.                 glutSolidDodecahedron();
  193.             glPopMatrix();
  194.         };
  195.  
  196.         glPushMatrix();
  197.             glColor4f(0.0f,0.6f,0.0f,1.0f);
  198.             glScalef(200.0f,1.0f,200.0f);
  199.             glutSolidCube(1.0);
  200.         glPopMatrix();
  201.     glPopMatrix();
  202.  
  203.     glutSwapBuffers();
  204. };
  205.  
  206. void event_skeydown(int key,int x,int y){
  207.     switch(key){
  208.         case GLUT_KEY_F11:{
  209.             if(win_fullscreen){
  210.                 glutReshapeWindow(win_size[0],win_size[1]);
  211.             }else{
  212.                 win_size[0]=view_port[2];
  213.                 win_size[1]=view_port[3];
  214.                 glutFullScreen();
  215.             };
  216.             win_fullscreen=!win_fullscreen;
  217.         };
  218.     };
  219. };
  220.  
  221. void event_keyup(unsigned char key,int x,int y){
  222.     key_states[key]=GL_FALSE;
  223. };
  224.  
  225. void event_keydown(unsigned char key,int x,int y){
  226.     key_states[key]=GL_TRUE;
  227.  
  228.     switch(key){
  229.         case 27:{
  230.             display_disabled=GL_TRUE;
  231.             exit(0);
  232.         break;};
  233.     };
  234. };
  235.  
  236. void timer_display(int ms){
  237.     glutPostRedisplay();
  238.     glutTimerFunc((unsigned int)ms,timer_display,ms);
  239. };
  240.  
  241. /**************************************************************************************************/
  242.  
  243. void glInit(){
  244.     glClearColor(0.0f,0.0f,0.0f,1.0f);
  245.  
  246.     //glEnable(GL_CULL_FACE);
  247.     //glCullFace(GL_BACK);
  248.  
  249.     glEnable(GL_DEPTH_TEST);
  250.     glDepthFunc(GL_LESS);
  251.  
  252.     glEnable(GL_LIGHT0);
  253.     glEnable(GL_NORMALIZE);
  254.     glEnable(GL_COLOR_MATERIAL);
  255.     glEnable(GL_LIGHTING);
  256.  
  257.     glLightfv(GL_LIGHT0,GL_AMBIENT, light_ambient);
  258.     glLightfv(GL_LIGHT0,GL_DIFFUSE, light_diffuse);
  259.     glLightfv(GL_LIGHT0,GL_SPECULAR,light_specular);
  260.     glLightfv(GL_LIGHT0,GL_POSITION,light_position);
  261.  
  262.     glMaterialfv(GL_FRONT,GL_AMBIENT,  mat_ambient);
  263.     glMaterialfv(GL_FRONT,GL_DIFFUSE,  mat_diffuse);
  264.     glMaterialfv(GL_FRONT,GL_SPECULAR, mat_specular);
  265.     glMaterialfv(GL_FRONT,GL_SHININESS,high_shininess);
  266. };
  267.  
  268. /**************************************************************************************************/
  269.  
  270. int main(int argc,char**argv){
  271.     memset(key_states,0,sizeof(key_states)*sizeof(*key_states));
  272.  
  273.     glutInit              (&argc,argv);
  274.     glutInitWindowSize    (view_port[2],view_port[3]);
  275.     glutInitWindowPosition(10,10);
  276.     glutInitDisplayMode   (GLUT_RGBA|GLUT_DOUBLE|GLUT_DEPTH);
  277.     glutCreateWindow      ("Dresmor Alakazard - Camera test");
  278.     glutSetKeyRepeat      (GLUT_KEY_REPEAT_OFF);
  279.  
  280.     glutReshapeFunc       (event_reshape );
  281.     glutDisplayFunc       (event_display );
  282.     glutKeyboardFunc      (event_keydown );
  283.     glutKeyboardUpFunc    (event_keyup   );
  284.     glutSpecialFunc       (event_skeydown);
  285.     glutMotionFunc        (event_motion  );
  286.     glutMouseFunc         (event_mouse   );
  287.  
  288.     glInit();
  289.  
  290.     timer_display(display_fps);
  291.  
  292.     glutMainLoop();
  293.  
  294.     return 0;
  295. };
clone this paste RAW Paste Data