Tutorial Part Three
Compilation: g++ -o crawl crawl.cpp -lglut -lGLU - lGL
run: ./crawl
Implementations for this tutorial
Allow Movement
New functions
void computePos(float deltaMove)
void computeDir(float deltaAngle)
void computeUp(float deltaUp)
register callbacks
glutSpecialFunc(pressKey);
glutIgnoreKeyRepeat(1);
glutSpecialUpFunc(releaseKey);
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <GL/glut.h>
#include <iostream>
#include <algorithm>
using namespace std;
// Global Variables
// Camera direction variables
float lx=0.0f;
float lz=-1.0f;
// XYZ position of the camera
float x=0.0f;
float z=5.0f;
float y = 1.0f;
//New global variables
float deltaAngle = 0.0f;
float deltaMove = 0.0f;
float deltaUp = 0.0f;
float angle = 0.0f;
//******************************************************
//Function for drawing a cube
void cubes()
{
glColor3f(1.0f, 1.0f, 1.0f);
glTranslatef(0.0f ,0.75f, 0.0f);
// Drawing cube with quad
//***********************************************************
glBegin(GL_QUADS);
glVertex3f( 1.0f, 1.0f, -1.0f); // Top-right of the quad (Top)
glVertex3f(-1.0f, 1.0f, -1.0f); // Top-left of the quad (Top)
glVertex3f(-1.0f, 1.0f, 1.0f); // Bottom-left of the quad (Top)
glVertex3f( 1.0f, 1.0f, 1.0f); // Bottom-right of the quad (Top)
glEnd();
//************************************************************
glBegin(GL_QUADS);
glVertex3f( 1.0f, -1.0f, 1.0f); // Top-right of the quad (Bottom)
glVertex3f(-1.0f, -1.0f, 1.0f); // Top-left of the quad (Bottom)
glVertex3f(-1.0f, -1.0f, -1.0f); // Bottom-left of the quad (Bottom)
glVertex3f( 1.0f, -1.0f, -1.0f); // Bottom-right of the quad (Bottom)
glEnd();
//************************************************************
glBegin(GL_QUADS);
glVertex3f( 1.0f, 1.0f, 1.0f); // Top-right of the quad (Front)
glVertex3f(-1.0f, 1.0f, 1.0f); // Top-left of the quad (Front)
glVertex3f(-1.0f, -1.0f, 1.0f); // Bottom-left of the quad (Front)
glVertex3f( 1.0f, -1.0f, 1.0f); // Bottom-right of the quad (Front)
glEnd();
//*************************************************************
glBegin(GL_QUADS);
glVertex3f( 1.0f, -1.0f, -1.0f); // Bottom-left of the quad (Back)
glVertex3f(-1.0f, -1.0f, -1.0f); // Bottom-right of the quad (Back)
glVertex3f(-1.0f, 1.0f, -1.0f); // Top-right of the quad (Back)
glVertex3f( 1.0f, 1.0f, -1.0f); // Top-left of the quad (Back)
glEnd();
//***********************************************************
glBegin(GL_QUADS);
glVertex3f(-1.0f, 1.0f, 1.0f); // Top-right of the quad (Left)
glVertex3f(-1.0f, 1.0f, -1.0f); // Top-left of the quad (Left)
glVertex3f(-1.0f, -1.0f, -1.0f); // Bottom-left of the quad (Left)
glVertex3f(-1.0f, -1.0f, 1.0f); // Bottom-right of the quad (Left)
glEnd();
//*************************************************************
glBegin(GL_QUADS);
glVertex3f( 1.0f, 1.0f, -1.0f); // Top-right of the quad (Right)
glVertex3f( 1.0f, 1.0f, 1.0f); // Top-left of the quad (Right)
glVertex3f( 1.0f, -1.0f, 1.0f); // Bottom-left of the quad (Right)
glVertex3f( 1.0f, -1.0f, -1.0f); // Bottom-right of the quad (Right)
glEnd();
}
//*************************************************************
//Will handle the resizing of the screen
void changeSize(int w, int h)
{
if (h == 0)
{
h = 1;
}
float ratio = w * 1.0 / h;
// Use the Projection Matrix
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
// Set the viewport to be the entire window
glViewport(0, 0, w, h);
// Set the correct perspective.
gluPerspective(45.0f, ratio, 0.1f, 150.0f);
// Get Back to the Modelview
glMatrixMode(GL_MODELVIEW);
}
//****************************************************************************
//New
void computePos(float deltaMove)
{
x += deltaMove * lx * 0.1f;
z += deltaMove * lz * 0.1f;
}
//*****************************************************************
//New
void computeDir(float deltaAngle)
{
angle += deltaAngle;
lx = sin(angle);
lz = -cos(angle);
}
//******************************************************************
//New
void computeUp(float deltaUp)
{
y += deltaUp *.01f;
}
//******************************************************************
void renderScene(void)
{
//movement has occured
if (deltaMove)
computePos(deltaMove);
if (deltaAngle)
computeDir(deltaAngle);
if (deltaUp)
computeUp(deltaUp);
// Clear Color and Depth Buffers
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Reset transformations
glLoadIdentity();
// Set the camera
//If conditions act as wall boundaries
if(y < 1)
{
y = 1;
}
if(y >= 15)
{
y = 14;
}
if(x > 49)
{
x = 49;
}
if(x < -49)
{
x = -49;
}
if(z < -49)
{
z = -49;
}
if(z > 49)
{
z = 49;
}
gluLookAt( x, y, z,
x+lx, y, z+lz,
0.0f, 1.0f, 0.0f);
glColor3f(.2f, .8f, 1.0f);
glBegin(GL_QUADS);
glVertex3f( 50.0f, 50.0f, 50.0f); // Top-right of the quad (Front)
glVertex3f(-50.0f, 50.0f, 50.0f); // Top-left of the quad (Front)
glVertex3f(-50.0f, -50.0f, 50.0f); // Bottom-left of the quad (Front)
glVertex3f( 50.0f, -50.0f, 50.0f); // Bottom-right of the quad (Front)
glEnd();
//**********************************************************************
glBegin(GL_QUADS);
glVertex3f( 50.0f, -50.0f, -50.0f); // Bottom-left of the quad (Back)
glVertex3f(-50.0f, -50.0f, -50.0f); // Bottom-right of the quad (Back)
glVertex3f(-50.0f, 50.0f, -50.0f); // Top-right of the quad (Back)
glVertex3f(50.0f, 50.0f, -50.0f); // Top-left of the quad (Back)
glEnd();
//**********************************************************************
glBegin(GL_QUADS);
glVertex3f(-50.0f, 50.0f, 50.0f); // Top-right of the quad (Left)
glVertex3f(-50.0f, 50.0f, -50.0f); // Top-left of the quad (Left)
glVertex3f(-50.0f, -50.0f, -50.0f); // Bottom-left of the quad (Left)
glVertex3f(-50.0f, -50.0f, 50.0f); // Bottom-right of the quad (Left)
glEnd();
//**********************************************************************
glBegin(GL_QUADS);
glVertex3f( 50.0f, 50.0f, -50.0f); // Top-right of the quad (Right)
glVertex3f( 50.0f, 50.0f, 50.0f); // Top-left of the quad (Right)
glVertex3f(50.0f, -50.0f, 50.0f); // Bottom-left of the quad (Right)
glVertex3f(50.0f, -50.0f, -50.0f); // Bottom-right of the quad (Right)
glEnd();
//******************************************************************************
// 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();
cubes();
glutSwapBuffers();
}
//****************************************************************************
//Initialization function
//Will be run once as start up
void init (void)
{
//Code for displaying instructions onto the terminal
printf("\n\n\nhello user\n\n");
printf("q to quit the program\n");
printf("r to reset camera\n");
printf("up key: move forward\n");
printf("down key: move backward\n");
printf("left/right key: rotate\n");
printf("F1/F2 key: move up/down\n");
// Enable smooth shading of color
glShadeModel(GL_SMOOTH);
//Enable depth test
glEnable(GL_DEPTH_TEST);
glClearDepth(1.0f);
glDepthFunc(GL_LEQUAL);
//For a nice perspecitve view
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
}
//********************************************************************************
//New
//Handles for pressing down on keys
void pressKey(int key, int xx, int yy)
{
switch (key)
{
case GLUT_KEY_LEFT : deltaAngle = -0.05f; break;
case GLUT_KEY_RIGHT : deltaAngle = 0.05f; break;
case GLUT_KEY_UP : deltaMove = 5.0f; break;
case GLUT_KEY_DOWN : deltaMove = -5.0f; break;
case GLUT_KEY_F1: deltaUp = 5.0f; break;
case GLUT_KEY_F2: deltaUp = -5.0f; break;
}
}
//New
//Handles for release of the keys
void releaseKey(int key, int x, int y)
{
switch (key)
{
case GLUT_KEY_LEFT :
case GLUT_KEY_RIGHT : deltaAngle = 0.0f; break;
case GLUT_KEY_UP :
case GLUT_KEY_DOWN : deltaMove = 0; break;
case GLUT_KEY_F1:
case GLUT_KEY_F2: deltaUp = 0.0f; break;
}
}
//Handles simple keys
void keyboard (unsigned char key, int xs, int ys)
{
switch(key)
{
case 'q':
exit(0);
break;
case 'r':
x=0.0f;
z=5.0f;
y = 1.0f;
lx=0.0f;
lz=-1.0f;
deltaAngle = 0.0f;
deltaMove = 0.0f;
deltaUp = 0.0f;
gluLookAt( x, y, z,
x+lx, y, z+lz,
0.0f, 1.0f, 0.0f);
break;
default:
break;
}
}
//********************************************************************************
//Main for our program
//Place at bottom of file
int main(int argc, char **argv)
{
// init GLUT and create window
// initalize variables and create window
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowPosition(100,100);
glutInitWindowSize(500,500);
glutCreateWindow("World Crawl Tutorial Part Three");
//GLUT CALLBACKS
//init will handle initialisations upon start up
init();
//function for actual rendering
glutDisplayFunc(renderScene);
glutIdleFunc(renderScene);
//function for handling resizes
glutReshapeFunc(changeSize);
//function for handling keyboard events
glutKeyboardFunc(keyboard);
glutSpecialFunc(pressKey);
glutIgnoreKeyRepeat(1);
glutSpecialUpFunc(releaseKey);
// enter GLUT event processing cycle
glutMainLoop();
//This should never hit
return 0;
}
No comments:
Post a Comment