What wrong with my code?

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
using namespace std;

#define WIN32_LEAN_AND_MEAN

bool RegisterWinClass(HINSTANCE &, WNDPROC, LPSTR);
bool CreateWinEx(HWND &, HINSTANCE &, LPSTR, LPSTR);
bool SetupPixelFormat(HDC &);
void InitialOGLScreen(int, int);
bool InitialOGL(HDC &, HWND &, HGLRC &, RECT &);
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 &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(&wcx))
{
return false;
}

return true;
}

bool CreateWinEx(HWND &hWnd, HINSTANCE &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 &hWnd, PAINTSTRUCT &ps)
{
BeginPaint(hWnd, &ps);
EndPaint(hWnd, &ps);
}

bool SetupPixelFormat(HDC &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, &pfd)) == FALSE)
{
return false;
}

if(SetPixelFormat(hDc, iPixelFormat, &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 &hDc, HWND &hWnd, HGLRC &hGLrc, RECT &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, &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(&msg, sizeof(MSG));
bool done = false;

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

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

}

return msg.wParam;
}

LRESULT CALLBACK MainMsg(HWND hWnd, UINT uiMsg, WPARAM wParam,
LPARAM lParam)
{
switch(uiMsg)
{
case WM_PAINT:
{
BeginPaint(hWnd, &g_ps);
EndPaint(hWnd, &g_ps);
break;
}
case WM_SIZE:
{
InitialOGLScreen(LOWORD(lParam), HIWORD(wParam));
GetClientRect(hWnd, &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

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”.

yes…

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

nothing appear…

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…)

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

I not sure but perhaps render() should be call between BeginPaint(hWnd, &g_ps) and EndPaint(hWnd, &g_ps); ???
No ???

I tried but still won’t work…

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

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…”

I dont use windows so i cant try this out but you could try putting SwapBuffers() under Render() like this:

{
	Render();
            SwapBuffers(g_hDc);
}

@author of the thread:
and remove this goofy WM_PAINT message processing with Begin/EndPaint (???).

does it work now ?

hmmmmm… not working… i tried everything…

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

edit: Nevermind, wrong answer removed.

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

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

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

hmmmm… still not working either

hmmm… I have solved the problem by deleted the whole file and re-type the same code again

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

Anyway, thanks for the replay guys…

Where is the wndproc?
wcx.lpfnWndProc = wndProc;

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

mmmm…

@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 !?