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

## 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

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628354
• Total Posts
2982243
• ### Similar Content

• Hi all,

I'm starting OpenGL using a tut on the Web. But at this point I would like to know the primitives needed for creating a window using OpenGL. So on Windows and using MS VS 2017, what is the simplest code required to render a window with the title of "First Rectangle", please?

• Hi, New here.
I need some help. My fiance and I like to play this mobile game online that goes by real time. Her and I are always working but when we have free time we like to play this game. We don't always got time throughout the day to Queue Buildings, troops, Upgrades....etc....
I was told to look into DLL Injection and OpenGL/DirectX Hooking. Is this true? Is this what I need to learn?
How do I read the Android files, or modify the files, or get the in-game tags/variables for the game I want?
Any assistance on this would be most appreciated. I been everywhere and seems no one knows or is to lazy to help me out. It would be nice to have assistance for once. I don't know what I need to learn.
So links of topics I need to learn within the comment section would be SOOOOO.....Helpful. Anything to just get me started.
Thanks,
Dejay Hextrix
• By mellinoe
Hi all,
First time poster here, although I've been reading posts here for quite a while. This place has been invaluable for learning graphics programming -- thanks for a great resource!
Right now, I'm working on a graphics abstraction layer for .NET which supports D3D11, Vulkan, and OpenGL at the moment. I have implemented most of my planned features already, and things are working well. Some remaining features that I am planning are Compute Shaders, and some flavor of read-write shader resources. At the moment, my shaders can just get simple read-only access to a uniform (or constant) buffer, a texture, or a sampler. Unfortunately, I'm having a tough time grasping the distinctions between all of the different kinds of read-write resources that are available. In D3D alone, there seem to be 5 or 6 different kinds of resources with similar but different characteristics. On top of that, I get the impression that some of them are more or less "obsoleted" by the newer kinds, and don't have much of a place in modern code. There seem to be a few pivots:
The data source/destination (buffer or texture) Read-write or read-only Structured or unstructured (?) Ordered vs unordered (?) These are just my observations based on a lot of MSDN and OpenGL doc reading. For my library, I'm not interested in exposing every possibility to the user -- just trying to find a good "middle-ground" that can be represented cleanly across API's which is good enough for common scenarios.
Can anyone give a sort of "overview" of the different options, and perhaps compare/contrast the concepts between Direct3D, OpenGL, and Vulkan? I'd also be very interested in hearing how other folks have abstracted these concepts in their libraries.
• By aejt
I recently started getting into graphics programming (2nd try, first try was many years ago) and I'm working on a 3d rendering engine which I hope to be able to make a 3D game with sooner or later. I have plenty of C++ experience, but not a lot when it comes to graphics, and while it's definitely going much better this time, I'm having trouble figuring out how assets are usually handled by engines.
I'm not having trouble with handling the GPU resources, but more so with how the resources should be defined and used in the system (materials, models, etc).
This is my plan now, I've implemented most of it except for the XML parts and factories and those are the ones I'm not sure of at all:
I have these classes:
For GPU resources:
Geometry: holds and manages everything needed to render a geometry: VAO, VBO, EBO. Texture: holds and manages a texture which is loaded into the GPU. Shader: holds and manages a shader which is loaded into the GPU. For assets relying on GPU resources:
Material: holds a shader resource, multiple texture resources, as well as uniform settings. Mesh: holds a geometry and a material. Model: holds multiple meshes, possibly in a tree structure to more easily support skinning later on? For handling GPU resources:
ResourceCache<T>: T can be any resource loaded into the GPU. It owns these resources and only hands out handles to them on request (currently string identifiers are used when requesting handles, but all resources are stored in a vector and each handle only contains resource's index in that vector) Resource<T>: The handles given out from ResourceCache. The handles are reference counted and to get the underlying resource you simply deference like with pointers (*handle).
And my plan is to define everything into these XML documents to abstract away files:
Resources.xml for ref-counted GPU resources (geometry, shaders, textures) Resources are assigned names/ids and resource files, and possibly some attributes (what vertex attributes does this geometry have? what vertex attributes does this shader expect? what uniforms does this shader use? and so on) Are reference counted using ResourceCache<T> Assets.xml for assets using the GPU resources (materials, meshes, models) Assets are not reference counted, but they hold handles to ref-counted resources. References the resources defined in Resources.xml by names/ids. The XMLs are loaded into some structure in memory which is then used for loading the resources/assets using factory classes:
Factory classes for resources:
For example, a texture factory could contain the texture definitions from the XML containing data about textures in the game, as well as a cache containing all loaded textures. This means it has mappings from each name/id to a file and when asked to load a texture with a name/id, it can look up its path and use a "BinaryLoader" to either load the file and create the resource directly, or asynchronously load the file's data into a queue which then can be read from later to create the resources synchronously in the GL context. These factories only return handles.
Factory classes for assets:
Much like for resources, these classes contain the definitions for the assets they can load. For example, with the definition the MaterialFactory will know which shader, textures and possibly uniform a certain material has, and with the help of TextureFactory and ShaderFactory, it can retrieve handles to the resources it needs (Shader + Textures), setup itself from XML data (uniform values), and return a created instance of requested material. These factories return actual instances, not handles (but the instances contain handles).

Is this a good or commonly used approach? Is this going to bite me in the ass later on? Are there other more preferable approaches? Is this outside of the scope of a 3d renderer and should be on the engine side? I'd love to receive and kind of advice or suggestions!
Thanks!
• By nedondev
I 'm learning how to create game by using opengl with c/c++ coding, so here is my fist game. In video description also have game contain in Dropbox. May be I will make it better in future.
Thanks.

• 10
• 9
• 11
• 24
• 11