here is my code
#include <stdlib.h>
#include <math.h>
#include <stdio.h>
#include <GL/glut.h>
#define ESC 27
bool CameraMoving = false;
float xLocation = 0.0f;
float yLocation = 0.0f;
float zLocation = 0.0f;
double rotate_y = 0;
double rotate_x = 0;
double rotate_z = 0;
float x = 0.0, y = -5.0, z = 0.0;
float deltaMove = 0.0;
float lx = 0.0, ly = 1.0, lz=0.0;
float angle = 0.0;
float deltaAngle = 0.0;
int isDragging = 0;
int xDragStart = 0;
void mouseWheel();
void changeSize(int w, int h)
{
float ratio = ((float)w) / ((float)h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, ratio, 0.1, 100.0);
glMatrixMode(GL_MODELVIEW);
glViewport(0, 0, w, h);
}
void update(void)
{
if (deltaMove) {
x += deltaMove * lx * 0.1;
y += deltaMove * ly * 0.1;
z += deltaMove * lz * 0.1;
}
glutPostRedisplay();
}
void renderScene(void)
{
int i, j;
glClearColor(0.0, 0.7, 1.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
gluLookAt( x, y, 1.0, x + lx, y + ly, 1.0, 0.0, 0.0, 1.0);
// Draw ground - 200x200 square colored green
glColor3f(0.0, 0.7, 0.0);
glBegin(GL_QUADS);
glVertex3f(-150.0, -150.0, 0.0);
glVertex3f(-150.0, 150.0, 0.0);
glVertex3f(150.0, 150.0, 0.0);
glVertex3f(150.0, -150.0, 0.0);
glEnd();
glTranslatef(xLocation, 0.0f, 0.0f);
glRotatef(rotate_x, 1.0, 0.0, 0.0);
glRotatef(rotate_y, 0.0, 1.0, 0.0);
glRotatef(rotate_z, 0.0, 0.0, 1.0);
glTranslatef(0.0, 0.0, 2.75); // position head 2.17up from ground
glPushMatrix();
glBegin(GL_POLYGON);
glColor3f(1.0, 0.0, 0.0); glVertex3f(0.5, -0.5, -0.5); // P1 is red
glColor3f(0.0, 1.0, 0.0); glVertex3f(0.5, 0.5, -0.5); // P2 is green
glColor3f(0.0, 0.0, 1.0); glVertex3f(-0.5, 0.5, -0.5); // P3 is blue
glColor3f(1.0, 0.0, 1.0); glVertex3f(-0.5, -0.5, -0.5); // P4 is purple
glEnd();
// White side - BACK
glBegin(GL_POLYGON);
glColor3f(1.0, 1.0, 1.0);
glVertex3f(0.5, -0.5, 0.5);
glVertex3f(0.5, 0.5, 0.5);
glVertex3f(-0.5, 0.5, 0.5);
glVertex3f(-0.5, -0.5, 0.5);
glEnd();
// Purple side - RIGHT
glBegin(GL_POLYGON);
glColor3f(1.0, 0.0, 1.0);
glVertex3f(0.5, -0.5, -0.5);
glVertex3f(0.5, 0.5, -0.5);
glVertex3f(0.5, 0.5, 0.5);
glVertex3f(0.5, -0.5, 0.5);
glEnd();
// Green side - LEFT
glBegin(GL_POLYGON);
glColor3f(0.0, 1.0, 0.0);
glVertex3f(-0.5, -0.5, 0.5);
glVertex3f(-0.5, 0.5, 0.5);
glVertex3f(-0.5, 0.5, -0.5);
glVertex3f(-0.5, -0.5, -0.5);
glEnd();
// Blue side - TOP
glBegin(GL_POLYGON);
glColor3f(0.0, 0.0, 1.0);
glVertex3f(0.5, 0.5, 0.5);
glVertex3f(0.5, 0.5, -0.5);
glVertex3f(-0.5, 0.5, -0.5);
glVertex3f(-0.5, 0.5, 0.5);
glEnd();
// Red side - BOTTOM
glBegin(GL_POLYGON);
glColor3f(1.0, 0.0, 0.0);
glVertex3f(0.5, -0.5, -0.5);
glVertex3f(0.5, -0.5, 0.5);
glVertex3f(-0.5, -0.5, 0.5);
glVertex3f(-0.5, -0.5, -0.5);
glEnd();
glPopMatrix();
if (CameraMoving)
xLocation -= 0.005f;
else
xLocation += 0.005f;
if (xLocation < -4.0f)
CameraMoving = false; // Reverse our direction so we are moving down
else if (xLocation > 4.0f) // Else if we have gone down too far
CameraMoving = true; // Reverse our direction so we are moving up
glutSwapBuffers(); // Make it all visible
}
void processNormalKeys(unsigned char key, int xx, int yy)
{
if (key == ESC || key == 'q' || key == 'Q') exit(0);
if (key == 'd')
rotate_y += 10;
else if (key == 'a')
rotate_y -= 10;
else if (key == 'w')
rotate_x += 10;
else if (key == 'z')
rotate_x -= 10;
else if (key == 'e')
rotate_z += 10;
else if (key == 's')
rotate_z -= 10;
glutPostRedisplay();
}
void pressSpecialKey(int key, int xx, int yy)
{
switch (key) {
case GLUT_KEY_UP: deltaMove = 0.1; break;
case GLUT_KEY_DOWN: deltaMove = -0.1; break;
case GLUT_KEY_RIGHT:rotate_z += 10; break;
case GLUT_KEY_LEFT:rotate_z -= 10; break;
}
}
void MouseWheel(int wheel, int direction, int x, int y)
{
wheel = 0;
if (direction == -1)
{
//zoom -= 0.5;
deltaMove = -0.1;
}
else if (direction == +1)
{
//zoom += 0.5;
deltaMove = 0.1;
}
glutPostRedisplay();
}
void releaseSpecialKey(int key, int x, int y)
{
switch (key) {
case GLUT_KEY_UP: deltaMove = 0.0; break;
case GLUT_KEY_DOWN: deltaMove = 0.0; break;
}
}
void mouseMove(int x, int y)
{
if (isDragging) { // only when dragging
// update the change in angle
deltaAngle = (x - xDragStart) * 0.005;
// camera's direction is set to angle + deltaAngle
lx = -sin(angle + deltaAngle);
ly = cos(angle + deltaAngle);
}
}
void mouseButton(int button, int state, int x, int y)
{
if (button == GLUT_LEFT_BUTTON) {
if (state == GLUT_DOWN) { // left mouse button pressed
isDragging = 1; // start dragging
xDragStart = x; // save x where button first pressed
}
else { /* (state = GLUT_UP) */
angle += deltaAngle; // update camera turning angle
isDragging = 0; // no longer dragging
}
}
}
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
glutInitWindowPosition(100, 100);
glutInitWindowSize(800, 400);
glutCreateWindow("My Camera Program");
glutReshapeFunc(changeSize); // window reshape callback
glutDisplayFunc(renderScene); // (re)display callback
glutIdleFunc(update); // incremental update
glutIgnoreKeyRepeat(1); // ignore key repeat when holding key down
glutMouseFunc(mouseButton); // process mouse button push/release
glutMotionFunc(mouseMove); // process mouse dragging motion
//glutMouseWheelFunc(MouseWheel);
glutKeyboardFunc(processNormalKeys); // process standard key clicks
glutSpecialFunc(pressSpecialKey); // process special key pressed
glutSpecialUpFunc(releaseSpecialKey); // process special key release
// OpenGL init
glEnable(GL_DEPTH_TEST);
glutMainLoop();
return 0;
}