Cubemapping?
Blitz3D Forums/Blitz3D Programming/Cubemapping?
| ||
Can someone (anyone) please explain what this cubemapping is all about and how it works/how to use it - in words of one syllable for idiots like me(!) |
| ||
My computer is too old to support cubemapping. I am unable to make the demo work! So I would appreciate if, in addition, someone post a screenshot !! |
| ||
Include "Inc_CubeMapping.bb" Graphics3D 640,480,16,2 SetBuffer BackBuffer() ; If user's graphics card does not support cubic mapping then quit example If GfxDriverCaps3D()<110 Then RuntimeError "Sorry, your graphics card does not support cubic environemnt maps." cam=CreateCamera() PositionEntity cam,0,10,-30 light1=CreateLight(2) PositionEntity light1,30,30,-30 LightRange light1,150 LightColor light1,255,255,255 light2=CreateLight(2) PositionEntity light2,-30,30,30 LightRange light2,150 LightColor light2,255,255,255 AmbientLight 150,150,150 ; Load object we will apply cubemap to - the classic teapot ;teapot=LoadMesh ("Teapot.3ds") teapot=CreateSphere(80) ScaleEntity teapot,5,5,5 PositionEntity teapot,0,9,0 EntityShininess teapot,1 EntityColor teapot,255,255,0 ; ground ground1=CreateCube() PositionEntity ground1,0,0,0 ScaleEntity ground1,40,1,40 ground2=CreateCube() PositionEntity ground2,0,0,40 ScaleEntity ground2,40,40,1 ground3=CreateCube() PositionEntity ground3,0,0,-40 ScaleEntity ground3,40,40,1 Texture=LoadTexture ("01.bmp") EntityTexture Ground1,Texture ; Create texture with color + cubic environment map + store in vram flags size=256 ;CreateCubeMap(64,teapot,2) CreateCubeMap(size,ground2,2) CreateCubeMap(size,ground3,2) While Not KeyDown(1) mxs#=mxs#+(MouseXSpeed()/5.0) mys#=mys#-(MouseYSpeed()/5.0) RotateEntity cam,mys#,-mxs#,0 MoveMouse GraphicsWidth()/2,GraphicsHeight ()/2 If KeyDown(200)=True Then MoveEntity cam,0,0,.3 ; move camera forward If KeyDown(208)=True Then MoveEntity cam,0,0,-.3 ; move camera back If KeyDown(205)=True Then MoveEntity cam,.3,0,0 ; move camera left If KeyDown(203)=True Then MoveEntity cam,-.3,0,0 ; move camera right TurnEntity teapot,0.2,0.2,0.2 UpdateCubemap() RenderWorld Flip Wend THE INCLUDE TYPE LIB ; ********************************************************************* ; * Include : Création de cubic map * ; * * ; * Version : V1.0 Par Philippe Agnisola * ; ********************************************************************* ; -------------------------- ; Type des CubeMap ; -------------------------- Type TCubeMap Field CubeCamera Field CubeTexture Field CubeEntity End Type ; ------------------------------------------------------------------------------- ; Fonction qui permet d'initialiser le cubemaping d'un objet ; ------------------------------------------------------------------------------- Function CreateCubeMap(Size,Entity,Index) Cube.TCubeMap=New TCubeMap Cube\CubeCamera=CreateCamera() Cube\CubeTexture=CreateTexture(Size,Size,1+128+256+512) Cube\CubeEntity=Entity EntityTexture Cube\CubeEntity,Cube\CubeTexture,0,Index End Function ; ------------------------------------------------------------------------ ; Fonction qui permet de rafraichir les cubes mapping ; ------------------------------------------------------------------------ Function UpdateCubemap() For Cube.TCubeMap=Each TCubeMap TmpTextureSize=TextureWidth(Cube\CubeTexture) ; -------------------------------- ; Affichage de la camera ; -------------------------------- ShowEntity Cube\CubeCamera ; --------------------------------- ; On cache l'entite attache ; --------------------------------- HideEntity Cube\CubeEntity ; ------------------------------------------------------------- ; On positionne la camera au centre de l'objet ; ------------------------------------------------------------- PositionEntity Cube\CubeCamera,EntityX#(Cube\CubeEntity),EntityY#(Cube\CubeEntity),EntityZ#(Cube\CubeEntity) CameraClsMode Cube\CubeCamera,False,True ; ------------------------------ ; Definition du viewport ; ------------------------------ CameraViewport Cube\CubeCamera,0,0,TmpTextureSize,TmpTextureSize ; ------------------ ; Face gauche ; ------------------ SetCubeFace Cube\CubeTexture,0 RotateEntity Cube\CubeCamera,0,90,0 RenderWorld CopyRect 0,0,TmpTextureSize,TmpTextureSize,0,0,BackBuffer(),TextureBuffer(Cube\CubeTexture) ; --------------- ; Face avant ; --------------- SetCubeFace Cube\CubeTexture,1 RotateEntity Cube\CubeCamera,0,0,0 RenderWorld CopyRect 0,0,TmpTextureSize,TmpTextureSize,0,0,BackBuffer(),TextureBuffer(Cube\CubeTexture) ; --------------- ; Face droite ; --------------- SetCubeFace Cube\CubeTexture,2 RotateEntity Cube\CubeCamera,0,-90,0 RenderWorld CopyRect 0,0,TmpTextureSize,TmpTextureSize,0,0,BackBuffer(),TextureBuffer(Cube\CubeTexture) ; ----------------- ; Face arriere ; ----------------- SetCubeFace Cube\CubeTexture,3 RotateEntity Cube\CubeCamera,0,180,0 RenderWorld CopyRect 0,0,TmpTextureSize,TmpTextureSize,0,0,BackBuffer(),TextureBuffer(Cube\CubeTexture) ; ------------------ ; Face dessus ; ------------------ SetCubeFace Cube\CubeTexture,4 RotateEntity Cube\CubeCamera,-90,0,0 RenderWorld CopyRect 0,0,TmpTextureSize,TmpTextureSize,0,0,BackBuffer(),TextureBuffer(Cube\CubeTexture) ; -------------------- ; Face dessous ; -------------------- SetCubeFace Cube\CubeTexture,5 RotateEntity Cube\CubeCamera,90,0,0 RenderWorld CopyRect 0,0,TmpTextureSize,TmpTextureSize,0,0,BackBuffer(),TextureBuffer(Cube\CubeTexture) ; -------------------------- ; On reaffiche l'entite ; -------------------------- ShowEntity Cube\CubeEntity ; ---------------------------- ; On cache la camera ; ---------------------------- HideEntity Cube\CubeCamera Next End Function |
| ||
Cube mapping can be used to make shiny / reflective surfaces, eg: shiny spheres, water. One way to explain it is to image a room, with 6 sides (roof,floor,and the four walls). In the middle is a shiny ball. If you look at the ball you can see a reflection of the walls. In Blitz, you would use SetCubeFace to "decorate" each of teh walls (called faces). These faces will then be reflected in the cube mapped object, eg: the shiny teapot from the demo. |
| ||
"Can someone (anyone) please explain what this cubemapping is all about and how it works/how to use it - in words of one syllable for idiots like me(!)" I'm sorry, but it's imnpossible to describe in one syllable words, but I'll try to make it simple. Cube maps are used to render reflections. Imagine you have an object that you want to be reflective. Well you know how a skybox works? Well a cubemap is like a skybox which is surrounding your model and projected onto it. In other words, if you have a skybox around your character, and a cubemap with the same textures as your skybox, as you rotate around the character with the camera you would appear to see the skybox reflected in the character's body. Cube map reflections stay oriented properly to the world unlike spherical environment maps. Now imagine that your skybox is animated. Well, your cubemap can be animated too. Now imagine that you're dynamically creating a skybox in realtime from your current viewpoint as you walk around the world. Top bottom, left right, front back... each of those you render to a texture each frame. Six 90 degree FOV square camera viewports, each 128x128. If you apply THAT to your model, then your model will appear to relect whatever is around them in the world wherever he goes. It'll look just like the Terminator chrome effect if you use high res maps. And if you use lower res or blurred maps then you can get an effect which is more like radiosity lighting, where everything near the character affects his color via reflected light. Another thing to keep in mind is that you do not have to re-render all six views erach frame. You can choose to render only the ones you think need to be updated. So if nobody will ever be able to see your object from below, you may be able to avoid rendering the bottom side of the cube map. Or, you could choose to render the maps every other frame. |
| ||
I'm sorry, but it's imnpossible to describe in one syllable words, but I'll try to make it simple. That was an exaggeration, but thanks Sswift. It all sounds very good! |
| ||
So what would be the best way to go about doing, say, cube mapped water? What sort of speed hit could I expect? |
| ||
i did cube mapped water and it didnt give me any slow down |
| ||
I just saw the original cube map demo in the gallery and it says that the source code for it (the water demo one) is included in the latest update package, can anyone find it? Jason. |
| ||
Hi Filax, I don't seem to get your demo working.. This is what I get; Any clues? John-Robin Smith |
| ||
John, how much vram you got? Try setting the Size variable to 64 or 128 IPete2. |
| ||
IPete2> Unfortunately, no big difference when I change the size variable. Something else DOES change, when I set my screen settings from hicolor (16 bits) to truecolor (24bits) See the image below. Still I don't think it's right either.. :( www.johnrobinsmith.com/example2.jpg |
| ||
that's what I get when I run it (www.johnrobinsmith.com/example2.jpg ) |
| ||
Only way I could get cubemapping to work was to load the texture with 256 flag... force it to Vmem. |
| ||
Bouncer> tried that too, but doesn't seem to help much :( |
| ||
John, I got an odd result too, but it wasnt what you've posted. Hmm hopefully someone will post a cure or a reason. IPete2. |
| ||
CameraClsMode camera,True,True did u use that command for your cube map camera? try using that command instead of the one mentioned in the example that comes with b3d |