Applying a semi-transparent texture to a node. Please please help

Hello!

Firstly I apologise if this is a simple question that has been answered many many times - I have spent the past few hours trawling the forum and google trying to piece together a working solution but am yet to have much success. Please can someone help me out?

I am trying to make panes of glass in my environment - all the glass objects are attached to a node called "glassNode". Below you can see the code that I am running to try and make all the objects attached to the "glassNode" transparent:


      {
         glassNode.setRenderQueueMode(Renderer.QUEUE_TRANSPARENT);
         BlendState tempBlendState = DisplaySystem.getDisplaySystem().getRenderer().createBlendState();
         tempBlendState.setBlendEnabled(true);
         tempBlendState.setSourceFunction(BlendState.SourceFunction.SourceAlpha );
         tempBlendState.setDestinationFunction(BlendState.DestinationFunction.OneMinusSourceAlpha);
         tempBlendState.setTestEnabled(true);
         tempBlendState.setTestFunction(BlendState.TestFunction.Always);
         tempBlendState.setEnabled(true);
         
         ZBufferState tempZBuffer = DisplaySystem.getDisplaySystem().getRenderer().createZBufferState();
         tempZBuffer.setEnabled(true);
         tempZBuffer.setWritable(false);
         tempZBuffer.setFunction(ZBufferState.TestFunction.EqualTo);
         glassNode.setRenderState(tempZBuffer);
         glassNode.setRenderState(tempBlendState);
         glassNode.updateRenderState();
      }


The code above has been pieced together from multiple code examples I've found - I know that I'm trying to apply multiple Render States to the same node, can I even do this?
I really need somebody to help me sort through the code I don't need. If you could also explain exactly what the ZBuffer object is doing I would really appreciate it.
Many thanks in regards,
Dave.

Yes, everything I see there looks correct; and multiple renderstates (different render states) are fine.

You could just apply them to the 'parent' node once though…



ZBuffering, simply put, is a data-set corresponding to the scene (in the gfx card itself); where 'faces' that are closer to the viewer are given a lower number (between 0-1).

http://en.wikipedia.org/wiki/Z-buffer  <- more info

Hi basixs,

Thanks for your speedy reply dude. The code I posted above half works…let me explain…

If I apply it to a box object I've made, the face closest to the camera view becoming transparent but the corresponding face on the other side of the box is still opaque. This means that if I've made a window from a box object I still cant see completely through the Box and out to the surrounding skybox. Can you suggest any explanation on how to resolve this?

Thanks again,

Dave

I hope it helps.


MaterialState.setMaterialFace(MaterialState.MaterialFace.FrontAndBack);


Hello again,

Thank you for your reply Mulova. I've tried to implement your suggestion and now have the following code:


   nodeInput.setRenderQueueMode(Renderer.QUEUE_TRANSPARENT);
         BlendState tempBlendState = DisplaySystem.getDisplaySystem().getRenderer().createBlendState();
         tempBlendState.setBlendEnabled(true);
         tempBlendState.setSourceFunction(BlendState.SourceFunction.SourceAlpha );
         tempBlendState.setDestinationFunction(BlendState.DestinationFunction.OneMinusSourceAlpha);
         tempBlendState.setTestEnabled(true);
         tempBlendState.setTestFunction(BlendState.TestFunction.Always);
         tempBlendState.setEnabled(true);
         nodeInput.setRenderState(tempBlendState);
         
         ZBufferState tempZBuffer = DisplaySystem.getDisplaySystem().getRenderer().createZBufferState();
         tempZBuffer.setEnabled(true);
         tempZBuffer.setWritable(false);
         tempZBuffer.setFunction(ZBufferState.TestFunction.EqualTo);
         nodeInput.setRenderState(tempZBuffer);
         
         MaterialState tempMaterialState = DisplaySystem.getDisplaySystem().getRenderer().createMaterialState();
         tempMaterialState.setEnabled(true);
         tempMaterialState.setMaterialFace(MaterialState.MaterialFace.FrontAndBack);
         nodeInput.setRenderState(tempMaterialState);
         nodeInput.updateRenderState();


Unfortunately I'm still having the same issues - the face of the box closest to the camera is transparent but the outer face is still opaque. Can anyone else help to shed light on the problem?
Once again many thanks in advance,
Dave.
EDIT: BTW, if it makes any difference the object does already have a texture applied to it. Perhaps this makes a difference? I've included the code that applies the texture below. Please note, the texture is applied and then I try to make the Box transparent using the code above.


               URL imgURL = null;
      imgURL=SetupRoom.class.getClassLoader().getResource(pathInput);
      Texture tempTexture = TextureManager.loadTexture(imgURL);
      tempTexture.setWrap(WrapMode.Repeat);
      tempTexture.setScale(scale);
      Renderer tempRenderer = DisplaySystem.getDisplaySystem().getRenderer();
      TextureState tempTextureState = tempRenderer.createTextureState();
      tempTextureState.setTexture(tempTexture);
      nodeInput.setRenderState(tempTextureState);

The problem is that all the faces of the box are part of the same mesh, thus cannot be sorted (by jME) to get the correct transparency.  Try creating a box out of 6 planes, that should do it :slight_smile: