Skip to main content
Bumped by Community user
Disambiguating reflection tag as discussed here https://gamedev.meta.stackexchange.com/q/2552/39518
Link
DMGregory
  • 141k
  • 23
  • 258
  • 401
Tweeted twitter.com/StackGameDev/status/736764703276425216
deleted 78 characters in body
Source Link
Kromster
  • 10.7k
  • 4
  • 55
  • 67

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:

  1. 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)

  2. I store the rendered scene as a texture in a framebuffer

    I store the rendered scene as a texture in a framebuffer

  3. I render the scene with a normal MVP

    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)

    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;

I'm working on a projet 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;

I'v been looking to find a way all day and I am really stuck...

Thank you for your help !

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;
Source Link
Zick
  • 99
  • 4

Environment mapping without cubemap (need coordinates projection)

I'm working on a projet 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;

I'v been looking to find a way all day and I am really stuck...

Thank you for your help !