Monday, April 30, 2012

Tutorial Four outcome


Tutorial Part 4:C++, OpenGL, Linux

Tutorial Part Four
Compilation: g++ -o crawl crawl.cpp -lglut -lGLU - lGL
run: ./crawl


This tutorial will deal with Textures

Note for the image files that are going to be used:
The size should be a power of 2, the file should be of type bmp
I will try doing a tutorial for other file types later

Will load in the textures once in the init function


   


#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;


float deltaAngle = 0.0f;
float deltaMove = 0.0f;
float deltaUp = 0.0f;
float angle = 0.0f;

//New global variables
//Name these what ever you like
GLuint texture_stone;
GLuint texture_wood;
GLuint texture_space;



 //New global variables
//Stuff for reading image file
unsigned char header[54];
unsigned int dataPos;
unsigned int image_width, image_height;
unsigned int imageSize;
unsigned char * data;
//******************************************************
//Read our BMP file
//******************************************************************************
GLuint loadBMP_custom(const char * imagepath, GLuint textureID)
{
    FILE * file = fopen(imagepath, "rb");
    if(!file)
    {
        printf("Image could not be opened\n");
        return 0;
    }
    if(fread(header, 1, 54, file)!= 54)
    {
        printf("Not a correct BMP file\n");
        return false;
    }
    if(header[0]!= 'B' || header[1] != 'M')
    {
        printf("Not a correct BMP file\n");
        return 0;
    }
   
    dataPos = *(int*)&(header[0x0A]);
    imageSize = *(int*)&(header[0x22]);
    image_width = *(int*)&(header[0x12]);
    image_height = *(int*)&(header[0x16]);
    if(imageSize == 0)
    {
        imageSize = image_width * image_height * 3;
    }
    if(dataPos == 0)
    {
        dataPos = 54;
    }
    data = new unsigned char [imageSize];
    fread(data,1,imageSize,file);
    fclose(file);
    glGenTextures(1, &textureID);
    glBindTexture(GL_TEXTURE_2D, textureID);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, image_width, image_height, 0 ,GL_BGR, GL_UNSIGNED_BYTE, data);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    free(data);
    return textureID;
}

//Function for drawing a cube
void cubes()
{

    glColor3f(1.0f, 1.0f, 1.0f);
    glTranslatef(0.0f ,0.75f, 0.0f);

glBegin(GL_QUADS);
      glNormal3f( 1.0f, 1.0f, -1.0f);
      glTexCoord2f(1.0f, 1.0f);
      glVertex3f( 1.0f, 1.0f, -1.0f); // Top-right of the quad (Top)
    
      glNormal3f(-1.0f, 1.0f, -1.0f);
      glTexCoord2f(0.0f, 1.0f);
      glVertex3f(-1.0f, 1.0f, -1.0f); // Top-left of the quad (Top)
    
      glNormal3f(-1.0f, 1.0f,  1.0f);
      glTexCoord2f(0.0f, 0.0f);
      glVertex3f(-1.0f, 1.0f,  1.0f); // Bottom-left of the quad (Top)
    
      glNormal3f( 1.0f, 1.0f,  1.0f);
      glTexCoord2f(1.0f, 0.0f);
      glVertex3f( 1.0f, 1.0f,  1.0f); // Bottom-right of the quad (Top)
      glEnd();
    

//**********************************************************************
      glBegin(GL_QUADS);
      glNormal3f( 1.0f, -1.0f,  1.0f);
      glTexCoord2f(1.0f, 1.0f);
      glVertex3f( 1.0f, -1.0f,  1.0f); // Top-right of the quad (Bottom)
    
      glNormal3f(-1.0f, -1.0f,  1.0f);
      glTexCoord2f(0.0f, 1.0f);
      glVertex3f(-1.0f, -1.0f,  1.0f); // Top-left of the quad (Bottom)
    
      glNormal3f(-1.0f, -1.0f, -1.0f);
      glTexCoord2f(0.0f, 0.0f);
      glVertex3f(-1.0f, -1.0f, -1.0f); // Bottom-left of the quad (Bottom)
    
      glNormal3f( 1.0f, -1.0f, -1.0f);
      glTexCoord2f(1.0f, 0.0f);
      glVertex3f( 1.0f, -1.0f, -1.0f); // Bottom-right of the quad (Bottom)
      glEnd();
//**********************************************************************
      glBegin(GL_QUADS);
      glNormal3f( 1.0f,  1.0f, 1.0f);
      glTexCoord2f(1.0f, 1.0f);
      glVertex3f( 1.0f,  1.0f, 1.0f);  // Top-right of the quad (Front)
    
      glNormal3f(-1.0f,  1.0f, 1.0f);
      glTexCoord2f(0.0f, 1.0f);
      glVertex3f(-1.0f,  1.0f, 1.0f);  // Top-left of the quad (Front)
    
      glNormal3f(-1.0f, -1.0f, 1.0f);
      glTexCoord2f(0.0f, 0.0f);
      glVertex3f(-1.0f, -1.0f, 1.0f);  // Bottom-left of the quad (Front)
    
      glNormal3f( 1.0f, -1.0f, 1.0f);
      glTexCoord2f(1.0f, 0.0f);
      glVertex3f( 1.0f, -1.0f, 1.0f);  // Bottom-right of the quad (Front)
      glEnd();
//**********************************************************************
      glBegin(GL_QUADS);
      glNormal3f( 1.0f, -1.0f, -1.0f);
      glTexCoord2f(1.0f, 1.0f);
      glVertex3f( 1.0f, -1.0f, -1.0f); // Bottom-left of the quad (Back)
    
      glNormal3f(-1.0f, -1.0f, -1.0f);
      glTexCoord2f(0.0f, 1.0f);
      glVertex3f(-1.0f, -1.0f, -1.0f); // Bottom-right of the quad (Back)
    
      glNormal3f(-1.0f,  1.0f, -1.0f);
      glTexCoord2f(0.0f, 0.0f);
      glVertex3f(-1.0f,  1.0f, -1.0f); // Top-right of the quad (Back)
    
      glNormal3f( 1.0f,  1.0f, -1.0f);
      glTexCoord2f(1.0f, 0.0f);
      glVertex3f( 1.0f,  1.0f, -1.0f); // Top-left of the quad (Back)
      glEnd();
//**********************************************************************
      glBegin(GL_QUADS);
      glNormal3f(-1.0f,  1.0f,  1.0f);
      glTexCoord2f(1.0f, 1.0f);
      glVertex3f(-1.0f,  1.0f,  1.0f); // Top-right of the quad (Left)
    
      glNormal3f(-1.0f,  1.0f, -1.0f);
      glTexCoord2f(0.0f, 1.0f);
      glVertex3f(-1.0f,  1.0f, -1.0f); // Top-left of the quad (Left)
    
      glNormal3f(-1.0f, -1.0f, -1.0f);
      glTexCoord2f(0.0f, 0.0f);
      glVertex3f(-1.0f, -1.0f, -1.0f); // Bottom-left of the quad (Left)
    
      glNormal3f(-1.0f, -1.0f,  1.0f);
      glTexCoord2f(1.0f, 0.0f);
      glVertex3f(-1.0f, -1.0f,  1.0f); // Bottom-right of the quad (Left)
      glEnd();
//**********************************************************************
      glBegin(GL_QUADS);
      glNormal3f( 1.0f,  1.0f, -1.0f);
      glTexCoord2f(1.0f, 1.0f);
      glVertex3f( 1.0f,  1.0f, -1.0f); // Top-right of the quad (Right)
    
      glNormal3f( 1.0f,  1.0f,  1.0f);
      glTexCoord2f(0.0f, 1.0f);
      glVertex3f( 1.0f,  1.0f,  1.0f); // Top-left of the quad (Right)
    
      glNormal3f( 1.0f, -1.0f,  1.0f);
      glTexCoord2f(0.0f, 0.0f);
      glVertex3f( 1.0f, -1.0f,  1.0f); // Bottom-left of the quad (Right)
    
      glNormal3f( 1.0f, -1.0f, -1.0f);
      glTexCoord2f(1.0f, 0.0f);
      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);
      //Draw stone walls**************************************************************
      glBindTexture(GL_TEXTURE_2D, texture_stone);
      glEnable( GL_TEXTURE_2D );
      glBegin(GL_QUADS);
      glNormal3f( 50.0f, 50.0f, 50.0f);
      glTexCoord2f(50.0f, 50.0f);
      glVertex3f( 50.0f,  50.0f, 50.0f);  // Top-right of the quad (Front)
    
      glNormal3f(-50.0f,  50.0f, 50.0f);
      glTexCoord2f(0.0f, 50.0f);
      glVertex3f(-50.0f,  50.0f, 50.0f);  // Top-left of the quad (Front)
    
      glNormal3f(-50.0f, -50.0f, 50.0f);
      glTexCoord2f(0.0f, 0.0f);
      glVertex3f(-50.0f, -50.0f, 50.0f);  // Bottom-left of the quad (Front)
    
      glNormal3f( 50.0f, -50.0f, 50.0f);
      glTexCoord2f(50.0f, 0.0f);
      glVertex3f( 50.0f, -50.0f, 50.0f);  // Bottom-right of the quad (Front)
      glEnd();
//**********************************************************************
      glBegin(GL_QUADS);
      glNormal3f( 50.0f, -50.0f, -50.0f);
      glTexCoord2f(50.0f, 50.0f);
      glVertex3f( 50.0f, -50.0f, -50.0f); // Bottom-left of the quad (Back)
    
      glNormal3f(-50.0f, -50.0f, -50.0f);
      glTexCoord2f(0.0f, 50.0f);
      glVertex3f(-50.0f, -50.0f, -50.0f); // Bottom-right of the quad (Back)
    
      glNormal3f(-50.0f,  50.0f, -50.0f);
      glTexCoord2f(0.0f, 0.0f);
      glVertex3f(-50.0f,  50.0f, -50.0f); // Top-right of the quad (Back)
    
      glNormal3f( 50.0f,  50.0f, -50.0f);
      glTexCoord2f(50.0f, 0.0f);
      glVertex3f(50.0f,  50.0f, -50.0f); // Top-left of the quad (Back)
      glEnd();
//**********************************************************************
      glBegin(GL_QUADS);
      glNormal3f(-50.0f,  50.0f,  50.0f);
      glTexCoord2f(50.0f, 50.0f);
      glVertex3f(-50.0f,  50.0f,  50.0f); // Top-right of the quad (Left)
    
      glNormal3f(-50.0f,  50.0f, -50.0f);
      glTexCoord2f(0.0f, 50.0f);
      glVertex3f(-50.0f,  50.0f, -50.0f); // Top-left of the quad (Left)
    
      glNormal3f(-50.0f, -50.0f, -50.0f);
      glTexCoord2f(0.0f, 0.0f);
      glVertex3f(-50.0f, -50.0f, -50.0f); // Bottom-left of the quad (Left)
    
      glNormal3f(-50.0f, -50.0f,  50.0f);
      glTexCoord2f(50.0f, 0.0f);
      glVertex3f(-50.0f, -50.0f,  50.0f); // Bottom-right of the quad (Left)
      glEnd();
//**********************************************************************
      glBegin(GL_QUADS);
      glNormal3f(50.0f,  50.0f, -50.0f);
      glTexCoord2f(50.0f, 50.0f);
      glVertex3f( 50.0f,  50.0f, -50.0f); // Top-right of the quad (Right)
    
      glNormal3f(50.0f, 50.0f, 50.0f);
      glTexCoord2f(0.0f, 50.0f);
      glVertex3f( 50.0f,  50.0f,  50.0f); // Top-left of the quad (Right)
    
      glNormal3f( 50.0f, -50.0f,  50.0f);
      glTexCoord2f(0.0f, 0.0f);
      glVertex3f(50.0f, -50.0f,  50.0f); // Bottom-left of the quad (Right)
    
      glNormal3f( 50.0f, -50.0f, -50.0f);
      glTexCoord2f(50.0f, 0.0f);
      glVertex3f(50.0f, -50.0f, -50.0f); // Bottom-right of the quad (Right)
         glEnd();

//******************************************************************************
    // Draw ground
    glBindTexture(GL_TEXTURE_2D, texture_wood);
    glColor3f(0.9f, 0.9f, 0.9f);
    glBegin(GL_QUADS);
        glTexCoord2f(100.0f, 100.0f);
        glVertex3f(-100.0f, 0.0f, -100.0f);
        glTexCoord2f(50.0f, 100.0f);
        glVertex3f(-100.0f, 0.0f,  100.0f);
        glTexCoord2f(0.0f, 0.0f);
        glVertex3f( 100.0f, 0.0f,  100.0f);
        glTexCoord2f(100.0f, 50.0f);
        glVertex3f( 100.0f, 0.0f, -100.0f);
    glEnd();
    glBindTexture(GL_TEXTURE_2D, texture_space);    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);

     texture_stone = loadBMP_custom("stone.bmp", texture_stone);
     texture_wood= loadBMP_custom("wood.bmp", texture_wood);
     texture_space = loadBMP_custom("space.bmp", texture_space);
     glEnable( GL_TEXTURE_2D );

//********************************************************************************





//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;
    }
}










//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;
}

Wednesday, April 25, 2012

World Crawl Tutorial Part 3: C++, OpenGl, linux

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;
}

World Crawl Part Two: C++, Opengl, linux

Tutorial Part Two
Compilation: g++ -o crawl crawl.cpp -lglut -lGLU - lGL
run: ./crawl

Implementations for this tutorial
Function for resizing the screen
register callbacks
   
    glutDisplayFunc(renderScene);
    glutIdleFunc(renderScene);
    glutReshapeFunc(changeSize);

Capabilities to render objects in scene
    Draws a cube, a floor, a wall

#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;
//******************************************************


//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);
}
//****************************************************************************



void renderScene(void)
{
    // Clear Color and Depth Buffers
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    // Reset transformations
    glLoadIdentity();
  
     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");
   
     // 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);

//********************************************************************************


//Handles simple keys
void keyboard (unsigned char key, int xs, int ys)

{
       switch(key)
      {
            case 'q':
                        exit(0);
                        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 Two");

    //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);

    // enter GLUT event processing cycle
    glutMainLoop();
  
    //This should never hit
    return 0;
}




World Crawl Tutorial C++, OpenGL, Linux

Going to take the tutorials very slow and keep adding on.
Ideal for first 3D course for University students. Click an ad if you like, I get money that way. cheers

Part One: (starting simple) Implementation of key board event

Notes for compilation: assuming you saved it as crawl and are in the right directory.

g++ -o crawl crawl.cpp -lglut -lGLU -lGL
or simply make your own makefile.
run by ./crawl


Will add on a lot to functions as tutorial progresses
The final product of this section of code should be a transparent screen
that can be closed by hitting the keyboard letter q

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <GL/glut.h>
#include <iostream>
#include <algorithm>
using namespace std;



//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");
    
     // 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); 
}
//****************************************************************




//Handles simple keys
void keyboard (unsigned char key, int xs, int ys)
{
       switch(key)
      {
            case 'q':
                        exit(0);
                        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 One");


    //init will handle initialisations upon start up
    init();


    //function for handling keyboard events
    glutKeyboardFunc(keyboard);

    // enter GLUT event processing cycle
    glutMainLoop();
   
    //This should never hit
    return 0;
}