Archived

This topic is now archived and is closed to further replies.

sSimontis

InitGL() Function failing in all tutorials

Recommended Posts

This weekend I have worked through the first 7 tutorials with Microsoft Visual Studio .NET, but I cannot start the .exe files. I get the message "Couldn''t initialize OpenGL", which comes if the function InitGL() fails in the tutorials. Do any of you guys know why this would be? I checked the source, and it seems okay. HEre is the source, but I see no problems. Can someone please help me out here?
// NeHe1.cpp : Defines the entry point for the application.

//


#include "stdafx.h"
#include "NeHe7.h"
#define MAX_LOADSTRING 100

// Global Variables:

HINSTANCE hInstance;								// current instance

TCHAR szTitle[MAX_LOADSTRING];					// The title bar text

TCHAR szWindowClass[MAX_LOADSTRING];			// the main window class name


// Variables needed by OpenGL:

HGLRC		hRC = NULL;  // Permanent RC

HDC			hDC = NULL;  // GDI context

HWND		hWnd = NULL; // Window Handle


//Boolean flags for program:

bool keys[256];          // KB buffer

bool active = TRUE;      // Is the program being used?

bool fullscreen = TRUE;  // Are we in fullscreen?


BOOL light;
BOOL lp;
BOOL fp;

GLfloat xrot;
GLfloat yrot;
GLfloat xspeed;
GLfloat yspeed;
GLfloat z = -0.5f;

GLfloat LightAmb[] = { 0.5f, 0.5f, 0.5f, 1.0f };

GLfloat LightDif[] = { 1.0f, 1.0f, 1.0f, 1.0f };

GLfloat LightPos[] = { 0.0f, 0.0f, 2.0f, 1.0f };

GLuint filter;
GLuint texture[3];

// Forward declarations of functions included in this code module:

GLvoid				ReSizeGLScene(GLsizei width, GLsizei height);
int					InitGL();
int					DrawGLScene();
GLvoid				KillGLWindow();
BOOL				CreateGLWindow(char* title, int width, int height, int bits, bool fullscreenflag);
LRESULT	CALLBACK	WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
int WINAPI			WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLne, int nShowCmd);
AUX_RGBImageRec*	LoadBMP(char* filename);
int					LoadGLTextures();

AUX_RGBImageRec* LoadBMP(char* filename)
{
	FILE *File = NULL;

	if(!filename)
	{
		return NULL;
	}

	File = fopen(filename, "r");

	if(File)
	{
		fclose(File);
		return auxDIBImageLoad(filename);
	}
	return NULL;
}

int LoadGLTextures()
{
	int Status = FALSE;

	AUX_RGBImageRec* TextureImage[1];

	memset(TextureImage, 0, sizeof(void*)*1);

	if(TextureImage[0] = LoadBMP("Data/Crate.BMP"))
	{
		Status = TRUE;

		glGenTextures(3, &texture[0]);

		glBindTexture(GL_TEXTURE_2D, texture[0]);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
		glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[0]->sizeX, TextureImage[0]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]->data);

		glBindTexture(GL_TEXTURE_2D, texture[1]);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
		glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[0]->sizeX, TextureImage[0]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]->data);

		glBindTexture(GL_TEXTURE_2D, texture[2]);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_NEAREST);
		gluBuild2DMipmaps(GL_TEXTURE_2D, 3, TextureImage[0]->sizeX, TextureImage[0]->sizeY, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]->data);
	}

	if(TextureImage[0])
	{
		if(TextureImage[0]->data)
		{
			free(TextureImage[0]->data);
		}
		free(TextureImage[0]);
	}

	return Status;
}

GLvoid ReSizeGLScene(GLsizei width, GLsizei height)
{
	if (height == 0)
	{
		height = 1;
	}

	glViewport(0, 0, width, height);

	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();

	gluPerspective(45.0f, (GLfloat)width / (GLfloat) height, 0.1f, 100.0f);

	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();

}

int InitGL()
{
	if(!LoadGLTextures())
	{
		return FALSE;
	}
	glEnable(GL_TEXTURE_2D);
	glShadeModel(GL_SMOOTH);
	glClearColor(0.0f, 0.0f, 0.0f, 0.5f);

	glClearDepth(1.0f);
	glEnable(GL_DEPTH_TEST);
	glDepthFunc(GL_LEQUAL);

	glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);

	glLightfv(GL_LIGHT1, GL_AMBIENT, LightAmb);
	glLightfv(GL_LIGHT1, GL_DIFFUSE, LightDif);

	glEnable(GL_LIGHT1);

	return TRUE;
}

int DrawGLScene()
{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glLoadIdentity();

	glTranslatef(0.0f, 0.0f, z);

	glRotatef(xrot, 1.0f, 0.0f, 0.0f);
	glRotatef(yrot, 0.0f, 1.0f, 0.0f);

	glBindTexture(GL_TEXTURE_2D, texture[filter]);
	glBegin(GL_QUADS);
	glNormal3f(0.0f, 0.0f, 1.0f);
	glNormal3f( 0.0f, 0.0f, 1.0f);					// Normal Pointing Towards Viewer

		glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f,  1.0f);	// Point 1 (Front)

		glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f,  1.0f);	// Point 2 (Front)

		glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f,  1.0f,  1.0f);	// Point 3 (Front)

		glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f,  1.0f,  1.0f);	// Point 4 (Front)

		// Back Face

		glNormal3f( 0.0f, 0.0f,-1.0f);					// Normal Pointing Away From Viewer

		glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f);	// Point 1 (Back)

		glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f,  1.0f, -1.0f);	// Point 2 (Back)

		glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f,  1.0f, -1.0f);	// Point 3 (Back)

		glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f);	// Point 4 (Back)

		// Top Face

		glNormal3f( 0.0f, 1.0f, 0.0f);					// Normal Pointing Up

		glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f,  1.0f, -1.0f);	// Point 1 (Top)

		glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f,  1.0f,  1.0f);	// Point 2 (Top)

		glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f,  1.0f,  1.0f);	// Point 3 (Top)

		glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f,  1.0f, -1.0f);	// Point 4 (Top)

		// Bottom Face

		glNormal3f( 0.0f,-1.0f, 0.0f);					// Normal Pointing Down

		glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, -1.0f, -1.0f);	// Point 1 (Bottom)

		glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, -1.0f, -1.0f);	// Point 2 (Bottom)

		glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f,  1.0f);	// Point 3 (Bottom)

		glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f,  1.0f);	// Point 4 (Bottom)

		// Right face

		glNormal3f( 1.0f, 0.0f, 0.0f);					// Normal Pointing Right

		glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f);	// Point 1 (Right)

		glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f,  1.0f, -1.0f);	// Point 2 (Right)

		glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f,  1.0f,  1.0f);	// Point 3 (Right)

		glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f,  1.0f);	// Point 4 (Right)

		// Left Face

		glNormal3f(-1.0f, 0.0f, 0.0f);					// Normal Pointing Left

		glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f);	// Point 1 (Left)

		glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f,  1.0f);	// Point 2 (Left)

		glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f,  1.0f,  1.0f);	// Point 3 (Left)

		glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f,  1.0f, -1.0f);	// Point 4 (Left)

	glEnd();			
	xrot += xspeed;
	yrot += yspeed;

	return TRUE;
}

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

	if(hRC)
	{
		if(!wglMakeCurrent(NULL, NULL))
		{
			MessageBox(NULL, "Release of RC and DC failed.", "Shutdown Error", MB_OK | MB_ICONEXCLAMATION);
		}

		if(!wglDeleteContext(hRC))
		{
			MessageBox(NULL, "Release of RC failed.", "Shutdown Error", MB_OK | MB_ICONEXCLAMATION);
		}

		hRC = NULL;
	}

	if (hDC && !ReleaseDC(hWnd, hDC))
	{
		MessageBox(NULL, "Release of DC failed.", "Shutdown Error", MB_OK | MB_ICONINFORMATION);
	}

	if(hWnd && !DestroyWindow(hWnd))
	{
		MessageBox(NULL, "Could not release hWnd", "Shutdown Error", MB_OK | MB_ICONINFORMATION);
		hWnd = NULL;
	}

	if(!UnregisterClass("OpenGL", hInstance))
	{
		MessageBox(NULL, "Failed to Unregister Window.", "Shutdown Error", MB_OK | MB_ICONINFORMATION);
		hInstance = NULL;
	}
}

BOOL CreateGLWindow(char* title, int width, int height, int bits, bool fullscreenflag)
{
	GLuint		PixelFormat;
	WNDCLASSEX	windowClass;
	DWORD		dwExStyle;
	DWORD		dwStyle;
	RECT		windowRect;

	windowRect.top = (long) 0;
	windowRect.left = (long) 0;
	windowRect.bottom = (long) height;
	windowRect.right = (long) width;

	fullscreen = fullscreenflag;

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

	if(!RegisterClassEx(&windowClass))
	{
		MessageBox(NULL, "Failed to register window class.", "Error", MB_OK | MB_ICONINFORMATION);
		return FALSE;
	}

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

		if(ChangeDisplaySettings(&dmScreenSettings, CDS_FULLSCREEN) != DISP_CHANGE_SUCCESSFUL)
		{
			if(MessageBox(NULL, "The requested fullscreen mode is not supported by\nyour video card. Use windowed mode instead?", "NeHe OpenGL", MB_YESNO | MB_ICONEXCLAMATION)==IDYES)
			{
				fullscreen = FALSE;
			}
			else
			{
				MessageBox(NULL, "Program will now close.", "Error", MB_OK | MB_ICONEXCLAMATION);
				return 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);

	if(!(hWnd = CreateWindowEx(dwExStyle, "OpenGL", title, WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
		0, 0, windowRect.right - windowRect.left, windowRect.bottom - windowRect.right,
		NULL, NULL, hInstance, NULL)))
	{
		KillGLWindow();
		MessageBox(NULL, "Window creation failure.", "Error", MB_OK | MB_ICONEXCLAMATION);
		return FALSE;
	}

	static PIXELFORMATDESCRIPTOR pfd ={
		sizeof(PIXELFORMATDESCRIPTOR),
			1,
			PFD_DRAW_TO_WINDOW |
			PFD_SUPPORT_OPENGL |
			PFD_DOUBLEBUFFER,
			PFD_TYPE_RGBA,
			bits,
			0, 0, 0, 0, 0, 0,
			0,
			0,
			0,
			0, 0, 0, 0,
			16,
			0,
			0,
			PFD_MAIN_PLANE,
			0,
			0, 0, 0
	};

	if(!(hDC = GetDC(hWnd)))
	{
		KillGLWindow();
		MessageBox(NULL, "Can''t create an OpenGL DC", "Error", MB_OK | MB_ICONEXCLAMATION);
		return FALSE;
	}

	if(!(PixelFormat = ChoosePixelFormat(hDC, &pfd)))
	{
		KillGLWindow();
		MessageBox(NULL, "Can''t find a suitable pixel format.", "Error", MB_OK | MB_ICONEXCLAMATION);
		return FALSE;
	}

	if(!(SetPixelFormat(hDC, PixelFormat, &pfd)))
	{
		KillGLWindow();
		MessageBox(NULL, "Can''t find a suitable pixel format.", "Error", MB_OK | MB_ICONEXCLAMATION);
		return FALSE;
	}

	if(!(wglCreateContext(hDC)))
	{
		KillGLWindow();
		MessageBox(NULL, "Cannot create a rendering context.", "Error", MB_OK | MB_ICONEXCLAMATION);
		return FALSE;
	}

	if(!wglMakeCurrent(hDC, hRC))
	{
		KillGLWindow();
		MessageBox(NULL, "Couldn''t activate the RC.", "Error", MB_OK | MB_ICONEXCLAMATION);
		return FALSE;
	}

	ShowWindow(hWnd, SW_SHOW);
	SetForegroundWindow(hWnd);
	SetFocus(hWnd);
	ReSizeGLScene(width, height);

	if(!InitGL())
	{
		KillGLWindow();
		MessageBox(NULL, "Couldn''t initialize OpenGL.", "Error", MB_OK | MB_ICONEXCLAMATION);
		return FALSE;
	}

	return TRUE;
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	switch(uMsg)
	{
	case WM_ACTIVATE:
	{
		if(!HIWORD(wParam))
		{
			active = TRUE;
		}
		else
		{
			active = FALSE;
		}
		return 0;
	}
	case WM_SYSCOMMAND:
		{
			switch(wParam)
			{
			case SC_SCREENSAVE:
			case SC_MONITORPOWER:
				return 0;
			}
			break;
		}
	case WM_CLOSE:
		{
			PostQuitMessage(0);
			return 0;
		}
	case WM_KEYDOWN:
		{
			keys[wParam] = TRUE;
			return 0;
		}
	case WM_KEYUP:
		{
			keys[wParam] = FALSE;
			return 0;
		}
	case WM_SIZE:
		{
			ReSizeGLScene(LOWORD(lParam), HIWORD(lParam));
			return 0;
		}
	}

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

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLne, int nShowCmd)
{
	MSG msg;
	BOOL done = false;

	if(MessageBox(NULL, "Would you like to start the program in fullscreen mode?", "Start Fullscreen?", MB_YESNO | MB_ICONQUESTION) == IDNO)
	{
		fullscreen = FALSE;
	}

	if(!CreateGLWindow("NeHe''s Textures, Lighting & Keyboard Tutorial", 640, 480, 16, fullscreen))
	{
		return 0;
	}

	while(!done)
	{
		if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
		{
			if(msg.message = WM_QUIT)
			{
				done = TRUE;
			}
			else
			{
				TranslateMessage(&msg);
				DispatchMessage(&msg);
			}
		}
		else
		{
			if(active)
			{
				if(keys[VK_ESCAPE])
				{
					done = TRUE;
				}
				else
				{
					DrawGLScene();
					SwapBuffers(hDC);
					if(keys[''L''] && !lp)
					{
						lp = TRUE;
						light = !light;

						if(!light)
						{
							glDisable(GL_LIGHTING);
						}
						else
						{
							glEnable(GL_LIGHTING);
						}
					}

					if(!keys[''L''])
					{
						lp = FALSE;
					}
					
					if(keys[''F''] & !fp)
					{
						fp = TRUE;
						filter += 1;
						if(filter > 2)
						{
							filter = 0;
						}
					}
					if(!keys[''F''])
					{
						fp = FALSE;
					}
					if(keys[VK_PRIOR])
					{
						z -= 0.02f;
					}
					if(keys[VK_NEXT])
					{
						z += 0.02f;
					}
					if(keys[VK_UP])
					{
						xspeed -= 0.01f;
					}
					if(keys[VK_DOWN])
					{
						xspeed += 0.01f;
					}
					if(keys[VK_RIGHT])
					{
						yspeed += 0.01f;
					}
					if(keys[VK_LEFT])
					{
						yspeed -= 0.01f;
					}

				}

			}
			if(keys[VK_F1])
			{
				keys[VK_F1] = FALSE;
				KillGLWindow();
				fullscreen = !fullscreen;
				if(!CreateGLWindow("NeHe''s Textures, Lighting & Keyboard Tutorial", 640, 480, 16, fullscreen))
				{
					return 0;
				}
			}
		}
	}

	KillGLWindow();
	return (msg.wParam);
}
Scott Simontis e-mail:ageofscott@NOSPAM.comcast.net AIM:ssimontis

Share this post


Link to post
Share on other sites
In your working directory (not debug/release, but the folders those folders are in...I think that''s still the convention with .NET), make sure that there is a folder called Data, and that it has a Crate.BMP inside of it.

If the BMP loading function fails, LoadGLTextures() fails, and that tells InitGL() to fail, returning that message...so try that.

Someone else was having that problem not long ago.



"TV IS bad Meatwad...but we f***in need it"

If you''re a girl under the age of 12, and you''re high on marijuana...don''t ride your bike. -TRUTH

Share this post


Link to post
Share on other sites
I have the directory in there, but have noticed somethign weird. If it is in the Debug directory,I can see the program starting and closing for a second, but not in the base folder. Any ideas?

Scott Simontis
e-mail:ageofscott@NOSPAM.comcast.net
AIM:ssimontis

Share this post


Link to post
Share on other sites