Archived

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

OGL initialization not correct?

This topic is 5740 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I''m a newbie to the OGL world and I''m learning how to init all the basic stuff. I got OGL SuperBible to help me. My problem is that whatever i draw is not shown... I create a window (WS_CLIPSIBLINGS and WS_CLIPCHILDERN) then I in a reaction to the WM_CREATE get the dc, set the pixel format for it, create the RC, I make it current. I also react to the WM_SIZE and set all the stuff (set Viewport, reset coord,set coord using glOrtho) . At WM_PAINT, i draw something, invalidaterect, swapbuffers. this doesn''t work, if i draw any lines/points/faces, they just don''t show (i use glFlush and it''s in between glBegin and glEnd). I also tried to do just glClearColor(1.0f,1.0f,0.0f,0.5f) and it brought me just that default green (windows desktop when reinstall, that''s it). WHatever I change those to, still the same color (it''s alone, no glBegin/glEnd, only Flush, Valid, Swap). I tried to change the swap/valid positions, nothing. If i comment one of those out, the screen (window) stays black, if I comment them both out, it''s green again!?? DC seems correct (no error when getting, the same for all other WinAPI func), gluErrorString keeps me telling "bad operation" (just translation from my native lang) twice (whereever I call them), but all the func return correct values... Help, what could be wrong???

Share this post


Link to post
Share on other sites
Why do you use WS_CLIPSIBLINGS and WS_CLIPCHILDREN?

In WM_PAINT, you should call ValidateRect rather than InvalidateRect. You are updating your client area. Read help if you don''t understand the difference.

Assuming your RC creaton succeeds, you need to find the exact place that gives you GL error. Make a function to check glGetError''s return value and add it after each gl call you have, like this:

void CheckError()
{
GLenum error = glGetError();
if (error != GL_NO_ERROR)
DebugBreak();
}

I''m not sure about this one, but try

glClearColor(...);
glClear(GL_COLOR_BUFFER_BIT);

Also, you usually need to call glClearColor only once (in init).

Share this post


Link to post
Share on other sites
Does something like this work?


  
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static HGLRC hRC;
static HDC hDC;
int width, height;

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

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

return 0;
break;

case WM_CLOSE:

wglMakeCurrent(hDC, NULL);
wglDeleteContext(hRC);

PostQuitMessage(0);

return 0;
break;

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

if(height == 0) // don''t want to divide by zero!

height = 1;


glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION); // set projection matrix

glLoadIdentity(); // reset projection matrix


// calculate aspect ratio of window

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

glMatrixMode(GL_MODELVIEW); // set modelview matrix

glLoadIdentity(); // reset modelview matrix


return 0;
break;

default:
break;
}

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


I''m using OpenGL Game Programming, and got the basic structure from that. It works fine for me!

Regards,
Mathematix.

Share this post


Link to post
Share on other sites
Ummm, tried the error checking but keeps telling Invalid Operation (don''t know why, it''s nearly the same code as in my working one) Here''s the code, maybe you''ll find something. Thanks.


  

long CALLBACK WndProc(HWND hWindow,UINT msg,WPARAM wParam,LPARAM lParam)
{
static HDC hDC;
static HGLRC hRC;

switch(msg)
{
case WM_CREATE:{
hDC=GetDC(hWindow);
SetupPixelFormat(hDC);
hRC=wglCreateContext(hDC);
wglMakeCurrent(hDC,hRC);

// vgSetupPixelFormat(hDC);

// vgSetupRenderingContext(hDC);


break;
}
case WM_PAINT:{


glClear(GL_COLOR_BUFFER_BIT);

glBegin(GL_LINE);

glColor3f(1.0,0.0,1.0);
glVertex3f(-10.,-10.,0);
glVertex3f(10.,10.,0);

glEnd();

glFlush();

ValidateRect(hWindow,NULL);

SwapBuffers(hDC);

break;
}
case WM_SIZE:{
int width=LOWORD(lParam),height=HIWORD(lParam);
/*
// Prevent a divide by zero
if(h == 0)
h = 1;

// Set Viewport to window dimensions
glViewport(0, 0, w, h);

// Reset coordinate system
glLoadIdentity();

// Establish clipping volume (left, right, bottom, top, near, far)
if (w <= h)
glOrtho (0.0f, 250.0f, 0.0f, 250.0f*h/w, 1.0, -1.0);
else
glOrtho (0.0f, 250.0f*w/h, 0.0f, 250.0f, 1.0, -1.0);
*/

glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION); // set projection matrix

glLoadIdentity(); // reset projection matrix

// calculate aspect ratio of window

gluPerspective(45.0f, (GLfloat) width/(GLfloat) height, 1.0f, 1000.0f);
glMatrixMode(GL_MODELVIEW); // set modelview matrix

glLoadIdentity(); // reset modelview matrix


break;
}
}

return DefWindowProc(hWindow,msg,wParam,lParam);
}

int vgSetupPixelFormat(HDC hDC)
{
int pixel_format;

static PIXELFORMATDESCRIPTOR pfd = {
sizeof(PIXELFORMATDESCRIPTOR), // Size of this structure

1, // Version of this

// structure


PFD_DRAW_TO_WINDOW | // Draw to window

// (not bitmap)


PFD_SUPPORT_OPENGL | // Support OpenGL calls

PFD_DOUBLEBUFFER, // Double-buffered mode

PFD_TYPE_RGBA, // RGBA Color mode

32, // Want 24bit color

0,0,0,0,0,0, // Not used to select mode

0,0, // Not used to select mode

0,0,0,0,0, // Not used to select mode

32, // Size of depth buffer

0, // Not used to select mode

0, // Not used to select mode

PFD_MAIN_PLANE, // Draw in main plane

0, // Not used to select mode

0,0,0 }; // Not used to select mode


pixel_format=ChoosePixelFormat(hDC, &pfd);

SetPixelFormat(hDC,pixel_format, &pfd);

return pixel_format;
}

HWND vgCreateWindow(char *window_name)
{
HWND hWindow;
WNDCLASS wc;

wc.cbClsExtra=0;
wc.cbWndExtra=0;
wc.hbrBackground=(HBRUSH)GetStockObject(BLACK_BRUSH);
wc.hCursor=LoadCursor(NULL,IDC_ARROW);
wc.hIcon=LoadIcon(NULL,IDI_APPLICATION);
wc.hInstance=GetModuleHandle(NULL);
wc.lpfnWndProc=WndProc;
wc.lpszClassName=window_name;
wc.lpszMenuName=NULL;
wc.style=CS_VREDRAW | CS_HREDRAW;

RegisterClass(&wc);

hWindow=CreateWindow(window_name,
window_name,
WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN | WS_CLIPSIBLINGS ,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
GetModuleHandle(NULL),
NULL);

ShowWindow(hWindow,SW_SHOW);
// UpdateWindow(hWindow);


return hWindow;
}

HGLRC vgSetupRenderingContext(HDC hDC)
{
HGLRC hRC;

hRC=wglCreateContext(hDC);
CheckError();
wglMakeCurrent(hDC,hRC);

return hRC;
}

void vgDestoryRenderingContext(HGLRC hRC)
{
wglDeleteContext(hRC);
}

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
I''m not sure about this, but I recall hearing somewhere that the window class needs to have the CS_OWNDC style flag set.

Share this post


Link to post
Share on other sites
Replace CS_VREDRAW | CS_HREDRAW with CS_OWNDC.

Set your modelview matrix to something other than identity. For example, use glFrustum or gluPerspective, or even gluOrtho2D.

Is your rendering context not NULL?

Does wglMakeCurrent succeed?

What line exactly gives you the Invalid Operation error?

In any event, you should perform error checking in your code.

Share this post


Link to post
Share on other sites
with that glFlush, just an language mistake (see to code below).

setpixel func:


  
int vgSetupPixelFormat(HDC hDC)
{
int pixel_format;

static PIXELFORMATDESCRIPTOR pfd = {
sizeof(PIXELFORMATDESCRIPTOR), // Size of this structure

1, // Version of this

// structure


PFD_DRAW_TO_WINDOW | // Draw to window

// (not bitmap)


PFD_SUPPORT_OPENGL | // Support OpenGL calls

PFD_DOUBLEBUFFER, // Double-buffered mode

PFD_TYPE_RGBA, // RGBA Color mode

32, // Want 24bit color

0,0,0,0,0,0, // Not used to select mode

0,0, // Not used to select mode

0,0,0,0,0, // Not used to select mode

32, // Size of depth buffer

0, // Not used to select mode

0, // Not used to select mode

PFD_MAIN_PLANE, // Draw in main plane

0, // Not used to select mode

0,0,0 }; // Not used to select mode


pixel_format=ChoosePixelFormat(hDC, &pfd);

SetPixelFormat(hDC,pixel_format, &pfd);

return pixel_format;
}


it draw the things now, just need to rewrite the WM_SIZE handling code...

Thanks in advance

Share this post


Link to post
Share on other sites