Cube Map Help!

Blitz3D Forums/Blitz3D Programming/Cube Map Help!

Idaho Razor(Posted 2005) [#1]
Graphics3D 800,600,16,2

SetBuffer BackBuffer()

;variables for grid size
Const Gridsize = 32
Global waterheight# = 5

;array for gridsize
Dim WaterY(Gridsize+1,Gridsize+1)
For i=0 To Gridsize
	For j=0 To Gridsize
		WaterY(i,j) = Rnd(0,360)
	Next
Next

visual = CreateCamera()
PositionEntity visual, 0,5,-15

cubecam = CreateCamera()
HideEntity cubecam

light = CreateLight()
PositionEntity light,0,0,15
RotateEntity light,0,0,0

AmbientLight 200,200,200

;Create my Sky
sky = CreateSphere()
skytex = LoadTexture("clouds.jpg")
EntityTexture sky,skytex
ScaleTexture skytex,.1,.1
ScaleEntity sky,15,15,15
FlipMesh sky
EntityOrder sky,1
EntityFX sky,1

;Create a floor
bottom = CreatePlane()
bottex = LoadTexture("oceanfloor.bmp")
EntityTexture bottom,bottex
ScaleTexture bottex,7,7
PositionEntity bottom,0,-7,0

;Create my Water
water = CreateTerrain(Gridsize)
TerrainShading water,True
TerrainDetail water,2000,True
PositionEntity water,-15,-5,-5
ScaleEntity water,4,5,4
EntityAlpha water,.8

cube = CreateCube()
matrix = LoadTexture("test.jpg")
EntityTexture cube,matrix
ScaleTexture matrix,3,3
PositionEntity cube, 35,5,35
ScaleEntity cube,5,5,5

cubemap = CreateTexture(256,256,1+128+256)
EntityTexture water,cubemap
SetCubeMode cubemap, 1

While Not KeyHit(1)

	PositionEntity sky,EntityX(visual),EntityY(visual),EntityZ(visual)

	If KeyDown(200) Then MoveEntity visual,0,0,0.3
	If KeyDown(208) Then MoveEntity visual,0,0,-0.3
	If KeyDown(203) Then TurnEntity visual,0,0.2,0
	If KeyDown(205) Then TurnEntity visual,0,-0.2,0
	
	TurnEntity cube,0.1,0.2,0.3
	
	;waves
	;For z=0 To Gridsize-1
	;	For x=0 To Gridsize-1
	;		height# = Sin(WaterY(z,x))
	;		ModifyTerrain water,x,z,Abs(height#/4),False
	;		WaterY(z,x) = WaterY(z,x)+1
	;	Next
	;Next
	
	UpdateCubeMap(cubemap,cubecam,water)
	
	RenderWorld
	UpdateWorld
	Flip
Wend

End

Function UpdateCubeMap(cubemap,camera,entity)

	tex_sz=TextureWidth(cubemap)
	
	ShowEntity camera
	
	HideEntity entity
	
	PositionEntity camera,EntityX#(entity),EntityY#(entity),EntityZ#(entity)
	
	CameraClsMode camera,False,True
	
	CameraViewport camera,0,0,tex_sz,tex_sz
	
	;Update Cubemap

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

End Function


Here is my code, my problem is that i can't get the cube map camera to align up so everything looks good. The reflection (off the water) is just all funny lookin... Can someone help me :-(


Idaho Razor(Posted 2005) [#2]
The arrow keys are to move, and whenever u look at the cube map and move not only do the locations change but it's all distorted and ugly looking on the edge of the plane... :-(


D4NM4N(Posted 2005) [#3]
i posted a cube mapped water thingy on my website a while ago, you can take a look, mabe itl help. unfortunately it goes a bit mad for objects below the water tho.
www.d-grafix.com/software.html


Idaho Razor(Posted 2005) [#4]
lol is it alright to use for commercial games :-D ?


D4NM4N(Posted 2005) [#5]
sure, a mention in the creds would be nice tho ;)
btw:
in your code try:

updateworld
updatecubemaps
renderworld
flip

this may fix it

Also i tried using sin/cos to adjust my waves but i found the slihgt angles between vertices effect the mapping of the cubemap and looks all distorted and crap.


Idaho Razor(Posted 2005) [#6]
I'll just use your source, and of course a credit mention!

though i did switch around those commands like you stated and that still did not work :-(

O'Well, your code works great Thank You!