Wednesday, April 25, 2012

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





No comments:

Post a Comment