How to compile an example from the 'Red Book' in Visual C++ 6.0?

Hello,everybody, I am a newcomer and I am learning opengl now, I’ve got some problems in compiling the examples in the “red book”, for example, I got the following code from the book:

#include <GL/gl.h>
#include <GL/glu.h>
#include “aux.h”

void myinit(void)
{
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glShadeModel(GL_FLAT);
glClearColor(0.0, 0.0, 0.0, 0.0);
}

void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);

glColor4f(1.0, 1.0, 0.0, 0.75);
glRectf(0.0, 0.0, 0.5, 1.0);

glColor4f(0.0, 1.0, 1.0, 0.75);
glRectf(0.0, 0.0, 1.0, 0.5);

/* draw colored polygons in reverse order in upper right */
glColor4f (0.0, 1.0, 1.0, 0.75);
glRectf (0.5, 0.5, 1.0, 1.0);

glColor4f (1.0, 1.0, 0.0, 0.75);
glRectf (0.5, 0.5, 1.0, 1.0);

glFlush();

}

void myReshape(GLsizei w, GLsizei h)
{
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if (w <= h)
gluOrtho2D (0.0, 1.0, 0.0, 1.0*(GLfloat)h/(GLfloat)w);
else
gluOrtho2D (0.0, 1.0*(GLfloat)w/(GLfloat)h, 0.0, 1.0);
glMatrixMode(GL_MODELVIEW);
}

int main(int argc, char** argv)
{
auxInitDisplayMode (AUX_SINGLE | AUX_RGBA);
auxInitPosition (0, 0, 500, 500);
auxInitWindow (argv[0]);
myinit();
auxReshapeFunc (myReshape);
auxMainLoop(display);
}

since it has a main function, it should be compilable and generates an executable, but VC generates the following errors:

e:\program files\microsoft visual studio\vc98\include\gl\gl.h(1152) : error C2144: syntax error : missing ‘;’ before type ‘void’
e:\program files\microsoft visual studio\vc98\include\gl\gl.h(1152) : error C2501: ‘WINGDIAPI’ : missing storage-class or type specifiers
e:\program files\microsoft visual studio\vc98\include\gl\gl.h(1152) : fatal error C1004: unexpected end of file found

can anybody tell me why? Thanks so much!!!
Note : I have added the necessary lib files to the project.

try putting a #include<windows.h> before including any gl headers. is this an old version of the red book you got this from? i’m surprised they’re using the aux library rather than glut.

Thanks. But new problem occurs after I incude the windows.h file before the gl headers, VC seems to hang up compiling that very simple cpp file, I restarted the computer but the problem persists. Any idea about that?

Originally posted by SThomas:
try putting a #include<windows.h> before including any gl headers. is this an old version of the red book you got this from? i’m surprised they’re using the aux library rather than glut.

I edited your code to work with VC.net(7). You probably will not need the #include “stdafx.h”, but you may need the changes in the main function, especially for reshape func and mainloop func. Also, make sure you have either added the libraries to your project settings or added the #pragma comments to do same.

// test.cpp : Defines the entry point for the console application.
//

#include “stdafx.h”
#include “windows.h”
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glaux.h>
#pragma comment (lib, “glaux.lib”)
#pragma comment (lib, “opengl32.lib”)
#pragma comment (lib, “glu32.lib”)

void myinit(void)
{
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glShadeModel(GL_FLAT);
glClearColor(0.0, 0.0, 0.0, 0.0);
}

void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);

glColor4f(1.0, 1.0, 0.0, 0.75);
glRectf(0.0, 0.0, 0.5, 1.0);

glColor4f(0.0, 1.0, 1.0, 0.75);
glRectf(0.0, 0.0, 1.0, 0.5);

/* draw colored polygons in reverse order in upper right */
glColor4f (0.0, 1.0, 1.0, 0.75);
glRectf (0.5, 0.5, 1.0, 1.0);

glColor4f (1.0, 1.0, 0.0, 0.75);
glRectf (0.5, 0.5, 1.0, 1.0);

glFlush();
}

void myReshape(GLsizei w, GLsizei h)
{
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if (w <= h)
gluOrtho2D (0.0, 1.0, 0.0, 1.0*(GLfloat)h/(GLfloat)w);
else
gluOrtho2D (0.0, 1.0*(GLfloat)w/(GLfloat)h, 0.0, 1.0);
glMatrixMode(GL_MODELVIEW);
}

int main(int argc, char** argv)
{
auxInitDisplayMode (AUX_SINGLE | AUX_RGBA);
auxInitPosition (0, 0, 500, 500);
auxInitWindow (argv[0]);
myinit();
auxReshapeFunc ((AUXRESHAPEPROC)myReshape);
auxMainLoop((AUXMAINPROC)display);
}

[This message has been edited by shinpaughp (edited 02-22-2003).]

Yes! Your code works perfectly in VC6.0 too, thanks a lot. I do not have to port the code to my opengl framework code any more!