![]() |
|
|
Home | | Syllabus | |
Assignments | |
Documentation
Solution to Assignment 4, Problem 1b
/*
CSCI 384 Assignment 4.
Problem 1b: spinning box with a user interface.
*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <GL/glut.h>
#define PI 3.14159
typedef GLfloat point3[3];
GLfloat spin[3] = {0.0, 0.0, 0.0};
void myinit(void)
{
glClearColor(1.0, 1.0, 1.0, 1.0);
glColor3f(1.0, 0.0, 0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-250, 250, -250, 250, -250, 250); /*Note, not ortho2D*/
glMatrixMode(GL_MODELVIEW);
}
/*Draw Quad faces*/
void rectangleFace(point3 p1, point3 p2, point3 p3, point3 p4){
/*Draw the quadrilateral*/
glBegin(GL_QUADS);
glVertex3fv(p1);
glVertex3fv(p2);
glVertex3fv(p3);
glVertex3fv(p4);
glEnd();
}
void display(void){
/*Note 3D points here. Set up array of 3D vertices*/
point3 vertices[8] = { {-50.0, 50.0, 50.0}, {-50.0, 50.0, -50.0}, {50.0, 50.0, -50.0}, {50.0, 50.0, 50.0},
{-50.0, -50.0, 50.0}, {-50.0, -50.0, -50.0}, {50.0, -50.0, -50.0}, {50.0, -50.0, 50.0} };
int i;
/*Note use of Depth buffer bit for hidden surface removal*/
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glRotatef(spin[0], 1.0, 0.0, 0.0); /*Rotate box*/
glRotatef(spin[1], 0.0, 1.0, 0.0);
glRotatef(spin[2], 0.0, 0.0, 1.0);
/*Start dividing the faces*/
glColor3f(1, 0, 0);
rectangleFace(vertices[0], vertices[1], vertices[5], vertices[4]);
glColor3f(0, 0, 1);
rectangleFace(vertices[1], vertices[2], vertices[6], vertices[5]);
glColor3f(1, 0, 0);
rectangleFace(vertices[2], vertices[3], vertices[7], vertices[6]);
glColor3f(0, 0, 1);
rectangleFace(vertices[3], vertices[0], vertices[4], vertices[7]);
glFlush();
glutSwapBuffers(); /*Display next buffer*/
}
/*User interface for spinning the box*/
void keys(unsigned char key, int x, int y){
int axis;
if(key == 'f') {
axis = 0;
spin[axis] += 2.0;
if (spin[axis] > 360.0)
spin[axis] = spin[axis] - 360.0;
}
else if(key == 'j'){
axis = 1;
spin[axis] += 2.0;
if (spin[axis] > 360.0)
spin[axis] = spin[axis] - 360.0;
}
else if(key == 'k'){
axis = 2;
spin[axis] += 2.0;
if (spin[axis] > 360.0)
spin[axis] = spin[axis] - 360.0;
}
glutPostRedisplay();
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
/*Note the GLUT_DOUBLE and GLUT_DEPTH here*/
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
myinit();
glutInitWindowSize(500,500);
glutInitWindowPosition(50,50);
glutCreateWindow("Spinning Quads");
glutDisplayFunc(display);
glutKeyboardFunc(keys);
glEnable(GL_DEPTH_TEST); /*For hidden surface removal*/
myinit();
glutMainLoop();
return 0;
}
Home | | Syllabus | | Assignments | | Documentation
Constance Royden--croyden@mathcs.holycross.edu
|