OGL initialization not correct?

Started by
10 comments, last by Caesar 22 years, 1 month ago
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???
Advertisement
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).
---visit #directxdev on afternet <- not just for directx, despite the name
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.
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);}  
Come on, guys, even a hint helps. I could rewrite it from the scratch but I won''t find the mistake then...
I''m not sure about this, but I recall hearing somewhere that the window class needs to have the CS_OWNDC style flag set.
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.
---visit #directxdev on afternet <- not just for directx, despite the name
Do you set your pixel format with the PFD_DOUBLEBUFFER flag set on the dwFlags member? If so, remember to call SwapBuffers(insert_HDC_name_here) after drawing.
it was probably the CS_OWNDC flag and as if it wasn''t enough, there''s nearly sure something wrong with the WM_SIZE message handling
quote:Original post by Caesar
... (i use glFlush and it''s in between glBegin and glEnd)...


A glFlush call between a glBegin/glEnd is *not* valid.

This topic is closed to new replies.

Advertisement