dxSetCubeFace(texture%,face%)

Parameters

texture - texture
face - face of cube to select. This should be one of the following values:
0: left (negative X) face
1: forward (positive Z) face - this is the default.
2: right (positive X) face
3: backward (negative Z) face
4: up (positive Y) face
5: down (negative Y) face

Description

Selects a cube face for direct rendering to a texture.

This command should only be used when you wish to draw directly to a cubemap texture in real-time. Otherwise, just loading a pre-rendered cubemap with a flag of 128 will suffice.

To understand how this command works exactly it is important to recognise that Blitz treats cubemap textures slightly differently to how it treats other textures. Here's how it works...

A cubemap texture in Blitz actually consists of six images, each of which must be square 'power' of two size - e.g. 32, 64, 128 etc. Each corresponds to a particular cube face. These images are stored internally by Blitz, and the texture handle that is returned by dxLoadTexture/dxCreateTexture when specifying the cubemap flag, only provides access to one of these six images at once (by default the first one, or '0' face).

This is why, when loading a cubemap texture into Blitz using dxLoadTexture, all the six cubemap images must be laid out in a specific order (0-5, as described above), in a horizontal strip. Then Blitz takes this texture and internally converts it into six separate images.

So seeing as the texture handle returned by dxCreateTexture / dxLoadTexture only provides access to one of these images at once (no. 1 by default), how do we get access to the other five images? This is where dxSetCubeFace comes in. It will tell Blitz that whenever you next draw to a cubemap texture, to draw to the particular image representing the face you have specified with the face parameter.

Now you have the ability to draw to a cubemap in real-time.

To give you some idea of how this works in code, here's a function that updates a cubemap in real-time. It works by rendering six different views and copying them to the cubemap texture buffer, using dxSetCubeFace to specify which particular cubemap image should be drawn to.

; Start of code

Function UpdateCubeMap( tex,camera )

tex_sz=dxTextureWidth(tex)

; do left view
dxSetCubeFace tex,0
dxRotateEntity camera,0,90,0
dxRenderWorld

; copy contents of dxBackBuffer to cubemap
dxCopyRect 0,0,tex_sz,tex_sz,0,0,dxBackBuffer(),dxTextureBuffer(tex)

; do forward view
dxSetCubeFace tex,1
dxRotateEntity camera,0,0,0
dxRenderWorld
dxCopyRect 0,0,tex_sz,tex_sz,0,0,dxBackBuffer(),dxTextureBuffer(tex)

; do right view
dxSetCubeFace tex,2
dxRotateEntity camera,0,-90,0
dxRenderWorld
dxCopyRect 0,0,tex_sz,tex_sz,0,0,dxBackBuffer(),dxTextureBuffer(tex)

; do backward view
dxSetCubeFace tex,3
dxRotateEntity camera,0,180,0
dxRenderWorld
dxCopyRect 0,0,tex_sz,tex_sz,0,0,dxBackBuffer(),dxTextureBuffer(tex)

; do up view
dxSetCubeFace tex,4
dxRotateEntity camera,-90,0,0
dxRenderWorld
dxCopyRect 0,0,tex_sz,tex_sz,0,0,dxBackBuffer(),dxTextureBuffer(tex)

; do down view
dxSetCubeFace tex,5
dxRotateEntity camera,90,0,0
dxRenderWorld
dxCopyRect 0,0,tex_sz,tex_sz,0,0,dxBackBuffer(),dxTextureBuffer(tex)

EndFunction

; End of code

All rendering to a texture buffer affects the currently selected face. Do not change the selected cube face while a buffer is locked.

Finally, you may wish to combine the vram 256 flag with the cubic mapping flag when drawing to cubemap textures for faster access.

See also: dxCreateTexture, dxLoadTexture, SetCubeMode.

Example

; dxSetCubeFace Example
; -------------------

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

dxGraphics3D width,height,depth,mode
dxSetBuffer dxBackBuffer()

; 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=dxCreateCamera()
dxPositionEntity 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=dxCreateCamera()
dxHideEntity cube_cam

light=dxCreateLight()
dxRotateEntity light,90,0,0

; Load object we will apply cubemap to - the classic teapot
teapot=dxLoadMesh("media/teapot.x")
dxScaleEntity teapot,3,3,3
dxPositionEntity teapot,0,10,0

; Create some scenery

; ground
ground=dxCreatePlane()
dxEntityColor ground,168,133,55
ground_tex=dxLoadTexture("media/sand.bmp")
dxScaleTexture ground_tex,10,10
dxEntityTexture ground,ground_tex

; sky
sky=dxCreateSphere(24)
dxScaleEntity sky,500,500,500
dxFlipMesh sky
dxEntityFX sky,1
sky_tex=dxLoadTexture("media/sky.bmp")
dxEntityTexture sky,sky_tex

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

; camel
camel=dxLoadMesh("media/camel.x")
dxFitMesh 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=dxCreatePivot()
dxPositionEntity ufo_piv,0,15,0
ufo=dxLoadMesh("media/green_ufo.x",ufo_piv)
dxPositionEntity ufo,0,0,10

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

; Apply cubic environment map to teapot
dxEntityTexture teapot,tex

While Not dxKeyDown(1)

; Control camera

; mouse look

mxs#=mxs#+(dxMouseXSpeed()/5.0)
mys#=mys#+(dxMouseYSpeed()/5.0)

dxRotateEntity cam,mys#,-mxs#,0

dxMoveMouse width/2,height/2

; move camera forwards/backwards/left/right with cursor keys

If dxKeyDown(200)=True Then dxMoveEntity cam,0,0,.2 ; move camera forward
If dxKeyDown(208)=True Then dxMoveEntity cam,0,0,-.2 ; move camera back

If dxKeyDown(205)=True Then dxMoveEntity cam,.2,0,0 ; move camera left
If dxKeyDown(203)=True Then dxMoveEntity cam,-.2,0,0 ; move camera right

; Turn ufo pivot, causing child ufo mesh to spin around it (and teapot)
dxTurnEntity ufo_piv,0,2,0

; Hide our main camera before updating cube map - we don't need it to be rendererd every time cube_cam is rendered
dxHideEntity cam

; Update cubemap
UpdateCubemap(tex,cube_cam,teapot)

; Show main camera again
dxShowEntity cam

dxRenderWorld

dxText 0,0,"Use mouse to look around"
dxText 0,20,"Use cursor keys to change camera position"

dxFlip

Wend


Function UpdateCubemap(tex,camera,entity)

tex_sz=dxTextureWidth(tex)

; Show the camera we have specifically created for updating the cubemap
dxShowEntity 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
dxHideEntity entity

; Position camera where the entity is - this is where we will be rendering views from for cubemap
dxPositionEntity camera,dxEntityX#(entity),dxEntityY#(entity),dxEntityZ#(entity)

dxCameraClsMode 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
dxCameraViewport camera,0,0,tex_sz,tex_sz

; Update cubemap

; do left view
dxSetCubeFace tex,0
dxRotateEntity camera,0,90,0
dxRenderWorld
dxCopyRect 0,0,tex_sz,tex_sz,0,0,dxBackBuffer(),dxTextureBuffer(tex)

; do forward view
dxSetCubeFace tex,1
dxRotateEntity camera,0,0,0
dxRenderWorld
dxCopyRect 0,0,tex_sz,tex_sz,0,0,dxBackBuffer(),dxTextureBuffer(tex)

; do right view
dxSetCubeFace tex,2
dxRotateEntity camera,0,-90,0
dxRenderWorld
dxCopyRect 0,0,tex_sz,tex_sz,0,0,dxBackBuffer(),dxTextureBuffer(tex)

; do backward view
dxSetCubeFace tex,3
dxRotateEntity camera,0,180,0
dxRenderWorld
dxCopyRect 0,0,tex_sz,tex_sz,0,0,dxBackBuffer(),dxTextureBuffer(tex)

; do up view
dxSetCubeFace tex,4
dxRotateEntity camera,-90,0,0
dxRenderWorld
dxCopyRect 0,0,tex_sz,tex_sz,0,0,dxBackBuffer(),dxTextureBuffer(tex)

; do down view
dxSetCubeFace tex,5
dxRotateEntity camera,90,0,0
dxRenderWorld
dxCopyRect 0,0,tex_sz,tex_sz,0,0,dxBackBuffer(),dxTextureBuffer(tex)

; Show entity again
dxShowEntity entity

; Hide the cubemap camera
dxHideEntity camera

End Function

Index