bricklore
bricklore

Reputation: 4165

opengl - possibility of a mirroring shader?

Until today, when I wanted to create reflections (a mirror) in opengl, I rendered a view into a texture and displayed that texture on the mirroring surface.

What i want to know is, are there any other methods to create a mirror in opengl?

And 2. can this be done lonely in shaders (e.g. geometry shader) ?

Upvotes: 3

Views: 3851

Answers (2)

gan_
gan_

Reputation: 717

There are no universal way to do that, in any 3D API i know of. Depending on your case there are several possible techniques with different downsides.

  • Planar reflections: That's what you are doing already. Note that your mirror needs to be flat and you have to clip so anything closer than the mirror ins't rendered into the texture.
  • Good old cubemaps: attach a cubemap to each mirror then sample it in the reflection direction. This works for any surface but you will need to render the cubemaps (which can be done only once if you don't care about moving objects being reflected). I don't think you can do this without shaders but only the mirror will need one. Its a very common technique as it's easy do implement, can be dynamic and fairly cheap while being easy to integrate into an existing engine.
  • Screen space ray-marching: It's what danny-ruijters suggested. Kind of like SSAO : for each pixel, sample the depth buffer along the reflection vector until you hit something. This has the advantage to be applicable anywhere (on arbitrary complex surfaces) however it can only reflect stuff that appear on screen which can introduce lots of small artifacts but it's completly dynamic and very simple to implement. Note that you will need an additional pass (or rendering normals into a buffer) to access your scene final color in while computing the reflections. You absolutely need shaders for that, but it's post process so it won't interfere with the scene rendering if that's what you fear. Some modern game engines use this to add small details to reflective surfaces without the burden of having to compute/store cubemaps.

They are probably many other ways to render mirrors but these are the tree main one (at least for what i know) ways of doing reflections.

Upvotes: 5

Danny Ruijters
Danny Ruijters

Reputation: 3420

Ray-tracing. You can write a ray-tracer in the fragment shader (every fragment follows a ray). Ray-tracers can perfectly deal with reflection (mirroring) on all kinds of surfaces.

You can find an OpenGL example here and a WebGL example including mirroring here.

Upvotes: 6

Related Questions