OpenGL transparent texture issue - opengl

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

Related

Color dodge in OpenGL

I need to render an image on top of a background in OpenGL and I'm trying to get the same result as the "Color Dodge" in Photoshop but I'm not able to do it.
Right now I'm doing:
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE);
// background
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, background);
glBegin(GL_TRIANGLE_STRIP);
glTexCoord2f(0.0, 0.0);
...
glEnd();
glDisable(GL_TEXTURE_2D);
// image
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, image);
glBegin(GL_TRIANGLE_STRIP);
glTexCoord2f(0.0, 0.0);
...
glEnd();
glDisable(GL_TEXTURE_2D);
The background is a tga with no alpha channel. The image is a tga with alpha channel.
This renders the image with alpha on the background but way too bright.
I read that it should be as easy as:
glBlendFunc(GL_ONE, GL_ONE);
But the image despite of having alpha channel gets rendered as a white square.
Clearly I'm doing something wrong.
You're not going to be able to use blending to get the equivalent of the Photoshop "Color Dodge" effect. It's a more complicated mathematical function than can be expressed using standard blending logic. So you're going to have to come up with some programmatic blending methodology to make it work.
There is a way to make color dodge in GL blend func. It's like the Photoshop version of that mixing mode, but only it's darker than photoshop's "Color Dodge". You have to use this type of function:
glBlendFunc(GL_DST_COLOR, GL_ONE);

Anti-aliasing with GL_TRIANGLE_FAN

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.

Opengl: issue with masking

I'm working on creating a hole in a wall using masking in opengl, my code is quit simple like this,
//Draw the mask
glEnable(GL_BLEND);
glBlendFunc(GL_DST_COLOR,GL_ZERO);
glBindTexture(GL_TEXTURE_2D, texture[3]);
glBegin(GL_QUADS);
glTexCoord2d(0,0); glVertex3f(-20,40,-20);
glTexCoord2d(0,1);glVertex3f(-20,40,40);
glTexCoord2d(1,1);glVertex3f(20,40,40);
glTexCoord2d(1,0);glVertex3f(20,40,-20);
glEnd();
//Draw the Texture
glBlendFunc(GL_ONE, GL_ONE);
glBindTexture(GL_TEXTURE_2D, texture[2]);
glBegin(GL_QUADS);
glTexCoord2d(0,0); glVertex3f(-20,40,-20);
glTexCoord2d(0,1);glVertex3f(-20,40,40);
glTexCoord2d(1,1);glVertex3f(20,40,40);
glTexCoord2d(1,0);glVertex3f(20,40,-20);
glEnd();
The problem is, I got the hole in the wall correctly but it's semi transparent, I'm getting like black shade over it, also I can see through it.
Here's a photo for what I'm getting:
any suggestions?
SOLVED :D
It was a problem with the surfaces' Normals, once I set the normals in the correct position.The black shade faded out.

OpenGl texture mapping blocking colours on FreeType?

I'm using FreeType in order to allow fonts to be used in OpenGL. However, I'm having a problem where I cannot change the font colour whenever I do texture mapping. No matter what I select using glColor3f it will just come out white. The texture works fine.
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
glColor3f(0.5,0.0,0.5);
glPushMatrix();
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_TEXTURE_2D);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
glBindTexture(GL_TEXTURE_2D, texName);
glBegin(GL_POLYGON);
glTexCoord2f(0,1); glVertex2f(-16,-16);
glTexCoord2f(0,0); glVertex2f(-16,16);
glTexCoord2f(1,0); glVertex2f(16,16);
glTexCoord2f(1,1); glVertex2f(16,-16);
glEnd();
glDisable(GL_TEXTURE_2D);
glDisable(GL_BLEND);
glPopMatrix();
glColor3f(1,0,0);
print(our_font, -300+screenWidth/2.0, screenHeight/2.0, "fifty two - %7.2f", spin);
This is the problem code, I can confirm that drawing a polygon beneath this code will indeed make it red. The text is not changing to red though which it should; if you remove the texture mapping above it will turn red again, I can only think it is a problem with enabling and disabling and I've forgotten to do something...?
Fixed it. Just after I disabled texturing I forgot to set the environment back to modulate:
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
adding this after disabling texture/blending fixes the problem.

render transparent textures

I have one texture that has some portions which are transparent transparent I want to apply over an object whose faces are some opaque material (or colour if it's simpler) but the final object gets transparent. I want the final object to be totally opaque.
Here is my code:
First I set the material:
glDisable(GL_COLOR_MATERIAL);
glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT);
glColor4f(0.00, 0.00, 0.00, 1.00);
glColorMaterial(GL_FRONT_AND_BACK, GL_DIFFUSE);
glColor4f(0.80, 0.80, 0.80, 1.00);
glColorMaterial(GL_FRONT_AND_BACK, GL_SPECULAR);
glColor4f(0.01, 0.01, 0.01, 1.00);
glEnable(GL_COLOR_MATERIAL);
Then I setup the VBOs
glBindTexture(GL_TEXTURE_2D, object->texture);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
glBindBuffer(GL_ARRAY_BUFFER, object->object);
glVertexPointer(3, GL_FLOAT, sizeof(Vertex), ver_offset);
glTexCoordPointer(2, GL_FLOAT, sizeof(Vertex), tex_offset);
glNormalPointer(GL_FLOAT, sizeof(Vertex), nor_offset);
And finally I draw the object
glEnable(GL_BLEND);
glDisable(GL_DEPTH_TEST);
glDisable(GL_TEXTURE_2D);
glBlendFunc(GL_ONE, GL_ZERO);
glDrawArrays(GL_TRIANGLES, 0, object->num_faces);
glEnable(GL_TEXTURE_2D);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glDrawArrays(GL_TRIANGLES, 0, object->num_faces);
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glDisable(GL_BLEND);
glEnable(GL_DEPTH_TEST);
I tried passing different arguments to glBlendFunc() with no prevail. I've uploaded the source here: http://dpaste.com/83559/
UPDATE
I get this, but I want this (or without texture this).
The 2nd and the 3rd picture are produces with glm. I studied the sources, but since my knowledge of OpenGL is limited I didn't understand much.
If you're trying to apply two textures to your object you really want to set two textures and use multitexturing to achieve this look. Your method is drawing the geometry twice which is a huge waste of performance.
Multitexturing will just sample from two texture units while only drawing the geometry once. You can do this with shaders (the way things really should be done) or you can still used the fixed function pipeline (see: http://bluevoid.com/opengl/sig00/advanced00/notes/node62.html)
AFAIK the blend function takes fragment colors (opposed to texture colors). So if you draw the object a second time with blending, the triangles become transparent.
What you want to accomplish could be done using multitexturing.
This is just a wild guess, as you have failed to provide any screenshots of what the actual problem is, but why do you disable the depth test? Surely you want to enable depth testing on the first pass with a standard GL_LESS and then do the second pass with GL_EQUAL?
Edit:
ie
glEnable(GL_BLEND);
glEnable(GL_DEPTH_TEST); // ie do not disable
glDepthFunc( GL_LESS ); // only pass polys have a z value less than ones already in the z-buffer (ie are in front of any previous pixels)
glDisable(GL_TEXTURE_2D);
glBlendFunc(GL_ONE, GL_ZERO);
glDrawArrays(GL_TRIANGLES, 0, object->num_faces);
// for the second pass we only want to blend pixels where they occupy the same position
// as in the previous pass. Therefore set to equal and only pixels that match the
// previous pass will be blended together.
glDepthFunc( GL_EQUAL );
glEnable(GL_TEXTURE_2D);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glDrawArrays(GL_TRIANGLES, 0, object->num_faces);
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glDisable(GL_BLEND);
Try disabling blending and drawing a single pass with your texture function set to GL_DECAL instead of GL_MODULATE. This will blend between the vertex color and the texture color based on the texture’s alpha channel, but leave the alpha channel set to the vertex color.
Note that this will ignore any lighting applied to the vertex color anywhere the texture was opaque, but this sounds like the intended effect, based on your description.
It will be much simpler with pixel shaders. otherwise I think you need multi-passes rendering or more than one texture.
You can find comprehensive details here :http://www.opengl.org/resources/faq/technical/transparency.htm