Solved

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

Posted on 2004-09-06
3
304 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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
matchUp  challenge 9 71
countHi challenge 25 84
pairs challenge 5 44
C# DataGridView_RowsAdded event not firing 1 37
Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
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…

708 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now