Author Topic: using Code::Blocks/MinGW to compile simple glut tutorial<solved>  (Read 7555 times)

Zenchuck

  • Guest
using Code::Blocks/MinGW to compile simple glut tutorial<solved>
« on: September 02, 2007, 05:39:52 pm »
Hi all,

I realize that these forums are not meant to tutor new programmers but I'm hoping I can break the rules and post a programming help request here.

I am interested in following the tutorials on videotutorialsrock.com. I am unable to get the most simple code to compile.

Here it is:
Code
#include <iostream>
#include <stdlib.h> //Needed for "exit" function

//Include OpenGL header files, so that we can use OpenGL
#ifdef __APPLE__
#include <OpenGL/OpenGL.h>
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif

using namespace std;

//Called when a key is pressed
void handleKeypress(unsigned char key, //The key that was pressed
int x, int y) {    //The current mouse coordinates
switch (key) {
case 27: //Escape key
exit(0); //Exit the program
}
}

//Initializes 3D rendering
void initRendering() {
//Makes 3D drawing work when something is in front of something else
glEnable(GL_DEPTH_TEST);
}

//Called when the window is resized
void handleResize(int w, int h) {
//Tell OpenGL how to convert from coordinates to pixel values
glViewport(0, 0, w, h);

glMatrixMode(GL_PROJECTION); //Switch to setting the camera perspective

//Set the camera perspective
glLoadIdentity(); //Reset the camera
gluPerspective(45.0,                  //The camera angle
   (double)w / (double)h, //The width-to-height ratio
   1.0,                   //The near z clipping coordinate
   200.0);                //The far z clipping coordinate
}

//Draws the 3D scene
void drawScene() {
//Clear information from last draw
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glMatrixMode(GL_MODELVIEW); //Switch to the drawing perspective
glLoadIdentity(); //Reset the drawing perspective

glBegin(GL_QUADS); //Begin quadrilateral coordinates

//Trapezoid
glVertex3f(-0.7f, -1.5f, -5.0f);
glVertex3f(0.7f, -1.5f, -5.0f);
glVertex3f(0.4f, -0.5f, -5.0f);
glVertex3f(-0.4f, -0.5f, -5.0f);

glEnd(); //End quadrilateral coordinates

glBegin(GL_TRIANGLES); //Begin triangle coordinates

//Pentagon
glVertex3f(0.5f, 0.5f, -5.0f);
glVertex3f(1.5f, 0.5f, -5.0f);
glVertex3f(0.5f, 1.0f, -5.0f);

glVertex3f(0.5f, 1.0f, -5.0f);
glVertex3f(1.5f, 0.5f, -5.0f);
glVertex3f(1.5f, 1.0f, -5.0f);

glVertex3f(0.5f, 1.0f, -5.0f);
glVertex3f(1.5f, 1.0f, -5.0f);
glVertex3f(1.0f, 1.5f, -5.0f);

//Triangle
glVertex3f(-0.5f, 0.5f, -5.0f);
glVertex3f(-1.0f, 1.5f, -5.0f);
glVertex3f(-1.5f, 0.5f, -5.0f);

glEnd(); //End triangle coordinates

glutSwapBuffers(); //Send the 3D scene to the screen
}

int main(int argc, char** argv) {
//Initialize GLUT
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(400, 400); //Set the window size

//Create the window
glutCreateWindow("Basic Shapes - videotutorialsrock.com");
initRendering(); //Initialize rendering

//Set handler functions for drawing, keypresses, and window resizes
glutDisplayFunc(drawScene);
glutKeyboardFunc(handleKeypress);
glutReshapeFunc(handleResize);

glutMainLoop(); //Start the main loop.  glutMainLoop doesn't return.
return 0; //This line is never reached
}

I am able to get the code to compile if I add

Code
#include <windows.h>

but the resulting application failes to initialize.

Could someone please try to compile this code and explain to me how it was done?
I have spent many hours trying to get this simple bit of code to work.

Thankyou.
« Last Edit: September 03, 2007, 07:18:20 am by Zenchuck »

Zenchuck

  • Guest
Re: using Code::Blocks/MinGW to compile simple glut tutorial
« Reply #1 on: September 03, 2007, 07:10:17 am »
I finally got this code to compile by adding

Code
#define GLUT_DISABLE_ATEXIT_HACK

just before

Code
#include <windows.h>

I have no idea why this works so maybe someone could shed some light.

Quote
There can be some slight issues with the GLUT libraries downloaded from Nate Robins OpenGL GLUT for Win32. Make sure to read the README file, to find out what directories to put glut.dll, glut.lib, and glut.h into. Visual Studio .NET 2003 has an issue with an inconsistent definition of the exit() system function. This may be fixed by copying the declaration of exit() from stdlib.h into glut.h, replacing the one there. Instead, you could also just #define GLUT_DISABLE_ATEXIT_HACK before you include glut.h in your program. Visual C++ Express 2005 does not have this problem.
http://www.clarkson.edu/class/cs446/log.html
« Last Edit: September 03, 2007, 07:14:53 am by Zenchuck »