PDA

View Full Version : What wrong with my code?



johncsl82
12-06-2003, 03:59 AM
hi...

My code:




#pragma comment(lib, "opengl32.lib")
#pragma comment(lib, "glu32.lib")
#include <windows.h>
#include <gl/gl.h>
#include <gl/glu.h>
#include <gl/glaux.h>
#include <iostream>
using namespace std;

#define WIN32_LEAN_AND_MEAN

bool RegisterWinClass(HINSTANCE &amp;, WNDPROC, LPSTR);
bool CreateWinEx(HWND &amp;, HINSTANCE &amp;, LPSTR, LPSTR);
bool SetupPixelFormat(HDC &amp;);
void InitialOGLScreen(int, int);
bool InitialOGL(HDC &amp;, HWND &amp;, HGLRC &amp;, RECT &amp;);
void Render();
WPARAM MainLoop();
LRESULT CALLBACK MainMsg(HWND, UINT, WPARAM, LPARAM);

HDC g_hDc = NULL;
HWND g_hWnd = NULL;
HGLRC g_hGLrc = NULL;
PAINTSTRUCT g_ps;
RECT g_rect;

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR, int)
{
if(!RegisterWinClass(hInstance, MainMsg, "OGLTest3"))
{
return -1;
}

if(!CreateWinEx(g_hWnd, hInstance, "OGLTest3", "OpenGL Test 3"))
{
return -2;
}

if(!InitialOGL(g_hDc, g_hWnd, g_hGLrc, g_rect))
{
return -3;
}

ShowWindow(g_hWnd, SW_SHOWNORMAL);
UpdateWindow(g_hWnd);
SetFocus(g_hWnd);

return (int)MainLoop();
}

bool RegisterWinClass(HINSTANCE &amp;hInstance, WNDPROC wndProc, LPSTR className)
{
WNDCLASSEX wcx;
wcx.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
wcx.cbSize = sizeof(WNDCLASSEX);
wcx.hInstance = hInstance;
wcx.lpfnWndProc = wndProc;
wcx.lpszClassName = className;

wcx.hbrBackground = NULL;
wcx.hCursor = LoadCursor(NULL, IDC_ARROW);
wcx.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wcx.hIconSm = LoadIcon(NULL, IDI_WINLOGO);

wcx.cbClsExtra = NULL;
wcx.cbWndExtra = NULL;
wcx.lpszMenuName = NULL;

if(!RegisterClassEx(&amp;wcx))
{
return false;
}

return true;
}

bool CreateWinEx(HWND &amp;hWnd, HINSTANCE &amp;hInstance, LPSTR className, LPSTR winCaption)
{
hWnd = CreateWindowEx(NULL,
className,
winCaption,
WS_OVERLAPPEDWINDOW,
0,
0,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
hInstance,
NULL);

if(!hWnd)
{
return false;
}

return true;
}

void Paint(HWND &amp;hWnd, PAINTSTRUCT &amp;ps)
{
BeginPaint(hWnd, &amp;ps);
EndPaint(hWnd, &amp;ps);
}

bool SetupPixelFormat(HDC &amp;hDc)
{
PIXELFORMATDESCRIPTOR pfd = {0};
int iPixelFormat = 0;

pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR);
pfd.nVersion = 1;

pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
pfd.dwLayerMask = PFD_MAIN_PLANE;
pfd.iPixelType = PFD_TYPE_RGBA;
pfd.cColorBits = 16; // or 16
pfd.cDepthBits = 16; // or 16
pfd.cAccumBits = 0;
pfd.cStencilBits = 0;

if((iPixelFormat = ChoosePixelFormat(hDc, &amp;pfd)) == FALSE)
{
return false;
}

if(SetPixelFormat(hDc, iPixelFormat, &amp;pfd) == FALSE)
{
return false;
}

return true;
}

void InitialOGLScreen(int width, int height)
{
if(height == 0)
{
height = 1;
}

glViewport(0, 0, width, height);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();

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

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}

bool InitialOGL(HDC &amp;hDc, HWND &amp;hWnd, HGLRC &amp;hGLrc, RECT &amp;rect)
{
hDc = GetDC(hWnd);

if(!SetupPixelFormat(hDc))
{
return false;
}

hGLrc = wglCreateContext(hDc);

if(wglMakeCurrent(hDc, hGLrc) == FALSE)
{
return false;
}

glEnable(GL_DEPTH_TEST);

GetClientRect(hWnd, &amp;rect);

InitialOGLScreen(rect.right, rect.bottom);

return true;
}

void Render()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear Screen And Depth Buffer
glLoadIdentity();

glTranslatef(-1.5f,0.0f,-6.0f); // Move Left 1.5 Units And Into The Screen 6.0
glBegin(GL_TRIANGLES); // Drawing Using Triangles
glColor3f(1.0f,0.0f,0.0f); // Set The Color To Red
glVertex3f( 0.0f, 1.0f, 0.0f); // Top
glColor3f(0.0f,1.0f,0.0f); // Set The Color To Green
glVertex3f(-1.0f,-1.0f, 0.0f); // Bottom Left
glColor3f(0.0f,0.0f,1.0f); // Set The Color To Blue
glVertex3f( 1.0f,-1.0f, 0.0f); // Bottom Right
glEnd(); // Finished Drawing The Triangle
glTranslatef(3.0f,0.0f,0.0f); // Move Right 3 Units
glColor3f(0.5f,0.5f,1.0f); // Set The Color To Blue One Time Only
glBegin(GL_QUADS); // Draw A Quad
glVertex3f(-1.0f, 1.0f, 0.0f); // Top Left
glVertex3f( 1.0f, 1.0f, 0.0f); // Top Right
glVertex3f( 1.0f,-1.0f, 0.0f); // Bottom Right
glVertex3f(-1.0f,-1.0f, 0.0f); // Bottom Left
glEnd();

SwapBuffers(g_hDc);
//SwapBuffers(hDC);
}

WPARAM MainLoop()
{
MSG msg;
ZeroMemory(&amp;msg, sizeof(MSG));
bool done = false;

while(!done)
{
if(PeekMessage(&amp;msg, NULL, 0, 0, PM_REMOVE))
{
if(msg.message == WM_QUIT)
{
done = true;
break;
}

TranslateMessage(&amp;msg);
DispatchMessage(&amp;msg);
}
else
{
Render();
}
}

return msg.wParam;
}

LRESULT CALLBACK MainMsg(HWND hWnd, UINT uiMsg, WPARAM wParam,
LPARAM lParam)
{
switch(uiMsg)
{
case WM_PAINT:
{
BeginPaint(hWnd, &amp;g_ps);
EndPaint(hWnd, &amp;g_ps);
break;
}
case WM_SIZE:
{
InitialOGLScreen(LOWORD(lParam), HIWORD(wParam));
GetClientRect(hWnd, &amp;g_rect);
break;
}
case WM_CLOSE:
{
PostQuitMessage(0);
break;
}
case WM_DESTROY:
{
PostQuitMessage(0);
break;
}
default:
{
break;
}
}

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


The problem is there are no objects appear on the screen.... and the whole screen just in black color

sebh
12-06-2003, 05:59 AM
Have you already try the gluLookAt function instead of using glTranslate?

You can specify your position, the point you watch to and the "orientation of our head".

johncsl82
12-06-2003, 08:36 AM
yes....

i use gluLookAt(0,0,6, 0,0,0, 0,1,0);

nothing appear........

sebh
12-06-2003, 09:40 AM
pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;


Perhaps there is a flag to enable the depth buffer?(i don't know very well win32 programming....)

johncsl82
12-06-2003, 10:19 AM
i use:
glEnable(GL_DEPTH_TEST | GL_DEPTH_BUFFER_BIT | GL_DEPTH);

and

pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER | PFD_DEPTH_DONTCARE;

but still not working.... no objects appear and the screen are just in black

sebh
12-06-2003, 12:33 PM
I not sure but perhaps render() should be call between BeginPaint(hWnd, &g_ps) and EndPaint(hWnd, &g_ps); ???
No ???

johncsl82
12-06-2003, 08:09 PM
I tried but still won't work....... http://www.opengl.org/discussion_boards/ubb/frown.gif

mikael_aronsson
12-07-2003, 03:51 AM
Does the OpenGL rendering work at all ?, try to set the clearcolor to red or something and run the application, if your window gets red then OpenGL is working fine and the problem is with the matrices possibly...

Mikael

DJSnow
12-07-2003, 06:03 AM
where are your objects located ?
try translating the cam, via glTranslate, that they CAN SEE the objects, it sounds like you are facing into the wrong direction...
as said in the red book: "...the cam is located at the origin and facing downwards the z axis, by default..."

onix
12-07-2003, 08:54 AM
I dont use windows so i cant try this out but you could try putting SwapBuffers() under Render() like this:

{
Render();
SwapBuffers(g_hDc);
}

DJSnow
12-07-2003, 09:28 AM
@author of the thread:
and remove this goofy WM_PAINT message processing with Begin/EndPaint (????).

DJSnow
12-09-2003, 02:42 AM
does it work now ?

johncsl82
12-09-2003, 02:57 AM
hmmmmm.... not working... i tried everything...

mikael_aronsson
12-09-2003, 04:18 AM
Originally posted by DJSnow:
@author of the thread:
and remove this goofy WM_PAINT message processing with Begin/EndPaint (????).

DO NOT REMOVE BeginPaint/EndPaint !!!

If you do windows is going to send WM_PAINT messages up the yazzooo, EndPaint clears an internal flag indicating that the window has ben repainted so no more WM_PAINT messages needs to be sent.

Mikael

Relic
12-09-2003, 04:49 AM
edit: Nevermind, wrong answer removed.

[This message has been edited by Relic (edited 12-09-2003).]

Overmind
12-09-2003, 04:04 PM
try changing in your window proc:




switch(uiMsg)
{
case ...:
...

default:
{
return DefWindowProc(hWnd, uiMsg, wParam, lParam); // add this
}
}

//return DefWindowProc(...) <-- this is wrong



You should only call DefWindowProc when you don't handle the message yourself so you get a default. For example, when a WM_PAINT message gets in, you already take care of the painting, so there is no need to call DefWindowProc because that would draw the default background over the things you painted before...

EDIT: proper code formating is not as easy as it looks http://www.opengl.org/discussion_boards/ubb/wink.gif

[This message has been edited by Overmind (edited 12-09-2003).]

johncsl82
12-10-2003, 05:10 AM
hmmmm..... still not working either

johncsl82
12-11-2003, 03:42 AM
hmmm.... I have solved the problem by deleted the whole file and re-type the same code again http://www.opengl.org/discussion_boards/ubb/frown.gif ....

It this means there is some thing wrong with my compiler.......??? I'm using MVC++ .NET

Anyway, thanks for the replay guys.... http://www.opengl.org/discussion_boards/ubb/biggrin.gif

12-11-2003, 10:29 AM
Where is the wndproc?
wcx.lpfnWndProc = wndProc;

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

mmmm....

DJSnow
12-12-2003, 07:24 AM
@mikael_aronsson:

ok, then i'm wondering why:
1. my code works perfectly without it
2. even the author of "opengl game programming" don't use this ???

but i'm sure that you have an answer !?

12-12-2003, 10:51 AM
>>ok, then i'm wondering why:
>>1. my code works perfectly without it

How does a Win32 Application process window messages without a window proc?

>>2. even the author of "opengl game >>programming" don't use this ???

No? are you sure?

DJSnow
12-13-2003, 05:28 AM
@where am i:
>>How does a Win32 Application process
>>window messages without a window proc?
should i send the source to you ???????????
don't answer: i will send you a small app, with our engine, that you can figure out it for yourself.

apart from that: i stated out "why my program works though" and not "hey, remove the window msg proc." - saying to guy, that he should try removing begin/end paint(), has nothing to do with saying "remove the window msg proc." - where did you get this information from ???

>>No? are you sure?
nearly, 99,9% - yes; although i don't have it by hand, right now yet; i will look into it, when i'm back at my apartment, this evening.