I'm working on a project with C++ and glsl (4.1). I have implemented a mirror object which is a plane at height 0 that works as follow:

  1. I render the scene with a MVP computed such that the camera position is inversed (cam.x, cam.y, -cam.z)
  2. I store the rendered scene as a texture in a framebuffer
  3. I render the scene with a normal MVP
  4. 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;

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;
  • 1
    \$\begingroup\$ mirror which is not a plane What shape exactly? If you wish to support all possible shapes, cubemap is what you need. cubemap ... is way too costly Have you tried it? Is it really that slow in your case? If so, try to render cubemap with lower level of details or texture resolution. \$\endgroup\$ May 26, 2016 at 16:27
  • \$\begingroup\$ Hi ! It's a very slightly "rounded" plane with a normal map to mimic the water waves, thats why I only need the face of the cubemap that's in front of the camera :) \$\endgroup\$
    – Zick
    May 26, 2016 at 18:02


You must log in to answer this question.

Browse other questions tagged .