Archived

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

LittleTaz

unknown problem (no drawing)

Recommended Posts

LittleTaz    122
Hi everybody! 2 days i ago, i bought this OGL Game Programming book ! i really like it. but: when i finished coding the simpliest OGL app (drawing a triangle), it simple displays anything !!! i downloaded the source for this chapter from the webpage, compiled it and it worked. i''ve been searching for the problem for one day ... i didn''t find any. this is where you come in. i hope you can tell me where the problem is. the code isn''t documentated at all, but easy to understand (hopefully), and pretty "short". would be great if you can help me !
  

#define WIN32_LEAN_AND_MEAN

#include <windows.h>

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

///////////////////////////

// defines


#define MY_APPLICATION_NAME		"GLTest1"
#define SCREEN_WIDTH			800
#define SCREEN_HEIGHT			600
#define SCREEN_BPP				32


///////////////////////////

// globals:

HDC		g_hDC;
bool	fullscreen = false;


///////////////////////////

// prototypes:

void RenderScene();


void ChangeResolution()
{

	DEVMODE	devModeScreen;

	memset(&devModeScreen,0,sizeof(devModeScreen));
	devModeScreen.dmSize = sizeof(devModeScreen);
	devModeScreen.dmPelsHeight = SCREEN_HEIGHT;
	devModeScreen.dmPelsWidth = SCREEN_WIDTH;
	devModeScreen.dmBitsPerPel = SCREEN_BPP;
	devModeScreen.dmFields = DM_PELSWIDTH | DM_PELSHEIGHT | DM_BITSPERPEL;

	if(ChangeDisplaySettings(&devModeScreen, CDS_FULLSCREEN) != DISP_CHANGE_SUCCESSFUL)
		fullscreen = false;

}


void SetupPixelFormat()
{

	int nPixelFormat;

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

	nPixelFormat = ChoosePixelFormat(g_hDC,&pfd);

	SetPixelFormat(g_hDC,nPixelFormat,&pfd);

}


///////////////////////////

// WindowProcedure

LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{

	static HGLRC		hRC;

	int width, height;

	switch(message)
	{
	case WM_CREATE:
		g_hDC = GetDC(hwnd);
		SetupPixelFormat();

		hRC = wglCreateContext(g_hDC);
		wglMakeCurrent(g_hDC,hRC);
		return 0;

		break;


	case WM_CLOSE:
		wglMakeCurrent(g_hDC,NULL);
		wglDeleteContext(hRC);
		ReleaseDC(hwnd,g_hDC);

		PostQuitMessage(0);
		return 0;
		break;

	case WM_SIZE:
		height = HIWORD(lParam);
		width = LOWORD(wParam);

		if(height==0)
		{
			height=1;
		}

		glViewport(0,0,width,height);
		glMatrixMode(GL_PROJECTION);
		glLoadIdentity();

		gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,1.0f,1000.0f);

		glMatrixMode(GL_MODELVIEW);
		glLoadIdentity();

		return 0;
		break;

	default:
		break;
	}

	return DefWindowProc(hwnd,message,wParam,lParam);

}

void InitGLStates()
{
	// enable the depth buffer and backface culling

	glEnable(GL_DEPTH_TEST);
	glEnable(GL_CULL_FACE);

	// use smooth shading

	glShadeModel(GL_SMOOTH);

	// Clear background to black

	glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
}


///////////////////////////

// WinMain

// Entrypoint

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow)
{

	WNDCLASSEX		windowClass;
	HWND			hWnd;
	MSG				msg;
	bool			done;
	DWORD			exWindowStyle, windowStyle;
	RECT			windowRect;

	if(fullscreen)
		ChangeResolution();

	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_APPLICATION);
	windowClass.hInstance = hInstance;
	windowClass.lpfnWndProc = (WNDPROC)WndProc;
	windowClass.lpszClassName = MY_APPLICATION_NAME;
	windowClass.lpszMenuName = NULL;
	windowClass.style = CS_HREDRAW | CS_VREDRAW;

	if(!RegisterClassEx(&windowClass))
	{
		MessageBox(NULL,"Error: WindowCreation failure","Error",MB_OK);
		return 0;
	}

	if(fullscreen)
	{
		exWindowStyle = WS_EX_APPWINDOW;
		windowStyle = WS_POPUP;
	}
	else
	{
		exWindowStyle = NULL;
		windowStyle = WS_OVERLAPPEDWINDOW | WS_VISIBLE | WS_SYSMENU |
			WS_CLIPCHILDREN | WS_CLIPSIBLINGS;

		windowRect.top = 0;
		windowRect.left = 0;
		windowRect.bottom = SCREEN_HEIGHT;
		windowRect.right = SCREEN_WIDTH;
	}

	AdjustWindowRectEx(&windowRect,windowStyle,NULL,exWindowStyle);

	hWnd = CreateWindowEx(exWindowStyle,
						MY_APPLICATION_NAME,MY_APPLICATION_NAME,
						windowStyle,
						0,0,
						windowRect.right,windowRect.bottom,
						NULL,
						NULL,
						hInstance,
						NULL);

	if(!hWnd)
	{
		MessageBox(NULL,"Error: WindowCreation failure","Error",MB_OK);
		return 0;
	}

	ShowWindow(hWnd,SW_SHOWNORMAL);
	UpdateWindow(hWnd);

	done=false;

	// finally initiate the GLStates:

	InitGLStates();

	while(!done)
	{
		PeekMessage(&msg,hWnd,NULL,NULL,PM_REMOVE);

		if(msg.message == WM_QUIT)
		{
			done = true;
		}
		else
		{

			RenderScene();
			
			TranslateMessage(&msg);
			DispatchMessage(&msg);

		}
	}

	// get back to normal desktop size:

	if(fullscreen)
		ChangeDisplaySettings(NULL,0);

	return msg.wParam;
}






///////////////////////////

// Render function

void RenderScene()
{

	// clear screen and depth buffer

	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);										
	glLoadIdentity();

	// move back 15 units

	glTranslatef(0.0f, 0.0f, -50.0f);

	// draw our smooth shaded triangle

	glBegin(GL_TRIANGLES);
		glColor3f(1.0f, 0.0f, 0.0f);				// red vertex

		glVertex3f(-10.0f, -10.0f, -5.0f);			
		glColor3f(0.0f, 1.0f, 0.0f);				// green vertex

		glVertex3f(20.0f, -10.0f, -5.0f);
		glColor3f(0.0f, 0.0f, 1.0f);				// blue vertex

		glVertex3f(-10.0f, 20.0f, -5.0f);
	glEnd();


	glFlush();
	SwapBuffers(g_hDC);			// bring backbuffer to foreground


}
  
thnx in advance for any suggestions ! Have a nice day !
Grimmm Games - Game Developer"

Share this post


Link to post
Share on other sites
try to replate that:

while(!done)
{ PeekMessage(&msg,hWnd,NULL,NULL,PM_REMOVE); if(msg.message == WM_QUIT)
{
done = true;
}
else
{
RenderScene();
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}

with something like:

while(!done)
{ if(PeekMessage(&msg,hWnd,NULL,NULL,PM_REMOVE))
{
if(msg.message == WM_QUIT)
done = true;
else
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
else
RenderScene();
}
}


but i have not tried if this is the only problem...

Share this post


Link to post
Share on other sites
LittleTaz    122
thnx for replying.
but your suggestion doesn''t work, sadly

maybe someone other found a solution !?
i hope so ! i can''t imagine where the problem could be. i compared the downloaded source with mine, but there is no really difference between them ...

the downloaded source, draws the triangle, but mine doesn''t

i really need your help !!!!

Have a nice day !
Grimmm Games - Game Developer

Share this post


Link to post
Share on other sites
Krippy2k    134
I would check out your code but cutting and pasting from here into MSVC just isnt happening. lol All the newlines disappear when the code is within the source tags.

If you send the code to me via email I will be happy to check it out for you.

toasted2k@yahoo.com

Seeya
Krippy

Share this post


Link to post
Share on other sites
Gorg    248
You are missing the viewport and projection matrix settings.

Put this at the begining of render scene :

  
//this will define how much of the window will be used for

//drawing. Here we ask for the full window

glViewport( 0, 0,SCREEN_WIDTH, SCREEN_HEIGHT );

glMatrixMode( GL_PROJECTION );
glLoadIdentity();


for the projection matrix, you have 2 choices :
Perpective (object farther from the viewpoint are smaller)
or orthogonal (object look the same size even if they are far
away from the viewpoint)


to see your triangles, the coordinates should fit in the
projection definition

Just put one of the following 2 function calls.

  
//for ortho:

glOrtho( 50, -50, -50, 50, 50, -50 );

//for perspective :

glFrustum( -5, 5, -5, 5, 1, 100 );

Share this post


Link to post
Share on other sites
Krippy2k    134
Actually he had the proper code to setup his perspective and viewport. It is in WndProc on WM_SIZE.

I already emailed him about this, but his problem was just a typo really. Just for reference here is the problem:

  
case WM_SIZE:
height = HIWORD(lParam);
width = LOWORD(wParam);


wParam is wrong on the width assignment it should be lParam. The LOWORD of wParam was equating to 0 so he was getting a width of 0 on his viewport.

Julio: Thanks! I knew there had to be a way. lol

Seeya
Krippy

Share this post


Link to post
Share on other sites