Wednesday, April 25, 2012

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




No comments:

Post a Comment