2D images flicker on screen when comb. w OpenGL

Hello everyone! In developing my current project I want to have the ability to draw 2D bitmap images directly to the screen, while also drawing a 3D image / scene at the same time.

I have a basic Bitmap class that helps me load the image resource and draw it, and it displays 2D images on the screen perfectly as long as I’m not doing any drawing with OpenGL at the same time. If I do both, however, the 2D image winds up flickering. Obviously not a desirable effect.

I figure this is probably because OpenGL is drawing to and swapping buffers, but I can’t figure out how to overcome this. For clarity, I’ve included my drawing method which is responsible for drawing to my screen.

Is there some extra work I need to be doing to overcome this, or should I be taking a different approach altogether? Thanks a lot for the help in advance.

<div class=“ubbcode-block”><div class=“ubbcode-header”>Click to reveal… <input type=“button” class=“form-button” value=“Show me!” onclick=“toggle_spoiler(this, ‘Yikes, my eyes!’, ‘Show me!’)” />]<div style=“display: none;”>
void Draw(HDC hDC)
//OpenGL drawing

//Make and draw a reference object
glColor3f(0.8f, 0.0f, 0.0f);
glTranslatef(0.0f, 0.75f, 0.0f);

	glVertex3f(_squareX, _squareY, _squareZ);                 //Bottom-left vertex
	glVertex3f(_squareX + _squareSize, _squareY, _squareZ);   //Bottom-right vertex
	glVertex3f(_squareX + _squareSize, _squareY + _squareSize, _squareZ);   //Top-Right Vertex
	glVertex3f(_squareX, _squareY + _squareSize, _squareZ);   //Top-left vertex.



//2D bitmap drawing
_myBitmap-&gt;Draw(hDC, 0, 0);


Are you trying to mix OpenGL with some other graphics library? Use OpenGL for both your 2D and 3D rendering and only swap when you are done drawing the entire scene.

I’m mixing OpenGl with some of my own code - namely a Bitmap class that will load a bitmap resource for use, and then draw it to the screen with a call to the Bitmap’s draw function. My goal was to make something that was very bare-bones and simple that would allow me to draw 2D images directly to the screen.

My reasoning here was that I could do all of the 3D rendering with OpenGL, then at the very end of my main draw method, do all of my 2D drawing to the screen. That’s obviously not working, however.

I’ve never looked into doing 2D image drawing with OpenGL, though. Would handling all of it with openGL be preferable to trying to figure out how to marry OpenGL and my own drawing methods in your opinion?

Yes, but what is the Bitmap class using to render? OpenGL? DirectX? GDI+?

The reasoning is sound, its the implementation that is in question.

Not only would it be preferable, I am not even sure that you can even do what it is you are trying to do using two separate graphics libraries.

It would be the Windows GDI that I’m using to draw my Bitmaps right to the screen.

So, if I have this correctly, the problem stems from the fact that using different methods of drawing together sort of get in the way of each other? I thought that I could use each of them independently seamlessly, but it looks like I was wrong. I just want to make sure that I know exactly what the problem is, not just the right way to go about it.

But to get everything going, I’ll look into doing my 2D drawing with openGL as well. Thanks! :slight_smile:

The main problem is that when you swap buffers, the scene gets updated to show your entire OpenGL rendering. The user sees this, if only for a fraction of a second, and then you render your bitmaps independently, which gives the impression of the bitmaps flashing. To achieve your goal (assuming its possible) you would have to somehow render from the bitmap to the OpenGL buffer before the swap, and never render the bitmap directly to the screen (it would always have to be passed to the OpenGL buffer). I do not know if such a thing is possible, or would be fast enough to be used if it were. In either case, just using OpenGL to render your bitmaps takes little effort, and will work great.

It should be fairly painless. A single quad with a texture on it should do.

It is recommended to switch totally to OpenGL, but before doing that try just to set one glFinish() before swapping buffers. It may help. :wink: