Skip to main content
GameDev.net gamedev.net
🔒 Locked

Why won't my program work

Started by FlamingBow Jul 4, 2007 at 9:15 AM 2 replies 950+ views
Original Post
FlamingBow
FlamingBow
I want to make a 3d cube with a texture painted on all sides of it. That isn't finished yet, but that's not the problem, the problem is that when I see the window, all I can see is a blank window, not even cleared to a color. The window seems like its part of the screen's background, if you know what I mean. And I already used glClearColor, and yet it still doesn't work. Try running the code below, and you will see what I mean? -So how can I fix this code? -And what did I do wrong?

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

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); 
const char g_szClassName[] = "glApp"; 

HGLRC hRC = NULL; 
HDC hDC = NULL; 
HWND hWnd = NULL; 
GLfloat rx, ry, rz;   

GLvoid ResizeScene(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(); 
}

GLvoid DestroyScene()
{
	UnregisterClass(g_szClassName, GetModuleHandle(NULL));
	wglDeleteContext(hRC); 
	ReleaseDC(hWnd, hDC);  
	wglMakeCurrent(NULL, NULL); 
}

BOOL CreateScene(GLsizei width, GLsizei height, int bits, char *caption)
{
	//set up opengl
	glShadeModel(GL_SMOOTH); 
	glEnable(GL_TEXTURE_2D); 
	glEnable(GL_DEPTH_TEST); 
	glDepthFunc(GL_LEQUAL); 
	glClearDepth(1.0f); 
	glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); 
	glClearColor(0.0f, 0.0f, 0.0f, 0.0f); 

	//get contexts
    hDC = GetDC(hWnd); 
	hRC = wglCreateContext(hDC); 

	//set up pixel format descriptors
	PIXELFORMATDESCRIPTOR pfd; 
	ZeroMemory(&pfd, sizeof(pfd)); 
	pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR); 
	pfd.nVersion = 1; 
	pfd.cColorBits = bits;
	pfd.cDepthBits = bits; 
	pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER; 
	pfd.iLayerType = PFD_MAIN_PLANE; 

	int pixel_format = ChoosePixelFormat(hDC, &pfd); 

	if (pixel_format == 0)
	{
		DestroyScene(); 
		return FALSE; 
	}

	if (!SetPixelFormat(hDC, pixel_format, &pfd))
	{
		DestroyScene(); 
		return FALSE; 
	}

	//create the window
	WNDCLASSEX wc; 
	wc.cbSize = sizeof(WNDCLASSEX); 
	wc.style = CS_VREDRAW | CS_HREDRAW | CS_OWNDC; 
	wc.lpfnWndProc = WndProc; 
	wc.cbClsExtra = 0; 
	wc.cbWndExtra = 0; 
	wc.hIcon = LoadIcon(NULL, IDI_APPLICATION); 
	wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION); 
	wc.hCursor = LoadCursor(NULL, IDC_ARROW); 
	wc.hInstance = GetModuleHandle(NULL); 
	wc.hbrBackground = NULL; 
	wc.lpszClassName = g_szClassName; 
	wc.lpszMenuName = NULL; 

	if (!RegisterClassEx(&wc))
	{
		DestroyScene(); 
		return FALSE; 
	}

	hWnd = CreateWindowEx (
		0, 
		g_szClassName, 
		"OpenGL", 
		WS_OVERLAPPEDWINDOW | WS_CLIPSIBLINGS | WS_CLIPCHILDREN, 
		200, 
		200, 
		width, 
		height, 
		NULL, NULL, GetModuleHandle(NULL), NULL); 

	if (hWnd == NULL)
	{
		DestroyScene(); 
		return FALSE; 
	}

	if (!wglMakeCurrent(hDC, hRC))
	{
		DestroyScene(); 
		return FALSE; 
	}

	ShowWindow(hWnd, SW_SHOW);
	UpdateWindow(hWnd); 
	ResizeScene(width, height); 

	return TRUE; 
}

GLvoid RenderScene()
{
	glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT); 
	glLoadIdentity(); 
	glTranslatef(0.0f, 0.0f, -6.0f); 

	glRotatef(rx, 1.0f, 0.0f, 0.0f); 
	glRotatef(ry, 0.0f, 1.0f, 0.0f); 
	glRotatef(rz, 0.0f, 0.0f, 1.0f); 

	//draw
	glBegin(GL_QUADS); 
		//top
		glVertex3f(-1.0f, 1.0f, -1.0f);   
		glVertex3f(1.0f, 1.0f, -1.0f); 
		glVertex3f(1.0f, 1.0f, 1.0f); 
		glVertex3f(-1.0f, 1.0f, 1.0f); 
		//bottom
		glVertex3f(-1.0f, -1.0f, -1.0f);   
		glVertex3f(1.0f, -1.0f, -1.0f); 
		glVertex3f(1.0f, -1.0f, 1.0f); 
		glVertex3f(-1.0f, -1.0f, 1.0f); 
		//front
		//right
		//back
		//left
	glEnd(); 

	rx += 0.3f; 
	ry += 0.2f; 
	rz += 0.4f; 
}

GLvoid UpdateScene()
{
	RenderScene(); 
	SwapBuffers(hDC); 
}

int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR lpCmdLine, int nCmdShow)
{
	MSG msg; 
	ZeroMemory(&msg, sizeof(msg)); 

	if (!CreateScene(800, 600, 16, "OpenGL"))
	{
		DestroyScene(); 
		return 1; 
	}

	while (msg.message != WM_QUIT)
	{
		if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
		{
			TranslateMessage(&msg); 
			DispatchMessage(&msg); 
		}

		else        //render graphics
			UpdateScene(); 
	}

	DestroyScene(); 
	return 0; 
}

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	switch (msg)
	{
	case WM_CLOSE:
		DestroyWindow(hwnd); 
		break; 

	case WM_DESTROY:
		DestroyScene(); 
		PostQuitMessage(0); 
		break; 

	case WM_SIZE:
		ResizeScene(LOWORD(lParam), HIWORD(lParam)); 
		break; 
	}

	return DefWindowProc(hwnd, msg, wParam, lParam); 
}


Black Knight
Black Knight
You must create the rendering context first before doing any gl* stuff.

1) choose set pixel format.
2) create set rendering context
3) init opengl states
4) render
5) be happy.
FlamingBow
FlamingBow
Ok, thanks. So I am guessing that all the problems are in my CreateScene() function, right?

And, ummmm... sorry, I am a complete noob to opengl.
I don't exactly get what you mean by all the stuff, especially that set rendering context thing.

Could anyone put my code from CreateScene() into the correct order, please?
Black Knight
Black Knight
BOOL CreateScene(GLsizei width, GLsizei height, int bits, char *caption){//create the window	WNDCLASSEX wc; 	wc.cbSize = sizeof(WNDCLASSEX); 	wc.style = CS_VREDRAW | CS_HREDRAW | CS_OWNDC; 	wc.lpfnWndProc = WndProc; 	wc.cbClsExtra = 0; 	wc.cbWndExtra = 0; 	wc.hIcon = LoadIcon(NULL, IDI_APPLICATION); 	wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION); 	wc.hCursor = LoadCursor(NULL, IDC_ARROW); 	wc.hInstance = GetModuleHandle(NULL); 	wc.hbrBackground = NULL; 	wc.lpszClassName = g_szClassName; 	wc.lpszMenuName = NULL; 	if (!RegisterClassEx(&wc))	{		DestroyScene(); 		return FALSE; 	}	hWnd = CreateWindowEx (		0, 		g_szClassName, 		"OpenGL", 		WS_OVERLAPPEDWINDOW | WS_CLIPSIBLINGS | WS_CLIPCHILDREN, 		200, 		200, 		width, 		height, 		NULL, NULL, GetModuleHandle(NULL), NULL); 	if (hWnd == NULL)	{		DestroyScene(); 		return FALSE; 	}//set up pixel format descriptors	PIXELFORMATDESCRIPTOR pfd; 	ZeroMemory(&pfd, sizeof(pfd)); 	pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR); 	pfd.nVersion = 1; 	pfd.cColorBits = bits;	pfd.cDepthBits = bits; 	pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER; 	pfd.iLayerType = PFD_MAIN_PLANE; 	int pixel_format = ChoosePixelFormat(hDC, &pfd); 	if (pixel_format == 0)	{		DestroyScene(); 		return FALSE; 	}	if (!SetPixelFormat(hDC, pixel_format, &pfd))	{		DestroyScene(); 		return FALSE; 	}		//get contexts    hDC = GetDC(hWnd); 	hRC = wglCreateContext(hDC); 	if (!wglMakeCurrent(hDC, hRC))	{		DestroyScene(); 		return FALSE; 	}//set up opengl	glShadeModel(GL_SMOOTH); 	glEnable(GL_TEXTURE_2D); 	glEnable(GL_DEPTH_TEST); 	glDepthFunc(GL_LEQUAL); 	glClearDepth(1.0f); 	glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); 	glClearColor(0.0f, 0.0f, 0.0f, 0.0f); 	ShowWindow(hWnd, SW_SHOW);	UpdateWindow(hWnd); 	ResizeScene(width, height); 	return TRUE; }

1) Register window class
2) Create your window.
3) choose set pixel format.
4) create set rendering context(wglCreateContext,wglMakeCurrent)
5) init opengl states
6) render
7) be happy.(if there are no other errors somewhere [smile])

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.