I have been working on a glsl shader that requires the camera position and direction of every frame in order to display some shapes on the pixels that appear to be 3D. However I couldn’t find any shader input from Panda3d that gives directly camera position and camera direction, so I tried to create them using shader translation matrices.
I am fairly new to the world of shaders, so I took a trial-and-error approach. The closest I managed to get to finding the camera position was by using the trans_view_to_world matrix to transform my vector and then exchanging the y with the z components of the vector. Here’s what it looks like in my code
vec4 camera_start = vec4(0.,0.,0.,1.);
vec4 camera_pos = trans_view_to_world * camera_start;
vec3 cp = vec3(camera_pos_4 , camera_pos_4 , camera_pos_4 );
return cp; // current camera position
Though this gives results that are seemingly correct (zooming in and out and panning seem to work rather well), the result is off. When the camera rotates the shapes shoot off in an unexpected direction and it always feels that they are further away from the camera that they should be.
Any ideas on how I can find the correct camera position would be very welcome, thanks a lot!
Hmm… The following itself won’t allow for things like zooming, I think, but if you just want the camera’s position and orientation, you should be able to get that by accessing its NodePath via a shader-input.
In your code, you would call the “setShaderInput” method of the NodePath to which the shader had been applied, passing in the name that you want the camera to have in your shader, and the camera-NodePath itself. Like this:
Hello, thanks a lot for your answer! I tried that but the problem is that this only sets up the camera at the initial position and orientation, but doesn’t update when I move around the scene. Instead, I would need it to update at runtime.
Although this topic already has an accepted solution, I just found out that it’s very easy to get the current camera position and direction vector (not complete orientation) in a GLSL shader from p3d_ViewMatrixInverse, like this: