![]() |
|
|
Home | | Syllabus | |
Assignments | |
Documentation
Solution to Assignment 5, Problem 1a
/*
CSCI 384 Assign 5.
Problem 1a--Walking through a scene.
*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <GL/glut.h>
#define PI 3.14159
GLfloat spin= 0.0;
GLfloat viewer[3] = {0.0, 0.0, 250.0};
void myinit(void)
{
glClearColor(1.0, 1.0, 1.0, 1.0);
glColor3f(1.0, 0.0, 0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, 1.0, 1.0, 400.0);
glMatrixMode(GL_MODELVIEW);
}
void display(void){
/*Note 3D points here. Set up array of 3D vertices*/
typedef GLfloat point3[3];
point3 vertices[5] = { {0.0, 0.0, 100.0}, {100.0, 0.0, 0.0},
{0.0, 100.0, 0.0}, {-100.0, 0.0, 0.0},
{0.0, 0.0, -100.0} };
int i;
/*Note use of Depth buffer bit for hidden surface removal*/
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
gluLookAt(viewer[0], viewer[1], viewer[2], 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
glRotatef(spin, 0.0, 1.0, 0.0); /*Rotate pyramid*/
/*Draw pyramid*/
glBegin(GL_TRIANGLES);
glColor3f(1.0, 0.0, 0.0);
for (i=0; i<3; i++){
glVertex3fv(vertices[i]);
}
glColor3f(0.0, 1.0, 0.0);
glVertex3fv(vertices[0]);
glVertex3fv(vertices[2]);
glVertex3fv(vertices[3]);
glColor3f(0.0, 0.0, 1.0);
glVertex3fv(vertices[3]);
glVertex3fv(vertices[2]);
glVertex3fv(vertices[4]);
glColor3f(1.0, 0.0, 1.0);
glVertex3fv(vertices[4]);
glVertex3fv(vertices[2]);
glVertex3fv(vertices[1]);
glEnd();
glBegin(GL_QUADS);
glColor3f(1.0, 1.0, 0.0);
glVertex3fv(vertices[0]);
glVertex3fv(vertices[3]);
glVertex3fv(vertices[4]);
glVertex3fv(vertices[1]);
glEnd();
glFlush();
glutSwapBuffers(); /*Display next buffer*/
}
void idle(void) {
spin = spin + 1.0;
if (spin > 360.0)
spin = spin - 360.0;
glutPostRedisplay();
}
/*Function to move camera with a keypress*/
void keys(unsigned char key, int x, int y){
if(key == 'x') viewer[0] -= 1.0;
if(key == 'X') viewer[0] += 1.0;
if(key == 'y') viewer[1] -= 1.0;
if(key == 'Y') viewer[1] += 1.0;
if(key == 'z') viewer[2] -= 1.0;
if(key == 'Z') viewer[2] += 1.0;
display();
}
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("Look At Pyramid");
glutDisplayFunc(display);
glutIdleFunc(idle);
glutKeyboardFunc(keys); /*Idle function, spins pyramid*/
glEnable(GL_DEPTH_TEST); /*For hidden surface removal*/
myinit();
glutMainLoop();
return 0;
}
Home | | Syllabus | | Assignments | | Documentation
Constance Royden--croyden@mathcs.holycross.edu
|