PDA

View Full Version : What is wrong??



08-28-2002, 03:17 PM
Hi,

Sorry for my bad english iam german. Iam using Dev-C++.

I have written this code and the screen is black only! What is wrong??

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

LRESULT CALLBACK WndProc (HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);

void InitOpenGL();
void ResizeOpenGL(int, int);
void EnableOpenGL(HWND hWnd, HDC *hDC, HGLRC *hRC);
void DisableOpenGL(HWND hWnd, HDC hDC, HGLRC hRC);

struct sets
{
bool Fullscreen;
int Width;
int Height;
int Bits;
bool Keys[256];
};

sets Settings = {true, 800, 600, 16};

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int iCmdShow)
{
WNDCLASS wc;
HWND hWnd;
HDC hDC;
HGLRC hRC;
MSG msg;
BOOL bQuit = false;

wc.style = CS_OWNDC;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
wc.lpszMenuName = NULL;
wc.lpszClassName = "GLSample";
RegisterClass(&wc);

DWORD style;

if(Settings.Fullscreen)
{
style = WS_VISIBLE | WS_POPUP;

DEVMODE dmScreenSettings;
memset(&dmScreenSettings,0,sizeof(dmScreenSettings));
dmScreenSettings.dmSize=sizeof(dmScreenSettings);
dmScreenSettings.dmPelsWidth= Settings.Width;
dmScreenSettings.dmPelsHeight= Settings.Height;
dmScreenSettings.dmBitsPerPel= Settings.Bits;
dmScreenSettings.dmFields=DM_BITSPERPEL|DM_PELSWID TH|DM_PELSHEIGHT;

if(ChangeDisplaySettings(&dmScreenSettings,CDS_FULLSCREEN) != DISP_CHANGE_SUCCESSFUL)
return 0;
}
else
style = WS_VISIBLE | WS_OVERLAPPEDWINDOW;

hWnd = CreateWindow(
"GLSample",
"Title",
style,
0, 0,
Settings.Width, Settings.Height,
NULL, NULL, hInstance, NULL);

EnableOpenGL(hWnd, &hDC, &hRC);
ResizeOpenGL(Settings.Width,Settings.Height);
InitOpenGL();

while(!bQuit)
{
if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
if(msg.message == WM_QUIT)
{
bQuit = TRUE;
}
else
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
else
{
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT);

glBegin(GL_LINES);
glColor3f( 1.0f, 0.0f, 0.0f); glVertex3f( 0.0f, 0.0f, 0.0f);
glColor3f( 1.0f, 0.0f, 0.0f); glVertex3f(-2.0f, 0.0f, 0.0f);
glEnd();

SwapBuffers( hDC );

}
}

DisableOpenGL(hWnd, hDC, hRC);
DestroyWindow(hWnd);


return msg.wParam;
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch(message)
{
case WM_CREATE:
return 0;

case WM_CLOSE:
PostQuitMessage(0);
return 0;

case WM_DESTROY:
return 0;

case WM_SIZE:
ResizeOpenGL(LOWORD(lParam),HIWORD(lParam));
return 0;

case WM_KEYUP:
Settings.Keys[wParam] = false;
return 0;

case WM_KEYDOWN:
Settings.Keys[wParam] = true;
switch(wParam)
{
case VK_ESCAPE:
PostQuitMessage(0);
return 0;
}
return 0;

default:
return DefWindowProc (hWnd, message, wParam, lParam);
}
}


void EnableOpenGL(HWND hWnd, HDC *hDC, HGLRC *hRC)
{
PIXELFORMATDESCRIPTOR pfd;
int iFormat;

*hDC = GetDC(hWnd);

ZeroMemory (&pfd, sizeof (pfd));
pfd.nSize = sizeof (pfd);
pfd.nVersion = 1;
pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
pfd.iPixelType = PFD_TYPE_RGBA;
pfd.cColorBits = Settings.Bits;
pfd.cDepthBits = 16;
pfd.iLayerType = PFD_MAIN_PLANE;
iFormat = ChoosePixelFormat (*hDC, &pfd);
SetPixelFormat (*hDC, iFormat, &pfd);

*hRC = wglCreateContext( *hDC );
wglMakeCurrent( *hDC, *hRC );
}

void DisableOpenGL(HWND hWnd, HDC hDC, HGLRC hRC)
{
if(Settings.Fullscreen)
ChangeDisplaySettings(NULL,0);

wglMakeCurrent (NULL, NULL);
wglDeleteContext (hRC);
ReleaseDC (hWnd, hDC);
}

void ResizeOpenGL(int width, int height)
{
glViewport(0, 0, width, (height==0) ? 1 : height);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();

gluPerspective(60.0, (float)width/(float)height, 1,100);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}

void InitOpenGL()
{
glShadeModel(GL_SMOOTH);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClearDepth(1.0f);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
}

mikael_aronsson
08-28-2002, 11:06 PM
Hi !

I only had time for a quick look, but first of all the glClear call only clears the color buffer, not the depth buffer, but you have the depth buffer enabled, the second problem is that you have not moved the "camera", you load the modelview matrix with an indentity matrix, put a glTranslate after that to move away from zero and you should start to see something.

Also remember that you have set the near clipping plane to 1 so anything you draw must be at least that amount away from the "camera" to be visible.

I hope that helps a bit,
Mikael

Gavin
08-29-2002, 12:54 AM
To check that you have set your window up properly change the glClearColor and check that changes the blank color, if this is the case start checking that your camera can actually see what you have drawn.

08-29-2002, 02:48 AM
Thanks!