Author Topic: Problem with depth and blending (and alpha)  (Read 839 times)

Offline paulados

  • byte
  • *
  • Posts: 4
    • View Profile
Problem with depth and blending (and alpha)
« on: February 10, 2014, 07:20:52 pm »
Hi everybody,

First of all, I'm new with JPCT (I've been testing it for a week). I'm trying to create an invisible object that should make objects after it invisible too. But I do not know how to make an object transparent (If played with setTransparency but it does not work as I want).

I will explain what I want to do with an example:

   Object3D cono = Primitives.getCone(90, 4, 1.5f);
   cono.setAdditionalColor(new Color(100, 0, 0, 100));
   cono.build();
   world.addObject(cono);

   Object3D plane = Primitives.getPlane(1, 10);
   plane.rotateX(3.141596f/4.0f);
   plane.setAdditionalColor(new Color(0, 100, 0, 100));
   plane.build();
   world.addObject(plane);
   world.getCamera().setPosition(0, 0, -20);
   world.getCamera().lookAt(cono.getTransformedCenter());

In this example, I draw a red cone, and a green plane is cutting it. What I want is that the areas of the green plane that are shown become transparent and to see blue instead of green. In OpenGL I can do it (with alpha in green plane) enabling first the dept test and disabling it then and enabling gl_blend. However, when I try to use glEnable(GL11.GL_DEPTH_TEST); I get GL11 not found.

There is a way to do it? I know this question could be very basic, but I only have work with OpenGL, and I still can't understand correctly jpct, and I do not know how to access OpenGL functionality from jpct.

Finally, I'll say that I use jpct because I want to use 3d objects and jpct support them.

Thanks, and I hope anyone could help me or give any tip.


Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11776
    • View Profile
    • http://www.jpct.net
Re: Problem with depth and blending (and alpha)
« Reply #1 on: February 10, 2014, 07:44:49 pm »
I'm not sure if i understand the question correctly, but anyway...you can use gl commands in jPCT if you absolutely have to. That GL11 can't be found might be some setup or import issue on your side, but it has nothing to do with the engine. However, doing so isn't best practice, because you are changing gl state behind the engine's back that way and that can cause all kinds of strange effects. Enabling the depth test is pointless anyway, because it's enabled by the engine itself.
If you have to use gl commands that are bound to the rendering of an object, you can implement the IRenderHook interface for that purpose. You just have to make sure to clean up things after the object has been rendered.
In your case, i'm not 100% sure what you need. But maybe you can create the same effect by rendering only the plane (in another world or by switching visibility), clear the color buffer only and then render the rest.
« Last Edit: February 10, 2014, 07:47:13 pm by EgonOlsen »

Offline paulados

  • byte
  • *
  • Posts: 4
    • View Profile
Re: Problem with depth and blending (and alpha)
« Reply #2 on: February 10, 2014, 09:05:20 pm »
Hi,

Thank you for your fast answer. First of all, I do not want to use gl commands, I try to explain what I wanted to do with gl commands, that is how I did it before. Indeed, I do not know in jpct the opengl configuration (I didn't know that depth test was enable, and I do not know which blending mode is enable, if anyone).

I'll try to explain better what I want. I have added an image. What I have, is a white background, then a red square and then a circle yellow (ordered by distance). What I want is that the yellow circle to create a hole in the square and to see the white background through the circle. I hope that now it could be a bit more clear  :)

About your recomendations, I have seen IRenderHook, but I do not know anything about shaders (perhaps I should learn). Anyway, I would like to know if there is a way to make an object transparent without shaders. About your other propositions, I'm not sure if your solution of making another world would work (because the circle and the square could intersect part of them, and I suppose they cannot intersect if they are in different worlds).

On the other hand, In the case of switching visibility, I suppose that I should change the plane to setVisibility(OBJ_INVISIBLE), but in which moment. After world.addObject(plane);?

Thanks,
P.


[attachment deleted by admin]

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11776
    • View Profile
    • http://www.jpct.net
Re: Problem with depth and blending (and alpha)
« Reply #3 on: February 10, 2014, 09:13:57 pm »
My suggestion should work in your case. Putting objects in different worlds doesn't mean that they can't intersect. They are doing this in the depth buffer anyway and if you don't clear it between rendering the two worlds, you should get the effect that you want. You just have to make sure to keep the cameras in sync (or use a shared instance).

Offline AGP

  • quad
  • ******
  • Posts: 1526
    • View Profile
Re: Problem with depth and blending (and alpha)
« Reply #4 on: February 10, 2014, 09:19:53 pm »
I think he's asking the same thing that I did: if you call the following two lines, you can then call setTransparency(0...100) and get objects of opacity 0...100%. I hope that I helped.

Code: [Select]
Config.glTransparencyOffset = 0.00f;
Config.glTransparencyMul = .01f;

Offline paulados

  • byte
  • *
  • Posts: 4
    • View Profile
Re: Problem with depth and blending (and alpha)
« Reply #5 on: February 11, 2014, 05:00:18 pm »
Hi again!!!

Thanks EgonOlsen and AGP for your responses. AGP, I try your solution, but it didn't work for me. I change configuration and transparency, but the object didn't get completely transparent, I don't know why.

EgonOlsen, your solution with 2 worlds and clearColorBufferOnly worked for me. Thus, I attach the code  if it could be useful for anyone.

Thanks !!!   ;D

[attachment deleted by admin]