• Advertisement
Sign in to follow this  

I cant figure out how to render many alpha-planes

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

I've tried to render a simple tree: It all works pretty well, when I keep it down to just two planes, but as soon as I try to render a tree with 3 or more planes, it all turns out some thing like this: (this isn't from my own application) I read a lot of tips about how to fix this problem (Eg. disable z-writes, render several times, ...), but I just cant seem to get anything to work properly. I'm using: (sorry about the syntax, it's from a java-wrapper) GL11.glEnable(GL11.GL_BLEND); GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA); Can anyone tell me how to make this work?

Share this post


Link to post
Share on other sites
Advertisement
The general mechanism is:
1. Draw all solid geometry (landscape) as normal (no blending, with depth test and depth write).
2. Draw all transparent geometry with depth testing but no depth writing:

GL11.glDepthMask(false); // Disable depth writing

For correct results transparent geometry also needs to be drawn back-to-front (so you'd have to split your trees into four different quads as the two crossed quads can't be correctly sorted.

Alternatively, for an easier and more robust method, don't use blending and instead use alpha testing.

GL11.glDisable(GL11.GL_BLEND); // no blending
GL11.glEnable(GL11.GL_ALPHA_TEST); // enable alpha test
GL11.glAlphaFunc(GL11.GL_GREATER, 0.6); // Draw pixels with greater than 0.6 alpha

This has the nice property that you don't need to sort, and can be drawn with regular depth testing and writing so you can do it in between all your regular geometry too. The downside is that you get hard edges instead of soft ones but IMHO you want that for trees and chain link fences anyway.

Share this post


Link to post
Share on other sites
The alpha testing works fine, thanks for that one.

But the blend function doesn't..

I kind of see your point about that I in most cases wont need gradual transparency. But just per principals I would like to get it to work anyway.

The tree:

dList = GL11.glGenLists(1);

GL11.glNewList(dList, GL11.GL_COMPILE);
GL11.glPushMatrix();

for (int i = 0, n = 5; i < n; i++)
{
GL11.glBegin(GL11.GL_QUADS);

GL11.glTexCoord2f(1, 1);
GL11.glNormal3f(1, 1, 0);
GL11.glVertex3f(1, 1, 0);
GL11.glTexCoord2f(0.5f, 1);
GL11.glNormal3f(0, 1, 0);
GL11.glVertex3f(0, 1, 0);
GL11.glTexCoord2f(0.5f, 0.02f);
GL11.glNormal3f(0, -1, 0);
GL11.glVertex3f(0, -1, 0);
GL11.glTexCoord2f(1, 0.02f);
GL11.glNormal3f(1, -1, 0);
GL11.glVertex3f(1, -1, 0);

GL11.glEnd();

GL11.glRotatef(360.000f/n, 0, 1, 0);
}

GL11.glPopMatrix();
GL11.glEndList();



The background is just a plane with texture.

And for rendering the tree I'll use

GL11.glEnable(GL11.GL_BLEND);
GL11.glDepthMask(false);
GL11.glCallList(dList);

But even though the tree is split up in half trees, rotating about the y-axis, the depth test doesn't seem to work correctly.

Share this post


Link to post
Share on other sites
If you will be using this kind of blending

glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

because when you will be blending the frontmost polygons, it needs to blend with something in the background.
Disabling depth writes is not going to solve that.

then you need to sort your polygons back to front or use a technique called order independent transparency.
http://developer.download.nvidia.com/SDK/9.5/Samples/3dgraphics_samples.html

Share this post


Link to post
Share on other sites
Hmm I tried to take a closer look at "Post Processing Effects with Anti Aliasing", but I can't make it do as I want it to do.. I kind of figured out that it got something to do with
glEnable(GL_MULTISAMPLE_ARB);
and
glEnable(GL_SAMPLE_ALPHA_TO_COVERAGE_ARB);

But thats all i can figure out myself :(

I can't really see how this is supposed to make the objects farthest behind getting rendered first.

Share this post


Link to post
Share on other sites
try to evaluate the Z value of the object order it, and them draw it form the farest object (the bigger object with z value ) to the nearest object

Share this post


Link to post
Share on other sites
Quote:
Original post by VildNinja
Hmm I tried to take a closer look at "Post Processing Effects with Anti Aliasing", but I can't make it do as I want it to do.. I kind of figured out that it got something to do with
glEnable(GL_MULTISAMPLE_ARB);
and
glEnable(GL_SAMPLE_ALPHA_TO_COVERAGE_ARB);

But thats all i can figure out myself :(

I can't really see how this is supposed to make the objects farthest behind getting rendered first.


You are probably looking at the wrong demo.
Search for order independent transparency. Download and run it.
You can also google it and find more info on it.

Share this post


Link to post
Share on other sites
Hurray!

I couldn't find any usable (for an openGL noob (me)) articles on the subject.
Therefore I wrote a class containing vectors to describe each corner of the quads. By the use of matrix operations, I were able to calculate the corners of the rotated quads, and therefore able to get the distance between the quads and origo in camera-space.

In my game loop, I sorted the quads, and draw them.

My only concern now is that the matrix operations and the sorting algorithm isn't hardware accelerated for the alpha shapes.. But it ain't a problem as long as I'm only learning openGL.

But thanks for the help!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement