Any way to get a mirror reflection?

BlitzMax Forums/MiniB3D Module/Any way to get a mirror reflection?

ICECAP(Posted 2011) [#1]
Hey guys, just started using minib3d, looking good so far. Just wondering if there is any way to create the Mirror that you can create in b3d.
I would use cube mapping, but holy hell does it slow down things.
So if anyone can offer a solution, I would be very pleased to hear it.

I thought maybe the extended version might be able to do it with shadars, but I have been unable to get it working. Plus I wasnt sure it was able to be compiled on mac and linx.
Downloaded it from the wiki but every time I try building it, it just complains about not being able to include maxgui (which i have).

So if anyone can help me with either of these i guess, much bowing to superiority will be done.

Last edited 2011


ICECAP(Posted 2011) [#2]
Idiot issue - double posted

Last edited 2011


ima747(Posted 2011) [#3]
Extended is quite out of date at this point, and may be officially abandoned as it was undergoing a full re-write at it's last activity... It does however work on mac and linux. As far as your compile issues, you can modify extended to compile without maxgui (I used to use it before I had maxgui quite some time ago) which may get you going. Not sure why it complains about a lack of maxgui if you have it installed... What OS are you building under? Linux has case sensitivity issues with module names...


SLotman(Posted 2011) [#4]
You can try to grab the scene render and use it as a texture (with 'BackBufferToTex'), which is slow - or you could do the blitz3d 'mirror': just mirror every entity in the level/area. It will double your geometry, but it will be fast.


Kryzon(Posted 2011) [#5]
Doubling the geometry and scaling negatively along the mirror's orientation is the way the N64 used for making mirrors for games (Banjo-Tooie comes to mind, as well as a glitch associated with this).
Although those mirrors were scarce (I think there was only one in the entire game), they did double the geometry instead of using render-targets.

You won't even have much extra geometry if your mirror is in a closed room with a few objects.
But using a render-target is much easier: place camera at the mirror's pivot, render to a texture (this is different than rendering to the backbuffer and copying it; you would have to use an OpenGL FrameBuffer object), and then update the mirror with this texture.
Choose the trade-off.

PS: OpenGL FrameBuffer 101; OpenGL FrameBuffer 201


AdamRedwoods(Posted 2011) [#6]
Interesting techniques.

Full-mesh reflection is slow-- better with higher versions of GL and FBO-- but I suppose you could also render smaller viewports and BackBufferToTex with smaller target cubemap sizes as well (64x64).... if you really needed to.


Kryzon(Posted 2011) [#7]
Good point; and using a smaller resolution texture would also get you a somewhat blurry image, so it might give a nicer look for free if that's what you're after (an oiled, dirty mirror).


ICECAP(Posted 2011) [#8]
Hey guys, thanks for the suggestions.
I'm going to keep away from using the cube mapping method, because it does slow down the performance alot, and i'm after the more casual gamer who might not have the most up to date and fast pc.
I wanted the feature basically to create a very shiny floor tile. I found shinyness isnt really that at all, its just adding specular of a perticular size.
So I guess I'll give the geometry doubling a go (but i'm not too keen on that option either, as it seems a huge overhead for such a small effect).

@Kryzon, would using OpenGLs FrameBuffer be a much faster or slower than the doubling of the geometry? I take it, its faster than using the backbuffer?


Kryzon(Posted 2011) [#9]
Using a framebuffer with an attached texture would be the official 'render-to-texture' we see a lot around.
It is definitely faster than grabbing from the backbuffer (the backbuffer being a kind of framebuffer itself) by using a CopyRect() or BackBufferToTex() in the case of miniB3D.

Reading a bit more now, that N64 case they had to double the geometry because that's the only way they could do to achieve reflection in that platform.
With OpenGL you don't need to double the geometry - you re-render it twice but with different view-points, and using the stencil buffer to crop the reflection to the mirror's area. This is known as Stencil Reflection; it's a multipass technique.

http://www.opengl.org/resources/code/samples/mjktips/Reflect.html
http://jerome.jouvie.free.fr/opengl-tutorials/Tutorial23.php
http://librairie.immateriel.fr/fr/read_book/9780596804824/ch06s06#
http://www.videotutorialsrock.com/opengl_tutorial/reflections/text.php

This might be an easier way to go without having to deal with FrameBuffers, and let's face it, it's freaking awesome.


ICECAP(Posted 2011) [#10]
Thats awesome Kryzon, I'm gonna have to take a serious look at that method.

Thanks for all your help! :D


SLotman(Posted 2011) [#11]
That stencil mirror still doubles the geometry (you have to render them twice, first with scale y*-1, then with normal scale).

The only difference is that you can define an area to clip the objects.

Still, if your camera drops below that area, or any object transverses the mirror - the effect is still ruined. (And yeah, I just implemented a test case here and saw it working... not much different from B3D mirror)

Last edited 2011


SLotman(Posted 2011) [#12]
Ouch! Even worst: apparently some Intel cards (just tested on a GMA 950) have no stencil buffer... so the objects appears below the mirror :(

Does anyone knows how to test a card for stencil buffer support???

Edit: nevermind, adding |GRAPHICS_STENCILBUFFER on graphics initialization solved the problem on the intel video.

Edit2: a small test below


Please, whoever can, try it and see if it works on your card: http://www.icongames.com.br/temp/mirror.zip

This could be a simple implementation of CreateMirror() for miniB3D. (Only caveat so far, you can only have 1 mirror on screen)

Last edited 2011


Warner(Posted 2011) [#13]
On my laptop, it doesn't show the mirror. The underground flickers a bit, but not constantly. I have an onboard SiS Mirage 3. It is a horrifying bad videocard.


SLotman(Posted 2011) [#14]
Is this your card?

And how it doesn't show 'the mirror'?
It doesn't show the objects reflected, or the mirror 'plane' itself?

(The mirror is just a cube with alpha...if stencil fails, it should just show the objects below the mirror area, no flicker)

If that is indeed your card, I wouldn't worry too much about it... that review itself says: "Not usable for 3D games", so... :P


SLotman(Posted 2011) [#15]
Anyway, here's the code I wrote:



Note that I put a small check: if the camera is below the plane, it won't render the reflection, improving the effect a little bit :)

If you want to see it ruined, just uncomment the line that moves the cone up and down to see the problem...

Last edited 2011


Warner(Posted 2011) [#16]
It does look like my card. But it supports DX9, so it might be a 3+. I never use it for gaming other than a NES emulator and some small games. However, I have no trouble running miniB3D.
It does show the plane, just not the reflected objects. So the fallback mode works.


Kryzon(Posted 2011) [#17]
I was thinking instead of re-positioning lights, meshes and other entities, to act on the camera instead: scale the camera in the negative vector of the orientation of the mirror. Then the scene will be flipped indirectly without having to re-position entities.

From what I tested, if only one axis is negatively scaled all meshes seemed to get flipped. But that is easy to fix.


SLotman(Posted 2011) [#18]
Just found one nasty bug in mini-b3d with this... just set the red cube alpha to 0.5, to see it display (or not) completely wrong while the camera rotates around...

Edit: fixed! On TGlobal.bmx, in RenderCamera function just change this - see the commented line and what I added:

Edit2: not fixed... my idea breaks blitzsky :(

Last edited 2011


AdamRedwoods(Posted 2011) [#19]
I found a few bugs with alpha sorting, not sure if it would help this situation.
http://www.blitzbasic.com/Community/posts.php?topic=88252


SLotman(Posted 2011) [#20]
No it won't... tested it too and it also breaks blitzsky.

Reading on the subject, there isn't much that can be done besides what is already there :(

Last edited 2011


ima747(Posted 2011) [#21]
Pretty cool. I'm sure this can find a usable home despite it's limitations. Great work, thanks!


Kryzon(Posted 2011) [#22]
The depth sorting problem is present even in the original Blitz3D.
Run this (in Blitz3D):



SLotman(Posted 2011) [#23]
I know - it's even worst on Blitz3D!

MiniB3D makes it a little better by sorting geometry by their positions - so for example multiple planes with alpha renders correctly, while on Blitz3D they don't.