updated to PB 5.42.
many users are using mesh as a terrain, i suggest the simplest terrain is made from a plane mesh (CreatePlane) and its vertices deformed vertically according to height map picture. the interpretation of the color is the choice of the programmer. it is in fact a severe abstraction of the terrain generator by Samuel.
Code: Select all
UsePNGImageDecoder()
UseJPEGImageDecoder()
Enumeration
#Window
#Camera
#sphere
#plane
EndEnumeration
Global Dim MeshData.PB_MeshVertex(0)
#CameraSpeed = 1
Declare Terrain()
Define.f KeyX, KeyY, MouseX, MouseY
If InitEngine3D()
Add3DArchive(".", #PB_3DArchive_FileSystem)
Add3DArchive(#PB_Compiler_Home + "Examples/3D/Data/Textures", #PB_3DArchive_FileSystem)
InitSprite()
InitKeyboard()
InitMouse()
ExamineDesktops()
DesktopW = DesktopWidth(0)
DesktopH = DesktopHeight(0)
If OpenWindow(#Window, 0, 0, DesktopW, DesktopH, "W wire/solid Frame ....mouse+arrow keys for the camera ")
If OpenWindowedScreen(WindowID(#Window), 0, 0, DesktopW, DesktopH, 0, 0, 0)
CreateCamera(#Camera, 0, 0, 100, 100)
MoveCamera(#Camera, 0, 110, 140, #PB_Absolute)
;MoveCamera(#Camera, 0, 170, 20, #PB_Absolute)
CameraLookAt(#Camera,0,0,0)
CameraBackColor(#Camera, RGB(254,236,186)) ;RGB(200,155,40)
CreateMaterial(1, LoadTexture(1, "Geebee2.bmp"))
MaterialCullingMode(1, #PB_Material_NoCulling)
CreateMaterial(0, LoadTexture(0, "terrain_texture.jpg"))
;CreateMaterial(0, LoadTexture(0, "white.jpg"))
MaterialCullingMode(0, #PB_Material_NoCulling)
CreateLight(0, RGB(255, 255, 255), 100, 100, 0)
AmbientColor(RGB(100, 100, 100))
EndIf
EndIf
Global TileCountX = 99, TileCountZ = 99 ; number of squares acroos x, and acroos z
Global Dim TerrainData.l(TileCountX, TileCountZ)
LoadImage(0, "terrain.png")
ResizeImage(0, TileCountX+1, TileCountZ+1)
StartDrawing(ImageOutput(0))
For z=0 To TileCountZ
For x=0 To TileCountX
colr=Point(x,z)
TerrainData(x,z)=Red(colr)
Next
Next
StopDrawing()
;*******************************************
;CreatePlane(#Mesh, TileSizeX, TileSizeZ, TileCountX, TileCountZ, TextureRepeatCountX, TextureRepeatCountZ)
CreatePlane(#plane,100, 100, TileCountX, TileCountZ, 1,1)
CreateEntity(#plane,MeshID(#plane),MaterialID(0), 0, 0, 0)
;MaterialShadingMode(0, #PB_Material_Wireframe)
wireFrame = 1
Terrain()
CreateEntityBody(#plane, #PB_Entity_StaticBody ,1,1,1)
UpdateMeshBoundingBox(#plane)
WorldShadows(#PB_Shadow_Modulative) ;#PB_Shadow_Additive)
CreateSphere(#sphere,3)
CreateEntity(#sphere, MeshID(#sphere), MaterialID(1), 0,35,0)
CreateEntityBody(#sphere, #PB_Entity_ConvexHullBody ,1,0.5,1)
WorldGravity(-20) ; the gravity here is twice the normal
Repeat
Repeat
Until WindowEvent()=0
If ExamineMouse()
MouseX = -MouseDeltaX()/20
MouseY = -MouseDeltaY()/20
EndIf
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
If KeyboardReleased(#PB_Key_W)
If wireFrame
MaterialShadingMode(0, #PB_Material_Wireframe)
wireFrame ! 1
Else
MaterialShadingMode(0, #PB_Material_Solid)
wireFrame ! 1
EndIf
EndIf
EndIf
DisableEntityBody(#plane, #False)
RotateCamera(#Camera, MouseY, MouseX, 0, #PB_Relative)
MoveCamera(#Camera, KeyX, 0, KeyY)
RotateEntity(#plane, 0,0.1,0, #PB_Relative)
RenderWorld()
FlipBuffers()
Until KeyboardPushed(#PB_Key_Escape) Or Quit = 1
Else
MessageRequester("Error", "The 3D Engine can't be initialized",0)
EndIf
End
Procedure Terrain()
GetMeshData(#plane,0, MeshData(), #PB_Mesh_Vertex | #PB_Mesh_Normal , 0, MeshVertexCount(#plane)-1)
c=0 : dist.f : u.f: v.f
For y = 0 To TileCountZ
For x = 0 To TileCountX
height.f = TerrainData(x,y)/10
MeshData(c)\y = height
c+1
Next
Next
SetMeshData(#plane,0, MeshData(), #PB_Mesh_Vertex | #PB_Mesh_Normal, 0, MeshVertexCount(#plane)-1)
NormalizeMesh(#plane)
UpdateMeshBoundingBox(#plane)
EndProcedure