PDA

View Full Version : glGetError() returning "invalid operation" before I do anything...



Rodzilla
03-14-2003, 08:05 PM
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.

shinpaughp
03-14-2003, 08:22 PM
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.

Rodzilla
03-14-2003, 08:58 PM
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;
}

shinpaughp
03-14-2003, 09:17 PM
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?

shinpaughp
03-14-2003, 09:30 PM
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();

Rodzilla
03-15-2003, 10:38 AM
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).

shinpaughp
03-15-2003, 10:55 AM
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?

Rodzilla
03-15-2003, 12:17 PM
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;
}

shinpaughp
03-15-2003, 12:46 PM
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.

Rodzilla
03-15-2003, 12:52 PM
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).]

shinpaughp
03-15-2003, 01:03 PM
What range are your coordinates? Are they 2D or 3D? Do you have a projection set up (ie gluPerspective, glOrtho, glOrtho2d, GLfrustum)?

shinpaughp
03-15-2003, 01:05 PM
It would be easier if you just post your render function.

Rodzilla
03-15-2003, 01:06 PM
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().

Rodzilla
03-15-2003, 01:07 PM
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();
}

shinpaughp
03-15-2003, 01:14 PM
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).]

Rodzilla
03-15-2003, 01:16 PM
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.

shinpaughp
03-15-2003, 01:18 PM
What is the range of your vertex coordinates? x, y, and z min and max?

Rodzilla
03-15-2003, 01:20 PM
m_length is 2.0

shinpaughp
03-15-2003, 01:21 PM
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

Rodzilla
03-15-2003, 01:24 PM
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).]

shinpaughp
03-15-2003, 01:28 PM
Try one last thing, glClearColor(0.0, 0.0, 0.0, 0.0) just before the clear command in your render function.

Also, from where is your render function called? Within the mainloop, outside of normal Win32 loop?

If you want, email your code so I can look at it and I'll find out what the problem might be.

Rodzilla
03-15-2003, 01:33 PM
It's called from within the main loop, and the main loop is called from within WinMain.

shinpaughp
03-15-2003, 01:40 PM
Where is your call to glSwapBuffers???

And, just saw your earlier edit. No ICQ or AIM. Probably should one of these days...

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

Rodzilla
03-15-2003, 01:42 PM
I wasn't aware that I needed to call glSwapBuffers()...

shinpaughp
03-15-2003, 01:44 PM
That should fix it... Call glSwapBuffers(g_hdc); at end of your render function.

Let me know if that works.

Because you are using double buffers, you need to swap the buffers.

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

shinpaughp
03-15-2003, 01:47 PM
My bad.
no gl at beginning just

SwapBuffers(g_hdc);

Did it again. Read again, should be plural.

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

Rodzilla
03-15-2003, 01:47 PM
error C2065: 'glSwapBuffers' : undeclared identifier

Rodzilla
03-15-2003, 01:52 PM
Ah, there we go. That worked. Thanks! http://www.opengl.org/discussion_boards/ubb/smile.gif

Now to figure out how to get it not to use 100% CPU http://www.opengl.org/discussion_boards/ubb/smile.gif

shinpaughp
03-15-2003, 02:01 PM
All of my OpenGL applications run at 100% CPU. I haven't figured out anything for that yet.

Glad I could help. http://www.opengl.org/discussion_boards/ubb/smile.gif

Rodzilla
03-23-2003, 05:55 PM
I found a fix, but it only works if you're developing for Windows. As I plan to do my ports later, I'm not sure how this would apply to other OSes...

If you add a check in your main loop for whether the program is active or not, you can have it tell windows to idle until it recieves a message using WaitMessage();