![]() |
![]() |
![]() |
Home | | Syllabus | |
Assignments | |
Documentation
Solution to Assignment 4, Problem 1a
/* CSCI 384 Assignment 4. Problem 1a: spinning box. */ #include <stdio.h> #include <stdlib.h> #include <math.h> #include <GL/glut.h> #define PI 3.14159 typedef GLfloat point3[3]; GLfloat spin = 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, 1.0, 1.0, 1.0); /*Rotate box*/ /*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*/ } /*Spin the box*/ void spinDisplay(){ spin += 2.0; /*Increment angle*/ if (spin > 360.0) spin = spin - 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); glutIdleFunc(spinDisplay); glEnable(GL_DEPTH_TEST); /*For hidden surface removal*/ myinit(); glutMainLoop(); return 0; } Home | | Syllabus | | Assignments | | Documentation
Constance Royden--croyden@mathcs.holycross.edu
|