glGetError() returning "invalid operation" before I do anything...

I was having a problem with OpenGL even clearing the background color in my Init() function, so I threw in a check for glGetError() at the beginning of my graphics class constructor. For some reason, right when my class is constructed, glGetError() returns error code 1282, or “invalid operation.”

Why could it be doing this? I haven’t called a single OpenGL function yet and it’s saying I performed an invalid operation?

Thanks for the help.

First, glGetError() will return 1282 - invalid operation if the GL context is not current (or not yet created). So, create your context first, then call glGetError. And, verify that any parent class or member class does not call GL functions in their constructors or prior to context being created.

If that doesn’t help, show some code.

That got rid of the errors, but now I’m getting the same results but without an error at all.

bool COGLGraphics::Init()
{
	GLuint		PixelFormat;
	static	PIXELFORMATDESCRIPTOR pfd=				// pfd Tells Windows How We Want Things To Be
	{
		sizeof(PIXELFORMATDESCRIPTOR),				// Size Of This Pixel Format Descriptor
			1,											// Version Number
			PFD_DRAW_TO_WINDOW |						// Format Must Support Window
			PFD_SUPPORT_OPENGL |						// Format Must Support OpenGL
			PFD_DOUBLEBUFFER,							// Must Support Double Buffering
			PFD_TYPE_RGBA,								// Request An RGBA Format
			Application()->GetColorBits(),				// Select Our Color Depth
			0, 0, 0, 0, 0, 0,							// Color Bits Ignored
			0,											// No Alpha Buffer
			0,											// Shift Bit Ignored
			0,											// No Accumulation Buffer
			0, 0, 0, 0,									// Accumulation Bits Ignored
			16,											// 16Bit Z-Buffer (Depth Buffer)  
			0,											// No Stencil Buffer
			0,											// No Auxiliary Buffer
			PFD_MAIN_PLANE,								// Main Drawing Layer
			0,											// Reserved
			0, 0, 0										// Layer Masks Ignored
	};
	
	if (!(g_hDC=GetDC(GetWindow()->GetHWnd())))							// Did We Get A Device Context?
	{
		throw CError("[COGLGraphics::Init()]:  Can't Create A GL Device Context");
		return false;
	}
	
	if (!(PixelFormat=ChoosePixelFormat(g_hDC,&pfd)))	// Did Windows Find A Matching Pixel Format?
	{
		throw CError("[COGLGraphics::Init()]:  Can't Find A Suitable PixelFormat");
		return FALSE;								// Return FALSE
	}
	
	if(!SetPixelFormat(g_hDC,PixelFormat,&pfd))		// Are We Able To Set The Pixel Format?
	{
		throw CError("[COGLGraphics::Init()]:  Can't Set The PixelFormat");
		return FALSE;								// Return FALSE
	}
	
	if (!(g_hRC=wglCreateContext(g_hDC)))				// Are We Able To Get A Rendering Context?
	{
		throw CError("[COGLGraphics::Init()]:  Can't Create A GL Rendering Context");
		return FALSE;								// Return FALSE
	}
	
	if(!wglMakeCurrent(g_hDC,g_hRC))					// Try To Activate The Rendering Context
	{
		throw CError("[COGLGraphics::Init()]:  Can't Activate The GL Rendering Context");
		return FALSE;								// Return FALSE
	}

	CheckErrors();

	glShadeModel(GL_SMOOTH);
	glClearColor(0.0f, 1.0f, 0.0f, 0.5f);
	glClearDepth(1.0f);
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	
	glEnable(GL_DEPTH_TEST);
	glDepthFunc(GL_LEQUAL);
	glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
	
	Resize(Application()->GetWidth(), Application()->GetHeight());
	
	CheckErrors();
	
	return true;
}

It is still saying invalid operation error? Or what exactly does it do now? What values do you get for variables g_hdc, PixelFormat, and g_hrc?

I just tested your line:

if (!(g_hDC=GetDC(GetWindow()->GetHWnd())))

and it doesn’t like it.

I assume this is a CWnd derived class? If a CWnd use this to get your hdc:

g_hdc = this->GetDC()->GetSafeHdc();

Sorry for the lack of clarification. It doesn’t give me any errors at all, but the window still doesn’t clear to the clear color, nor does it render my primitive (not shown).

The value for g_hDC is “unused: 12533708” (0x0601150d). The value for g_hRC is “unused: 4390973” (0x00010000).

Did you make the change I suggested?

Also, how is your rendering function set up? Is it within the OnPaint function if this is a CWnd or OnDraw if this is a CView? Do you have a timer set up? Do you have a glClear call at start of rendering pass?

No, I didn’t.

My CWindow class is a class that basically handles all the functions of a window. It implements WndProc, RegisterClass, etc…

COGLGraphics is a class that implements the actual graphics API – in this case, OpenGL.

GetWindow() gets a pointer to my main CWindow, and GetHWnd() returns the HWND of the window. That line works fine, and gives g_hDC a value of “unused: 12533708” (0x0601150d).

The way I have things setup, the window is first created through CWindow, and then a new COGLGraphics() is stored into a global graphics pointer. When this is done, Init() is called to initialize the graphics, as shown in the code above. After that, the main loop checks for windows messages, handles them, and renders everything.

The problem is that while all of the creation functions in COGLGraphics::Init() are working, the OpenGL functions that come right after them to clear the window and such don’t work. I end up with a blank window with a WHITE background color, instead of green as I’ve specified.

Here’s my WndProc function, which may help:

LRESULT CWindow::WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	assert(hWnd);
	assert(uMsg);
	assert(GetWindow() != NULL);
	

	bool bRunDef = false;
	
	switch(uMsg)
	{
	case WM_CREATE:
		{
			GetWindow()->m_hWnd = hWnd;
			GetWindow()->m_bActive = true;
			break;
		}

	case WM_SIZE:
		{
			if(Graphics())
				Graphics()->Resize(LOWORD(lParam),HIWORD(lParam));
			break;
		}

	default:
		bRunDef = true;
		break;
		
	}
	
	if(bRunDef == true)
		return DefWindowProc(hWnd,uMsg,wParam,lParam);

	return 0;
}

No, that doesn’t help except to explain it is a Win32 C++ app.

It doesn’t tell me anything about your rendering function, how you have it set up, what functions it calls.

Do you have glClear called at start of render function? Do you have BeginPaint and EndPaint calls? What gl functions are you using or trying to use. Are they extensions that you haven’t initialized? Are they erroring?

Try glGetError() after each GL call after wglMakeCurrent and see if something is erroring out.

At the beginning of every render, I call glClear(), glBegin(GL_QUADS), some glVertex3f() calls, and then glEnd().

For some reason, it’s giving me an invalid operation error after glEnd() is called.

[This message has been edited by Rodzilla (edited 03-15-2003).]

What range are your coordinates? Are they 2D or 3D? Do you have a projection set up (ie gluPerspective, glOrtho, glOrtho2d, GLfrustum)?

It would be easier if you just post your render function.

Here’s my resize function(called by Init()):

bool COGLGraphics::Resize(int width, int height)
{
	assert(height > 0);
	assert(width > 0);
	
	glViewport(0, 0, width, height);
	glMatrixMode(GL_PROJECTION);
	
	glLoadIdentity();
	
	gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,100.0f);
	
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
	
	CheckErrors();
	
	return true;
}

All the points are 3D points passed in via glVertex3f().

	Graphics()->Clear();
	
	Graphics()->BeginScene(G_QUADS);
	
	Graphics()->SetVertex3( m_length, m_length,-m_length);
	Graphics()->SetVertex3(-m_length, m_length,-m_length);
	Graphics()->SetVertex3(-m_length, m_length, m_length);
	Graphics()->SetVertex3( m_length, m_length, m_length);
	
	Graphics()->SetVertex3( m_length,-m_length, m_length);
	Graphics()->SetVertex3(-m_length,-m_length, m_length);
	Graphics()->SetVertex3(-m_length,-m_length,-m_length);
	Graphics()->SetVertex3( m_length,-m_length,-m_length);
	
	Graphics()->SetVertex3( m_length, m_length, m_length);
	Graphics()->SetVertex3(-m_length, m_length, m_length);
	Graphics()->SetVertex3(-m_length,-m_length, m_length);
	Graphics()->SetVertex3( m_length,-m_length, m_length);
	
	Graphics()->SetVertex3( m_length,-m_length,-m_length);
	Graphics()->SetVertex3(-m_length,-m_length,-m_length);
	Graphics()->SetVertex3(-m_length, m_length,-m_length);
	Graphics()->SetVertex3( m_length, m_length,-m_length);
	
	Graphics()->SetVertex3(-m_length, m_length, m_length);
	Graphics()->SetVertex3(-m_length, m_length,-m_length);
	Graphics()->SetVertex3(-m_length,-m_length,-m_length);
	Graphics()->SetVertex3(-m_length,-m_length, m_length);
	
	Graphics()->SetVertex3( m_length, m_length,-m_length);
	Graphics()->SetVertex3( m_length, m_length, m_length);
	Graphics()->SetVertex3( m_length,-m_length, m_length);
	Graphics()->SetVertex3( m_length,-m_length,-m_length);
	
	Graphics()->EndScene();
void COGLGraphics::BeginScene(eSceneMode mode)
{
	
	if(mode == G_POINTS)
		glBegin(GL_POINTS);
	
	else if(mode == G_LINES)
		glBegin(GL_LINES);
	
	else if(mode == G_LINE_STRIP)
		glBegin(GL_LINE_STRIP);
	
	else if(mode == G_LINE_LOOP)
		glBegin(GL_LINE_LOOP);
	
	else if(mode == G_TRIANGLES)
		glBegin(GL_TRIANGLES);
	
	else if(mode == G_TRIANGLE_STRIP)
		glBegin(GL_TRIANGLE_STRIP);
	
	else if(mode == G_TRIANGLE_FAN)
		glBegin(GL_TRIANGLE_FAN);
	
	else if(mode == G_QUADS)
		glBegin(GL_QUADS);
	
	else if(mode == G_QUAD_STRIP)
		glBegin(GL_QUAD_STRIP);
	
	else if(mode == G_POLYGON)
		glBegin(GL_POLYGON);

	CheckErrors();
}

void COGLGraphics::EndScene()
{
glEnd();

CheckErrors();
}

void COGLGraphics::Clear()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
}

void COGLGraphics::SetVertex3(CVector3 const &v)
{
glVertex3f(v.x, v.y, v.z);
}

void COGLGraphics::SetVertex3(float x, float y, float z)
{

glVertex3f(x, y, z);
CheckErrors();
}

Get rid of CheckErrors() from this function

void COGLGraphics::SetVertex3(float x, float y, float z)
{
glVertex3f(x, y, z);
CheckErrors();
}

EDIT:
and also from your BeginScene function.

glGetError is invalid between glBegin and glEnd.

[This message has been edited by shinpaughp (edited 03-15-2003).]

Heh…now I’m right back where I was a little while ago…no errors being reported but a blank white window that isn’t doing anything.

What is the range of your vertex coordinates? x, y, and z min and max?

m_length is 2.0

The white you are seeing is caused by the default glColor which is 1,1,1,1 so basically you are in the middle of your cube. Try

glTranslatef(0.0, 0.0, -50.0) just before your beginscene call in render function

It’s still all white. It’s also using 100% CPU for some reason but I’m assuming that’s a problem with my main loop and not implementing an idle function or timer yet…

Do you have ICQ or AIM? It might help speed this up…

Thanks.

[This message has been edited by Rodzilla (edited 03-15-2003).]