Must be the biggest beginner here

I have the book
“Beginning OpenGL Game Programming” by Dave Astle and Kevin Hawkins

I do not recommend this book primarily because of it’s lack of help references for errors and it’s claim to a full support website when it’s under construction.

But my question.

The book in Chapter 2 provides a code example to make a rotating triangle.

After typing out this code and copying the neccisary files from the cd for classes and such It won’t compile.

The errors come from gl.h header file and are along the lines of missing semicolon and un expected end of file. I have MSVC++ 6.0 and these headers come standard, so i have no reason to beleive that is the problem.

Also the cd provides these code examples within workspaces. The workspaces with the code on the cd will compile fine and run, however copying the same exact code to a different location unsing different workspaces causes the same compilation errors that the other code provides

Yes I included <gl/gl.h> and <gl/glu.h> and I also added opengl32.lib and glu32.lib to my project and neither of those are the problems.

The problem does exist in the workspace, or maybe in my version of C++ but that doesn’t explain why the books version works.

I’m gonna emphesise this once more to avoid confusion. The source code on the cd if compiled using the provided workspace and project compile properly and run properly. Copying the same exact source code into a new folder and using a new project and a new workspace causes errors during compiling from the standard MSVC++6.0 header file gl.h. These errors are:
c:\program files\microsoft visual studio\vc98\include\gl\gl.h(1151) : error C2144: syntax error : missing ‘;’ before type ‘void’
c:\program files\microsoft visual studio\vc98\include\gl\gl.h(1151) : error C2501: ‘WINGDIAPI’ : missing storage-class or type specifiers
c:\program files\microsoft visual studio\vc98\include\gl\gl.h(1151) : fatal error C1004: unexpected end of file found

Thank you for your time

The order in which you include header files is important.

So make sure you include windows.h (you need that) before any of the opengl headers.

Like this:

#include "windows.h"
#include "gl/gl.h"

Well you get the picture.

I already have windows.h included I’ll show you the code… see if you can make it work

in Gltest.cpp:

#define WIN32_LEAN_AND_MEAN
#define WIN32_EXTRA_LEAN

#include <windows.h>
#include <gl/gl.h>
#include <gl/glu.h>

#include “CGfxOpenGL.h”

bool exiting = false;
long windowW = 800;
long windowH = 600;
long windowBits = 32;
bool fullscreen = false;
HDC hDC;

CGfxOpenGL *g_glRender = NULL;

void SetupPixelFormat (HDC hDC)
{
int pixelFormat;

PIXELFORMATDESCRIPTOR pdf =
{
	sizeof(PIXELFORMATDESCRIPTOR),
		1,
		PFD_SUPPORT_OPENGL |
		PFD_DRAW_TO_WINDOW |
		PFD_DOUBLEBUFFER,
		PFD_TYPE_RGBA,
		32,
		0, 0, 0, 0, 0, 0,
		0,
		0,
		0,
		0, 0, 0, 0,
		16,
		0,
		0,
		PFD_MAIN_PLANE,
		0,
		0, 0, 0,
};

pixelFormat = ChoosePixelFormat(hDC, &pdf);
SetPixelFormat(hDC, pixelFormat, &pdf);

}

LRESULT CALLBACK MainWindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
static HDC hDC;
static HGLRC hRC;
int height, width;

switch (uMsg)
{
case WM_CREATE:
	hDC = GetDC(hWnd);
	SetupPixelFormat(hDC);
	hRC = wglCreateContext(hDC);
	wglMakeCurrent(hDC, hRC);
	break;
case WM_DESTROY:
case WM_QUIT:
case WM_CLOSE:
	wglMakeCurrent(hDC, NULL);
	wglDeleteContext(hRC);

	PostQuitMessage(0);
	break;
case WM_SIZE:
	height = HIWORD(lParam);
	width = LOWORD(lParam);

	g_glRender-&gt;SetupProjection(width, height);

	break;
case WM_ACTIVATEAPP:
	break;

case WM_PAINT:              // paint
    PAINTSTRUCT ps;
    BeginPaint(hWnd, &ps);
    EndPaint(hWnd, &ps);
    break;

case WM_LBUTTONDOWN:        // left mouse button
    break;

case WM_RBUTTONDOWN:        // right mouse button
    break;

case WM_MOUSEMOVE:          // mouse movement
    break;

case WM_LBUTTONUP:          // left button release
    break;

case WM_RBUTTONUP:          // right button release
    break;

case WM_KEYUP:
    break;

case WM_KEYDOWN:
	int fwKeys;
	LPARAM keyData;

	fwKeys = (int)wParam;
	keyData = lParam;

	switch(fwKeys)
	{
	case VK_ESCAPE:
		PostQuitMessage(0);
		break;
	default:
		break;
	}
	break;

default:
	break;
}

return DefWindowProc(hWnd, uMsg, wParam, lParam);

}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
WNDCLASSEX windowClass;
HWND hwnd;
MSG msg;
DWORD dwExStyle;
DWORD dwStyle;
RECT windowRect;

g_glRender = new CGfxOpenGL;

windowRect.left=(long)0;
windowRect.right=(long)windowW;
windowRect.top=(long)0;
windowRect.bottom=(long)windowH;

windowClass.cbSize			= sizeof(WNDCLASSEX);
windowClass.style			= CS_HREDRAW | CS_VREDRAW;
windowClass.lpfnWndProc		= MainWindowProc;
windowClass.cbClsExtra		= 0;
windowClass.cbWndExtra		= 0;
windowClass.hInstance		= hInstance;
windowClass.hIcon			= LoadIcon(NULL, IDI_APPLICATION);
windowClass.hCursor			= LoadCursor(NULL, IDC_ARROW);
windowClass.hbrBackground	= NULL;
windowClass.lpszMenuName	= NULL;
windowClass.lpszClassName	= "GLClass";
windowClass.hIconSm			= LoadIcon(NULL, IDI_WINLOGO);

if(!RegisterClassEx(&windowClass))
	return 0;

if(fullscreen)
{
	DEVMODE dmScreenSettings;
	memset(&dmScreenSettings, 0, sizeof(dmScreenSettings));
	dmScreenSettings.dmSize = sizeof(dmScreenSettings);
	dmScreenSettings.dmPelsWidth = windowW;
	dmScreenSettings.dmPelsHeight = windowH;
	dmScreenSettings.dmBitsPerPel = windowBits;
	dmScreenSettings.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;

	if(ChangeDisplaySettings(&dmScreenSettings, CDS_FULLSCREEN) != DISP_CHANGE_SUCCESSFUL)
	{
		MessageBox(NULL, "Display Mode Failed", NULL, MB_OK);
		fullscreen = FALSE;
	}
}

if (fullscreen)
{
	dwExStyle = WS_EX_APPWINDOW;
	dwStyle = WS_POPUP;
	ShowCursor(FALSE);
}
else
{
	dwExStyle = WS_EX_APPWINDOW | WS_EX_WINDOWEDGE;
	dwStyle = WS_OVERLAPPEDWINDOW;
}

AdjustWindowRectEx(&windowRect, dwStyle, FALSE, dwExStyle);

hwnd = CreateWindowEx(NULL,
	"GLClass",
	"BOGLGP - Chapter 2 - Open GL Application",
	dwStyle | WS_CLIPCHILDREN | WS_CLIPSIBLINGS,
	0, 0,
	windowRect.right - windowRect.left,
	windowRect.bottom - windowRect.top,
	NULL,
	NULL,
	hInstance,
	NULL);

hDC = GetDC(hwnd);

if (!hwnd)
	return 0;

ShowWindow(hwnd, SW_SHOW);
UpdateWindow(hwnd);

g_glRender-&gt;Init();

while (!exiting)
{
	g_glRender-&gt;Prepare(0.0f);
	g_glRender-&gt;Render();
	SwapBuffers(hDC);

	while (PeekMessage (&msg, NULL, 0, 0, PM_NOREMOVE))
	{
		if (!GetMessage (&msg, NULL, 0, 0))
		{
			exiting = true;
			break;
		}
		
		TranslateMessage (&msg);
		DispatchMessage (&msg);
	}
}

delete g_glRender;

if (fullscreen)
{
	ChangeDisplaySettings(NULL, 0);
	ShowCursor(TRUE);
}

return (int)msg.wParam;

}

in CGfxOpenGL.cpp:
#ifdef _WINDOWS
#include <windows.h>
#endif

#include <gl/gl.h>
#include <gl/glu.h>
#include <math.h>
#include “CGfxOpenGL.h”

// disable implicit float-double casting
#pragma warning(disable:4305)

CGfxOpenGL::CGfxOpenGL()
{
}

CGfxOpenGL::~CGfxOpenGL()
{
}

bool CGfxOpenGL::Init()
{
// clear to black background
glClearColor(0.0, 0.0, 0.0, 0.0);

m_angle = 0.0f;

return true;

}

bool CGfxOpenGL::Shutdown()
{
return true;
}

void CGfxOpenGL::SetupProjection(int width, int height)
{
if (height == 0) // don’t want a divide by zero
{
height = 1;
}

glViewport(0, 0, width, height);        // reset the viewport to new dimensions
glMatrixMode(GL_PROJECTION);            // set projection matrix current matrix
glLoadIdentity();                       // reset projection matrix

// calculate aspect ratio of window
gluPerspective(52.0f,(GLfloat)width/(GLfloat)height,1.0f,1000.0f);

glMatrixMode(GL_MODELVIEW);             // set modelview matrix
glLoadIdentity();                       // reset modelview matrix

m_windowWidth = width;
m_windowHeight = height;

}

void CGfxOpenGL::Prepare(float dt)
{
m_angle += 0.1f;
}

void CGfxOpenGL::Render()
{
// clear screen and depth buffer
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();

// move back 5 units and rotate about all 3 axes
glTranslatef(0.0, 0.0, -5.0f);
glRotatef(m_angle, 1.0f, 0.0f, 0.0f);
glRotatef(m_angle, 0.0f, 1.0f, 0.0f);
glRotatef(m_angle, 0.0f, 0.0f, 1.0f);

// lime greenish color
glColor3f(0.7f, 1.0f, 0.3f);

// draw the triangle such that the rotation point is in the center
glBegin(GL_TRIANGLES);
    glVertex3f(1.0f, -1.0f, 0.0f);
    glVertex3f(-1.0f, -1.0f, 0.0f);
    glVertex3f(0.0f, 1.0f, 0.0f);
glEnd();

}

in CGfxOpenGL.h:

#ifndef __GL_COMPONENT
#define __GL_COMPONENT

#define PI 3.14159
#define TWO_PI PI*2.0
#define HALF_PI PI/2.0

class CGfxOpenGL
{
private:
int m_windowWidth;
int m_windowHeight;

float m_angle;

public:
CGfxOpenGL();
virtual ~CGfxOpenGL();

bool Init();
bool Shutdown();

void SetupProjection(int width, int height);

void Prepare(float dt);
void Render();

};

#endif

in CGfxOpenGL.cpp:
#ifdef _WINDOWS
#include <windows.h>
#endif

This could be the problem. You said with the original workspace it works and when you copy the source into your own workspace it doesn’t work.

In CGfxOpenGL.cpp the windows.h header file is only included when the preprocessor macro _WINDOWS is defined. Try either removing the #ifdef and #endif or defining the _WINDOWS macro in the project settings.

Alright obviously I don’t know as much CPP as i thought i did… how would i include that into my workspace?

O.k. overmind that did wonders for me, and then i linked the opengl.lib and glu.lib which fixed some more problems but i still have one error that i got before and i don’t know how i got this close before hehe… anyway I took out the #ifdef and #endif lines in CGfxOpenGL.cpp and linked the opengl librarys but now i get this error:

LIBCD.lib(crt0.obj) : error LNK2001: unresolved external symbol _main
Debug/GLtest.exe : fatal error LNK1120: 1 unresolved externals

I got this one yesterday and i dont know how but my C++ teacher doesn’t know what _main isn’t found. He said to make sure windows.h was included… maybe there’s just another lib i need to link. But in the end I’m stumped

thanks tons for your help so far

there are two types of project, console applications and win32 applications.
Win32 applications expect WinMain as an entry point
Console applications expect main() as an entry point

you have the wrong entry point for your application, either change the type of project or change your main function

O.k. sweet that worked perfect

I tried that once before but i probobly forgot to link the .libs at that point… thank you alot now i can actually go to chapter 3!!! haha

Exscuse me for my english very bad
You could simplify your project using GLUT rather than Win32 directily.
I’m a beginner and I consider the redbook enough simple and complet.
Bye

You could obtain the rotation of triangle in this way, using GLUT

 
# include <windows.h>
# include <stdlib.h>
# include <gl/glut.h>
# include <gl/gl.h>
# include <gl/glu.h>

GLfloat	rtri=0;	
			    
void aggiorna (int w, int h)
{
glViewport (0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
gluPerspective(100.0, (GLfloat) w/(GLfloat) h, 0.10, 100.0);
glMatrixMode(GL_MODELVIEW);
}
void disegna (void)
{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);	
		
	glLoadIdentity();
	
	gluLookAt ( 0.0, 0.0, 1.0,
                0.0, 0.0, -1.0,
                0.0, 1.0, 0.0);
	glPushMatrix();

    glTranslatef(-0.5f,0.0f,-1.0f);  // Muove a sinistra di 1.5 unità e dentro lo schermo di 1.0 unità
    glRotatef(rtri,0.0f,1.0f,0.0f);						// Ruota il triangolo sull'asse Y ( NUOVA )
	glBegin(GL_TRIANGLES); // Disegna usando i triangoli
	    glColor3f(0.0,1.0,0.0);             
       glVertex3f( 0.0f, 0.4f, 0.0f); 
	    glColor3f(1.0f,0.0f,0.0f); 
       glVertex3f(-0.4f,-0.4f, 0.0f); 
        glColor3f(0.0f,0.0f,1.0f); 
       glVertex3f( 0.4f,-0.4f, 0.0f); 
    glEnd(); // Finisce di disegnare il triangolo

	glPopMatrix();

     glPushMatrix();
glutSwapBuffers();
}
int main(int argc, char** argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE| GLUT_RGB);
glutInitWindowSize(800,600);
glutInitWindowPosition(100,150);
glutCreateWindow("Prove con OpenGL");
glutDisplayFunc(disegna);
glutIdleFunc(rotazione); //richiama l'animazione, facendo ruotare le figure
glutReshapeFunc(aggiorna);
glutMainLoop();
return 0;
}

Originally posted by <Curious George>:
[b]I have the book
“Beginning OpenGL Game Programming” by Dave Astle and Kevin Hawkins

I do not recommend this book primarily because of it’s lack of help references for errors and it’s claim to a full support website when it’s under construction.
[/b]
I’d just like to mention that the support website went live a day or two after this was posted. Our contact information is listed there and we’re more than happy to answer any questions related to the book. We’re confident that the book is quite appropriate to anyone who is brand new to OpenGL, but as we state several times in the book, we assume that the reader is comfortable programming in C++ and on their platform of choice.