Help needed with my openGL code - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Help needed with my openGL code

Xrid3rXrid3r Posts: 1Member
Hello guys!

I've been working on my own openGL window for a while now..
I can compile it in dev c++ without errors, but it refuses to display my window.
Hope you can tell me what I've doing wrong or what I've missed.

Here is the code:
[code]
#include
#include
#include
#include
#include
#include
#include
#include "time.h"

LRESULT CALLBACK WndProc(HWND hWND,UINT umsg,LPARAM lParam,WPARAM wParam);

HGLRC hRC = NULL;
HDC hDC = NULL;
HWND hWnd = NULL;
HINSTANCE hInstance;

bool active = true;
bool keys[256];

GLvoid ResizeGLScene(GLsizei width,GLsizei height)
{
if(height == 0)
{
height = 1;
}
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f,(GLfloat)width / (GLfloat) height,0.1f,100.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}

int InitGLScene(GLvoid)
{
glClearColor(0.0f,0.0f,0.0f,0.23f);
glClearDepth(2.0f);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glHint(GL_PERSPECTIVE_CORRECTION_HINT,GL_NICEST);
return true;
}

int DrawGLScene(GLvoid)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslatef(0.23f,0.23f,0.23f);
glBegin(GL_QUADS);
glVertex3f(0.20f,0.20f,0.20f);
glVertex3f(0.20f,-0.20f,0.20f);
glVertex3f(-0.20f,0.20f,0.20f);
glVertex3f(-0.20f,-0.20f,0.20f);

glVertex3f(-0.20f,-0.20f,0.20f);
glVertex3f(0.20f,-0.20f,0.20f);
glVertex3f(-0.20f,0.20f,0.20f);
glVertex3f(0.20f,0.20f,0.20f);

glVertex3f(0.20f,0.20f,0.20f);
glVertex3f(0.20f,-0.20f,0.20f);
glVertex3f(-0.20f,0.20f,0.20f);
glVertex3f(-0.20f,-0.20f,0.20f);

glVertex3f(0.20f,0.20f,0.20f);
glVertex3f(0.20f,-0.20f,0.20f);
glVertex3f(-0.20f,0.20f,0.20f);
glVertex3f(-0.20f,-0.20f,0.20f);

glVertex3f(0.20f,0.20f,0.20f);
glVertex3f(0.20f,-0.20f,0.20f);
glVertex3f(-0.20f,0.20f,0.20f);
glVertex3f(-0.20f,-0.20f,0.20f);

glVertex3f(0.20f,0.20f,0.20f);
glVertex3f(0.20f,-0.20f,0.20f);
glVertex3f(-0.20f,0.20f,0.20f);
glVertex3f(-0.20f,-0.20f,0.20f);
glEnd();
}
bool drawGLScene(char *title,int width,int height,int bits)
{
GLuint PixelFormat;
DWORD dwStyle;
DWORD dwExStyle;
WNDCLASS wc;
RECT windowRect;
windowRect.left = (long) 0;
windowRect.top = (long) height;
windowRect.right = (long) 0;
windowRect.bottom = (long) width;

hInstance = GetModuleHandle( NULL );
wc.style = CS_VREDRAW | CS_HREDRAW | CS_OWNDC;
wc.lpfnWndProc = (WNDPROC) WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hbrBackground = NULL;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL,IDI_WINLOGO);
wc.hCursor = LoadCursor(NULL,IDC_ARROW);
wc.lpszMenuName = NULL;
wc.lpszClassName = "OPENGL";
if(!RegisterClass(&wc))
{
MessageBox(NULL,"Could not find the current register class","REGISTER ERROR",MB_OK | MB_ICONINFORMATION);
return false;
}

if(hWnd != CreateWindowEx(dwStyle,
"OPENGL",
title,
WS_CLIPCHILDREN |
WS_CLIPSIBLINGS |
dwExStyle,
0,0,
windowRect.top-windowRect.left,
windowRect.bottom-windowRect.right,
NULL,
NULL,
hInstance,
NULL))
{
MessageBox(NULL,"Could not find the current window", "Window ERROR", MB_OK | MB_ICONSTOP);
return false;
}

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

InitGLScene();
SetFocus(0);
DrawGLScene();
ShowWindow(hWnd,SW_SHOW);
SetForegroundWindow(hWnd);
SetFocus(hWnd);
}

LRESULT CALLBACK WndProc(HWND hWnd,UINT umsg,LPARAM lParam,WPARAM wParam)
{
switch(umsg)
{
case WM_SYSCOMMAND:
switch(wParam)
{
case SC_MONITORPOWER:
case SC_SCREENSAVE:
break;
};

case WM_KEYDOWN:
keys[wParam] = true;
break;

case WM_KEYUP:
keys[wParam] = false;
break;
};

case WM_RESIZE:
ResizeGLScene(LOWORD(lParam),HIWORD(wParam));
break;

return DefWindowProc(hWnd,umsg,lParam,wParam);
}
int WINAPI WinMain(HINSTANCE hIntance,HINSTANCE hPrevInst,LPSTR iCmdLine,int iCmdShow)
{
MSG msg;
bool done = false;

drawGLScene("Mickes",640,480,16);
while(!done)
{
if(PeekMessage(&msg,NULL,0,0,PM_REMOVE))
{
if(msg.message == WM_QUIT)
{
done = true;
}
else
{
DispatchMessage(&msg);
TranslateMessage(&msg);
}
SwapBuffers(hDC);
DrawGLScene();
}
return (msg.wParam);
}
}

[/code]


~Xrid3r

Comments

  • jadrianjadrian Posts: 5Member
    A couple of items appear to be missing...unless you just didn't include them in your post. I didn't see where you called ChoosePixelFormat, SetPixelFormat, wglCreateContext, and wglMakeCurrent. Without these things, you have no context to render to. As a matter of fact, you initialize hDC to NULL, but I don't see where you attach it to an actual device context. I'm more familiar with MFC, but you need something like this:

    [code]
    HDC hDC = hWnd->GetSafeHdc();

    int nPixelFormat = ChoosePixelFormat(hDC, &pfd);
    SetPixelFormat(hDC, nPixelFormat, &pfd);

    hRC = wglCreateContext(hDC);
    wglMakeCurrent(hDC, hRC);
    [/code]

    This, of course, must be done after creating your main window, but before any other openGL calls.
Sign In or Register to comment.