Facebook
From MT, 7 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 283
  1. // Defines
  2. #define GLUT_DISABLE_ATEXIT_HACK
  3. #define SKE_BENDANGLE_MAX 30.0f
  4. #define SKE_BENDANGLE_SPEED 5.0f
  5.  
  6. // Includes
  7. // GL
  8. #include "GL/glut.h"
  9. // GL-shaders
  10. #include "shader_ext.h"
  11.  
  12. // GL-obals
  13. const double PI = 3.1415926535897932384626433832795;
  14.  
  15. // Global model rotation angle
  16. float g_modelRot = 0.0f;
  17.  
  18. // Handle keyboard input
  19. void handleKeyboard(int key, int x, int y)
  20. {
  21.         // Parse key
  22.         switch  (key)
  23.         {
  24.                 // Left arrow
  25.                 case GLUT_KEY_LEFT:
  26.                         g_modelRot -= 1.0f;
  27.                         break;
  28.  
  29.                 // Right arrow
  30.                 case GLUT_KEY_RIGHT:
  31.                         g_modelRot += 1.0f;
  32.                         break;
  33.         }
  34.  
  35.         // Wrap around (-360, 360)
  36.         if (g_modelRot > 360.0f)
  37.                 g_modelRot -= 360.0f;
  38.         else if (g_modelRot < -360.0f)
  39.                 g_modelRot += 360.0f;
  40. }
  41.  
  42. // Calculates frame time
  43. float getFrameTime()
  44. {
  45.         // Set time base
  46.         static int baseTime = glutGet(GLUT_ELAPSED_TIME);
  47.        
  48.         // Get time diff
  49.         int currTime = glutGet(GLUT_ELAPSED_TIME);
  50.         int diff = currTime - baseTime;
  51.        
  52.         // Update time base
  53.         baseTime = currTime;
  54.        
  55.         // Return time in ms
  56.         return (diff > 0) ? diff / 1000.0f : 0.0f;
  57. }
  58.  
  59. // Drawing (display) routine.
  60. void drawScene(void)
  61. {      
  62.         // STUDENTS CODE //
  63.  
  64.         // TODO: Update bending angle variable and send the value to the vertex program in every frame
  65.         // Your bending angle should animate smoothly between SKE_BENDANGLE_MAX and -SKE_BENDANGLE_MAX
  66.  
  67.         // Step one: declare bending angle variable and current delta flag (it can be local and static or global)
  68.         // Hint: it's up to you how you are going to handle incrementation and decrementation in loop
  69.  
  70.         static float g_modelRot = 0.0f;
  71.         static int delta = 1;
  72.         static float bendAngle = 1;
  73.  
  74.         // Step two: update your bending variable using getFrameTime() and SKE_BENDANGLE_SPEED to achieve smooth movement
  75.  
  76.         bendAngle += delta * sin(getFrameTime()) * SKE_BENDANGLE_SPEED;
  77.  
  78.         // Step three: clamp your bending variable between SKE_BENDANGLE_MAX and -SKE_BENDANGLE_MAX and update delta flag if needed
  79.  
  80.         if(bendAngle > (SKE_BENDANGLE_MAX * PI/180))
  81.         {
  82.                 bendAngle = (SKE_BENDANGLE_MAX * PI/180);
  83.                 delta = -1.0;
  84.         }
  85.         else if(bendAngle < (-SKE_BENDANGLE_MAX * PI/180))
  86.         {
  87.                 bendAngle = (-SKE_BENDANGLE_MAX * PI/180);
  88.                 delta = 1.0;
  89.         }
  90.  
  91.         // Last step: Update angle variable
  92.         if (g_bendAngle_loc >= 0)
  93.                 glUniform1fARB(g_bendAngle_loc, bendAngle /*jo³*/);
  94.         // STUDENTS CODE END //
  95.  
  96.         // Clear screen to background color.
  97.         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  98.  
  99.         // Set foreground (or drawing) color.
  100.         glColor3f(0.0, 0.0, 0.0);
  101.  
  102.         // Set polygon mode.
  103.         glPolygonMode(GL_FRONT, GL_FILL);
  104.  
  105.         // Draw cone (4 units height)
  106.         glPushMatrix();
  107.                 glTranslated(-1.5, 0.5, -6.0);
  108.                 glRotated(90 + g_modelRot, 0.0, 1.0, 0.0);
  109.                 glutSolidCone(1.0, 4.0, 30, 30);
  110.                 //glutWireCone(1.0, 4.0, 30, 30);
  111.         glPopMatrix();
  112.  
  113.         // Draw teapot (1 unit height)
  114.         glPushMatrix();
  115.                 glTranslated(0.0, -1.2, -6.0);
  116.                 glRotated(-90 + g_modelRot, 0.0, 1.0, 0.0);
  117.                 glutSolidTeapot(1.0);
  118.         glPopMatrix();
  119.  
  120.         // Flush created objects to the screen, i.e., force rendering.
  121.         glFlush();
  122. }
  123.  
  124. // Initialization routine.
  125. void setup(void)
  126. {
  127.         // Setup light 0
  128.         GLfloat light0_position[] = { 10.0, 10.0, 10.0, 0.0 };
  129.         glLightfv(GL_LIGHT0, GL_POSITION, light0_position);
  130.  
  131.         // Enable Z-buffer
  132.         glClearDepth(1.0f);
  133.         glDepthFunc(GL_LESS);
  134.         glEnable(GL_DEPTH_TEST);
  135.  
  136.    // Set background (or clearing) color.
  137.    glClearColor(1.0, 1.0, 1.0, 0.0);
  138. }
  139.  
  140. // OpenGL window reshape routine.
  141. void resize(int w, int h)
  142. {
  143.         // Calculate aspect ratio
  144.         const float ar = (float) w / (float) h;
  145.        
  146.         // Setup frustum
  147.         glViewport(0, 0, w, h);
  148.         glMatrixMode(GL_PROJECTION);
  149.         glLoadIdentity();
  150.         glFrustum(-ar, ar, -1.0, 1.0, 2.0, 100.0);
  151.  
  152.         // Reset modelview
  153.         glMatrixMode(GL_MODELVIEW);
  154.         glLoadIdentity() ;
  155. }
  156.  
  157. // Timer function (forces angle animation and scene redisplay)
  158. void myTimerFunc(int val)
  159. {
  160.         // Force redisplay
  161.         glutPostRedisplay();
  162.  
  163.         // Re-enable handler
  164.         glutTimerFunc(50, myTimerFunc, val);
  165. }
  166.  
  167. // Main routine: defines window properties, creates window,
  168. // registers callback routines and begins processing.
  169. int main(int argc, char **argv)
  170. {  
  171.    // Initialize GLUT.
  172.    glutInit(&argc, argv);
  173.  
  174.    // Set display mode as single-buffered and RGB color.
  175.    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
  176.    
  177.    // Set OpenGL window size.
  178.    glutInitWindowSize(500, 500);
  179.  
  180.    // Set position of OpenGL window upper-left corner.
  181.    glutInitWindowPosition(100, 100);
  182.    
  183.    // Create OpenGL window with title.
  184.    glutCreateWindow("GK - SKE");
  185.    
  186.    // Initialize application
  187.    setup();
  188.  
  189.    // Setup shaders
  190.    setupShaders();
  191.    
  192.    // Register display routine.
  193.    glutDisplayFunc(drawScene);
  194.  
  195.    // Keyboard handler
  196.    glutSpecialFunc(handleKeyboard);
  197.  
  198.    // Register reshape routine.
  199.    glutReshapeFunc(resize);  
  200.  
  201.    // Start animation
  202.    glutTimerFunc(50, myTimerFunc, 50);
  203.    
  204.    // Begin processing.
  205.    glutMainLoop();
  206.  
  207.    return 0;  
  208. }
  209.