Jump to content
  • Advertisement

Archived

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

kappa

Trying to learn OpenGL from NEHE, I have a problem.

This topic is 5224 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I need help! I have tried to go through nehe's exelent tuturials but I got stuck on tuturial number 8 by something rather anoying. I run the application and everything works like it should (stars, light, etc...), but when I close it I get an error that states: "release of rendering context failed." (note errors in KillGLWindow) I have been trying to figure this out for several hours now and I can't see whats wrong, it's probbably something whery easy but I just can't seem to find it.
#include <windows.h>
#include <gl\gl.h>
#include <gl\glu.h>
#include <gl\glaux.h>
#include <cstdio>

HGLRC hRC = NULL; //rendering device

HDC	  hDC =	NULL; // GDI device context

HWND  hWnd= NULL; // windows handle

HINSTANCE hInstance;

bool twinkle = true;
bool keys[256];
bool active = true;
bool fullscreen = true;
bool blend;
bool light;
bool lp;
bool fp;
bool bp;
bool tp;

const num=50;

typedef struct
{
	int r,g,b;
	GLfloat dist;
	GLfloat angle;
}stars;
stars star[num];

GLfloat zoom=-15.0f;
GLfloat tilt= 90.0f;
GLfloat spin;
GLfloat	xrot;
GLfloat	yrot;
GLfloat zrot;
GLfloat xspeed;
GLfloat yspeed;
GLfloat z=-5.0f;

GLfloat LightAmbient[]={ 0.5f, 0.5f, 0.5f, 1.0f };
GLfloat LightDiffuse[]={ 1.0f, 1.0f, 1.0f, 1.0f };
GLfloat LightPosition[]={0.0f, 0.0f, 2.0f, 1.0f };

GLuint  loop;
GLuint	filter;
GLuint	texture[1];

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

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

	if(!filename)
	{
		return 0;
	}

	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("star.bmp"))
	{
		Status = TRUE;
		glGenTextures(3, &texture[0]);
		
		glBindTexture(GL_TEXTURE_2D, texture[0]);
		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);
		
	}

	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);										
	glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
	glBlendFunc(GL_SRC_ALPHA,GL_ONE);
	glEnable(GL_BLEND);

	for(loop=0; loop<num; loop++)
	{
		star[loop].angle=0.0f;
		star[loop].dist=(float(loop)/num)*5.0f;
		star[loop].r=rand()%256;
		star[loop].g=rand()%256;
		star[loop].b=rand()%256;
	}
	return TRUE;
}

int DrawGLScene()
{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glBindTexture(GL_TEXTURE_2D, texture[0]);

	for(loop=0; loop<num; loop++)
	{
		glLoadIdentity();
		glTranslatef(0.0f,0.0f,zoom);
		glRotatef(tilt,1.0f,0.0f,0.0f);
		glRotatef(star[loop].angle,0.0f,1.0f,0.0f);
		glTranslatef(star[loop].dist,0.0f,0.0f);
		glRotatef(-star[loop].angle,0.0f,1.0f,0.0f);
		glRotatef(-tilt,1.0f,0.0f,0.0f);
	
		if(twinkle)
		{
			glColor4ub(star[num-loop-1].r, star[num-loop-1].g, star[num-loop-1].b, 255);
			glBegin(GL_QUADS);
				glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f,-1.0f, 0.0f);
				glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f,-1.0f, 0.0f);
				glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 0.0f);
				glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 0.0f);
			glEnd();
		}

		glRotatef(spin, 0.0f, 0.0f, 1.0f);
		glColor4ub(star[loop].r, star[loop].g,star[loop].b, 255);
		glBegin(GL_QUADS);
			glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f,-1.0f, 0.0f);
			glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f,-1.0f, 0.0f);
			glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 0.0f);
			glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 0.0f);
		glEnd();
	
	spin+=0.02f;
	star[loop].angle+=float(loop)/num;
	star[loop].dist -=0.01f;

	
	}
	return TRUE;
}

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

	if(hRC) //rendering context???

	{
		if(!wglMakeCurrent(NULL,NULL))
		{
			MessageBox(NULL,"Release of DC and HC failed.", "SHUTDOWN ERROR", MB_OK | MB_ICONINFORMATION);
		}
	
		if(!wglDeleteContext(hRC))
		{
			MessageBox(NULL,"Release of rendering context failed.", "SHUTDOWN ERROR", MB_OK | MB_ICONINFORMATION);
		}
		hRC=NULL;
	}

	if(hDC && !ReleaseDC(hWnd,hDC))
	{
		MessageBox(NULL,"Release device context failed.","SHUTDOWN ERROR", MB_OK|MB_ICONINFORMATION);
		hDC = NULL;
	}

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

	if(!UnregisterClass("OpenGL",hInstance))
	{
		MessageBox(NULL, "Could not unregister class.", "SHUTDOWN ERROR", MB_OK | MB_ICONINFORMATION);
		hInstance = NULL;
	}
}

BOOL CreateGLWindow(char* title, int width, int height, int bits, bool fullscreenflag)
{
	GLuint   PixelFormat;
	WNDCLASS wc;
	DWORD	 dwExStyle;
	DWORD	 dwStyle;

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

	fullscreen=fullscreenflag;

	hInstance		= GetModuleHandle(NULL);
	wc.style		= CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
	wc.lpfnWndProc	= (WNDPROC) WndProc;
	wc.cbClsExtra	= 0;
	wc.cbWndExtra	= 0;
	wc.hInstance	= hInstance;
	wc.hIcon		= LoadIcon(NULL, IDI_WINLOGO);
	wc.hCursor		= LoadCursor(NULL, IDC_ARROW);
	wc.hbrBackground= NULL;
	wc.lpszMenuName	= NULL;
	wc.lpszClassName= "OpenGL";

	if(!RegisterClass(&wc))
	{
		return FALSE;
	}

	if(fullscreen)
	{
		DEVMODE dmScreenSettings;
		memset(&dmScreenSettings, 0, sizeof(dmScreenSettings));
		dmScreenSettings.dmSize=sizeof(dmScreenSettings);
		dmScreenSettings.dmPelsWidth	= width;
		dmScreenSettings.dmPelsHeight	= height;
		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 GL",MB_YESNO|MB_ICONEXCLAMATION)==IDYES)
			{
				fullscreen=FALSE;
			}
			else
			{
				MessageBox(NULL,"Program Will Now Close.","ERROR",MB_OK|MB_ICONSTOP);
				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 |
							dwStyle,
							0, 0,
							WindowRect.right - WindowRect.left,
							WindowRect.bottom - WindowRect.top,
							NULL,
							NULL,
							hInstance,
							NULL)))
	{
		KillGLWindow();							// Reset The Display

		MessageBox(NULL,"Window Creation Error.","ERROR",MB_OK|MB_ICONEXCLAMATION);
		return FALSE;							// 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 a GL device context.","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 set pixelformat.","ERROR",MB_OK|MB_ICONEXCLAMATION);
		return FALSE;
	}

	if(!(hRC=wglCreateContext(hDC)))
	{
		KillGLWindow();
		MessageBox(NULL,"Can't create a GL rendering context","ERROR", MB_OK|MB_ICONEXCLAMATION);
		return FALSE;
	}

	if(!wglMakeCurrent(hDC,hRC))						// Try To Activate The Rendering Context

	{
		KillGLWindow();							// Reset The Display

		MessageBox(NULL,"Can't Activate The GL Rendering Context.","ERROR",MB_OK|MB_ICONEXCLAMATION);
		return FALSE;							// Return FALSE

	}

	ShowWindow(hWnd,SW_SHOW);						
	SetForegroundWindow(hWnd);						
	SetFocus(hWnd);								
	ReSizeGLScene(width, height);
	
	if(!InitGL())
	{
		KillGLWindow();
		MessageBox(NULL,"Initialization failed","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:							//Screen saver trying to start?

				case SC_MONITORPOWER:						//Monitor trying to enter powersave?

					return 0;								//Prevent it from happening.

			}
			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		lpCmdLine,
					int			nCmdShow)
{
	MSG  msg;
	BOOL done = FALSE;
	
	if(MessageBox(NULL,"Would you like to run in fullscreen mode?", "Start fullscreen?", MB_YESNO|MB_ICONQUESTION)==IDNO)
	{
		fullscreen = FALSE;
	}

	if(!CreateGLWindow("Open GL", 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['T'] && !tp)
					{
						tp=TRUE;
						twinkle=!twinkle;
					}
					if(!keys['T'])
					{
						tp = false;
					}
					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])
					{
						yspeed+=0.02f;
					}
					if(keys[VK_DOWN])
					{
						yspeed-=0.02f;
					}
					if(keys[VK_RIGHT])
					{
						xspeed+=0.02f;
					}
					if(keys[VK_LEFT])
					{
						xspeed-=0.02f;
					}

					if(keys['B'])
					{
						bp=TRUE;
						blend = !blend;
						if(blend)
						{
							glEnable(GL_BLEND);
							glDisable(GL_DEPTH_TEST);
						}
						else
						{
							glDisable(GL_BLEND);
							glEnable(GL_DEPTH_TEST);
						}
					}
					if(!keys['B'])
					{
						bp=false;
					}

				}
			}

			

			if(keys[VK_F1])
			{
				keys[VK_F1]=FALSE;
				KillGLWindow();
				fullscreen=!fullscreen;
				
				if(!CreateGLWindow("Open GL",640,480,16, fullscreen))
				{
					return 0;
				}
			}
		}
	}

	KillGLWindow();
	return (msg.wParam);
} 
[edited by - kappa on February 27, 2004 6:24:51 PM]

Share this post


Link to post
Share on other sites
Advertisement
I found out what was wrong,
this line:
glGenTextures(3, &texture[0]);
should have been this:
glGenTextures(1, &texture[0]);


wonder why you did not get an error?


[edited by - kappa on February 28, 2004 5:26:35 AM]

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!