Saturday, March 28, 2009

Web Exercise 2

Here is the sample from the OpenGl redbook running, This screenshot was taken after I messed around with gluPerspective():


Here are some examples of my simple camera movement system. I was able to get the camera to pan left and right, up and down, and also tilt left and right and up and down. I used the home key to reset the camera to the origin. I made a simple model of an airplane to demonstrate my camera. Here are pictures of the plane after various camera movements:



// Camera4.cpp
// Demonstrates Camera Movement
// CSC 370 March 2009
// Michael Haizlip

#include
#include

// Rotation amounts
static GLfloat xRot = 0.0f;
static GLfloat yRot = 0.0f;
static GLfloat zRot = 0.0f;
static GLfloat zMove= 0.0f;
static GLfloat yMove= 0.0f;
static GLfloat xMove= 0.0f;


// Change viewing volume and viewport. Called when window is resized
void ChangeSize(int w, int h)
{
GLfloat fAspect;
// Prevent a divide by zero
if(h == 0)
h = 1;

// Set Viewport to window dimensions
glViewport(0, 0, w, h);

fAspect = (GLfloat)w/(GLfloat)h;

// Reset coordinate system
glMatrixMode(GL_PROJECTION);
glLoadIdentity();

// Produce the perspective projection
gluPerspective(60.0f, fAspect, 1.0, 400.0);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}


// This function does any needed initialization on the rendering
// context. Here it sets up and initializes the lighting for
// the scene.
void SetupRC()
{
// Light values and coordinates
GLfloat whiteLight[] = { 0.45f, 0.45f, 0.45f, 1.0f };
GLfloat sourceLight[] = { 0.25f, 0.25f, 0.25f, 1.0f };
GLfloat lightPos[] = { -50.f, 25.0f, 250.0f, 0.0f };

glEnable(GL_DEPTH_TEST); // Hidden surface removal
glFrontFace(GL_CCW); // Counter clock-wise polygons face out
glDisable(GL_CULL_FACE); // Do not calculate inside of jet

// Enable lighting
glEnable(GL_LIGHTING);

// Setup and enable light 0
glLightModelfv(GL_LIGHT_MODEL_AMBIENT,whiteLight);
glLightfv(GL_LIGHT0,GL_AMBIENT,sourceLight);
glLightfv(GL_LIGHT0,GL_DIFFUSE,sourceLight);
glLightfv(GL_LIGHT0,GL_POSITION,lightPos);
glEnable(GL_LIGHT0);

// Enable color tracking
glEnable(GL_COLOR_MATERIAL);

// Set Material properties to follow glColor values
glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);

// Black blue background
glClearColor(0.0f, 1.0f, 1.0f, 1.0f );
}

// Respond to arrow keys
void SpecialKeys(int key, int x, int y)
{
//Move Forwards and Backwards
if(key == GLUT_KEY_UP)
zMove += 5.0f;

if(key == GLUT_KEY_DOWN)
zMove -= 5.0f;

//Pan left and right
if(key == GLUT_KEY_RIGHT)
{
xMove -= 5.0f;
xRot -= 5.0f;
}
if(key == GLUT_KEY_LEFT)
{
xMove += 5.0f;
xRot += 5.0f;
}
//pan straight up and down
if(key == GLUT_KEY_PAGE_UP)
{
yRot += 5.0f;
yMove += 5.0f;
}
if(key == GLUT_KEY_PAGE_DOWN)
{
yRot -= 5.0f;
yMove -= 5.0f;
}
//reset to home
if(key == GLUT_KEY_HOME)
{
xRot = 0.0f;
yRot = 0.0f;
zRot = 0.0f;
zMove= 0.0f;
yMove= 0.0f;
xMove= 0.0f;
}
//rotate right
if(key == GLUT_KEY_INSERT)
xRot += 5.0f;
//rotate left
if(key == GLUT_KEY_END)
xRot -= 5.0f;

// Refresh the Window
glutPostRedisplay();
}


// Called to draw scene
void RenderScene(void)
{
float fZ,bZ;

// Clear the window with current clearing color
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

fZ = 100.0f;
bZ = -100.0f;

glLoadIdentity();

gluLookAt (xMove, yMove, zMove, xRot, yRot, zRot, 0.0, 1.0, 0.0);

glScalef (0.5,0.5,0.5);
// Set material color, Red
glColor3f(1.0f, 1.0f, 1.0f);

glBegin(GL_QUADS);

// Top Panel
glNormal3f(0.0f, 1.0f, 0.0f);
glVertex3f(-3.0f, 5.0f, fZ);
glVertex3f(3.0f, 5.0f, fZ);
glVertex3f(3.0f, 5.0f,bZ);
glVertex3f(-3.0f, 5.0f, bZ);

// Bottom Panel
glNormal3f(0.0f, -1.0f, 0.0f);
glVertex3f(-3.0f, -5.0f, fZ);
glVertex3f(-3.0f, -5.0f, bZ);
glVertex3f(3.0f, -5.0f,bZ);
glVertex3f(3.0f, -5.0f, fZ);

glNormal3f(1.0f, 1.0f, 0.0f);
glVertex3f(-6.0f, 0.0f, fZ);
glVertex3f(-3.0f, 5.0f, fZ);
glVertex3f(-3.0f, 5.0f, bZ);
glVertex3f(-6.0f, 0.0f,bZ);

glNormal3f(-1.0f, 1.0f, 0.0f);
glVertex3f(-3.0f, -5.0f, fZ);
glVertex3f(-6.0f, 0.0f, fZ);
glVertex3f(-6.0f, 0.0f,bZ);
glVertex3f(-3.0f, -5.0f, bZ);

glNormal3f(1.0f, -1.0f, 0.0f);
glVertex3f(3.0f, 5.0f, fZ);
glVertex3f(6.0f, 0.0f, fZ);
glVertex3f(6.0f, 0.0f,bZ);
glVertex3f(3.0f, 5.0f, bZ);

glNormal3f(-1.0f, -1.0f, 0.0f);
glVertex3f(6.0f, 0.0f, fZ);
glVertex3f(3.0f, -5.0f, fZ);
glVertex3f(3.0f, -5.0f, bZ);
glVertex3f(6.0f, 0.0f,bZ);

glEnd();

glBegin(GL_TRIANGLES);

glNormal3f(0.0f, 1.0f, 0.0f);
glColor3f(0.0f, 0.0f, 0.0f);
glVertex3f(0.0f, 0.0f, -120.0f);
glVertex3f(3.0f, 5.0f, bZ);
glVertex3f(6.0f, 0.0f, bZ);

glNormal3f(1.0f, 1.0f, 0.0f);
glVertex3f(0.0f, 0.0f, -120.0f);
glVertex3f(-3.0f, 5.0f, bZ);
glVertex3f(3.0f, 5.0f, bZ);

glNormal3f(-1.0f, 1.0f, 0.0f);
glVertex3f(0.0f, 0.0f, -120.0f);
glVertex3f(-6.0f, 0.0f, bZ);
glVertex3f(-3.0f, 5.0f, bZ);

glNormal3f(0.0f, -1.0f, 0.0f);
glVertex3f(0.0f, 0.0f, -120.0f);
glVertex3f(6.0f, 0.0f, bZ);
glVertex3f(3.0f, -5.0f, bZ);

glNormal3f(-1.0f, -1.0f, 0.0f);
glVertex3f(0.0f, 0.0f, -120.0f);
glVertex3f(3.0f, -5.0f, bZ);
glVertex3f(-3.0f, -5.0f, bZ);

glNormal3f(1.0f, -1.0f, 0.0f);
glVertex3f(0.0f, 0.0f, -120.0f);
glVertex3f(-3.0f, -5.0f, bZ);
glVertex3f(-6.0f, 0.0f, bZ);

glEnd();

glBegin(GL_QUADS);

glColor3f(0.0f, 0.0f, 0.0f);
glVertex3f(6.0f, 0.0f, fZ);
glVertex3f(60.0f, 0.0f, fZ);
glVertex3f(60.0f, 0.0f, 40.0f);
glVertex3f(6.0f, 0.0f,-40.0f);

glVertex3f(-6.0f, 0.0f,-40.0f);
glVertex3f(-60.0f, 0.0f, 40.0f);
glVertex3f(-60.0f, 0.0f, fZ);
glVertex3f(-6.0f, 0.0f, fZ);

glVertex3f(0.0f, 0.0f,fZ);
glVertex3f(0.0f, 50.0f, fZ);
glVertex3f(0.0f, 50.0f, 80.0f);
glVertex3f(0.0f, 0.0f, 50.0f);

glEnd();

// Buffer swap
glutSwapBuffers();
}

int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(800, 600);
glutCreateWindow("Perspective Projection");
glutReshapeFunc(ChangeSize);
glutSpecialFunc(SpecialKeys);
glutDisplayFunc(RenderScene);
SetupRC();
glutMainLoop();

return 0;
}

No comments:

Post a Comment