PDA

View Full Version : mirror using shaders



SergeVrt
12-28-2003, 12:47 PM
Hi,
for some reasons ( I use some rendering code which I could not change and even look at ( as the project separated on several stages and I could access only limited parts of code othe goes for me only as libs )) ) I have problems using way to implement mirror using glScale with -1 as one of param or using transform matrix ( which reflects scene on plane but almost the same way - scaling world by - 1 ) approaches ( I cannot use this as there is a bug somewhere in the code ( I could not change) and this bug leads that calls to glCullFace give no expected result ( the objects on scene are not properly culled).

a bit complicated situation... http://www.opengl.org/discussion_boards/ubb/wink.gif

but I could use shaders.

what I think of as possible approaches ( though did not tried some)

scaling not in main Gl/C code but in a vertex shader
but here as face culling happens in rendering pipeling after vertex shader is applied actually seems this would not give me advantage if I scale -1 in vertex shader...( maybe there is somehow a way to change winding order in shader http://www.opengl.org/discussion_boards/ubb/smile.gif ?)

also possibly I could use pbuffers and then flip texcoords to achive 'mirror' effect ( or apply pixel shader when using pbuffer content in such way as ( in pseudo code)

float2 texCoord = In.texCoord;
texCoord.x = 1- texCoord.x;
float4 c = tex2D( texture0, texCoord);
return c;//return COLOR


but once again here there some problems with my mentioned code - preferably I should not use pbuffers....

finally I can do ( and it works) rendering to
framebuffer and then glCopyTexSubimage something like in a sample here http://www.geocities.com/dimi_christop/OGLDemos/ogldemos.html

I tried this and actually it is a bit too slow ( also I did not use glScale rather I used flipping of coords of the given texture like explainied here http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnopen/html/msdn_gl7.asp
( Figure 9)

so what are other possible options to have mirrors ( maybe shader based) and are there some such examples
(which preferably do not use pbuffers or glCopyTexSubimage) and where I could avoid that bugs which affect culling after using glScale ( -1 ) in my code on the code I could not manage or fix ( which is not under my control..)



[This message has been edited by SergeVrt (edited 12-28-2003).]

JanHH
12-28-2003, 03:52 PM
I don't see why the solution with glCopyTexSubimage should be too slow, or rather, how faster solutions could look. I think you have to render the scene upside-down (or whatever) to get the mirror image, and the tex-copy and the drawing of an orthogonal quad to display it does not cost much performance (I think). If you know that only cetain parts of the scnee can be visible in the mirror image at all, you might render only thos upside-down to get the mirror texture.

Probably this answer does not really help you http://www.opengl.org/discussion_boards/ubb/wink.gif.

Jan

dorbie
12-29-2003, 08:37 AM
I don't see how a shader helps you do a mirror except for the mirror matrix op which you can do anyway.

A mirror implementation requires higher level support that can't be encapsulated in a shader and in fact isn't greatly helped by a shader, unless you want a more sophisticated computation of the reflection vector in which case you would use a texture based approach and some kind of shader.

[This message has been edited by dorbie (edited 12-29-2003).]

jwatte
12-29-2003, 12:28 PM
Mirror algorithm that works with any kind of black rendering box that allows you to get access to the back buffer (for CopyTexSubImage), lets you control SwapBuffers (for the multi-passing) and lets you control the "camera" (view part of modelview):

Render the scene from the point of view of the mirrored camera, WITHOUT any mirroring/winding change. Copy to texture. Then render the mirror in the "correct" scene, and texture it with the texture -- with the texture coordinates applied such that the correct, mirrored image appears.

SergeVrt
12-29-2003, 12:42 PM
Originally posted by jwatte:
Mirror algorithm that works with any kind of black rendering box that allows you to get access to the back buffer (for CopyTexSubImage), lets you control SwapBuffers (for the multi-passing) and lets you control the "camera" (view part of modelview):

Render the scene from the point of view of the mirrored camera, WITHOUT any mirroring/winding change. Copy to texture. Then render the mirror in the "correct" scene, and texture it with the texture -- with the texture coordinates applied such that the correct, mirrored image appears.

OK I think I will follow this way. so thanks for answers.
I