I'm not getting any anti-aliasing when using drawing GL_TRIANGLE_FANs with this code:
glDisable(GL_DEPTH_TEST);
// Blended points, lines, and polygons.
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glHint(GL_POINT_SMOOTH_HINT, GL_NICEST);
glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
glHint(GL_POLYGON_SMOOTH_HINT, GL_NICEST);
glEnable(GL_POINT_SMOOTH);
glEnable(GL_LINE_SMOOTH);
glEnable(GL_POLYGON_SMOOTH);
What am I doing wrong?
Edge antialiasing is always a hint, the implementation (you don't specify which) is free to ignore it per the spec. For various reasons (e.g. the inability to handle intersecting polygons, bad interactions with framebuffer blending) this sort of antialiasing has fallen out of favor and been replaced by multisample-based algorithms that work at the framebuffer level. There is an ARB multisample extension to control this (I believe it's default in recent versions of the spec, actually). Or often the drivers have ways to enable it globally without source code modification.
Related
I have issue with texture with alpha channel. I'm rendering a palm tree with leaves:
but as you can see, sky is over leaves on the left side of the picture.
In my code, sky is rendered, then i render the trees.
Here is my code which renders one palm tree:
RenderFrame(0);//trunk
//glColor3f(0.0, 0.6, 0.0);
glEnable(GL_BLEND);
glDisable(GL_CULL_FACE);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
leaves.RenderFrame(0);
glEnable(GL_CULL_FACE);
glDisable(GL_BLEND);
Like others stated, it seems that the order of rendering is wrong. I've had this issue in the past and it isn't a simple solution, especially since you are using deprecated immediate mode. Take a look at these solutions in this question: OpenGL ES2 Alpha test problems
Is there a blending equation that can be made using the OpenGL glBlendFunc that would allow for a transparent color (RGBA) to be rendered behind an additive overlay.
Rendering Ontop:
This effect can be achieved using glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
Rendering Underneath:
Is there an equation for this blending effect?
There are no glBlendFunc options for directly drawing overlay. Info can be found here:
http://benmcdowell.com/implementing-photoshop-blend-modes-in-opengl/
However, your effect doesn't seem to be overlay, seems to be either screen
glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_COLOR);
or additive
glBlendFunc(GL_SRC_ALPHA, GL_ONE);
I'd suggest you try all combinations, there aren't that many possible. If you need crazier effects however, you'll need to code shaders.
P.S. I lied. You don't HAVE to code shaders to do crazy effects like overlay, but you have to draw so many times it becomes unusable in real time. I should have said it's better to use shaders.
glEnable (GL_BLEND);
glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
and used this in the fragment shader.
I've used Alpha blend to get the transparency working however it only seems to work from one side.
Not sure what the problem is, am new to programming and shading.
http://imgur.com/WDK4qjc Link to see the picture
I think you see 2 distant faces blending where color is darker.
Maybe Culling is not activated.
Face culling is the ability to discard face drawing upon certain condition.
To achieve what you want, You have to discard faces not facing the camera which is call backface culling. You do this:
glEnableGL(GL_CULL_FACE); //(enable face culling)
glCullFace(GL_BACK); //(discard back faces)
Everything is fine. It's called Transparency Sorting.
Some more info:
http://www.opengl.org/archives/resources/faq/technical/transparency.htm
Rendering transparent objects in OpenGL
opengl z-sorting transparency
Is there a simple algorithm, given a depth vector (ie the one going into the screen) to determine which polygon (or rather, just which triangle) is 'behind' the other one from their coordinates so I can sort them and get their transparency effects to behave appropriately? I was thinking about comparing overlaps of minimum and maximum values but this can fail, particularly when the range of one polygon in the depth direction goes beyond both the minimum and maximum depth direction of the other. Any ideas?
After investigation it would appear NO there is no such way. See opengl.org/wiki/Transparency_Sorting. Basically the problem is ill defined since we are talking about 2d overlaps in 3d space so can get pathological cases where A>B B>C C>A: here there is no possible order which works.
The problem is that the alpha blending function
glBlendEquation(GL_FUNC_ADD);
glBendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
is not commutative: it relies on subtraction. This is why order matters: A-B != B-A. This however is the best way to do transparency which lots of complicated color effects etc. The way round this is either order independent transparency (eg depth peeling which is apprently costly) or brute force eg. divide up your polygons into arbitrary smaller ones until the sort can be done. This could be made more sophisticated by only dividing up your polygons along the lines where they overlap in the x-y dimensions of the view port. But it's still all a bit of a pain.
If on the other hand your transparency is a simple example (ie. when it explicitly doesn't make a difference to the visuals what order the transparencies are in eg. for me all transparent objects were rectangles of the same grey colour ) none of this matters! It's all a red herring: the problems comes from THINKING it does matter. What we need to do is stop providing information to the z-buffer and telling opengl that it needs to consider the order (which then subsequently gets messed up). So in this case we just need to write
glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
glEnable( GL_BLEND );
glEnable(GL_DEPTH_TEST);
glDepthMask(GL_FALSE);
glBegin(GL_TRIANGLES);
// draw here
glEnd();
glDisable(GL_BLEND);
glDisable(GL_DEPTH_TEST);
glDepthMask(GL_TRUE);
where the glDepthMask(GL_FALSE) is ensuring depth information isn't being written. You can rotate all you want and the transparencies look fine! But this is dependent on being in a situation where the image outcome DOESN'T depend on the order: it's just that with all the documentation you might think your situation does depend on it when it might not.
If it does depend on the order (it would look different from one angle as opposed to another using the above and you don't want it to) and you don't want to do fancy things or complicated sorts you can use a 'lesser' form of transparency by using commutative blending (multiplicative or additive). You still have to disable z writing and you can do things like
Multiplicative
glBlendFunc(GL_ZERO,GL_SRC_COLOR);
glEnable( GL_BLEND );
glEnable(GL_DEPTH_TEST);
glDepthMask(GL_FALSE);
glBegin(GL_TRIANGLES);
// draw here
glEnd();
glDisable(GL_BLEND);
glDisable(GL_DEPTH_TEST);
glDepthMask(GL_TRUE);
Additive
glBlendFunc(GL_ONE,GL_ONE);
glEnable( GL_BLEND );
glEnable(GL_DEPTH_TEST);
glDepthMask(GL_FALSE);
glBegin(GL_TRIANGLES);
// draw here
glEnd();
glDisable(GL_BLEND);
glDisable(GL_DEPTH_TEST);
glDepthMask(GL_TRUE);
and play about with these to see if they look OK. The point is try these 'lesser' approaches to transparency before looking into complicated sorts/order independent approaches because these will have much lower overheads and might work for your situation depending on what you are doing.
I am using OpenGL with C++ (but without shaders and GLSL) and drawing spheres (the pool ball) and cylinders (the cue stick) using the glu library functions to draw them. I am using glBindTexture() to bind the texture and loading the texture using the SOIL library.
As you can see from the screenshots there are jagged edges to both the cylinder and the sphere. Calling the following glHint() How do I get rid of the jagged edges. The gluSphere() has 25 stacks and slices, and the gluCylinder() has 100 stacks and slices. Increasing the stacks and slices does not improve the image quality.
Using freeglut for the rest of the drawing
glHint(GL_POLYGON_SMOOTH_HINT, GL_NICEST); //Smooth polygons
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); //Best perspective corrections
glHint(GL_POINT_SMOOTH_HINT, GL_NICEST); //Smooth points
glHint(GL_LINE_SMOOTH_HINT, GL_NICEST); //Smooth lines
First of all your question is related to aliasing term, this is what is happen when tringles and some other primitives (these are very basic objects which create whole scene) are rasterized (based on geometric description some pixels on the screen are colored or not).
Try to look for "How to turn on antialiasing" - here is many usefull informations about this and some related topics: http://www.glprogramming.com/red/chapter06.html.
In your case it will be probably glEnable for GL_POLYGON_SMOOTH and GL_BLEND for sure.
eg.
glEnable (GL_POLYGON_SMOOTH);
glEnable (GL_BLEND);
glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glHint (GL_POLYGON_SMOOTH_HINT, GL_DONT_CARE);
If you plan to use lines there will be GL_LINE_SMOOTH and GL_LINE_SMOOTH_HINT.
For future projects try to not use so complex models if they are far from viewer (making as many slices for sphere), this will cause loss of performance.