Skip to main content
GameDev.net gamedev.net
🔒 Locked

OpenGL Flickering in Thread

Started by nareshn2008 Aug 27, 2008 at 2:27 AM 15 replies 6k views
Original Post
nareshn2008
nareshn2008
Hi I have created OpenGL device and trying render it in thread. But rendering seems to be flickering and even if i do only clear screen then it also flickers. If i use Timer Rather then Thread then it's working perfect but i donot want to use windows timer. In Thread i am checking interval of time and rendering in time of interval like render each frame in 50 ms using QueryPerformanceCounter functions. Please suggest me if anyone knows what is problem ?
V-man
V-man
The only time I've seen flickering is in this case
http://www.opengl.org/wiki/index.php/Platform_specifics:_Windows#It.27s_flickering.21_Why.3F
mmakrzem
mmakrzem
50ms = 20 fps. That's not very fast. When you say flicker, do mean the objects on the screen appear choppy when moving? or do you mean you see them, and then you don't for a brief second?
nareshn2008
nareshn2008
Hi

Hi

According to V-man spec. All is setup good. I am using dialog to render application and i have modified windows style in MFC and also erase background.

According mmakrzem yes 20 fps is too low but i just trying resolve my problem then i can increase fps. In Rendering i can't see anything it Just flickers and shows last opened screen what we render or just like if we open any render applications it shows last opened programm buffer to screen.
stein
stein
Did you create your context in the thread from which your trying to render?
| Stein Nygård - http://steinware.dk |
nareshn2008
nareshn2008
Hi stein

I did not create context in thread. i just created as normally methods. I just want to render device in thread. Because i am getting video stream buffer from USB camera and i want to show it texture and update continiously. So USB camera SDK gives callback that calls everytime when buffer arrives from camrea using thread. So i have to display this buffer to texture and render openGL context.

This is the simple idea.
stein
stein
Okay...
You have to make your rendercontext "active" in that thread before rendering anything.
wglMakeCurrent should be what you re looking for :)

Dont forget to set the context in your main thread (and any other threads you may have) if your rendering from there too.
| Stein Nygård - http://steinware.dk |
Rasmadrak
Rasmadrak
Also, make sure you're using double buffering. And v-sync.
"Game Maker For Life, probably never professional thou." =)
nareshn2008
nareshn2008
Hi

Sorry for late reply.

Yes i am using Double buffer and vsync. If i am creating device in thread then rendering goes fine.

Is there any solution not to create device in thread ? I mean i just want to render in thread . because thread creation is just like callback from camera so thread creation is not in my control.
stein
stein
Well, did you actually _read_ my reply?
| Stein Nygård - http://steinware.dk |
nareshn2008
nareshn2008
Hi Stein

Yes i had read your reply :) Thanx for reply....


But i am doing as you said. i am creating device in thread and makeCurrent in thread. That's working. But my problem is i am creating new thread and trying to redner device in another thread.I know i have to make current device to another thread.

But i am little confused on wglMakeCurrent .. Can you explain small example for wglMakeCurrent ?


That will great helps me...
stein
stein
Okay.

You create your context in thread A. You already call wglMakeCurrent in thread A, and you can draw to your device from this thread.

Now, before rendering anything from thread B (the one you call "new thread"), you simply call wglMakeCurrent with the same context ptrs.

If you subsequently try to render anything from thread A, you must again call wglMakeCurrent.

You follow? :)
| Stein Nygård - http://steinware.dk |
nareshn2008
nareshn2008
Hi

Yeah i got the idea. Thanx buddy :)
nareshn2008
nareshn2008
Hi Stein

I tried as you said but nothing seems to done as i need . I am trying to explain with little code example that you can explain me or correct me.

So i am creating device in main and render it to thread. that's what i want to do....


HDC   hDC = NULL;HGLRC hRC = NULL;int main(){    DWORD dwThread1ID;   // Create Windows for Rendering   ghWnd = initializeWindows();    hDC = GetDC(ghWnd);     // Setup Basic Pixel Format for OpenGL    bSetupPixelFormat(hDC);       hRC = wglCreateContext( hDC );       wglMakeCurrent( hDC, hRC );     // Create Thread here    HANDLE hThreads = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)ThreadFunc1,                            NULL, 0, &dwThread1ID);    // Wait for Rendering    WaitForSingleObject(hThreads,INFINITE);      if (hRC)  	wglDeleteContext(hRC);   if (hDC)     ReleaseDC(ghWnd, hDC);    return 0;}GLvoid ThreadFunc1(void){	while (TRUE)	{           wglMakeCurrent( hDC, hRC );	   // Clear the main window.  Only _one_ thread should be doing this.	   glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );	   glPushMatrix();	   glTranslatef(0.0, 0.0, -radius);	   // Define our point of reference	   gluLookAt(0.5,1.0,2.0,0.0,0.2,0.0,0.0,1.0,0.0);	   DrawWave(0.6, 0.7, -2.5);	   glPopMatrix();	   glFinish();	   wglMakeCurrent(NULL, NULL);		}	}


BTW. I also know if i creat device in thread then it will work.
Kambiz
Kambiz
You said you are using double buffering so where is SwapBuffers(hDC)?
wglMakeCurrent( hDC, hRC ) needs to be called only once before gl function calls from another thread, so you can remove it from the loop and delete wglMakeCurrent(NULL, NULL).
nareshn2008
nareshn2008
Hi

Yes i am using Double buffer. i forgot in post code only.

I tell you problem has been solved. all is now working fine because there is problems in my engine .dll . Still i did not found any problem but Building new .dll from begining is just fine.

Now i can do what i want from engine dll. I donot it is complier setting or something like that wired bug.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.