# OpenGL Alpha-blending woes (it's CLOSE!)

This topic is 4373 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

The short version: I'm trying to draw a star-box (texture alpha=1), then a fade-out skybox over it (texture alpha=1 in the center, fade to 0 at the edges), then some geometry over that. Try as I may, and I've read the tutorials and the redbook and appropriate sections of the bluebook, I just can't seem to get it to come out right. :( The medium-length version: Please see 2 posts down. (Some said my long version was too long to follow, so I tried to summarize.) The long version: Here are some clips from posts I made to <news:comp.graphics.api.opengl>, they contain a URL to some screen shots, and a few code snippets. --- Here's what I've got: My source image is a .png, so the data is stored ARGB. So I tell GL that it's BGRA, but to use reversed bytes. So my calls look something like this:
// Earlier in setup:
gl.glEnable (GL.GL_BLEND);
gl.glBlendFunc (GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA);

// When I'm creating texture:
gl.glBindTexture (GL.GL_TEXTURE_2D, textureID);
gl.glTexParameteri (GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MAG_FILTER,
GL.GL_NEAREST);
gl.glTexParameteri (GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MIN_FILTER,
GL.GL_NEAREST);
gl.glTexImage2D (GL.GL_TEXTURE_2D, 0, GL_RGBA8 width, height, 0,
GL_BGRA_EXT, GL_UNSIGNED_INT_8_8_8_8_REV, imageData);
gl.glBindTexture (GL.GL_TEXTURE_2D, 0);


The problem that I'm running into is: even though my texture.png looks fine when I display it over the top of another image, and it fades nicely from the center with alpha=1.0 to the edges where alpha=0.0, as a texture, even where the texture-alpha is very small (say, 0.1), it's still behaving as if alpha=1.0, but with very faint color. then there's a "clean break" at alpha=0. This does not give the desired "see the other objects through my texture" effect. --- Here is a .zip with a handful of screenshots. Here's what I'm trying to do: * Draw a field of stars - ...then clear the depth buffer * Draw a blue-faded sky (alpha=1 at zenith, alpha=0 near the edges, with a gradual fade between.) - ... then clear the depth buffer * Draw some geometry. Some of it is textured and/or blended. My screen shots come from 3 separate runs: * Scenario 1 (Alpha)
gl.glAlphaFunc (GL.GL_GREATER, 0.0f);
gl.glEnable (GL.GL_ALPHA_TEST);
gl.glDisable (GL.GL_BLEND);


Pict 2, 4 Flaws: Blue sky fades to white, until we get to the alpha=0 part, then there's a sudden cutoff. I'm wanting the blue to gently fade away and expose the starfield underneath. * Scenario 2 (both)
gl.glBlendFunc (GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA);
gl.glEnable (GL.GL_BLEND);

gl.glAlphaFunc (GL.GL_GREATER, 0.0f);
gl.glEnable (GL.GL_ALPHA_TEST);
gl.glDisable (GL.GL_BLEND);


Pict 1 Flaws: Same as scenario #1 * Scenario 3 (blend)
gl.glBlendFunc (GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA);
gl.glEnable (GL.GL_BLEND);


Pict 5 Flaws: I think of this as the closest to what I want, in that the blue gradually fades out, but it STILL doesn't show -- say, at the alpha=0.5 point -- the starfield behind it; there's still that sudden cutoff when we get to alpha=0. In addition, the geometry is weird. While the "deck" does, indeed show some faded tiles (as desired), the purple cube & cylindar are mysteriously see-through, even though they're supposed to be alpha=1, not-blended, not-textured, normal-old-geometry (as in the other pix.) NOTES: My geometry consists of... * A red gluSphere * 3 axis lines, red, green blue = X, Y, Z. * A white, smaller gluSphere on the end of the X-line * A white-ish emmitting gluSphere to represent where my light-source is. * A purple gluCube * A purple gluCylinder * A rectangle with a solid "grassy" texture. Also, I glClear(GL_DEPTH_BUFFER_BIT) after the starfield and the sky-box (both are skybox objects, just with different textures. Pict#6 shows just the starfield (no skybox) -- it has all alpha=1, and appears to work pretty much as I'd expect. My problems seem to come when I try to draw a variable-alpha-texture over the top of it. --- (NOTE: my skyboxes are cubes, not spheres. They're drawn to "face inward.") I guess I should take some comfort in the fact that this doesn't appear to be an obvious n00b mistake. As in, no one's piped-up with "well, it's clear that you're calling glFoo(), when you need to glBar()", or whatever... Can anyone point me to sample code that does this sort of thing? By "this sort of thing", I specifically mean... * Draw a field of stars (alpha=1 for whole texture.) - ...then clear the depth buffer * Draw a blue-faded sky (alpha=1 at zenith, alpha=0 near the edges, with a gradual fade between.) - ... then clear the depth buffer * Draw some geometry. Some of it is textured and/or blended. Thanks! [Edited by - Chum on October 5, 2006 9:36:14 AM]

##### Share on other sites
i cant concentrate with long posts but i assume u want a skybox of stars

draw box first
then disable depthtest
draw stars around camera

see my website all starboxs were made with this method

##### Share on other sites
Ok, I'll try to restate in a shorter way:

* I am successfully drawing a skybox object containing a field of stars, my "starbox"
* The STARbox texture is alpha=1 throughout, and draws correctly.
* I have a 2nd skybox that I try to draw over the top of that one, it is my "sky" skybox
* The "SKY" box has a texture that is all blue, with alpha=1 in the center fading to alpha=0 just below the horizon, and a GRADUAL FADE between the two.
* My problem seems to be that, in the places where 0 < alpha < 1, while the colors are faded (as expected), the alpha does not blend -- that is, at the alpha=0.5 part of the picture, I'd expect to see some blue, but a few stars starting to poke through, etc., down to where alpha=0.

...Instead, it's as if everything alpha > 0 is treadted as alpha=1, with a sudden cuttoff to alpha=0.

My 1st post has code examples of what I'm doing, including a description of the code and faulty behaviour of the screenshots at this link

I hope that's easier to follow.

##### Share on other sites
sorry i still dont understand fully (its early)
but ill try (dont use glAlphaTest
*draw the starbox
*enable blending with src_ALPHA 1-src_alpha
*draw the blue texture
*glDisable( GL_BLEND );
*draw eveything else

1. 1
2. 2
3. 3
4. 4
Rutin
12
5. 5

• 12
• 16
• 10
• 14
• 10
• ### Forum Statistics

• Total Topics
632659
• Total Posts
3007691
• ### Who's Online (See full list)

There are no registered users currently online

×