0% found this document useful (0 votes)
40 views

cs602 Code Assignment 2

This document contains the code for a 3D OpenGL program that rotates a teapot object. The teapot starts on the left side of the window and rotates for 3 seconds before moving to the center and increasing its rotation speed. It then rotates for another 3 seconds in the center before moving to the right side with an even higher rotation speed. The rotation speed and position of the teapot is updated every 10 milliseconds. Functions are included to initialize OpenGL, draw the teapot, update the rotation and position, handle window resizing, and display the scene.

Uploaded by

News site
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views

cs602 Code Assignment 2

This document contains the code for a 3D OpenGL program that rotates a teapot object. The teapot starts on the left side of the window and rotates for 3 seconds before moving to the center and increasing its rotation speed. It then rotates for another 3 seconds in the center before moving to the right side with an even higher rotation speed. The rotation speed and position of the teapot is updated every 10 milliseconds. Functions are included to initialize OpenGL, draw the teapot, update the rotation and position, handle window resizing, and display the scene.

Uploaded by

News site
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

//SaMu

#include <GL/glut.h>

#include <GL/gl.h>

#include <GL/glu.h>

#include <cmath>

const int WINDOW_WIDTH = 800;

const int WINDOW_HEIGHT = 600;

float rotationAngle = 0.0f; // Current rotation angle

float initialRotationSpeed = 45.0f; // Initial rotation speed in degrees per second

float rotationSpeed = initialRotationSpeed; // Current rotation speed in degrees per second

float rotationDuration = 3.0f; // Rotation duration in seconds

enum CubeState

STATE_LEFT,

STATE_MIDDLE,

STATE_RIGHT,

STATE_STOP

};

CubeState cubeState = STATE_LEFT; // Initial cube state

float elapsedTime = 0.0f; // Elapsed time for current state


// Function to initialize OpenGL

void init()

glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // Set the clear color to black

glClearDepth(1.0f); // Set the depth buffer

glEnable(GL_DEPTH_TEST); // Enable depth testing

glDepthFunc(GL_LEQUAL); // Set the depth testing function

glMatrixMode(GL_PROJECTION); // Set the matrix mode to projection

glLoadIdentity(); // Load the identity matrix

// Set the perspective projection

gluPerspective(45.0f, (GLfloat)WINDOW_WIDTH / (GLfloat)WINDOW_HEIGHT, 0.1f, 100.0f);

glMatrixMode(GL_MODELVIEW); // Set the matrix mode to modelview

// Function to draw the teapot

void drawTeapot()

glLoadIdentity(); // Load the identity matrix

if (cubeState == STATE_LEFT)

// Translate the teapot to the left side of the canvas


glTranslatef(-1.5f, 0.0f, -6.0f);

else if (cubeState == STATE_MIDDLE)

// Move the teapot to the middle of the window

glTranslatef(0.0f, 0.0f, -6.0f);

else if (cubeState == STATE_RIGHT)

// Move the teapot to the right side of the window

glTranslatef(1.5f, 0.0f, -6.0f);

// Rotate the teapot around the Y-axis

glRotatef(rotationAngle, 0.0f, 1.0f, 0.0f);

// Draw the teapot

glutSolidTeapot(1.0);

glFlush(); // Flush the OpenGL commands

// Function to update the rotation and cube movement

void update(int value)

{
elapsedTime += 0.01f; // Update the elapsed time

if (cubeState == STATE_LEFT)

if (elapsedTime >= rotationDuration)

cubeState = STATE_MIDDLE; // Move to the middle state

elapsedTime = 0.0f; // Reset the elapsed time

rotationSpeed = initialRotationSpeed * 2; // Increase the rotation speed

else if (cubeState == STATE_MIDDLE)

if (elapsedTime >= rotationDuration)

cubeState = STATE_RIGHT; // Move to the right state

elapsedTime = 0.0f; // Reset the elapsed time

rotationSpeed = initialRotationSpeed * 3; // Increase the rotation speed

else if (cubeState == STATE_RIGHT)

if (elapsedTime >= rotationDuration)

cubeState = STATE_STOP; // Stop the rotation


rotationSpeed = 0.0f; // Set rotation speed to 0

if (cubeState != STATE_STOP)

rotationAngle += rotationSpeed * 0.01f; // Update the rotation angle

if (rotationAngle >= 360.0f)

rotationAngle -= 360.0f;

glutPostRedisplay(); // Notify GLUT that the display has changed

// Continue updating if not in the stop state

if (cubeState != STATE_STOP)

glutTimerFunc(10, update, 0); // Call the update function after 10 milliseconds

// Function to handle window resize

void reshape(int width, int height)

if (height == 0)
height = 1; // Prevent division by zero

glViewport(0, 0, width, height); // Set the viewport to cover the entire window

glMatrixMode(GL_PROJECTION); // Set the matrix mode to projection

glLoadIdentity(); // Load the identity matrix

gluPerspective(45.0f, (GLfloat)width / (GLfloat)height, 0.1f, 100.0f); // Set the perspective projection

glMatrixMode(GL_MODELVIEW); // Set the matrix mode to modelview

// Function to display the scene

void display()

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear the color and depth buffers

glLoadIdentity(); // Load the identity matrix

// Draw the teapot

drawTeapot();

glutSwapBuffers(); // Swap the front and back buffers (double buffering)

int main(int argc, char **argv)

glutInit(&argc, argv); // Initialize GLUT


glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); // Enable double buffering, RGB
color mode, and depth testing

glutInitWindowSize(WINDOW_WIDTH, WINDOW_HEIGHT);

glutCreateWindow("SaMu");

init(); // Initialize OpenGL

glutDisplayFunc(display); // Register display callback function

glutReshapeFunc(reshape); // Register reshape callback function

glutTimerFunc(10, update, 0); // Start the update timer

glutMainLoop(); // Enter the GLUT event loop

return 0;

You might also like