0

following my another question, I have provided the code that draws the rectangle as follows:

void COpenGLControl::DrawRectangleOnTopOfTexture()
{
wglMakeCurrent(hdc, hrc);
glPushAttrib(GL_ENABLE_BIT|GL_CURRENT_BIT);
glDisable(target);
glColor3f(1.0f,0.0f,0.0f);
glBegin(GL_LINE_LOOP);
   glVertex2f(RectangleToDraw.at(0),RectangleToDraw.at(1));
   glVertex2f(RectangleToDraw.at(0),RectangleToDraw.at(3));
   glVertex2f(RectangleToDraw.at(2),RectangleToDraw.at(3));
   glVertex2f(RectangleToDraw.at(2),RectangleToDraw.at(1));
glEnd();
glPopAttrib();
SwapBuffers(hdc);
wglMakeCurrent(NULL, NULL);
}  

void COpenGLControl::OnDraw(CDC *pDC)
{
// TODO: Camera controls
wglMakeCurrent(hdc,hrc);
glLoadIdentity();
gluLookAt(0,0,1,0,0,0,0,1,0);
glTranslatef(m_fPosX, m_fPosY, 0.0f);
glScalef(m_fZoom,m_fZoom,1.0);
setViewRectangle();
if (WantToDrawRectangle)
    DrawRectangleOnTopOfTexture();
wglMakeCurrent(NULL, NULL);
}  

the problem here is:
the red rectangle disappears right after the OnDraw returns. I mean for example when you do a fixed zoom in or fixed zoom out by pressing a button on the dialog, a red rectangle flashes and then immediately disappears.
Or when you make the OnDraw run constanly ( for example you pan with high speed ), you get something like this: enter image description here
surely red rectangles are deleted(disappeared) after a short time but this track of red rectangles is shown for a short period of time?

How can I make the current red rectangle be rendered on the texture until the next call of OnDraw?
Also how can I make the current red rectangle be deleted as soon as the next call of OnDraw occurs?

If the second question is hard to answer, no problem please at least answer the first. Obviously if the user does panning with a normal speed this track of rectangles won't be shown.

Community
  • 1
  • 1
Sepideh Abadpour
  • 2,550
  • 10
  • 52
  • 88
  • As Ramya Maithreyi pointed out your buffer swap logic is unorthodox. You may need to clear the color buffer if the situation ever arises that your map texture doesn't cover the entire screen. And judging by your other question, part of the problem is that you are only drawing the underlying textured quad using a timer event, while the red squares are drawn everytime your window has to be repainted. – Andon M. Coleman Aug 29 '13 at 07:44
  • AAndon M.Coleman is there a way in wgl API not to use double buffering. because when I use glFlush instead of the swapBuffers(hdc,hrc) I lose every thing. – Sepideh Abadpour Aug 31 '13 at 01:41
  • I mean I even lose my ogl window. – Sepideh Abadpour Aug 31 '13 at 01:47
  • You can create your context with single buffering, but it won't draw correctly in Windows Vista or newer. – Andon M. Coleman Aug 31 '13 at 01:55
  • so what can I do? @Andon M.Coleman – Sepideh Abadpour Aug 31 '13 at 02:26
  • @AndonM.Coleman can you have a look at [my another question](http://stackoverflow.com/questions/18535584/how-to-choose-the-right-buffer-to-draw-and-stop-it-from-swapping-continuously) or have a chat with me or at least introduce me introduce me an excellent tutorial on how to use buffers in multiple drawings that can not be done in a single function or a single thread – Sepideh Abadpour Aug 31 '13 at 02:34

1 Answers1

1

I guess your issue is related to double buffering.

Some devices do not delete the content of the back buffer when it is being swapped. My suggestion would be to clear the screen explicitly (glClear) before you start rendering into the new buffer. This should handle the issue related to multiple red rectangles.

Secondly I am not sure if there are more than necessary buffer swaps happening in your code. Please check if there other explicit or implicit calls to Swap Buffer which might result in the wrong buffer being displayed. (The final buffer displayed might not be the buffer in which you drew the rectangle).

Ramya Maithreyi
  • 507
  • 3
  • 12
  • +1: Wow, I completely missed that. The buffer swap is in `DrawRectangleOnTopOfTexture (...)` instead of `OnDraw (...)`. You're absolutely right - it shouldn't matter if this is the entire draw sequence, but if `DrawRectangleOnTopOfTexture (...)` is ever called in multiple locations it is going to really mess things up. – Andon M. Coleman Aug 29 '13 at 07:36
  • @Ramya Maithrey AAndon M.Coleman is there a way in wgl API not to use double buffering. because when I use glFlush instead of the swapBuffers(hdc,hrc) I lose every thing. I mean I even lose my ogl window – Sepideh Abadpour Aug 31 '13 at 01:42