Sign in to follow this  
Eric Shi

OpenGL encounter weird problem when turn on the color blending.

Recommended Posts

hi guys! I am a rookie in OpenGL, so, please do not laugh at me about my silly questions, aha.

 

here is the thing.

 

I need to turn on the color blending to deliver transparent effect. But weird thing happens, the front side, which is facing the light, looks not bad, like this:[attachment=22381:QQ??20140629143801.png]

 

but the back side looks awful, seems like some parts of surface are missing, latticed holes, like this:

[attachment=22382:QQ??20140629143822.png]

 

I tried to add another light on the back side, but both sides turn out to be awful with holes then.

 

Is it a issue related to lights? or texture? or depth test? because if I disable the GL_DEPTH_TEST, holes are gone, but the whole body looks awful, not the way I want it to be looked like.

 

Or it is something else? I even do not know where to get started to solve this problem, someone please help me!

 

 

 

 

For more details, now down to only one object in the scene.

 

This is the front side of the lung:

[attachment=22411:1.png]

 

This is the back side of the lung:

[attachment=22412:2.png]

 

 

BUT, if I change the light in the scene to pointing at the back side:

 

The front side:

[attachment=22413:3.png]

 

The back side:

[attachment=22414:4.png]

 

If I turn on two lights, one pointing at the front, the other pointing at the back, both sides of the object covered with meshes. If remove all lights in the scene, the object looks dark, which is obvious, and both sides of meshes remain. This makes me believe that the light is not the cause of the issue, right?

 

More angles of view, might help you guys to see the meshes more clearly:

[attachment=22415:333333.png]

 

[attachment=22416:QQ??20140630224309.png]

 

[attachment=22417:QQ??20140630224328.png]

Edited by eric_lie

Share this post


Link to post
Share on other sites

if you show us your code , we will see what is causing this trouble

we need to see something like this

 

all opengl calls [initialization and drawing)

then drawing modelitself

all glcalls after drawing

just show us your code, 

 

 

=====

try to set up only one light in the back and see if problem occurs for back of the model) (or set ambient to the same color like diffusion color)

 

its really hard to say what is causing this problem (it may be depth test, wrong order of face drawing, wrong normals for polygons etc)

Share this post


Link to post
Share on other sites

Sounds like you may have a culling problem.

 

Try turning on the depth buffer and setting cull mode to none and see what it looks like.

Share this post


Link to post
Share on other sites

The most common problem with transparency is that rendering order of faces does not match the requirements of the rendering algorithm. The simplest algorithm needs faces to be rendered in back to front order. This implies meshes to be ordered in dependence of the view, that meshes must not be concave (or else they need to be divided up if a free view is allowed), and meshes must not touch or overlap (or else z-figthing will occur).

 

As WiredCat mentioned we need more details, but also above the code level. What algorithm is used? How are the meshes organized?

 

When you have a problem with a complex scene, reducing complexity first helps to narrow down the cause. E.g. Does the problem occur even if only a single organ is rendered, ...

Edited by haegarr

Share this post


Link to post
Share on other sites
 

if you show us your code , we will see what is causing this trouble
we need to see something like this
 
all opengl calls [initialization and drawing)
then drawing modelitself
all glcalls after drawing
just show us your code, 
 
 
=====
try to set up only one light in the back and see if problem occurs for back of the model) (or set ambient to the same color like diffusion color)
 
its really hard to say what is causing this problem (it may be depth test, wrong order of face drawing, wrong normals for polygons etc)

 


I set ambient to the same color like diffusion color, problem remains.

If I set a light from the back side, the back side of the model looks fine but the front face becomes the weird side instead, and if turn out all lights in the scene, then the whole model is covered with meshes. I thought the light can solve this problem someway somehow at first, but when I turn on all the lights, both in front and back, both sides of meshes remain.

So now I think may be the light is not the key point here.

My whole solution is a little bit complex, it takes time for me to the extract all OpenGL parts out, I will paste my codes here later.

Share this post


Link to post
Share on other sites

Sounds like you may have a culling problem.

 

Try turning on the depth buffer and setting cull mode to none and see what it looks like.

 

you mean glDisable(GL_CULL_FACE) ?

Share this post


Link to post
Share on other sites

The most common problem with transparency is that rendering order of faces does not match the requirements of the rendering algorithm. The simplest algorithm needs faces to be rendered in back to front order. This implies meshes to be ordered in dependence of the view, that meshes must not be concave (or else they need to be divided up if a free view is allowed), and meshes must not touch or overlap (or else z-figthing will occur).

 

As WiredCat mentioned we need more details, but also above the code level. What algorithm is used? How are the meshes organized?

 

When you have a problem with a complex scene, reducing complexity first helps to narrow down the cause. E.g. Does the problem occur even if only a single organ is rendered, ...

 

All organ models are 3DS files which generated by 3DS MAX, and be rendered into the scene from inside to outside, but the thing is, I am rendering a human body here, they can not be simply organized by inside and outside, or front and back, right? Just like you said, I need a free view, I need the whole body can be rotated so that you can see the body from any angle of view. So, at this point, organ A covers organ B (but you still can see organ B through organ A because of transparency), but if rotated around, organ B covers organ A instead. 

 

But if I disable the depth test, if organ A is rendered last, organ A is always covering organ B no matter which side you look at the model, then the whole body looks creepy and weird. It is totally not what I want. The funny part is, now meshes are gone.

 

What's more, if set all color alpha value to 1, which means no transparency at all, and enable the depth test back on, everything looks fine.

 

Like I said to WiredCat, I will paste codes up here after I review all over my solution.

 

Problem still remains even if only one organ in the scene.

 

thanks a lot!

Edited by eric_lie

Share this post


Link to post
Share on other sites
Since you appear to be in the medical industry (a colleague of jenny_wui’s?) where performance is not the main ingredient, you should research depth peeling.
 
http://developer.download.nvidia.com/SDK/10/opengl/src/dual_depth_peeling/doc/DualDepthPeeling.pdf
http://gamedevs.org/uploads/interactive-order-independent-transparency.pdf
 
 
L. Spiro Edited by L. Spiro

Share this post


Link to post
Share on other sites
The following is not meant to turn you away, but since you described yourself as a "rookie in OpenGL", I think it should be pointed out to you to prevent any misconceptions:

OpenGL is a thin (most will argue still too thick) api towards the GPU, providing you with the most basic interface to render and shade triangles. You may have noticed, that it doesn't provide any means to load models or textures. The newer versions of OpenGL don't even support lighting out of the box. The idea is that you implement those things on top of OpenGL. This holds for transparency as well. Transparency is not as simple as enabling blending, you have to implement some form of algorithm for it on top of OpenGL. "Depth peeling", as suggested by L.Spiro, is on of those techniques. Splitting the model into parts, whose rendering order gets determined by the camera position (what haegarr suggested), is another one. There are quite a few more.

Using OpenGL (or, for that matter, OpenGL ES, Direct3D, Metal, Mantle, ...) means that you will have to write a lot of code around it,
as these are not intended to be full fledged rendering engines.

Share this post


Link to post
Share on other sites

so less details, but anyway try this:

divide whole model to different organs, then try to render every organ where you begin from those that are deep in the body and you end with drawing lungs

 

also glBlendFunc needs to be set properly (and sometimes glColor4f <- alpha value)

 

we also dont know if your textures have alpha channel but i guess they don't)

 

At first you need to draw parts that are not transparent and then you draw transparent organs in proper order.

 

 

if you manage to sort your faces you will get almost the same effect like disabling GL_DEPTH_TEST but you'll see properly displayed model

 

 

this also try to lower z_near value (in glFrustum) 

 

or scale in ex lungs so lungs faces wont touch other organs

 

 

 

=====================

anyway youll have to sort faces or that would be far way faster and easier -organs

 

 

this is what ahppens when you sort faces.

yu.jpg

 

 

 

 

one that i can say is i see there z fighting problem or wrong face order or wrong blending factors

Edited by WiredCat

Share this post


Link to post
Share on other sites

Looking at those images, I don't think sorting the meshes will help.

 

It looks like some of them actual wrap around the other sub meshes.

 

If that is the case then you have a much more complex problem.

 

I would suggest using volume rendering, but as you stated you a newb at OpenGL, that's going to be a big issue for you. You might be able to just grab an existing OpenGL volume renderer and use it without understanding it though.

 

Have a look at http://idav.ucdavis.edu/~okreylos/ResDev/VolVis/MainPage.html

 

If that seems too much for you, then see if you can split the meshes that cause problems into two, then sort them before rendering.

 

Hope this gives you some ideas.

Share this post


Link to post
Share on other sites

 

you mean glDisable(GL_CULL_FACE) ?

never diable cullling, instead reverse what "front face" is.

 

You have a well defined geometry (veru well :)) so:

 

gl.enable(this.m_pGL.CULL_FACE); // yes, I wish to cull
      gl.cullFace(this.m_pGL.BACK); // I want to cull back face
     gl .frontFace(this.m_pGL.CCW); // the front face is counter clock wise face

 

 

... so if after this you change fronts face difintion to

 

  gl .frontFace(this.m_pGL.CW);

 

you will establish front faces as the back faces, thus getting culled front faces "the back faces"

 

and back faces as front faces, being processed, with their vertex attributes in take

Share this post


Link to post
Share on other sites

mind yet, that if you shade back faces with general set up of lighting or other parameters, you may get stranger results, unless you realy understand the geometry with its attributes. (you cannot observe back faces through culled front faces, if you get what I mean- very metaphysical stuff)

Share this post


Link to post
Share on other sites

was this scaned from ultra sound device, or an arbitrary artificial model being?

 

It is just a 3DS model generated by 3DS MAX.

Share this post


Link to post
Share on other sites

so did you make any tests?

 

I am working on it. I am sorry to say that what you mentioned did not solve my problem.

 

Now it seems like not that easy to solve this issue. Some of you guys recommend me to resort all objects in a proper way, but even if only a single organ displayed in the scene, the problem still remains. And the thing is, in my case, each object is mapping to other stuff, resorting means changing everything. It does take time to solve this. Seems like a major problem to a rookie like me. 

 

I think Ohforf sake is right, Transparency is not as simple as enabling color blending, maybe this is the key point here!

 

Thanks anyway!smile.png smile.png

Share this post


Link to post
Share on other sites


Now it seems like not that easy to solve this issue. Some of you guys recommend me to resort all objects in a proper way, ...

Yep. Or else you try depth peeling as is suggested above by LS.

 


... but even if only a single organ displayed in the scene, the problem still remains. ...

Maybe this is because of the already mentioned concavity of the meshes? Question is, if you simplify the scene down to a single organ, do you have a chance to notice whether the problem occurs only if you look through a concavity. If so, then we are on the right track when suspecting the drawing order. A solution then will be to use sub-meshes.

 
But if concavity is not the cause, then we need to investigate further.
 


… And the thing is, in my case, each object is mapping to other stuff, resorting means changing everything. It does take time to solve this.  ...

This isn't a problem solely related to yours. It is common in game engines and elsewhere. And hence there is a solution :)

 

It is possible to have more than a single order on objects. Notice that it is recommended to have several organizing structures, one for each task to do. It is absolutely fine to have a logical organization of the objects, a spatial organization (if collision or proximity is an issue), a render order, and perhaps more. Don't stuck with the über scene graph approach, or you will be lost sooner or later!

 

For example, you iterate the scene description and detect all objects that need to be rendered. You insert each object into a list (which is emptied before the scene is iterated). After finishing, you sort the list by some criterion, in your case using the distance from the current camera. Object rendering then is done in the order given by the list. So rendering has no influence on other aspects of object organization, and nevertheless is done in the required way.

 


I think Ohforf sake is right, Transparency is not as simple as enabling color blending, maybe this is the key point here!

Absolutely. 

Share this post


Link to post
Share on other sites

 

Now it seems like not that easy to solve this issue. Some of you guys recommend me to resort all objects in a proper way, but even if only a single organ displayed in the scene, the problem still remains. 

 

can you upload somewhere this model i want to see it (one organ that is not properly displayed), maybe some face normals are reversed

Edited by WiredCat

Share this post


Link to post
Share on other sites

 

 

Now it seems like not that easy to solve this issue. Some of you guys recommend me to resort all objects in a proper way, but even if only a single organ displayed in the scene, the problem still remains. 

 

can you upload somewhere this model i want to see it (one organ that is not properly displayed), maybe some face normals are reversed

 

 

you mean the 3DS model? I am sure that all my 3DS model are accurate. 

 

Is there any possibility that when I render a 3DS model into an OpenGL scene, the normals are reversed by some inconspicuous mistakes somehow?

 

I upload more detail images, you can check it out.rolleyes.gif rolleyes.gif

Share this post


Link to post
Share on other sites


Yep. Or else you try depth peeling as is suggested above by LS.

 

I am reading those articles she gives to me, I think they would be helpful.

 


if you simplify the scene down to a single organ, do you have a chance to notice whether the problem occurs only if you look through a concavity.

 

I upload some detail images.

 


This isn't a problem solely related to yours. It is common in game engines and elsewhere. And hence there is a solution
 
It is possible to have more than a single order on objects. Notice that it is recommended to have several organizing structures, one for each task to do. It is absolutely fine to have a logical organization of the objects, a spatial organization (if collision or proximity is an issue), a render order, and perhaps more. Don't stuck with the über scene graph approach, or you will be lost sooner or later!
 
For example, you iterate the scene description and detect all objects that need to be rendered. You insert each object into a list (which is emptied before the scene is iterated). After finishing, you sort the list by some criterion, in your case using the distance from the current camera. Object rendering then is done in the order given by the list. So rendering has no influence on other aspects of object organization, and nevertheless is done in the required way.

 

This is quite inspiring, I will try it out! thanks!biggrin.png

Share this post


Link to post
Share on other sites

 

haegarr, on 30 Jun 2014 - 10:45 AM, said:

snapback.png


This isn't a probl.......he required way.

 

This is quite inspiring, I will try it out! thanks!biggrin.png

 

 

few post earilier you said that you already checked this solution.

You lie and post another images that say nothing.

I cut out of this topic,

Share this post


Link to post
Share on other sites

I am reading those articles she gives to me, I think they would be helpful.

She is actually a he. Just pointing out.

The problem you are seeing stems from the nature of your blend operation. Every surface slightly darkens what lies behind them, which allows the user to percieve an order of the surfaces, but also means that the order of mixing together the colors is important. There are other blend operations, like additive blending (glBlendFunc(GL_ONE, GL_ONE);) where the order doesn't matter. Can you get away with additive blending?

If not, mixing the colors of different surfaces seen on top of each other must be performed in the correct order: Mixing from back to front. As you already know, there are multiple, very different approaches, to achieving this. Some require you to write shaders, some only require you to do s.th. with your meshes, some work in all cases, some only if you can make certain geometric assumptions for your modell or allow for certain artifacts.

For example, consider a single convex object. If you render the back facing triangles first, their color will get blended with the background color. If you now render the front facing triangles, their color will get blended with the result of the previous blending operation and you get the correct result. So for a single convex object, you can get away with the simple trick of rendering the back faces first, and then the front faces which will, for each pixel, always be closer to the camera. Automatic and perfect back to front sorting. Sadly, you have more than one object, and those intestines are far from convex.

So exploiting convexity is out of the question, at least directly. Which leaves you with 2 questions: Do you want "perfect" results? Do you want to use shaders, or do you prefer a geometric solution?

Haegarr is suggesting a geometric solution: Cut your scene into pieces (or your objects into subobjects) and sort them back to front. But what pieces? Is it enough to cut them into convex pieces? The answer is no, just cutting them into convex pieces will not guarentee artifact free pictures. For example, assume that you make one subobject out of each triangle (a single triangle is convex). Then there are certain situations where you can not find an ordering for the triangles, that lead to a back to front rendering. For example, consider these 4 quads (also works with 3 triangles), where each quad has one end in front of the previous but behind the next.
  DD        BB
AAAAAAAAAAAABBAA
AAAAAAAAAAAABBAA
  DD        BB
  DD        BB
  DD        BB
  DD        BB
CCDDCCCCCCCCCCCC
CCDDCCCCCCCCCCCC
  DD        BB
For these, no correct rendering order exists. At least one of the quads has to be split. This is the point, where you have to decide, if you can live with the occasional artifact, or if you want to descend into the ugliness of splitting faces, and inserting them into a BSP-tree or s.th. similar.

If you go the geometric route, I would follow haegarr's advise: Automatically cut the objects into smaller, approximately convex and not elongated pieces, sort those pieces back to front then render each object twice, first by culling away the front faces (only rendering back faces) then by culling away the back faces (only rendering front faces). Then take a look at it and decide, if the remaining artifacts are acceptable, preferably while experimenting with different parameters for your cutting algorithm. However, keep in mind, that the geometric sorting approach can get very ugly, so if you have the compute power to spare, and the time to read further into OpenGL, then Depth-peeling and other "order independent transparency" techniques can be a reasonable choice.

Share this post


Link to post
Share on other sites


few post earilier you said that you already checked this solution.
You lie and post another images that say nothing.
I cut out of this topic,

 

I am really really sorry if I said or did something to upset you. If so, I have to say I definitely didn't mean it! there must be some misunderstanding between us.sad.png

I deeply appreciate your help here, why would I lie about anything?

 

 

 


try to set up only one light in the back and see if problem occurs for back of the model) (or set ambient to the same color like diffusion color)

 

I tried, problem does still remain. I already upload the images.

 


divide whole model to different organs, then try to render every organ where you begin from those that are deep in the body and you end with drawing lungs

 

I have not tried this yet, as I said before, my objects are all mapping other stuff, I need to figure it out how to do it in a proper way before I do any modification on models.

 


also glBlendFunc needs to be set properly (and sometimes glColor4f <- alpha value)

 

I tried a couple in glBlendFunc, and also tried change the alpha value. If I use a lower value, like 0.3, the meshes looks thinner but still exist.

 


we also dont know if your textures have alpha channel but i guess they don't)

 

Yes, my textures have no alpha channel, I got bitmap files, I just load the texture by gluBuild2DMipmaps. If there has any alpha channel in texture, I don't know where to modify it.

 


At first you need to draw parts that are not transparent and then you draw transparent organs in proper order.

 

This is still about resorting, I have to say again, I will try it, since modifying my model is a little bit complex, I prefer to try other way first. And I have no non-transparent objects in the scene.

 


this also try to lower z_near value (in glFrustum) 

 

I use gluPerspective, the near value is already 0.01f.

 


anyway youll have to sort faces or that would be far way faster and easier -organs
 
 
this is what ahppens when you sort faces.

 

I will try it later.

 

 

 

 

 

Again, I am really sorry.

Share this post


Link to post
Share on other sites

I use gluPerspective, the near value is already 0.01f.

A low near value is the main reason behind z-fighting (which manifests as flickering between objects near each other).
Always use the highest value possible that does not cause visual artifacts with near-clipping.
If the far plane is at 1,000.0 for example then a good near might be 10.0 or 5.0.


L. Spiro

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this  

  • Partner Spotlight

  • Forum Statistics

    • Total Topics
      627638
    • Total Posts
      2978327
  • Similar Content

    • By xhcao
      Before using void glBindImageTexture(    GLuint unit, GLuint texture, GLint level, GLboolean layered, GLint layer, GLenum access, GLenum format), does need to make sure that texture is completeness. 
    • By cebugdev
      hi guys, 
      are there any books, link online or any other resources that discusses on how to build special effects such as magic, lightning, etc. in OpenGL? i mean, yeah most of them are using particles but im looking for resources specifically on how to manipulate the particles to look like an effect that can be use for games,. i did fire particle before, and I want to learn how to do the other 'magic' as well.
      Like are there one book or link(cant find in google) that atleast featured how to make different particle effects in OpenGL (or DirectX)? If there is no one stop shop for it, maybe ill just look for some tips on how to make a particle engine that is flexible enough to enable me to design different effects/magic 
      let me know if you guys have recommendations.
      Thank you in advance!
    • By dud3
      How do we rotate the camera around x axis 360 degrees, without having the strange effect as in my video below? 
      Mine behaves exactly the same way spherical coordinates would, I'm using euler angles.
      Tried googling, but couldn't find a proper answer, guessing I don't know what exactly to google for, googled 'rotate 360 around x axis', got no proper answers.
       
      References:
      Code: https://pastebin.com/Hcshj3FQ
      The video shows the difference between blender and my rotation:
       
    • By Defend
      I've had a Google around for this but haven't yet found some solid advice. There is a lot of "it depends", but I'm not sure on what.
      My question is what's a good rule of thumb to follow when it comes to creating/using VBOs & VAOs? As in, when should I use multiple or when should I not? My understanding so far is that if I need a new VBO, then I need a new VAO. So when it comes to rendering multiple objects I can either:
      * make lots of VAO/VBO pairs and flip through them to render different objects, or
      * make one big VBO and jump around its memory to render different objects. 
      I also understand that if I need to render objects with different vertex attributes, then a new VAO is necessary in this case.
      If that "it depends" really is quite variable, what's best for a beginner with OpenGL, assuming that better approaches can be learnt later with better understanding?
       
    • By test opty
      Hello all,
       
      On my Windows 7 x64 machine I wrote the code below on VS 2017 and ran it.
      #include <glad/glad.h>  #include <GLFW/glfw3.h> #include <std_lib_facilities_4.h> using namespace std; void framebuffer_size_callback(GLFWwindow* window , int width, int height) {     glViewport(0, 0, width, height); } //****************************** void processInput(GLFWwindow* window) {     if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)         glfwSetWindowShouldClose(window, true); } //********************************* int main() {     glfwInit();     glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);     glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);     glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);     //glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);     GLFWwindow* window = glfwCreateWindow(800, 600, "LearnOpenGL", nullptr, nullptr);     if (window == nullptr)     {         cout << "Failed to create GLFW window" << endl;         glfwTerminate();         return -1;     }     glfwMakeContextCurrent(window);     if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))     {         cout << "Failed to initialize GLAD" << endl;         return -1;     }     glViewport(0, 0, 600, 480);     glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);     glClearColor(0.2f, 0.3f, 0.3f, 1.0f);     glClear(GL_COLOR_BUFFER_BIT);     while (!glfwWindowShouldClose(window))     {         processInput(window);         glfwSwapBuffers(window);         glfwPollEvents();     }     glfwTerminate();     return 0; }  
      The result should be a fixed dark green-blueish color as the end of here. But the color of my window turns from black to green-blueish repeatedly in high speed! I thought it might be a problem with my Graphics card driver but I've updated it and it's: NVIDIA GeForce GTX 750 Ti.
      What is the problem and how to solve it please?
  • Popular Now