Draw Order in OpenGL - c++

I am rendering an OpenGL scene that include some bitmap text. It is my understanding the order I draw things in will determine which items are on top.
However, my bitmap text, even though I draw it last, is not on top!
For instance, I am drawing:
1) Background
2) Buttons
3) Text
All at the same z depth. Buttons are above the background, but text is invisible. It I change the z depth of the text, I can see it, but I then have other problems.
I am using the bitmap text method from Nehe's Tutorials.
How can I make the text visible without changing the z depth?

You can simply disable the z-test via
glDisable (GL_DEPTH_TEST); // or something related..
If you do so the Z of your text-primitives will be ignored. Primitives are drawn in the same order as your call the gl-functions.
Another way would be to set some constant z-offset via glPolygonOffset (not recommended) or set the depth-compare mode to something like GL_LESS_EQUAL (the EQUAL is the important one). That makes sure that primitives drawn with the same depth are rendered ontop of each other.
Hope that helps.

You can also use glDepthFunc (GL_ALWAYS).

Related

Opengl, blend only when destination pixels' alpha value is positive

I'm searching for a function/way to make blending work only when destination pixels' (i.e. the back buffer) alpha value is greater than 0.
What i'm looking for is something like the glAlphaFunc which tests the incoming fragments, but in my case i want to test the fragments already found in the back buffer.
Any ideas?
Thank you in advance
ps. I cannot do a pixel-by-pixel test in the drawing function because this is set as a callback function to the user.
Wait, your answer is somewhat confusing, but i think what you're looking for is something like this : opengl - blending with previous contents of framebuffer
Sorry for this, but i think it's better answering instead of commenting.
So, let me explain better giving an example.
Let's say we have to draw something (whatever the user wants, like a table) and after that (before swapping the buffers of course) we must draw over it the "saved" textures using blending.
Let's say we have to draw two transparent boxes. If those boxes are to be saved in a different texture, this can be done by:
Clear the screen with (0, 0, 0, 0)
set blend function (GL_ONE, GL_ZERO)
draw the box
save it to texture.
Now, whenever the user wants to redraw them all, he simply draws the main theme (the table) and over it draws the textures using blend function (GL_SOURCE_ALPHA, GL_ONE_MINUS_SOURCE_ALPHA).
This works fine. But if the user wants to save both boxes in one texture and the boxes overlap, how can we save the blending of those two boxes without blend them with the "cleared" background?
Summarizing, the final image of the whole painting should be a table with two boxes (let's say a yellow and a green box) over it, blended with function (GL_SOURCE_ALPHA, GL_ONE_MINUS_SOURCE_ALPHA).

Qt OpenGL - Translucent objects show content from behind window

I'm using Qt5 and its OpenGL integration, and am running into a problem when I try to draw translucent objects. When an object is translucent, whatever is visible behind my OpenGL window is shown within the screen area of that object, instead of the object being blended with whatever is already in the colour buffer. I have started watching YouTube videos through my translucent objects, as whatever shows through is live.
Interestingly, the most see-through an object gets seems to occur at half opacity - full opacity renders it solid, while zero opacity renders nothing at all (and whatever was previously in the background of the 3D scene remains there). Rendering translucent objects last does not fix the issue.
I have noticed that this also happens when I enable mipmaps on my textures - as the distance to a point on an object increases, the pixel concerned becomes more translucent and displays whatever is behind the OpenGL window. The issue occurs both on my Windows and OSX machines.
Is this a known issue? Is there a workaround? Google hasn't proven too helpful.
Hah, that's a funny one. I can't tell you what is going on, because it normally takes some extra effort to make windows actually transparent; in Windows you have to select a framebuffer format with an alpha channel and call DwnEnableBlurBehindWindow to actually achieve this effect. And as far as I know Qt doesn't do this.
But if it does here are a few hints:
Make sure you clear your framebuffer to alpha=1
When rendering translucent objects keep the destination alpha value 1, i.e. don't use blending modes and functions that modify the destination alpha value, or force it to 1.
There's actually little use for the alpha channel on the main window framebuffer, except for implementing window translucency effects. Unless you need those you should choose an either pixel format without an alpha channel for your window framebuffer, or keep all its pixels alpha values at full opacity.

Methods of zooming in/out with openGL (C++)

I am wondering what kind of methods are commonly used when we do zoom in/out.
In my current project, I have to display millions of 2D rectangles on the screen, and I am using a fixed viewport and changing glortho2D variables when I have to zoom in/out.
I am wondering if this is a good way of doing it and what other solution can I use.
I also have another question which I think it is related to
how should I do zoom in/out.
As I said, I am currenly using a fixed viewport and changing glortho2D variables in my code, and I assumed that opengl will be able to figure out which rectangles are out of the screen and not render them. However, it seems like opengl is redrawing all the rectangles again and again. The rendering time of viewing millions of rectangles (zoom out) is equal to vewing hundreds of rectangles (zoom into a particular area), which is opposite of what I expected. I am wondering if it is related to the zooming methods I used or am I missing something important.
ie . I am using VBO while rendering the rectangles.
and I assumed that opengl will be able to figure out which rectangles are out of the screen
You assumed wrong
and not render them.
OpenGL is a rather dumb drawing API. There's no such thing like a scene in OpenGL. All it does is coloring pixels on the framebuffer one point, line or triangle at a time. When geometry lies outside the viewport it still has to be processed up to the point it gets clipped (and then discarded).

OpenGl selection

I'm creating a box and placing "magnets" on the bottom. The sides are slightly see through(alpha is somewhere between .2 and .5) and the bottom is solid. I'm trying to use gluUnProject() to select where the "magnet" is placed, but when the sides of the box are rendered, I can't get my magnets into the box.
Is there anyway to still have the sides of the wall to be rendered but ignore them for the sake of mouse clicks?
I've tried GL_CULL_FACE but at first glance that doesn't seem to be what I'm looking for.
So if I understand correctly, you have semi-transparent boxes and when the magnet is inside the boxes you want to see the magnet in according to the semi-transparency of the boxes.
My guess is that when you're drawing the boxes you have the depth writes turned on, this way if boxes happen to get drawn before the magnet, then when you draw the magnet it will fail the depth test and the part that's inside won't get drawn as a result.
The easiest way to do this is:
Draw all the solid objects first
Disable depth writes:
glDepthMask(GL_FALSE);
Use an order-independent blending function when drawing the semi-transparent objects, for example:
glBlendFunc(GL_ONE, GL_ONE)
Draw all your transparent objects
Enable depth writes again
glDepthMask(GL_TRUE);
Bear in mind this simple method will only work if you can get away with using an commutative blending equation, if not then consider using order-independent transparency, a good article is "Efficient Layered Fragment Buffer Techniques" By Pyarelal Knowles, Geoff Leach, and Fabio Zambetta

Drawing statically in Open GL

I am developing a paint-like application using C++ and Open GL. But every time i draw objects like circle, lines etc they don't ** stay ** on the page. By this I mean that every new object I draw is getting placed on a blank page. How do I get my drawn objects to persist?
OpenGL has no geometry persistency. Basically it's pencils, brushes and paint, with which you draw on a canvas called the "framebuffer". So after you drawn something and clear the framebuffer, it will not reappear in some magic way.
There are two solutions:
you keep a list of all drawing operations and at each redraw you repaint everything from that list.
After drawing something copy the image in the framebuffer to a texture and instead of glClear you fill the background with that texture.
Both techniques can be combined.
Just don't clear the framebuffer and anything you draw will stay on the screen. This is the same method I use to allow users to draw on my OpenGL models. This is only good for marking up an image, since by using this method you can't erase what you've drawn, unless your method of erasing is to draw using your background color.