Code:
#include <math.h>
#include <gl\glut.h>
#include <gl\gl.h>
#include <gl\glu.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <dos.h>
static float angle=0.0,ratio;
float x=0.0f,y=1.75f,z=5.0f;
//Variables for the position of the camera in 3D space
float lx=0.0f,ly=0.0f,lz=-1.0f;
static GLint snowman_display_list;
int CameraUp = 0, CameraDown = 0;
//Integer values used for moving the camera POSITION in amounts along the Y axis
int LookAngleDown = 0, LookAngleUp = 0;
//Integer values used for moving the camera VIEW ANGLE - Y AXIS
void changeSize(int w, int h)
{
// Prevent a divide by zero, when window is too short
// (you cant make a window of zero width).
if(h == 0)
h = 1;
ratio = 1.0f * w / h;
// Reset the coordinate system before modifying
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
// Set the viewport to be the entire window
glViewport(0, 0, w, h);
// Set the clipping volume
gluPerspective(45,ratio,1,1000);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(x, y, z,
x + lx,y + ly,z + lz,
0.0f,1.0f,0.0f);
}
void drawSnowMan() {
glColor3f(1.0f, 1.0f, 1.0f);
// Draw Body
glTranslatef(0.0f ,0.75f, 0.0f);
glutSolidSphere(0.75f,20,20);
// Draw Head
glTranslatef(0.0f, 1.0f, 0.0f);
glutSolidSphere(0.25f,20,20);
// Draw Eyes
glPushMatrix();
glColor3f(0.0f,0.0f,0.0f);
glTranslatef(0.05f, 0.10f, 0.18f);
glutSolidSphere(0.05f,10,10);
glTranslatef(-0.1f, 0.0f, 0.0f);
glutSolidSphere(0.05f,10,10);
glPopMatrix();
// Draw Nose
glColor3f(1.0f, 0.5f , 0.5f);
glRotatef(0.0f,1.0f, 0.0f, 0.0f);
glutSolidCone(0.08f,0.5f,10,2);
}
GLuint createDL() {
GLuint snowManDL;
// Create the id for the list
snowManDL = glGenLists(1);
// start list
glNewList(snowManDL,GL_COMPILE);
// call the function that contains
// the rendering commands
drawSnowMan();
// endList
glEndList();
return(snowManDL);
}
void initScene() {
glEnable(GL_DEPTH_TEST);
snowman_display_list = createDL();
}
void orientMe(float ang) {
lx = sin(ang);
lz = -cos(ang);
glLoadIdentity();
gluLookAt(x, y, z,
x + lx,y + ly,z + lz,
0.0f,1.0f,0.0f);
}
void moveMeFlat(int direction) {
x = x + direction*(lx)*0.1;
z = z + direction*(lz)*0.1;
glLoadIdentity();
gluLookAt(x, y, z,
x + lx,y + ly,z + lz,
0.0f,1.0f,0.0f);
}
void renderScene(void) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//***********************************************//
// Code for the camera moving DOWN the Y axis
//***********************************************//
if (CameraDown > 0)
//If the camera should be moving down...
{
if (y > 0)
//if the y loc angle is greater than the middle of the screen(0) then...
y = y - 0.1;
//Move camera down (Y-Axis)
orientMe(angle);
//Re-view the world with the changes
CameraDown = CameraDown - 1;
//de-crement the camera down counter value
if (y <= 0)
// if the loc y has finally reached the middle of the screen
{
CameraUp = CameraDown;
CameraDown = 0;
}
}
//**************************************************
//***********************************************//
// Code for the camera moving UP the Y axis
//***********************************************//
if (CameraUp > 0)
{
y = y + 0.1;
//Move the camera up (Y-axis)
orientMe(angle);
//Re-view the world with the changes
CameraUp = CameraUp - 1;
//Decrement the camera up counter value
}
//*************************************************
//***********************************************//
// Code for the camera ANGLE moving UP the Y axis
//***********************************************//
//S has been pressed
if (LookAngleUp >= 1)
{
ly = ly + 0.005;
orientMe(angle);
LookAngleUp = LookAngleUp - 1;
if ((LookAngleUp == 0) && (ly != 0.0f))
{
LookAngleDown = 50;
orientMe(angle);
}
}
//**********************************************
//***********************************************//
// Code for the camera ANGLE moving down the Y axis
//***********************************************//
//X has been pressed
if (LookAngleDown >= 1)
//Camera view angle on the Y axis is movin downward
{
ly = ly - 0.005;
orientMe(angle);
LookAngleDown = LookAngleDown - 1;
/******************************************************/
/******************************************************/
/******************************************************/
//THIS IS WHAT I CAN'T WORK OUT! The first IF condition below executes, stating
//that the value of ly is 0.000000, yet the second IF statement which should
//execute because ly equals 0.000000 WON'T EXECUTE! ANY help wouLd be
//GREATLY appreciated!
//* NOTE! To activate the code below, Press the "s" button on your keyboard,
//wait for the view of the 3D world to return to it's centre postion
//(it only takes a few seconds)then watch the command prompt.
if (LookAngleDown == 0)
{
printf("ly should equal 0.000000\n");
printf("ly actually equals %f\n\n\n\n", ly);
}
if (ly == 0.000000)
{
printf("IF YOU CAN READ THIS CODE THEN YOU HAVE FIXED MY PROBLEM!\n\n\n\n");
}
//***********************************************************
/******************************************************/
/******************************************************/
if ((LookAngleDown == 0) && (ly == 0.00))
//Camera ANGLE has been moving down and has reached the middle of the screen
{
printf("HEY!!!!\n\n");
LookAngleDown = 0;
LookAngleUp = 0;
orientMe(angle);
}
else if ((LookAngleDown == 0) && (ly < 0.0f))
{
LookAngleUp = 50;
orientMe(angle);
}
}
//**************************************************
// Draw ground
glColor3f(0.9f, 0.9f, 0.9f);
glBegin(GL_QUADS);
glVertex3f(-100.0f, 0.0f, -100.0f);
glVertex3f(-100.0f, 0.0f, 100.0f);
glVertex3f( 100.0f, 0.0f, 100.0f);
glVertex3f( 100.0f, 0.0f, -100.0f);
glEnd();
// Draw 36 SnowMen
for(int i = -3; i < 3; i++)
for(int j=-3; j < 3; j++) {
glPushMatrix();
glTranslatef(i*10.0,0,j * 10.0);
glCallList(snowman_display_list);;
glPopMatrix();
}
glutSwapBuffers();
}
void inputKey(int key, int x, int y) {
switch (key) {
case GLUT_KEY_LEFT :
angle -= 0.01f;
orientMe(angle);break;
case GLUT_KEY_RIGHT :
angle +=0.01f;
orientMe(angle);break;
case GLUT_KEY_UP :
moveMeFlat(2);break;
case GLUT_KEY_DOWN :
moveMeFlat(-2);break;
}
}
void processNormalKeys(unsigned char key, int MouseLocX, int MouseLocY) {
if (key == 27)
{
exit(0);
}
if (key == 'a')
{
CameraDown = 0;
CameraUp = 20;
}
if (key == 'z')
{
CameraUp = 0;
CameraDown = 20;
}
if (key == 's')
{
LookAngleUp = 50;
}
if (key == 'x')
{
LookAngleDown = 50;
}
}
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowPosition(100,100);
glutInitWindowSize(640,360);
glutCreateWindow("SnowMen from 3D-Tech");
initScene();
glutSpecialFunc(inputKey);
glutKeyboardFunc(processNormalKeys);
glutDisplayFunc(renderScene);
glutIdleFunc(renderScene);
glutReshapeFunc(changeSize);
glutMainLoop();
fflush(0);
return(0);
}