Solved

Rendering Problem with OpenGL OR Thread OR C++ OR WIN32

Posted on 2004-09-06
3
306 Views
Last Modified: 2013-12-04
Part of a program i'm developing has an OpenGL interface. I originally wroted the program
as non-threaded, but have decieded to add a thread to repeatedly render the GUI, allowing
rendering while I block waiting on a comm port. However when I add a loop in a thread which
calls the rendering function, it won't render, but If I call the rendering function from the main
message loop it will. (I've more code as required)
[IN THE CODE BELOW wgui is a global (only 1) instance of the class WirmedGUI)]
[IN THE CODE BELOW the problem is not caused by equalizeBuffers();]

------------- MESSAGE LOOP CALL ------------------
      //Enter the message loop
      while(!done)      
      {
            //Poll for waiting messages
            while(PeekMessage( &lpMsg, NULL , 0, 0, PM_REMOVE) && !done)
            {
                  if (lpMsg.message==WM_QUIT) done=true;      //Check for the end condition                  
                  TranslateMessage( &lpMsg );
                  DispatchMessage( &lpMsg );
                  
            }
            
            //trigger an update in the GUI
            wgui.updateImage(false);              //<---------------------------------------------------THIS CALL WORKS
      }

------------- THREAD LOOP CALL ------------------
//Rendering Thread Primary Function
DWORD WINAPI WirmedGUI::ThreadFunc( LPVOID lpParam )
{
      //While were not done
      while(!wgui.done)
      {
            printf("THREAD RENDER\n");
            
            //Update the image
            wgui.updateImage(false);          //<-----------------------------------------------------THIS CALL DOESN'T WORK
      }      
      return 0;
}

------------- THREAD LOOP CALL ------------------
//Update internal scene representation, call DrawGLScene (to execute the GL commands), and revaildate the display.
void WirmedGUI::updateImage(bool justgui=false)
{      
      DrawGLScene(justgui);
      SwapBuffers(hDC);
      ValidateRgn(hWnd, NULL);
      printf("RENDER [hWnd:%i, hDC:%i] > ",hWnd,hDC);
      equalizeBuffers(); //Make sure the frame buffers are the same (prevents flickering of partially updated images      
}
0
Comment
Question by:Falcon_Zero
  • 2
3 Comments
 
LVL 48

Accepted Solution

by:
AlexFM earned 500 total points
ID: 11988689
wgui.updateImage(false);          //<-----------------------------------------------------THIS CALL DOESN'T WORK

What does this mean "doesn't work"? Function is not called, ot screen is not redrawn? Do you see printf output?

However, I think it doesn't matter. Main thread should draw the screen, and COM port should be handled in the worker thread. This is standard program design, I suggest you to change your program accordingly. Drawing on the screen from worker thread creates a lot of problems.
0
 
LVL 1

Author Comment

by:Falcon_Zero
ID: 11988736
The result varies from computer to computer. On one PC i get very fast flickering 1inch bands of the OpenGL scene against the previous background (Desktop in this case) on another, I just get a black screen, which leads me to suspect a problem with OpenGL or something causing a problem which is manifest by OpenGL.
As for placing the comm functions in a worker,  I was really trying to avoid having to change the architecture of the program, which is already 10k lines long. The multithreaded side of things is in responce to a change in requirements. If I can make the above code work, thats the extent of the multithreading required. (Only ever 2 threads for this program (main & gfx).)
I agree that its better to have the gfx in the main thread, but I since I'm trying to change this with the minimum of rewriting i'll take a beating for bad style, but i'll also get
the praise for making the target date.
0
 
LVL 1

Author Comment

by:Falcon_Zero
ID: 12032050
Well, I guess I'll have to bite the bullet on this one. I'm no closer to a solution, so I'll just have to rewrite the com side of things as multi-threaded.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
A short article about problems I had with the new location API and permissions in Marshmallow
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

827 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question