You can play around with this, until the good Rob comes around to cleaning up his code :); SetCubeFace Example
; -------------------
path$ = "F:\Programmer\Blitz3D\help\commands\3d_examples\media\"
width=640
height=480
depth=0
mode=2
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(path+"teapot.x")
ScaleEntity teapot,3,3,3
PositionEntity teapot,0,10,0
; Create some scenery
; ground
ground=CreateFace(10)
ScaleEntity ground,20,20,20
;EntityColor ground,168,133,55
ground_tex=LoadTexture(path+"sand.bmp")
ScaleTexture ground_tex,10,10
EntityTexture ground,ground_tex
;FlipMesh ground
EntityFX ground,1
; sky
sky=CreateSphere(24)
ScaleEntity sky,500,500,500
FlipMesh sky
EntityFX sky,1
sky_tex=LoadTexture(path+"sky.bmp")
EntityTexture sky,sky_tex
; cactus
cactus=LoadMesh(path+"cactus2.x")
FitMesh cactus,-5,0,-5,2,6,.5
; camel
camel=LoadMesh(path+"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(path+"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 ground,tex
Global waterang# = 0.0
While Not KeyDown(1)
For i = 1 To 9
If KeyHit(1+i)
SetCubeMode tex,i
End If
Next
; Control camera
; mouse look
mxs#=mxs#+(MouseXSpeed()/5.0)
mys#=mys#+(MouseYSpeed()/5.0)
RotateEntity cam,mys#,-mxs#,roll#
If KeyDown(30) Then roll# = roll#+1.0
If KeyDown(44) Then roll# = roll#-1.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
Water(ground)
; Update cubemap
UpdateCubemap(tex,cube_cam,ground,cam)
RenderWorld
Text 0,0,"Use mouse to look around"
Text 0,20,"Use cursor keys to change camera position"
Flip
Wend
End
Function Water(mesh)
If Lower$(EntityClass(mesh)) = "mesh"
n_surf = CountSurfaces(mesh)
For s = 1 To n_surf
surf = GetSurface(mesh,s)
n_vert = CountVertices(surf)-1
For v = 0 To n_vert
VertexCoords surf,v,VertexX(surf,v),Sin(waterang+(v*60))*0.001,VertexZ(surf,v)
Next
Next
UpdateNormals mesh
waterang = waterang + 5.0
End If
End Function
Function UpdateCubemap(tex,camera,entity,ocam,flat=True)
CameraProjMode ocam,0
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
If flat
PositionEntity camera,EntityX#(ocam),EntityY#(entity)-(EntityY(ocam)-EntityY(entity)),EntityZ(ocam)
Else
PositionEntity camera,EntityX#(entity),EntityY#(entity),EntityZ#(entity)
End If
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
CameraProjMode ocam,1
End Function
;Creates a single sided face
;segmented
Function CreateFace(segs=1,parent=0)
mesh=CreateMesh( parent )
surf=CreateSurface( mesh )
stx#=-.5
sty#=stx
stp#=Float(1)/Float(segs)
y#=sty
For a=0 To segs
x#=stx
v#=a/Float(segs)
For b=0 To segs
u#=b/Float(segs)
AddVertex(surf,x,0,y,u,v) ; swap these for a different start orientation
x=x+stp
Next
y=y+stp
Next
For a=0 To segs-1
For b=0 To segs-1
v0=a*(segs+1)+b:v1=v0+1
v2=(a+1)*(segs+1)+b+1:v3=v2-1
AddTriangle( surf,v0,v2,v1 )
AddTriangle( surf,v0,v3,v2 )
Next
Next
UpdateNormals mesh
Return mesh
End Function Have fun!
|