I'm working on a projetproject with C++ and glsl (4.1). I have implemented a mirror object which is a plane at height 0 that works as follow:
- I render the scene with a MVP computed such that the camera position is inversed (cam.x, cam.y, -cam.z)
I render the scene with a MVP computed such that the camera position is inversed
(cam.x, cam.y, -cam.z) - I store the rendered scene as a texture in a framebuffer
I store the rendered scene as a texture in a framebuffer
- I render the scene with a normal MVP
I render the scene with a normal MVP
- I render the mirror object with the fragment color computed as: (size being the size of the texture I rendered to the framebuffer)
I render the mirror object with the fragment color computed as: (size being the size of the texture I rendered to the framebuffer)
float _u = gl_FragCoord.x; float _v = gl_FragCoord.y ; _u = _u / size.x; _v = 1 - (_v / size.y); _uv = vec2(_u, _v); reflection = texture(tex_mirror, _uv).rgb;float _u = gl_FragCoord.x; float _v = gl_FragCoord.y; _u = _u / size.x; _v = 1 - (_v / size.y); _uv = vec2(_u, _v); reflection = texture(tex_mirror, _uv).rgb;
This implementation works well but I now need to work with a mirror which is not a plane. So I am looking for a way to lookup the same texture I store in the Framebuffer but with the texture coordinates computed from the reflected vector (i.e. find the _uv coordinates from R = reflect(view_dir, vertex_normal)).
Is it possible ? Or is there another way to do this lookup without having to compute a environment cubemap (which is way too costly since my environment is dynamic and I would have to compute 6 textures at each frame)
I have tried this but it doesn't work at all (though I think it should work):
vec3 v = normalize(view_dir); vec3 n = vertex_normal; vec4 R = MVP * vec4(reflect(v, n), 1.0); R = R/R.w; vec2 _uv = R.xy;
I'v been looking to find a way all day and I am really stuck...
Thank you for your help !
vec3 v = normalize(view_dir);
vec3 n = vertex_normal;
vec4 R = MVP * vec4(reflect(v, n), 1.0);
R = R/R.w;
vec2 _uv = R.xy;