- // Defines
- #define GLUT_DISABLE_ATEXIT_HACK
- #define SKE_BENDANGLE_MAX 30.0f
- #define SKE_BENDANGLE_SPEED 5.0f
- // Includes
- // GL
- #include "GL/glut.h"
- // GL-shaders
- #include "shader_ext.h"
- // GL-obals
- const double PI = 3.1415926535897932384626433832795;
- // Global model rotation angle
- float g_modelRot = 0.0f;
- // Handle keyboard input
- void handleKeyboard(int key, int x, int y)
- {
- // Parse key
- switch (key)
- {
- // Left arrow
- case GLUT_KEY_LEFT:
- g_modelRot -= 1.0f;
- break;
- // Right arrow
- case GLUT_KEY_RIGHT:
- g_modelRot += 1.0f;
- break;
- }
- // Wrap around (-360, 360)
- if (g_modelRot > 360.0f)
- g_modelRot -= 360.0f;
- else if (g_modelRot < -360.0f)
- g_modelRot += 360.0f;
- }
- // Calculates frame time
- float getFrameTime()
- {
- // Set time base
- static int baseTime = glutGet(GLUT_ELAPSED_TIME);
- // Get time diff
- int currTime = glutGet(GLUT_ELAPSED_TIME);
- int diff = currTime - baseTime;
- // Update time base
- baseTime = currTime;
- // Return time in ms
- return (diff > 0) ? diff / 1000.0f : 0.0f;
- }
- // Drawing (display) routine.
- void drawScene(void)
- {
- // STUDENTS CODE //
- // TODO: Update bending angle variable and send the value to the vertex program in every frame
- // Your bending angle should animate smoothly between SKE_BENDANGLE_MAX and -SKE_BENDANGLE_MAX
- // Step one: declare bending angle variable and current delta flag (it can be local and static or global)
- // Hint: it's up to you how you are going to handle incrementation and decrementation in loop
- static float g_modelRot = 0.0f;
- static int delta = 1;
- static float bendAngle = 1;
- // Step two: update your bending variable using getFrameTime() and SKE_BENDANGLE_SPEED to achieve smooth movement
- bendAngle += delta * sin(getFrameTime()) * SKE_BENDANGLE_SPEED;
- // Step three: clamp your bending variable between SKE_BENDANGLE_MAX and -SKE_BENDANGLE_MAX and update delta flag if needed
- if(bendAngle > (SKE_BENDANGLE_MAX * PI/180))
- {
- bendAngle = (SKE_BENDANGLE_MAX * PI/180);
- delta = -1.0;
- }
- else if(bendAngle < (-SKE_BENDANGLE_MAX * PI/180))
- {
- bendAngle = (-SKE_BENDANGLE_MAX * PI/180);
- delta = 1.0;
- }
- // Last step: Update angle variable
- if (g_bendAngle_loc >= 0)
- glUniform1fARB(g_bendAngle_loc, bendAngle /*jo³*/);
- // STUDENTS CODE END //
- // Clear screen to background color.
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- // Set foreground (or drawing) color.
- glColor3f(0.0, 0.0, 0.0);
- // Set polygon mode.
- glPolygonMode(GL_FRONT, GL_FILL);
- // Draw cone (4 units height)
- glPushMatrix();
- glTranslated(-1.5, 0.5, -6.0);
- glRotated(90 + g_modelRot, 0.0, 1.0, 0.0);
- glutSolidCone(1.0, 4.0, 30, 30);
- //glutWireCone(1.0, 4.0, 30, 30);
- glPopMatrix();
- // Draw teapot (1 unit height)
- glPushMatrix();
- glTranslated(0.0, -1.2, -6.0);
- glRotated(-90 + g_modelRot, 0.0, 1.0, 0.0);
- glutSolidTeapot(1.0);
- glPopMatrix();
- // Flush created objects to the screen, i.e., force rendering.
- glFlush();
- }
- // Initialization routine.
- void setup(void)
- {
- // Setup light 0
- GLfloat light0_position[] = { 10.0, 10.0, 10.0, 0.0 };
- glLightfv(GL_LIGHT0, GL_POSITION, light0_position);
- // Enable Z-buffer
- glClearDepth(1.0f);
- glDepthFunc(GL_LESS);
- glEnable(GL_DEPTH_TEST);
- // Set background (or clearing) color.
- glClearColor(1.0, 1.0, 1.0, 0.0);
- }
- // OpenGL window reshape routine.
- void resize(int w, int h)
- {
- // Calculate aspect ratio
- const float ar = (float) w / (float) h;
- // Setup frustum
- glViewport(0, 0, w, h);
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- glFrustum(-ar, ar, -1.0, 1.0, 2.0, 100.0);
- // Reset modelview
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity() ;
- }
- // Timer function (forces angle animation and scene redisplay)
- void myTimerFunc(int val)
- {
- // Force redisplay
- glutPostRedisplay();
- // Re-enable handler
- glutTimerFunc(50, myTimerFunc, val);
- }
- // Main routine: defines window properties, creates window,
- // registers callback routines and begins processing.
- int main(int argc, char **argv)
- {
- // Initialize GLUT.
- glutInit(&argc, argv);
- // Set display mode as single-buffered and RGB color.
- glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
- // Set OpenGL window size.
- glutInitWindowSize(500, 500);
- // Set position of OpenGL window upper-left corner.
- glutInitWindowPosition(100, 100);
- // Create OpenGL window with title.
- glutCreateWindow("GK - SKE");
- // Initialize application
- setup();
- // Setup shaders
- setupShaders();
- // Register display routine.
- glutDisplayFunc(drawScene);
- // Keyboard handler
- glutSpecialFunc(handleKeyboard);
- // Register reshape routine.
- glutReshapeFunc(resize);
- // Start animation
- glutTimerFunc(50, myTimerFunc, 50);
- // Begin processing.
- glutMainLoop();
- return 0;
- }