Terrain creation

Everything related to 3D programming
pfaber11
Enthusiast
Enthusiast
Posts: 145
Joined: Sat Apr 13, 2019 12:17 pm

Terrain creation

Post by pfaber11 »

Been busy today trying to get some terrain working . I Haven't had any success yet and am a little dismayed so I am going to post my code here and perhaps someone can tell me where I'm going wrong .

Code: Select all

Debug LoadTexture(6,"mazeblank.png")
Debug LoadTexture(7,"brick.png")
Debug CreateMaterial(8,TextureID(6))
Debug AddMaterialLayer(8,TextureID(7),#PB_Material_Add)

Debug CreateTerrain(0, 513, 12000, 2, 2, "amaze12.png", "dat")
BuildTerrain(0)
RenderWorld()
FlipBuffers()
User avatar
Paul
PureBasic Expert
PureBasic Expert
Posts: 1243
Joined: Fri Apr 25, 2003 4:34 pm
Location: Canada
Contact:

Re: Terrain creation

Post by Paul »

Not sure what anyone is suppose to do with the code you posted since you can't actually compile it ;)

Why don't you try looking in the folder: "PureBasic/Examples/3D"
Maybe start with "Terrain.pb" to get an idea of how things work then adapt to your need.
Image Image
applePi
Addict
Addict
Posts: 1404
Joined: Sun Jun 25, 2006 7:28 pm

Re: Terrain creation

Post by applePi »

Look the concise and simple way to create a terrain in Comtois code here: viewtopic.php?f=36&t=65855&p=526034#p489107
pfaber11
Enthusiast
Enthusiast
Posts: 145
Joined: Sat Apr 13, 2019 12:17 pm

Re: Terrain creation

Post by pfaber11 »

I was thinking maybe someone who knows could explain how to make the most simple of terrains . I don't need any fancy stuff or moving cameras I would just like some very simple terrain and from there I can build on it . Thanks for reading .
pfaber11
Enthusiast
Enthusiast
Posts: 145
Joined: Sat Apr 13, 2019 12:17 pm

Re: Terrain creation

Post by pfaber11 »

Well here is my latest effort and I think I'm getting the idea build terrain is coming up with an 0 using debug and I can't work out why. the terrain is a height map 2048x2048 and the material are 512x512 . they are all .png files but I can't see why this would be a problem . Thanks for reading and please try and help.

Code: Select all

UsePNGImageDecoder()
UsePNGImageEncoder()

InitEngine3D()
InitSprite()
InitKeyboard()
Add3DArchive("C:/Users/pfabe/Documents",#PB_3DArchive_FileSystem)
OpenScreen(1366,768,32,"")

  light = CreateLight(#PB_Any ,RGB(255, 255, 190), 4000, 1200, 1000,#PB_Light_Directional)
    SetLightColor(light, #PB_Light_DiffuseColor, RGB(250,250,230)) 
    LightDirection(light ,0.55, -0.3, -0.75) 
    AmbientColor(RGB(10,10,10))
Debug SetupTerrains(LightID(Light), 3000, #PB_Terrain_NormalMapping)
Debug LoadImage(4,"amaze12.png")
Debug LoadTexture(1,"blue.png")
Debug LoadTexture(2,"paulstree.png")


Debug CreateMaterial(#PB_Any,TextureID(1))
;Debug AddMaterialLayer(#PB_Any, TextureID(2))

Debug CreateTerrain(#PB_Any, 513,600,60,1,"terraingroup", "dat")
Debug BuildTerrain(5)
CreateCamera(0,0,0,100,100)
MoveCamera(0,300,200,200)

CameraLookAt(0,40,10,500)
CameraBackColor(0,#Green)
Repeat
RenderWorld()
FlipBuffers()
ExamineKeyboard()
If KeyboardPushed(#PB_Key_A)
        
        a=a-2
        RotateCamera(0,0,a,0)
      EndIf
      If KeyboardPushed(#PB_Key_D)
        
        a=a+2
        RotateCamera(0,0,a,0)
      EndIf
      If KeyboardPushed(#PB_Key_W)
        b=0
        b=b+2
        MoveCamera(0,0,0,b)
      EndIf
      If KeyboardPushed(#PB_Key_S)
        b=0
        b=b-2
        MoveCamera(0,0,0,b)
      EndIf
      
    Until KeyboardPushed(#PB_Key_Escape)   
    
    End
#NULL
Addict
Addict
Posts: 1440
Joined: Thu Aug 30, 2007 11:54 pm
Location: right here

Re: Terrain creation

Post by #NULL »

Is that name pfabe (without an r) correct in that path?
LoadImage() will look in the current directory (use Debug GetCurrentDirectory() to see) and not use the archives like LoadTexture etc. does.
Also read the doc for BuildTerrain()
Before building a terrain, tiles have to be defined with DefineTerrainTile(), and textures added with AddTerrainTexture().
pfaber11
Enthusiast
Enthusiast
Posts: 145
Joined: Sat Apr 13, 2019 12:17 pm

Re: Terrain creation

Post by pfaber11 »

Hi Null yes the directory is correct. Thanks for replying . been at this for two days now . pfabe without the r. It finds the files ok. will take on board what you said and look at it again. Thanks for taking the time to reply .
pfaber11
Enthusiast
Enthusiast
Posts: 145
Joined: Sat Apr 13, 2019 12:17 pm

Re: Terrain creation

Post by pfaber11 »

Well I still haven't got any terrain but I think I'm Getting there. What is the shortest amount of lines to create a terrain .? I really need something simple to start with. A simple height map or small piece of terrain.
I'm finding the examples to be too complicated.
applePi
Addict
Addict
Posts: 1404
Joined: Sun Jun 25, 2006 7:28 pm

Re: Terrain creation

Post by applePi »

what is the shortest amount of lines to create a terrain
only 5 lines, all the other code is an accessories such as the materials, the camera is essential it is your eyes exactly watching the virtual world
here is the shortest demo, it adapted from terrain.pb from the purebasic 3D example, i have removed many lines, also removing the screenRequester , i have added a very big Sphere in the terrain center. and the camera ie our eyes is looking to it
i have also removed the ability to compile with opengl context to reduce the code size

Code: Select all

; adapted from terrain.pb from 3D examples
#CameraSpeed = 10

Define.f KeyX, KeyY, MouseX, MouseY

If InitEngine3D()
  
  InitSprite()
  InitKeyboard()
  InitMouse()
  
  OpenWindow(0, 0, 0, 1000, 600, "terrain", #PB_Window_ScreenCentered | #PB_Window_SystemMenu)
  OpenWindowedScreen(WindowID(0), 0, 0, 800, 600, 0, 0, 0)


    Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Textures/", #PB_3DArchive_FileSystem)
    Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Textures/nvidia", #PB_3DArchive_FileSystem)
    Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Packs/desert.zip", #PB_3DArchive_Zip)
    
    ;- Light 
    ;
    light = CreateLight(#PB_Any ,RGB(190, 190, 190), 4000, 1200, 1000,#PB_Light_Directional)
    SetLightColor(light, #PB_Light_SpecularColor, RGB(255*0.4, 255*0.4,255*0.4)) 
    LightDirection(light ,0.55, -0.3, -0.75) 
    AmbientColor(RGB(255*0.2, 255*0.2,255*0.2))
    
    ;- Camera 
    ;
    CreateCamera(0, 0, 0, 100, 100)
    ;MoveCamera(0,  800, 400, 80, #PB_Absolute)
    MoveCamera(0,  896, 502, 5500, #PB_Absolute)
    CameraBackColor(0, RGB(5, 5, 10))
    ;CameraRenderMode(0, #PB_Camera_Wireframe)

    ;- Terrain definition
    SetupTerrains(LightID(Light), 3000, #PB_Terrain_NormalMapping)
    ; initialize terrain
    CreateTerrain(0, 513, 12000, 600, 1, "TerrainGroup", "dat")
    ; set all texture will be use when terrrain will be constructed 
    AddTerrainTexture(0,  0, 100, "dirt_grayrocky_diffusespecular.jpg",  "dirt_grayrocky_normalheight.jpg")
    ; Build terrains
    DefineTerrainTile(0, 0, 0, "terrain513.png", 0, 0)  
    BuildTerrain(0)  
    
        
    ; SkyBox
    ;
    SkyBox("desert07.jpg")
    
    CreateMaterial(6, LoadTexture(1, "ValetCoeur.jpg"))
    MaterialCullingMode(6, #PB_Material_NoCulling        )
    sphere = CreateSphere(#PB_Any, 1000)
    sphere = CreateEntity(#PB_Any, MeshID(sphere), MaterialID(6), 0, 400,0 )
    
    
   Repeat
    
    Repeat   
      Event = WindowEvent()
      
      If Event = #PB_Event_CloseWindow  ; If the user has pressed on the close button
        Quit = 1
      
      EndIf  
      
    Until Event = 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
        
        If KeyboardReleased(#PB_Key_A)
          ; to deremine the current coordinates of the camera
          Debug CameraX(0)
          Debug CameraY(0)
          Debug CameraZ(0)
        EndIf
        
        
      EndIf
      
      If ExamineMouse()
        MouseX = -MouseDeltaX() * #CameraSpeed * 0.05
        MouseY = -MouseDeltaY() * #CameraSpeed * 0.05
      EndIf
      MoveCamera(0, CameraX(0), TerrainHeight(0, CameraX(0), CameraZ(0)) + 60, CameraZ(0), #PB_Absolute)
      MoveCamera  (0, KeyX, 0, KeyY)
      RotateCamera(0,  MouseY, MouseX, 0, #PB_Relative)  
      
      RenderWorld()
      FlipBuffers()
      
    Until KeyboardPushed(#PB_Key_Escape) Or Quit = 1
    
    End 


EndIf 

Edit: notes:
1- change number 60 in line 101 to 400 and the camera appears as if it is riding a tall Giraffe
2- remove line 101 and the camera will not be restricted to the terrain surface and we can move anywhere, this useful to go high and see the terrain from high altitude
3- DefineTerrainTile(0, tx, ty, "terrain513.png", ty % 2, tx % 2)
the "terrain513.png" is the HeightMap, position the mouse on the function DefineTerrainTile and press F1 to get all the help and info about it
4- number 600 in line 38 is the scale , make it more and we will get more bumpy terrain
6- use another HeightMap such as "Lensflare5.jpg" but change the scale from 600 to 2000 and we will get a volcano
7- you may need to change #CameraSpeed = 10 to more values to speed the camera movement
pfaber11
Enthusiast
Enthusiast
Posts: 145
Joined: Sat Apr 13, 2019 12:17 pm

Re: Terrain creation

Post by pfaber11 »

Hi everybody I found out why my code would never run correctly and the answer was it needed Directx 9c to be able to do the terrain stuff . Downloaded this than installed done a restart and bobs your uncle it works . So if you're running windows 10 and having problems with terrain download the older Directx 9c and for me it sorted it . Bonjour .
pfaber11
Enthusiast
Enthusiast
Posts: 145
Joined: Sat Apr 13, 2019 12:17 pm

Re: Terrain creation

Post by pfaber11 »

Hi was just wondering what is the best method for making a height map for pure basic.shades of red or shades of grey. I have made many in AGK2 but not sure what the best way to do it with PB. Only been using PB for a couple of weeks and really like it . Really glad I got on board . Great forum too. Thanks for reading .
Post Reply