#version 330
out vec4 fColor[2];
in vec2 texcoord;
in vec2 ScreenPosition;
uniform sampler2D tex1;
uniform sampler2D tex2;
uniform sampler2D tex3;
uniform mat4 inverse_view_projection_matrix; //Does not work, always says every fragment is at the camerapos. In fact, it doesn't actually matter what vec4 you use with this matrix, it will always return the camera position, or at least something really close to it.
uniform mat4 inverse_projection_matrix; //Works, but not on Z. produces incorrect Z values. X and Y values look approximately correct, but I'm suspicious.
uniform mat4 inverse_view_matrix; //Does not work, always says every fragment is at the camerapos. In fact, it doesn't actually matter what vec4 you use with this matrix, it will always return the camera position, or at least something really close to it.
uniform vec3 lightpos;
uniform vec3 lightcolor;
uniform vec3 camerapos;
uniform float range;
uniform float dropoff;
//far and near clip planes
uniform float jafar;
uniform float janear;
//Another thing that should work
vec4 WorldPosFromDepth(float depth) {
float z = depth * 2.0 - 1.0;
vec4 clipSpacePosition = vec4(texcoord * 2.0 - 1.0, z, 1.0);
vec4 viewSpacePosition = inverse_projection_matrix * clipSpacePosition;
//I don't actually know if this correct, and if the viewMatrix has any scaling, then this is incorrect. I have no idea how to solve this.
float depthRange = jafar - janear;
float farin = depth * depthRange + janear;
viewSpacePosition.z = farin; // Because the STUPID inverse projection matrix won't handle Z correctly! Not sure if supposed to be negative, not sure if supposed to be scaled.
//We now have the world relative to the camera. We have to move the world back to where it's supposed to be.
//(means I haven't written it yet)
vec4 worldSpacePosition = viewSpacePosition; //if I put in inverse_view_matrix * viewSpacePosition it always comes out as being at the camera position.
return worldSpacePosition;
}
void main()
{
//Grab the values from the initial opaque pass buffers
vec4 tex1_value = texture2D(tex1, texcoord);
vec4 tex2_value = texture2D(tex2, texcoord);
vec4 tex3_value = texture2D(tex3, texcoord);
//Get the Clip-Space Position (gl_Position of the fragment)
// vec4 clipSpacePos = vec4(ScreenPosition.x,ScreenPosition.y, (tex3_value.x * (jafar + janear))-janear, 1.0);
// vec4 clipSpacePos = vec4(ScreenPosition.x,ScreenPosition.y, 2.0 * tex3_value.x - 1.0, 1.0);
//Find the world position. PROJECTION MATRICES ARE NON-INVERTIBLE!!!
vec4 world_pos = WorldPosFromDepth(tex3_value.x);
//world_pos -= camerapos;
float mask = float(tex3_value.w != 0);
// fColor[0] = tex1_value;
fColor[0] = (world_pos ) * mask;
fColor[1] = tex2_value;
}