// 6/7/06
// Andrew
// OpenGL 3d engine using glut
#include <GL/glut.h>
#include <cmath>
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
using namespace std;
// window global variables
int WINDOW_WIDTH = 800;
int WINDOW_HEIGHT = 600;
int windowids[1]; // can be used to store multiple window ids in case there are multiple windows
char title[13] = "test engine";
// call back functions to be used
void initializecallbacks();
void displaycallback();
void keyboardcallback(unsigned char key, int x, int y);
void passivemotioncallback(int x, int y);
void reshapecallback(int w, int h);
void timercallback(int time);
// setup visual requirments
void initializevisuals();
// variables used for movement
const float piover180 = 0.0174532925f; // pi / 180, for degree to radian transform
float heading; // direction character is facing
float xpos; // x-axis position of the player
float zpos; // z-axis position of the player
float ypos = 0.5; // y-axis position of the player
GLfloat yrot; // y-axis rotation
GLfloat lookupdown = 0.0f; // value to look up or down
GLfloat z=0.0f; // Depth Into The Screen
int mousebasex = WINDOW_WIDTH/2;
int mousebasey = WINDOW_HEIGHT/2;
int main(int argc, char* argv[])
{
glutInit(&argc, argv);
initializevisuals();
initializecallbacks();
glutMainLoop();
return 0;
}
//----------------------------------------------------------------------------//
// call back section //
//----------------------------------------------------------------------------//
void initializecallbacks()
{
// setting up the call back functions
glutDisplayFunc(displaycallback); // display call back
glutKeyboardFunc(keyboardcallback); // keyboard call back
glutPassiveMotionFunc(passivemotioncallback); // passive mouse motion call back
glutReshapeFunc(reshapecallback); // screen reshape call back
glutTimerFunc(15, timercallback, 0); // timer call back
}
void keyboardcallback(unsigned char key, int x, int y)
{
switch (key)
{
// miscellaneous input
case 27: // 'esc'
exit(0); // end the program
break;
// movement input
case 'a':
xpos -= (float)sin(heading*piover180 + 90.0f*piover180) * 0.05f;
zpos -= (float)cos(heading*piover180 + 90.0f*piover180) * 0.05f;
break;
case 'd':
xpos += (float)sin(heading*piover180 + 90.0f*piover180) * 0.05f;
zpos += (float)cos(heading*piover180 + 90.0f*piover180) * 0.05f;
break;
case 'w':
xpos -= (float)sin(heading*piover180) * 0.05f;
zpos -= (float)cos(heading*piover180) * 0.05f;
break;
case 's':
xpos += (float)sin(heading*piover180) * 0.05f;
zpos += (float)cos(heading*piover180) * 0.05f;
}
int dims[4];
glGetIntegerv(GL_VIEWPORT, dims);
int dx = x - mousebasex;
int dy = y - mousebasey;
mousebasex = x;
mousebasey = y;
// if (x < 20)
// {
// glutWarpPointer(dims[2]/2, y);
// mousebasex = dims[2]/2;
// }
// if (x > (dims[2]/2 - 20))
// {
// glutWarpPointer(dims[2]/2, y);
// mousebasex = dims[2]/2;
// }
lookupdown += dy * 0.5f;
heading -= dx * 0.5f;
if(heading < 0.0f)
heading = heading + 360.0f;
else if(heading > 360.0f)
heading = heading - 360.0f;
yrot = heading;
}
void passivemotioncallback(int x, int y)
{
int dims[4];
glGetIntegerv(GL_VIEWPORT, dims);
int dx = x - mousebasex;
int dy = y - mousebasey;
mousebasex = x;
mousebasey = y;
if (x < 20)
{
glutWarpPointer(dims[2]/2, y);
mousebasex = dims[2]/2;
}
if (x > (dims[2] - 20))
{
glutWarpPointer(dims[2]/2, y);
mousebasex = dims[2]/2;
}
lookupdown += dy * 0.5f;
heading -= dx * 0.5f;
if(heading < 0.0f)
heading = heading + 360.0f;
else if (heading > 360.0f)
heading = heading - 360.0f;
yrot = heading;
}
void reshapecallback(int w, int h)
{
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION); // Select The Projection Matrix
glLoadIdentity(); // Reset The Projection Matrix
// Calculate The Aspect Ratio Of The Window
gluPerspective(45.0f,(GLfloat)w/(GLfloat)h,0.1f,100.0f);
glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix
glLoadIdentity(); // Reset The Modelview Matrix
}
void timercallback(int time)
{
glutPostRedisplay();
glutTimerFunc(10, timercallback, 0);
}
void displaycallback()
{
int dims[4];
glGetIntegerv(GL_VIEWPORT, dims);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear The Screen And The Depth Buffer
glMatrixMode(GL_MODELVIEW);
glLoadIdentity(); // Reset The View
GLfloat xtrans = -xpos;
GLfloat ztrans = -zpos;
GLfloat ytrans = 0; // -walkbias-0.25f;
GLfloat sceneroty = 360.0f - yrot;
glRotatef(lookupdown,1.0f,0,0);
glRotatef(sceneroty,0,1.0f,0);
glTranslatef(xtrans, ytrans, ztrans);
glBegin(GL_QUADS);
glColor3f(0.0f,1.0f,0.0f); // Set The Color To Green
glVertex3f( 0.5f, 0.5f,-0.5f); // Top Right Of The Quad (Top)
glVertex3f(-0.5f, 0.5f,-0.5f); // Top Left Of The Quad (Top)
glVertex3f(-0.5f, 0.5f, 0.5f); // Bottom Left Of The Quad (Top)
glVertex3f( 0.5f, 0.5f, 0.5f); // Bottom Right Of The Quad (Top)
glColor3f(1.0f,0.5f,0.0f); // Set The Color To Orange
glVertex3f( 0.5f,-0.5f, 0.5f); // Top Right Of The Quad (Bottom)
glVertex3f(-0.5f,-0.5f, 0.5f); // Top Left Of The Quad (Bottom)
glVertex3f(-0.5f,-0.5f,-0.5f); // Bottom Left Of The Quad (Bottom)
glVertex3f( 0.5f,-0.5f,-0.5f); // Bottom Right Of The Quad (Bottom)
glColor3f(1.0f,0.0f,0.0f); // Set The Color To Red
glVertex3f( 0.5f, 0.5f, 0.5f); // Top Right Of The Quad (Front)
glVertex3f(-0.5f, 0.5f, 0.5f); // Top Left Of The Quad (Front)
glVertex3f(-0.5f,-0.5f, 0.5f); // Bottom Left Of The Quad (Front)
glVertex3f( 0.5f,-0.5f, 0.5f); // Bottom Right Of The Quad (Front)
glColor3f(1.0f,1.0f,0.0f); // Set The Color To Yellow
glVertex3f( 0.5f,-0.5f,-0.5f); // Bottom Left Of The Quad (Back)
glVertex3f(-0.5f,-0.5f,-0.5f); // Bottom Right Of The Quad (Back)
glVertex3f(-0.5f, 0.5f,-0.5f); // Top Right Of The Quad (Back)
glVertex3f( 0.5f, 0.5f,-0.5f); // Top Left Of The Quad (Back)
glColor3f(0.0f,0.0f,1.0f); // Set The Color To Blue
glVertex3f(-0.5f, 0.5f, 0.5f); // Top Right Of The Quad (Left)
glVertex3f(-0.5f, 0.5f,-0.5f); // Top Left Of The Quad (Left)
glVertex3f(-0.5f,-0.5f,-0.5f); // Bottom Left Of The Quad (Left)
glVertex3f(-0.5f,-0.5f, 0.5f); // Bottom Right Of The Quad (Left)
glColor3f(1.0f,0.0f,1.0f); // Set The Color To Violet
glVertex3f( 0.5f, 0.5f,-0.5f); // Top Right Of The Quad (Right)
glVertex3f( 0.5f, 0.5f, 0.5f); // Top Left Of The Quad (Right)
glVertex3f( 0.5f,-0.5f, 0.5f); // Bottom Left Of The Quad (Right)
glVertex3f( 0.5f,-0.5f,-0.5f); // Bottom Right Of The Quad (Right)
glEnd(); // Done Drawing The Quad
glFlush();
glutSwapBuffers();
}
void initializevisuals()
{
// creating the window and drawing buffers
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH); // create double buffer and z-depth buffer
glutInitWindowSize(WINDOW_WIDTH, WINDOW_HEIGHT); // create window using predetermined window sizes
glutInitWindowPosition(0,0);
windowids[0]=glutCreateWindow(title);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // set background color to white
glClearDepth(1.0); // enables clearing of the depth buffer
glEnable(GL_DEPTH_TEST); // enable depth testing
glDepthFunc(GL_LESS); // the type of depth test to do
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // really nice perspective calculations, got from NeHe tutorial 10
int dims[4];
glGetIntegerv(GL_VIEWPORT, dims);
glMatrixMode(GL_PROJECTION); // Select The Projection Matrix
glLoadIdentity(); // Reset The Projection Matrix
// Calculate The Aspect Ratio Of The Window
gluPerspective(45.0f,(GLfloat)dims[2]/(GLfloat)dims[3],0.1f,100.0f);
glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix
glLoadIdentity(); // Reset The Modelview Matrix
}