Why do my semi-opaque vertices make background objects brighter in OpenGL? - opengl

I am rendering 4 vertices (a square) in front of a colored cube. The vertices are colored white, but are blended at 0.5f.
Related: Why does my colored cube not work with GL_BLEND?
Please could someone tell me why the colored cube appears brighter when obscured by the semi-opaque square?
Cube rendered without square in front:
Normal cube http://img408.imageshack.us/img408/2853/normalcube.png
And, rendered with the square:
Cube with square http://img142.imageshack.us/img142/6255/brightsquare.png
Please see the code used to create the colored cube, the code used to actually draw the cube, and the code where the cube and square are rendered.
This is the code in my init function:
glEnable(GL_CULL_FACE);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE);

I'd say it's because your semi-transparent square gets added to the existing pixels, thus incrementing their intensity.
The documentation for glBlendFunc() recommends setting the second parameter to GL_ONE_MINUS_SRC_ALPHA, that is the boilerplate for implementing transparency. Try it.

Related

Is there any way to make front face of gluCylinder() transparent?

I am using gluCylinder() to create a cylinder in openGL and then plotting points inside the cylinder with Depth Test On .
When i see the front view of the cylinder, the points inside the cylinder are obstructed by front face.
To make front face of the cylinder translucent i am using Blending.
I am using below functions.
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
But whatever coloring or alpha value i assign to the cylinder the front face is not looking transparent due to its back face.
Tell whether it is possible to do with blending only or else i need to introduce lighting for both the faces of Cylinder.Here it clearly visible the change in the color of front face and back face of cylinder. And the points inside the cylinder are not visible due to being obstructed by front face of cylinder.
You should be able to accomplish this by drawing the cylinder twice, while culling the front faces the first time, and culling the back faces the second time. This way, you can draw the front and back parts differently, e.g. by making the front part transparent.
The code sequence could look like this:
// Draw back part of cylinder, opaque.
glEnable(GL_CULL_FACE);
glCullFace(GL_FRONT);
gluCylinder(...);
// Draw points.
// Draw front part of cylinder, transparent.
glCullFace(GL_BACK);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
gluCylinder(...);
glDisable(GL_BLEND);
If I understand you right then no, you can't do it with blending alone.
If the cylinder's normals all point outward then you also won't be able to see the cylinder's internal parts no matter what you do.
I do something similar to show characters behind walls and it goes like this - render your scene normally and save it all to a framebuffer. Then render what you want shown behind with the buffer contents on top, using a custom shader to make a bubble of transparency around the thing you want shown behind.
Not sure if I am explaining it well or not but it unfortunately requires multiple steps to get the results you want.
Your problem is still a bit unclear to me despite the image but I will attempt to answer based on my perception of your issue.
You are drawing a cylinder and have geometry (lines or other models) inside the cylinder. You want the cylinder to look translucent so the inner objects are visible. Here is one way to do it. Assuming your render functions are drawCylinder() and drawPoints().
init()
{
...
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
...
}
drawScene()
{
glDepthMask(GL_FALSE);
glEnable(GL_BLEND);
drawCylinder();
glDisable(GL_BLEND);
glDepthMask(GL_TRUE);
drawPoints();
}
doing so will make sure that the points are drawn regardless of the cylinder. Try using lower values of alpha for your cylinder color.
Please note this is one way to do it. I suggest using shaders to have more control over blending as well as exploring fragment/pixel discard options.

OpenGL alpha blending sometimes creating black quads

I'm trying to make simple shading for a 2D OpenGL scene by placing semi-transparent black quads over the scene, but in certain places instead of darkening the scene it completely blacks it out. I'm using glBlendFunc(GL_DST_COLOR, GL_SRC_ALPHA) for the shader quads, and glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) for all the other drawing underneath.
This normally works, dimming the area under the semi-transparent quad, but in certain situations it behaves oddly. When the area under the semi-transparent quad has only had one quad drawn behind it, it behaves normally. When another quad (or multiple quads) are drawn on top of the back quad, it sometimes behaves normally, sometimes blacks out the quad entirely, or sometimes blacks out everything beneath the last-drawn quad (e.g. if I had a red texture with a blue dot on top of it, the red beneath would be blacked out, but the blue dot would remain unchanged).
I thought this looked like the quads drawing in the wrong order, but I have GL_DEPTH_TEST disabled, so I'm not sure if this is odd behavior from the blending, misorder, or something else entirely. Any ideas as to what could be causing this?

Draw a cube with a stroke and fill in OpenGL

I need to draw a load of cubes and would like them to be white with black stroke. At the moment I am storing all of these cubes in a VBO and I can draw them in wireframe and filled with no outline.
I would like to draw them like the image on the left in this image, stroked only on the sides facing the camera, not like the right.
I am using OpenGL.
What you want is to remove hidden lines.
If you want to draw a wireframe object with hidden lines removed, one approach is to draw the outlines using lines and then fill the interiors of the polygons making up the surface with polygons having the background color.
You need to glEnable(GL_CULL_FACE); in order to get back-face culling of triangles not visible automatically applied. Provided the "winding order" of your triangles is consistent of course (clockwise or anti-clockwise). If they're wound in the opposite direction, you can tell OpenGL which direction to use with glFrontFrace(GL_CW | GL_CCW) and whether to cull front or back facing triangles with glCullFace(GL_BACK | GL_FRONT).

Rendering transparent 3d shape(quad) with equal z-order

I render two quads with OpenGL with equal z. When I have DEPTH enabled I get following image, but when it is off I get what I need one fruit over another. Is it possible to draw quads with equal z, as I want? My OGL settings:
glEnable(GL_BLEND); glEnable(GL_ALPHA_TEST); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
EDIT sorry I forgot to mention that left topmost quad I call to render first, and I use orthographic
EDIT +1 when I render quads with different Z I get the same image, how to fix it?
The z-buffer doesn't care about transparency and so you have to sort the drawing order of your quads by their approximate depth.
I would change their depth to be different (you can use an orthographic projection to have them appear the same size) and then draw the further quad first.
Also, it is never a good idea to draw two polygons at the same depth even without transparency because of z-fighting.
I experimented and saw that I have same problem when I render cube, so the problem was because of vertices ordering, and possibly backface culling parameters.

OpenGL Alpha blending with wrong color

I am trying to create a simple ray tracer. I have a perspective view which shows the rays visibly for debugging purposes.
In my example screenshot below I have a single white sphere to be raytraced and a green sphere representing the eye.
Rays are drawn as lines with
glLineWidth(10.0f)
If a ray misses the sphere it is given color glColor4ub(100,100,100,100);
in my initialization code I have the following:
glEnable(GL_ALPHA_TEST);
glAlphaFunc(GL_GREATER, 0.0f);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA,GL_SRC_ALPHA);
You can see in the screen shot that for some reason, the rays passing between the perspective view point and the sphere are being color blended with the axis line behind the sphere, rather than with the sphere itself.
Here is a screenshot:
Can anyone explain what I am doing wrong here?
Thanks!!
Is it a possibility you cast those rays before you draw the sphere?
Then if Z-buffer is enabled, the sphere's fragments simply won't be rendered, as those parts of rays are closer. When you are drawing something semi-transparent (using blending), you should watch the order you draw things carefully.
In fact I think you cannot use Z-buffer in any sensible way together with ray-tracing process. You'll have to track Z-order manually. While we are at it OpenGL might not be the best API to visualize ray-tracing process. (It will do so possibly much slower than pure software ray-tracer)
You dont need the glAlphaFunc, disable it.
Light rays should be blended by adding to the buffer: glBlendFunc(GL_ONE, GL_ONE) (for premultiplied alpha, which you chose.
Turn off depth buffer writing (not testing) when rendering the rays: glDepthMask(GL_FALSE)
Render the rays last.
AlphaTest is only for discarding fragments - not for blending them. Check the spec
By using it, you are telling OpenGL that you want it to throw away the pixels instead of drawing them, so you won't can any transparent blending. The most common blending function is
glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); You can also check out the OpenGL Transparency FAQ.