# OpenGL Another problem with Depth, this time with an alpha channel

## Recommended Posts

Langecrew    114
Hello everybody! It's me again! I have been trying for quite some time now to render intersecting tetrahedrons whose base triangles are coplanar. Basically, everything is going great. Now, I've had to try to add semi-transparency to my little tetrahedrons. a past post for those unfamiliar with my problems: http://www.gamedev.net/community/forums/topic.asp?topic_id=426329 The problem that I now have is that: The intersecting tetrahedrons are laid out in rows and columns, each one intersects all the others bordering it. The tetrahedrons render fine and look great...from many angles, I can see the intersecting tetrahedrons along with all the tetrahedrons behind them. However, when I rotate to certain angles, I still see transparent tetrahedrons, but I can't see the ones behind them; the transparency doesn't seem to let the other parts of the image bleed through. Please see the screenshot below, perhaps it will be more clear. The topmost of the two drawings represents how the arrangement should render from all angles. If you look closely at the rendering at the bottom of the shot you can see that it does show some overlapping with transparency, but not all of the way down the row...also you can see that it is in fact rendering all these tetrahedrons, it just won't show them. It seems to me that there is probably just something stupid going on somewhere, but my knowledge of OpenGL is too limited for me to know. Thanks again! -Langecrew-

##### Share on other sites
Guest Anonymous Poster
The thing is no matter if you use D3D or OpenGL the second you start alpha blending you have to sort your polygons. This is because even if the pixel is transparent its written to Depth Buffer causing everything else behind it to be occluded.

Look at this for possible solutions:
http://www.opengl.org/wiki/index.php/Alpha_Blending

##### Share on other sites
Guest Anonymous Poster
Forgot to say you have to disable depth testing when alpha blending things after sorting them.

##### Share on other sites
KulSeran    3267
You have 2 choices
1) you can sort all your polygons by their depth from the camera,
then draw them farthest to closest.
2) you can disable depth writes (leave depth tests on), and render all your
transparent objects.

##### Share on other sites
Langecrew    114
No dice. I'd be plenty willing to sort them, but how do you approach that if the collection of objects can be rotated to any angle? I tried keeping track of the angles of rotation before, and I didn't really get any satisfactory results.

I've honestly tried to avoid being the guy that posts about 2 pages of code and just asks "what's wrong with my code?" but I think I'm coming to that point. Either I've been missing something very fundamental this whole time, or OpenGL has a bug that causes unexpected behavior when primitives overlap/intersect.

anybody care to take a crack at my code?

##### Share on other sites
KulSeran    3267
There is no bug, what is happening is that it draws everything as it comes in. You can't properly blend
over the background if the background isnt there yet. So you have to draw what is in back first.

This means that you need to draw the back objects before the front ones. So, how?
You must have some position information for your objects, and your objects need to be seperaterated in some way.
So you cant just be drawing points with no order.

So maybe you should post a bit of code so we know how you are drawing your objects.
ie. are they a for loop with glVector3d () calls? or a loop over objects?

This whole thing is easier if you have objects of some sort that contain the position of the center of the object.

struct Object{  vector3d pos;  float size;  ...  }

from there you just need to sort on distance, so sort by
(camera.pos - cur_object.pos).size();
where size() is the vector magnitude.
That way you have all your objects in the order of distance to them, thus you can draw them in order.

If you are rotating the objects and not the just the camera, then you need to preform the roation on the data.
Since the gl calls dont affect your actuall dataset.
So, if you rotate and translate the objects with gl calls, then you need to use those same rotations and translations on your data
OR you need to preform the inverse of those actions on your camera before you sort the collection.

Since the camera is a single point and the things you are drawing are many points, it will be faster to transform the camera.
So, you need the rotation/translation of your object collection, and then invert them. From there you have to apply it to
your camera. This application to the camera depends on your camera code.

##### Share on other sites
l0calh05t    1796
alpha to coverage. solves all those pesky alpha sorting problems. (but only allows 6 levels of transparency, when using 6x oversampling. and requires oversampling.)

##### Share on other sites
shurcool    439
Try disabling the depth testing completely. It should look the way you want it to (ie. the way it does on the "good" screenshot) from all angles in that case, since it's what makes only the front transparent objects show up (and not the ones behind) in your "bad" screenshot.

glDisable(GL_DEPTH_TEST);

Note that this is not a very good way to do transparency, and it will not work unless all of your objects are equally transparent.

##### Share on other sites
Langecrew    114
Hi guys,
I wanted to post back to let you know that I'm still here. I got side-tracked on some major bugs in a different project here at work. That's why I've been out of the loop for a few days here.

KulSeran, I think you might be on to something here. Seeing as how I'm so blazingly new to OpenGL programming, I've never done anything like how you suggest. I am actually doing both, rotating the collection of objects, AND moving the camera. I use rotation on both the outline of the measurement volume and the tetrahedrons inside, as well as their outlines. I move the camera when panning left/right or up/down. So you're saying that, if I simply apply the rotation information to the actual data, then I should be OK?
Is that to say that I should:
1. apply the rotation info to the data
2. NOT rotate what is rendered, but instead render the rotated data?
What approach should I try if I move the camera, and rotate the objects?

l0calh05t, I'm intrigued. I searched for "alpha to coverage" and I found some sample code online. I couldn't get the pre-made EXE to run, and I have not yet gotten the time to look at the code at all...but it's on my list also.

shurcool, I've tried playing with the Depth Test many times. If that could provide an appropriate solution, then I think I've missed something. When I render everything with Depth Test off, then everything looks all strange when I rotate. It looks like, if you follow the tetrahedron in front when the scene is first rendered, and rotate, the tetrahedron that was in front visually looks like it stays in front of all the others even when you know it has moved behind others. It looks really wierd.

Let me give this just a couple more shots before I post my code. That way, you guys don't have to spend the time to look through my source.

Thanks again, have a great day!
-Langecrew-

##### Share on other sites
Digitalfragment    1504
To avoid the artifact that you mentioned with the depth testing disabled, you *HAVE* to sort your objects. OpenGL & D3D are not ray tracers, they are rasterizers that expect your transparent polygons to be written in back to front order to render completely.

If you cant do this per object because they are overlapping, then do it per triangle. If you really need the detail, implement the sorting using an oldschool BSP tree. But per object is usually enough, you will get less artifacts sorting that way than with no sorting. Just do it based on the distance of the centre of each object to the camera.

On another note, you want to leave depth-testing on so that solid objects occlude your transparent ones, but you want to be turning depth-writing off (glDepthMask) so the other transparent objects arent obscured.

- Shaun

##### Share on other sites
Langecrew    114
Man, when I set out to do this, I didn't know it would be such a specific application that it would be this much hassle. Oh well...I'm almost there...

Thanks for the input!
-Langecrew-

## Create an account

Register a new account

• ### Similar Content

• I'm trying to get some legacy OpenGL code to run with a shader pipeline,
The legacy code uses glVertexPointer(), glColorPointer(), glNormalPointer() and glTexCoordPointer() to supply the vertex information.
I know that it should be using setVertexAttribPointer() etc to clearly define the layout but that is not an option right now since the legacy code can't be modified to that extent.
I've got a version 330 vertex shader to somewhat work:
#version 330 uniform mat4 osg_ModelViewProjectionMatrix; uniform mat4 osg_ModelViewMatrix; layout(location = 0) in vec4 Vertex; layout(location = 2) in vec4 Normal; // Velocity layout(location = 3) in vec3 TexCoord; // TODO: is this the right layout location? out VertexData { vec4 color; vec3 velocity; float size; } VertexOut; void main(void) { vec4 p0 = Vertex; vec4 p1 = Vertex + vec4(Normal.x, Normal.y, Normal.z, 0.0f); vec3 velocity = (osg_ModelViewProjectionMatrix * p1 - osg_ModelViewProjectionMatrix * p0).xyz; VertexOut.velocity = velocity; VertexOut.size = TexCoord.y; gl_Position = osg_ModelViewMatrix * Vertex; } What works is the Vertex and Normal information that the legacy C++ OpenGL code seem to provide in layout location 0 and 2. This is fine.
What I'm not getting to work is the TexCoord information that is supplied by a glTexCoordPointer() call in C++.
Question:
What layout location is the old standard pipeline using for glTexCoordPointer()? Or is this undefined?

Side note: I'm trying to get an OpenSceneGraph 3.4.0 particle system to use custom vertex, geometry and fragment shaders for rendering the particles.

• Hi i am new to this forum  i wanted to ask for help from all of you i want to generate real time terrain using a 32 bit heightmap i am good at c++ and have started learning Opengl as i am very interested in making landscapes in opengl i have looked around the internet for help about this topic but i am not getting the hang of the concepts and what they are doing can some here suggests me some good resources for making terrain engine please for example like tutorials,books etc so that i can understand the whole concept of terrain generation.

• By KarimIO
Hey guys. I'm trying to get my application to work on my Nvidia GTX 970 desktop. It currently works on my Intel HD 3000 laptop, but on the desktop, every bind textures specifically from framebuffers, I get half a second of lag. This is done 4 times as I have three RGBA textures and one depth 32F buffer. I tried to use debugging software for the first time - RenderDoc only shows SwapBuffers() and no OGL calls, while Nvidia Nsight crashes upon execution, so neither are helpful. Without binding it runs regularly. This does not happen with non-framebuffer binds.
GLFramebuffer::GLFramebuffer(FramebufferCreateInfo createInfo) { glGenFramebuffers(1, &fbo); glBindFramebuffer(GL_FRAMEBUFFER, fbo); textures = new GLuint[createInfo.numColorTargets]; glGenTextures(createInfo.numColorTargets, textures); GLenum *DrawBuffers = new GLenum[createInfo.numColorTargets]; for (uint32_t i = 0; i < createInfo.numColorTargets; i++) { glBindTexture(GL_TEXTURE_2D, textures[i]); GLint internalFormat; GLenum format; TranslateFormats(createInfo.colorFormats[i], format, internalFormat); // returns GL_RGBA and GL_RGBA glTexImage2D(GL_TEXTURE_2D, 0, internalFormat, createInfo.width, createInfo.height, 0, format, GL_FLOAT, 0); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); DrawBuffers[i] = GL_COLOR_ATTACHMENT0 + i; glBindTexture(GL_TEXTURE_2D, 0); glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + i, textures[i], 0); } if (createInfo.depthFormat != FORMAT_DEPTH_NONE) { GLenum depthFormat; switch (createInfo.depthFormat) { case FORMAT_DEPTH_16: depthFormat = GL_DEPTH_COMPONENT16; break; case FORMAT_DEPTH_24: depthFormat = GL_DEPTH_COMPONENT24; break; case FORMAT_DEPTH_32: depthFormat = GL_DEPTH_COMPONENT32; break; case FORMAT_DEPTH_24_STENCIL_8: depthFormat = GL_DEPTH24_STENCIL8; break; case FORMAT_DEPTH_32_STENCIL_8: depthFormat = GL_DEPTH32F_STENCIL8; break; } glGenTextures(1, &depthrenderbuffer); glBindTexture(GL_TEXTURE_2D, depthrenderbuffer); glTexImage2D(GL_TEXTURE_2D, 0, depthFormat, createInfo.width, createInfo.height, 0, GL_DEPTH_COMPONENT, GL_FLOAT, 0); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glBindTexture(GL_TEXTURE_2D, 0); glFramebufferTexture(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, depthrenderbuffer, 0); } if (createInfo.numColorTargets > 0) glDrawBuffers(createInfo.numColorTargets, DrawBuffers); else glDrawBuffer(GL_NONE); if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) std::cout << "Framebuffer Incomplete\n"; glBindFramebuffer(GL_FRAMEBUFFER, 0); width = createInfo.width; height = createInfo.height; } // ... // FBO Creation FramebufferCreateInfo gbufferCI; gbufferCI.colorFormats = gbufferCFs.data(); gbufferCI.depthFormat = FORMAT_DEPTH_32; gbufferCI.numColorTargets = gbufferCFs.size(); gbufferCI.width = engine.settings.resolutionX; gbufferCI.height = engine.settings.resolutionY; gbufferCI.renderPass = nullptr; gbuffer = graphicsWrapper->CreateFramebuffer(gbufferCI); // Bind glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo); // Draw here... // Bind to textures glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, textures[0]); glActiveTexture(GL_TEXTURE1); glBindTexture(GL_TEXTURE_2D, textures[1]); glActiveTexture(GL_TEXTURE2); glBindTexture(GL_TEXTURE_2D, textures[2]); glActiveTexture(GL_TEXTURE3); glBindTexture(GL_TEXTURE_2D, depthrenderbuffer); Here is an extract of my code. I can't think of anything else to include. I've really been butting my head into a wall trying to think of a reason but I can think of none and all my research yields nothing. Thanks in advance!

• Hi everyone, I've shared my 2D Game Engine source code. It's the result of 4 years working on it (and I still continue improving features ) and I want to share with the community. You can see some videos on youtube and some demo gifs on my twitter account.
This Engine has been developed as End-of-Degree Project and it is coded in Javascript, WebGL and GLSL. The engine is written from scratch.
This is not a professional engine but it's for learning purposes, so anyone can review the code an learn basis about graphics, physics or game engine architecture. Source code on this GitHub repository.
I'm available for a good conversation about Game Engine / Graphics Programming
• By C0dR
I would like to introduce the first version of my physically based camera rendering library, written in C++, called PhysiCam.
Physicam is an open source OpenGL C++ library, which provides physically based camera rendering and parameters. It is based on OpenGL and designed to be used as either static library or dynamic library and can be integrated in existing applications.

The following features are implemented:
Physically based sensor and focal length calculation Autoexposure Manual exposure Lense distortion Bloom (influenced by ISO, Shutter Speed, Sensor type etc.) Bokeh (influenced by Aperture, Sensor type and focal length) Tonemapping
You can find the repository at https://github.com/0x2A/physicam

I would be happy about feedback, suggestions or contributions.

• 9
• 9
• 18
• 9
• 17