How to enable transparency in 3d Model rendering?

I am loading a model using the ofxAssimpModelLoader addon, and I am using drawFaces() call of the API to daw the model. the entire model is drawn opaque.
some parts of the model are transparent but they are still rendered opaque, how do I enable transparency?

not sure if this is your issue, but there is ofEnableAlphaBlending()

could also be that the transparency is lost when your model is loaded.
you should make sure that your model format supports transparency when loaded with assimp/ofxAssimpModelLoader.

Yes I agree that ofEnableAlphaBlending() is something to look at. I’m not sure, but alpha blending may be not compatible with ofEnableDepthTest().

Also, don’t forget to use colors that have an alpha channel specified! You can either make a color with the alpha channel specified, or change the alpha for an existing color:

ofColor color = ofColor(255, 0, 0, 127); // 50% red
color.a = 255; // color is now 100% opaque

If the transparency is getting lost when loading the model, you can always loop thru the colors in the ofMesh and set new values for each color:

// for an ofMesh called mesh
    for(size_t i{0}; i < mesh.getColors().size(); ++i)
        ofColor color = mesh.getColor(i);
        color.a = 20; // set a new alpha from 0 - 255
        mesh.setColor(i, color);

Transparency and depth sorting is not a trivial issue. I have run into some problems before and there are quite some mentions of it in this forum. I don’t know if there are definitive fixes for this in OF, however I did try to read some things that put it into perspective, like this:


I had some issues as well figuring out transparency for my workflow, which is using blender to create fbx models. I give you a quick overview of what I did, it might help you or someone else out. Below I attach an example project based on my findings.

In Blender I set the model as transparent and give an alpha value as in the image below.

On the coding side I inheritated the ofxAssimpModelLoader class to in order to have access to the internals.

After loading an object the meshes are checked if they have alpha < 1.0.
If this is the case, the mesh blend mode is set to OF_BLENDMODE_ADD, in order to allow transparency. Maybe this is the issue you encounter in your workflow as well, for me it didn’t work out of the box between Blender and OF.

When rendering, it is important to render solid objects first and transparent objects afterwards. If you don’t do it in this order, the solid objects behind transparent objects are not drawn.
This part is quite tricky, in theory you would have to render every vertice in the correct order regarding its distance to the camera, but you might be able to find some work-arounds depending on your project to make this easier.

Make sure to also check that the meshes inside your model are in the correct order, or a transparent mesh may hide another mesh as well. (141,3 KB)

1 Like

Hi @mkrebs .
Thank you for CModel3D class.
I’m experimenting with the 3d loader sample. I loaded 2 CModel3D collada *.dae objects. But although one is transparent, what is inside does not show the other. I tried to show below what I am trying to do via FreeCAD. How can I do that?


3D and alpha is an age old issue unfortunately.

Typically the way to solve this is:

  • draw all your opaque objects first with depth sorting enabled.
  • then draw all your transparent objects with the furthest drawn first and the closest ( to the camera ) drawn last.

Looking at your image I can see that the rods and the large base will be tricky as the base is closer to the camera but depending on where you are measuring from it might actually be considered further than some of these rods.

In a case like this you may want to try and get all the meshes from the model into a vector of triangles and then sort the triangle centers individually before drawing.

Not a super easy/trivial problem :grimacing:

Hope that is helpful!

1 Like

Thank you your answer @theo
After I sended the post, I realised that I need to sort as you say. But In this case big base unit also has a bunch of through holes on there. So I also need to draw those hole’s back faces.
Keeping all the triangles in one vector seems like the right way to go.
But this is beyond my 3d grafic knowledge.
I desided to import all parts as indvidual assimp model and to sort them by trasparentcy for now.