MeshManual 2 - UV Coordinates
Posted: Sat Oct 18, 2008 7:46 am
I thought this might come in useful to someone... It's a variation of the MeshManual demo in Examples/Sources and features UV coordinates to correctly display the clouds.jpg texture on each of the 6 cube faces.
Code: Select all
;
; ------------------------------------------------------------
;
; PureBasic - Manual Mesh 2
;
; (c) 2003 - Fantaisie Software
;
; ------------------------------------------------------------
;
#CameraSpeed = 5
IncludeFile "Screen3DRequester.pb"
Define.f KeyX, KeyY, MouseX, MouseY
If InitEngine3D()
Add3DArchive("Data", #PB_3DArchive_FileSystem)
InitSprite()
InitKeyboard()
InitMouse()
If Screen3DRequester()
; The data has to be organize in this order: Vertex, Normal, Color, UVCoordinate (per vertex)
;
; Create a cube, manually.. See the DataSection, for more precisions
;
CreateMesh(0, 100)
SetMeshData(0, #PB_Mesh_Vertex | #PB_Mesh_UVCoordinate, ?CubeData2, 26)
SetMeshData(0, #PB_Mesh_Face, ?CubeDataIndex, 12)
CreateMaterial(0, LoadTexture(0, "clouds.jpg"))
;MaterialAmbientColor(0, #PB_Material_AmbientColors)
CreateEntity(0, MeshID(0), MaterialID(0))
CreateCamera(0, 0, 0, 100, 100)
CameraLocate(0, 0, 0, 1000)
Repeat
Screen3DEvents()
ClearScreen(RGB(0, 0, 0))
If ExamineKeyboard()
If KeyboardPushed(#PB_Key_Left)
KeyX = -#CameraSpeed
ElseIf KeyboardPushed(#PB_Key_Right)
KeyX = #CameraSpeed
Else
KeyX = 0
EndIf
If KeyboardPushed(#PB_Key_Up)
KeyY = -#CameraSpeed
ElseIf KeyboardPushed(#PB_Key_Down)
KeyY = #CameraSpeed
Else
KeyY = 0
EndIf
EndIf
If ExamineMouse()
MouseX = -(MouseDeltaX()/10)*#CameraSpeed/2
MouseY = -(MouseDeltaY()/10)*#CameraSpeed/2
EndIf
RotateEntity(0, 1, 1, 1, #PB_Relative)
RotateCamera(0, MouseY, MouseX, RollZ, #PB_Relative)
MoveCamera (0, KeyX, 0, KeyY)
RenderWorld()
Screen3DStats()
FlipBuffers()
Until KeyboardPushed(#PB_Key_Escape) Or Quit = 1
EndIf
Else
MessageRequester("Error", "The 3D Engine can't be initialized",0)
EndIf
End
DataSection
CubeData2:
Data.f -100.0,-100.0,100.0 ; 0 position
Data.f 0.0005,0.9995 ; 0 UV
Data.f -100.0,-100.0,100.0 ; 1 position
Data.f 0.9995,0.9995 ; 1 UV
Data.f -100.0,-100.0,100.0 ; 2 position
Data.f 0.9995,0.9995 ; 2 UV
Data.f -100.0,100.0,100.0 ; 3 position
Data.f 0.0005,0.0005 ; 3 UV
Data.f -100.0,100.0,100.0 ; 4 position
Data.f 0.0005,0.9995 ; 4 UV
Data.f -100.0,100.0,100.0 ; 5 position
Data.f 0.9995,0.0005 ; 5 UV
Data.f -100.0,-100.0,-100.0 ; 6 position
Data.f 0.0005,0.9995 ; 6 UV
Data.f -100.0,-100.0,-100.0 ; 7 position
Data.f 0.9995,0.0005 ; 7 UV
Data.f -100.0,-100.0,-100.0 ; 8 position
Data.f 0.9995,0.9995 ; 8 UV
Data.f -100.0,100.0,-100.0 ; 9 position
Data.f 0.0005,0.0005 ; 9 UV
Data.f -100.0,100.0,-100.0 ; 10 position
Data.f 0.0005,0.0005 ; 10 UV
Data.f -100.0,100.0,-100.0 ; 11 position
Data.f 0.9995,0.0005 ; 11 UV
Data.f 100.0,-100.0,100.0 ; 12 position
Data.f 0.0005,0.9995 ; 12 UV
Data.f 100.0,-100.0,100.0 ; 13 position
Data.f 0.0005,0.9995 ; 13 UV
Data.f 100.0,-100.0,100.0 ; 14 position
Data.f 0.0005,0.9995 ; 14 UV
Data.f 100.0,-100.0,100.0 ; 15 position
Data.f 0.9995,0.9995 ; 15 UV
Data.f 100.0,100.0,100.0 ; 16 position
Data.f 0.0005,0.0005 ; 16 UV
Data.f 100.0,100.0,100.0 ; 17 position
Data.f 0.9995,0.0005 ; 17 UV
Data.f 100.0,100.0,100.0 ; 18 position
Data.f 0.9995,0.9995 ; 18 UV
Data.f 100.0,-100.0,-100.0 ; 19 position
Data.f 0.0005,0.0005 ; 19 UV
Data.f 100.0,-100.0,-100.0 ; 20 position
Data.f 0.0005,0.9995 ; 20 UV
Data.f 100.0,-100.0,-100.0 ; 21 position
Data.f 0.9995,0.9995 ; 21 UV
Data.f 100.0,100.0,-100.0 ; 22 position
Data.f 0.0005,0.0005 ; 22 UV
Data.f 100.0,100.0,-100.0 ; 23 position
Data.f 0.9995,0.0005 ; 23 UV
Data.f 100.0,100.0,-100.0 ; 24 position
Data.f 0.9995,0.0005 ; 24 UV
Data.f 100.0,100.0,-100.0 ; 25 position
Data.f 0.9995,0.0005 ; 25 UV
CubeDataIndex:
Data.w 1,7,19
Data.w 19,13,1
Data.w 4,18,24
Data.w 24,10,4
Data.w 0,15,17
Data.w 17,3,0
Data.w 14,21,25
Data.w 23,16,12
Data.w 20,8,11
Data.w 11,22,20
Data.w 6,2,5
Data.w 5,9,6
EndDataSection