OpenB3D - CameraProject for entity behind camera

BlitzMax Forums/MiniB3D Module/OpenB3D - CameraProject for entity behind camera

icebeararmy(Posted 2016) [#1]
Hey :)

Is it possible to get real values with CameraProject() for an entity, that's behind the camera? It looks like it just flips the z-axis. But I actually need the screenposition of an entity that's behind the camera for something that comes from behind the camera. I'm trying to implement light-scattering with this shader: https://github.com/Erkaman/glsl-godrays
The shader needs the position of the lightsource, and I set the value like this:
CameraProject camera,EntityX(light,1),EntityY(light,1),EntityZ(light,1)
SetFloat2 (lightshader,"uScreenSpaceSunPos",ProjectedX()/x_resolution,1.0-ProjectedY()/y_resolution)

It works good as long as my camera points to the light, but as soon as the camera turns around, the light-rays are coming from the wrong side. This image shows the problem:

I need the ProjectedX and ProjectedY to have negative values I guess, but how?


angros47(Posted 2016) [#2]
OpenB3D uses gluProject, for that. The argument is discussed in details here:

https://groups.google.com/forum/#!topic/comp.graphics.api.opengl/9ZVBUbV8vnI


icebeararmy(Posted 2016) [#3]
Hmm, I guess I just disable the shader as soon as the light isn't in viewspace anymore. Thanks anyway :)


Kryzon(Posted 2016) [#4]
If you transform the location of the light to the camera local space, the new light Z should tell if the light is in front or behind the camera (or the near plane, if you subtract the near plane distance). Like, negative or positive. You can use TFormPoint for this.
Is this what you mean?

Edit: unless that's exactly what's already happening.
Can't you flip the X coordinate when the light is behind? This should give the correct position.
Depending on how the math in gluProject works for objects behind the camera you might need instead to mirror the X and Y coordinates based on the centre of the screen (i.e top-left corner becomes bottom-right and so on).


icebeararmy(Posted 2016) [#5]
Flipping like this?
1.0-(ProjectedX()/x_resolution)

That doesn't work sadly.


Kryzon(Posted 2016) [#6]
In the demo the rays disappear when you face away from the sun because the effect is caused by the blurring of the bright spot in the pixel shader -- if the bright spot isn't on the screen (like when you're facing away from it), nothing is blurred and there are no rays.
So your idea of disabling the effect (or fading it out) when the light is behind the camera should make it behave the same.

I guess in reality you could still see some rays if there's dust or fog in the place you're in, even if you're facing away from the light source, but I'm not sure how to reproduce that.