Advertisement
Guest User

Copied Code

a guest
Mar 20th, 2012
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.15 KB | None | 0 0
  1. /*
  2.  * File:   main.cpp
  3.  * Author: Admin
  4.  *
  5.  * Created on March 20, 2012, 3:31 PM
  6.  */
  7.  
  8. #include <cstdlib>
  9. #include <GL/freeglut.h>
  10. #include "ModelView.h"
  11.  
  12. void resize(int width, int height);
  13. void display(void);
  14. void special(int key, int x, int y);
  15. void key(unsigned char key, int x, int y);
  16. void idle();
  17. void init(int argc, char** argv);
  18. void mouseWheel(int button, int direction, int x, int y);
  19. void mouse(int button, int direction, int x, int y);
  20.  
  21. //testing
  22. #define RADIUS    1.0f
  23. void drawTestObject();
  24.  
  25. const GLfloat light_ambient[] = {0.0f, 0.0f, 0.0f, 1.0f};
  26. const GLfloat light_diffuse[] = {1.0f, 1.0f, 1.0f, 1.0f};
  27. const GLfloat light_specular[] = {1.0f, 1.0f, 1.0f, 1.0f};
  28. const GLfloat light_position[] = {2.0f, 5.0f, 5.0f, 0.0f};
  29.  
  30. const GLfloat mat_ambient[] = {0.7f, 0.7f, 0.7f, 1.0f};
  31. const GLfloat mat_diffuse[] = {0.8f, 0.8f, 0.8f, 1.0f};
  32. const GLfloat mat_specular[] = {1.0f, 1.0f, 1.0f, 1.0f};
  33. const GLfloat high_shininess[] = {100.0f};
  34.  
  35. int main(int argc, char** argv)
  36. {
  37.     // inits
  38.     init(argc, argv);
  39.     glutMainLoop();
  40.  
  41.     return 0;
  42. }
  43.  
  44. void init(int argc, char** argv)
  45. {
  46.     glutInitWindowSize(640, 480);
  47.     glutInitWindowPosition(40, 40);
  48.     glutInit(&argc, argv);
  49.     glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH | GLUT_MULTISAMPLE);
  50.  
  51.     // window
  52.     glutCreateWindow("TestEngine");
  53.     glutReshapeFunc(resize);
  54.     glutDisplayFunc(display);
  55.     glutSpecialFunc(special);
  56.     glutIdleFunc(idle);
  57.     glutMouseWheelFunc(mouseWheel);
  58.     glutMouseFunc(mouse);
  59.    
  60.     glutSetOption(GLUT_ACTION_ON_WINDOW_CLOSE, GLUT_ACTION_CONTINUE_EXECUTION);
  61.     glClearColor(1, 1, 1, 1);
  62.     glEnable(GL_CULL_FACE);
  63.     glCullFace(GL_BACK);
  64.  
  65.     glEnable(GL_DEPTH_TEST);
  66.     glDepthFunc(GL_LESS);
  67.  
  68.     glEnable(GL_LIGHT0);
  69.     glEnable(GL_NORMALIZE);
  70.     glEnable(GL_COLOR_MATERIAL);
  71.  
  72.     glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
  73.     glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
  74.     glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
  75.     glLightfv(GL_LIGHT0, GL_POSITION, light_position);
  76.  
  77.     glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
  78.     glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
  79.     glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
  80.     glMaterialfv(GL_FRONT, GL_SHININESS, high_shininess);
  81. }
  82.  
  83. void idle()
  84. {
  85.     glutPostRedisplay();
  86. }
  87.  
  88. void resize(int width, int height)
  89. {
  90.     const float ar = (float)width / (float)height;
  91.  
  92.     glViewport(0, 0, width, height);
  93.  
  94.     glMatrixMode(GL_PROJECTION);
  95.     glLoadIdentity();
  96.     glFrustum(-ar, ar, -1.0, 1.0, 2.0, 100.0);
  97.  
  98.     glMatrixMode(GL_MODELVIEW);
  99.     glLoadIdentity();
  100. }
  101.  
  102. void display(void)
  103. {
  104.     const double t = glutGet(GLUT_ELAPSED_TIME) / 1000.0;
  105.     const double a = t * 90.0;
  106.  
  107.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  108.     glEnable(GL_LIGHTING);
  109.  
  110.     glColor3d(0, 1, 0);
  111.     glPushMatrix();
  112.  
  113.     glTranslated(0, 1.2, -6);
  114.     glRotated(60, 1, 0, 0);
  115.     glRotated(a, 0, 0, 1);
  116.     drawTestObject();
  117.  
  118.     glPopMatrix();
  119.  
  120.     glDisable(GL_LIGHTING);
  121.     glColor3d(0.1, 0.1, 0.4);
  122. }
  123.  
  124. void drawTestObject()
  125. {
  126.    glBegin( GL_LINE_LOOP );
  127.     glNormal3d( 1.0, 0.0, 0.0 ); glVertex3d( RADIUS, RADIUS, 0.0 ); glVertex3d( RADIUS, 0.0, RADIUS ); glVertex3d( RADIUS,-RADIUS, 0.0 ); glVertex3d( RADIUS, 0.0,-RADIUS );
  128.   glEnd();
  129.   glBegin( GL_LINE_LOOP );
  130.     glNormal3d(-1.0, 0.0, 0.0 ); glVertex3d(-RADIUS, RADIUS, 0.0 ); glVertex3d(-RADIUS, 0.0,-RADIUS ); glVertex3d(-RADIUS,-RADIUS, 0.0 ); glVertex3d(-RADIUS, 0.0, RADIUS );
  131.   glEnd();
  132.   glBegin( GL_LINE_LOOP );
  133.     glNormal3d( 0.0, 1.0, 0.0 ); glVertex3d( RADIUS, RADIUS, 0.0 ); glVertex3d( 0.0, RADIUS,-RADIUS ); glVertex3d(-RADIUS, RADIUS, 0.0 ); glVertex3d( 0.0, RADIUS, RADIUS );
  134.   glEnd();
  135.   glBegin( GL_LINE_LOOP );
  136.     glNormal3d( 0.0,-1.0, 0.0 ); glVertex3d( RADIUS,-RADIUS, 0.0 ); glVertex3d( 0.0,-RADIUS, RADIUS ); glVertex3d(-RADIUS,-RADIUS, 0.0 ); glVertex3d( 0.0,-RADIUS,-RADIUS );
  137.   glEnd();
  138.   glBegin( GL_LINE_LOOP );
  139.     glNormal3d( 0.0, 0.0, 1.0 ); glVertex3d( RADIUS, 0.0, RADIUS ); glVertex3d( 0.0, RADIUS, RADIUS ); glVertex3d(-RADIUS, 0.0, RADIUS ); glVertex3d( 0.0,-RADIUS, RADIUS );
  140.   glEnd();
  141.   glBegin( GL_LINE_LOOP );
  142.     glNormal3d( 0.0, 0.0,-1.0 ); glVertex3d( RADIUS, 0.0,-RADIUS ); glVertex3d( 0.0,-RADIUS,-RADIUS ); glVertex3d(-RADIUS, 0.0,-RADIUS ); glVertex3d( 0.0, RADIUS,-RADIUS );
  143.   glEnd();
  144. }
  145.  
  146. // keyboard functions
  147.  
  148. void special(int key, int x, int y)
  149. {
  150.     switch (key)
  151.     {
  152.         case GLUT_KEY_UP:
  153.             break;
  154.         case GLUT_KEY_DOWN:
  155.             break;
  156.         case GLUT_KEY_LEFT:
  157.             break;
  158.         case GLUT_KEY_RIGHT:
  159.             break;
  160.         default:
  161.             break;
  162.     }
  163. }
  164.  
  165. void key(unsigned char key, int x, int y)
  166. {
  167.     switch (key)
  168.     {
  169.         case 'W':
  170.         case 'w':
  171.             break;
  172.  
  173.         case 'A':
  174.         case 'a':
  175.             break;
  176.  
  177.         case 'S':
  178.         case 's':
  179.             break;
  180.  
  181.         case 'D':
  182.         case 'd':
  183.             break;
  184.  
  185.         default:
  186.             break;
  187.     }
  188. }
  189.  
  190. void mouseWheel(int button, int direction, int x, int y)
  191. {
  192.  
  193. }
  194.  
  195. void mouse(int button, int direction, int x, int y)
  196. {
  197.  
  198. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement