Cube Mapping Question

Blitz3D Forums/Blitz3D Programming/Cube Mapping Question

wmaass(Posted 2003) [#1]
This is probably a really easy answer to my question so forgive my ignorance. I played around with the teapot, camel, ufo, cactus demo and tired to just substitute the teapot with a body of water shaped thingy, see...



As you can see the reflection doesn't look right. I tried just a squished cube and got roughly the same issue. Can cube mapping help me do some realistic looking water? I've seen Robs demo, any idea what I'm NOT doing?


Ross C(Posted 2003) [#2]
Hey, are you using a sphere map for this? If so, you'll never really get it to look right. A cube map is cool, cause it's a 6 sided texture. When you load it in you got to arrange the texture so it's in a horizontal strip, with 6 images, each image in a ^2 ie. 32, 64 or 128 or 256. It's really good to represent the world around it. Look at the command for SetCubeFace, for updating the cubemap in real time.


wmaass(Posted 2003) [#3]
Actually other than me changing the teapot to a puddle, this is straight from the example in the new docs -- so it is a cube map I believe that also uses SetCubeFace.


Ken Lynch(Posted 2003) [#4]
You might need to play about with the position and zoom settings of the cube map camera to get it looking right.


Neochrome(Posted 2003) [#5]
i dont understand the Cube Mapping at all and have no idea what to use it for?


wmaass(Posted 2003) [#6]
Good point about the cube map camera pos. I tried parenting it to the main camera but I guess thats not a good idea. I'll try some other things but if ANYONE has a clue please post.


Ross C(Posted 2003) [#7]
@neomancer
ok, imagine you have a skybox. It has six sides to it. Now imagine you could texture an entity, for simplicity say a cube, by wrapping the skybox around the cube. That's basically what it is. But it moves according to the camera too, just like a sphereical reflexion map.

@wmaass

The teapot demo only uses a spheremap, so you might wanna try applying a cube map to see if it's any better.


wmaass(Posted 2003) [#8]
You sure? Here is the code.

; SetCubeFace Example
; -------------------

width=640
height=480
depth=0
mode=0

Graphics3D width,height,depth,mode
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,-10

; Create separate camera for updating cube map - this allows us to manipulate main camera and cube camera which avoids any confusion
cube_cam=CreateCamera()

light=CreateLight()
RotateEntity light,90,0,0

; Load object we will apply cubemap to - the classic teapot
;teapot=LoadMesh("media/laketest.x")
teapot=CreateCube()


ScaleEntity teapot,5,.05,5
;EntityAlpha teapot,.5
PositionEntity teapot,0,.5,0
PositionEntity cube_cam,3,1,3

; Create some scenery

; ground
ground=CreatePlane()
EntityColor ground,168,133,55
ground_tex=LoadTexture("media/sand.bmp")
ScaleTexture ground_tex,10,10
EntityTexture ground,ground_tex

; sky
sky=CreateSphere(24)
ScaleEntity sky,500,500,500
FlipMesh sky
EntityFX sky,1
sky_tex=LoadTexture("media/sky.bmp")
EntityTexture sky,sky_tex

; cactus
cactus=LoadMesh("media/cactus2.x")
FitMesh cactus,-5,0,-5,2,6,.5

; camel
camel=LoadMesh("media/camel.x")
FitMesh camel,5,0,-5,6,5,4

; Load ufo to give us a dynamic moving object that the cubemap will be able to reflect
ufo_piv=CreatePivot()
PositionEntity ufo_piv,0,15,0
ufo=LoadMesh("media/green_ufo.x",ufo_piv)
PositionEntity ufo,0,0,10

 ; Create texture with color + cubic environment map + store in vram flags
tex=CreateTexture(256,256,1+128+256)

 ; Apply cubic environment map to teapot
EntityTexture teapot,tex

While Not KeyDown(1)

	; Control camera
	
	; mouse look
	
	mxs#=mxs#+(MouseXSpeed()/5.0)
	mys#=mys#+(MouseYSpeed()/5.0)

	RotateEntity cam,mys#,-mxs#,0
	
	campyaw#=EntityYaw#(cam)
	RotateEntity cube_cam,0,campyaw#,0

	MoveMouse width/2,height/2

	; move camera forwards/backwards/left/right with cursor keys
	
	If KeyDown(200)=True Then MoveEntity cam,0,0,.2 ; move camera forward
	If KeyDown(208)=True Then MoveEntity cam,0,0,-.2 ; move camera back

	If KeyDown(205)=True Then MoveEntity cam,.2,0,0 ; move camera left
	If KeyDown(203)=True Then MoveEntity cam,-.2,0,0 ; move camera right
	
	
	
	; Turn ufo pivot, causing child ufo mesh to spin around it (and teapot)
	TurnEntity ufo_piv,0,2,0

	; Update cubemap
	UpdateCubemap(tex,cube_cam,teapot)

	RenderWorld
	
	Text 0,0,"Use mouse to look around"
	Text 0,20,"Use cursor keys to change camera position"
	Text 0,30,campyaw#
	
	Flip

Wend


Function UpdateCubemap(tex,camera,entity)

	tex_sz=TextureWidth(tex)

	; Show the camera we have specifically created for updating the cubemap
	ShowEntity camera
	
	; Hide entity that will have cubemap applied to it. This is so we can get cubemap from its position, without it blocking the view
	HideEntity entity

	; Position camera where the entity is - this is where we will be rendering views from for cubemap
	PositionEntity camera,EntityX#(entity),EntityY#(entity),EntityZ#(entity)

	CameraClsMode camera,False,True
	
	; Set the camera's viewport so it is the same size as our texture - so we can fit entire screen contents into texture
	CameraViewport camera,0,0,tex_sz,tex_sz

	; Update cubemap

	; do left view	
	SetCubeFace tex,0
	RotateEntity camera,0,90,0
	RenderWorld
	CopyRect 0,0,tex_sz,tex_sz,0,0,BackBuffer(),TextureBuffer(tex)
	
	; do forward view
	SetCubeFace tex,1
	RotateEntity camera,0,0,0
	RenderWorld
	CopyRect 0,0,tex_sz,tex_sz,0,0,BackBuffer(),TextureBuffer(tex)
	
	; do right view	
	SetCubeFace tex,2
	RotateEntity camera,0,-90,0
	RenderWorld
	CopyRect 0,0,tex_sz,tex_sz,0,0,BackBuffer(),TextureBuffer(tex)
	
	; do backward view
	SetCubeFace tex,3
	RotateEntity camera,0,180,0
	RenderWorld
	CopyRect 0,0,tex_sz,tex_sz,0,0,BackBuffer(),TextureBuffer(tex)
	
	; do up view
	SetCubeFace tex,4
	RotateEntity camera,-90,0,0
	RenderWorld
	CopyRect 0,0,tex_sz,tex_sz,0,0,BackBuffer(),TextureBuffer(tex)
	
	; do down view
	SetCubeFace tex,5
	RotateEntity camera,90,0,0
	RenderWorld
	CopyRect 0,0,tex_sz,tex_sz,0,0,BackBuffer(),TextureBuffer(tex)
	
	; Show entity again
	ShowEntity entity
	
	; Hide the cubemap camera
	HideEntity camera
	
End Function



Bouncer(Posted 2003) [#9]
you have to put the cam where the teapot is. You have to "photograph" the environment from the reflecting objects "eyes" if you know what I mean.


simonh(Posted 2003) [#10]
I believe this has something to do with the fact that with cubemapping, the cubemapped object has to have a lot of vertices otherwise you get a texture 'warping' effect similar to that seen in early PlayStation games. Try replacing the cube with a grid.


Gabriel(Posted 2003) [#11]
Thanks for the tip, Simon, I was wondering where the warping was coming from in something i was playing with.


wmaass(Posted 2003) [#12]
Increasing the polys on my pond helped the warping, thanks! But, the reflection still seems off like the pic above. It seems like I should place the cube_cam in the middle of the pond, but that doesn't seem to do it either.


Ross C(Posted 2003) [#13]
sorry for the mis-information. i thought u were refering to the teapot example that came with blitz :S ekkkk!