Create terrains and textures for the CreateTerrain()function

Share your advanced PureBasic knowledge/code with the community.
WolfgangS
Enthusiast
Enthusiast
Posts: 174
Joined: Fri Apr 25, 2003 3:30 pm

Create terrains and textures for the CreateTerrain()function

Post by WolfgangS »

HI !
This small and incomplete tutorial will show you how to create a terrain and the texture for the CreateTerrain() function in Purebasic.
Please Note: If you know better tools or something else please don't wait and add your knowledge to this thread !

To show a nice terrain like the one you can see at the Purebasic's Terrain demo you need 2 things:
A terrain and a texture.

To create a terrain you can take one of this tools (i prefer terragen):
http://www.planetside.co.uk/
http://www.tenermerx.com/sc3maped/

You need also a tool to create the texture from the terrain (i prefer terrTexGen).
http://www.delphigl.de/projects/terrtexgen.html
or a PlugIn for Photoshop (i never tested it)
http://developer.nvidia.com/object/nv_t ... tools.html


The terrain:
OK. At first you have to create a terrain and save it as bitmap file.
(Terragen can only save the terrain as raw file - use PaintShop or a similar programm to convert the .raw files to .bmp)
Make sure the size of your terrain is (n*8+1)*(n*8+1) pixel: also 129*129 or 257*257 or 513*513 usw.
IMPORTANT: make sure the file is in 8 bit depth and greyscale. This different greycolors give the altitude information of the terrain.

The texture:
Start TerrTexGen or similar, load the terrain and create the right texture for it (make sure you have about the same size). The programm is very simple and i think it isn't neccessarily to explaint how to do it. TerrTexgen saves the file and bitmap when you press "Generate". Then you take your painting programm again and resize the just saved bitmap texture to the right size (256*256 -> 257*257 usw.)

Move both pictures to you folder and create a programm like this:


Code: Select all

;
#CameraSpeed = 5
IncludeFile "..\Screen3DRequester.pb"
DefType.f KeyX, KeyY, MouseX, MouseY
If InitEngine3D()
  InitSprite()
  InitKeyboard()
  InitMouse()
  UsePNGImageDecoder()
  
  If Screen3DRequester()

    AmbientColor(RGB(255,255,255))
    If CreateMaterial  (0, LoadTexture(0, "texture12.png"))=0  ;<-- An you texture here
      MessageRequester("","Error CreateMaterial",0):End:EndIf
    
      If CreateTerrain("terrain13.png", MaterialID(0), 4, 0.6, 4, 4)=0  ;<-- An you terrain here

        MessageRequester("","Error CreateTerrain",0):End:EndIf

    CreateCamera(0, 0, 0, 100, 100)
    CameraLocate(0, 128, 25, 128)
    Add3DArchive("dome.zip",#PB_3DArchive_Zip)
    SkyDome("dome1.bmp",0)
    Repeat
      Screen3DEvents()
      If ExamineKeyboard()
        If KeyboardPushed(#PB_Key_a)
          KeyX = -#CameraSpeed*2
        ElseIf KeyboardPushed(#PB_Key_d)
          KeyX = #CameraSpeed*2
        Else
          KeyX = 0
        EndIf

        If KeyboardPushed(#PB_Key_w)
          KeyY = -#CameraSpeed*2
        ElseIf KeyboardPushed(#PB_Key_s)
          KeyY = #CameraSpeed*2
        Else
          KeyY = 0
        EndIf
      EndIf
      
      If ExamineMouse()
        MouseX = -(MouseDeltaX()/10)*#CameraSpeed/2
        MouseY = -(MouseDeltaY()/10)*#CameraSpeed/2
      EndIf
      Height.f = TerrainHeight(CameraX(0), CameraZ(0))
      RotateCamera(0, MouseX, MouseY, RollZ)
      MoveCamera  (0, KeyX, -CameraY(0)+Height+8, KeyY)
      RenderWorld()
      FlipBuffers()
    Until KeyboardPushed(#PB_Key_Escape) Or Quit = 1
  EndIf
Else
  MessageRequester("Error", "The 3D Engine can't be initialized",0)
EndIf
  
End
Now you should see your terrain. If you don't like it, create an other one.

If you take a look to the Purebasic Terrain demo, you will see Fred uses a second texture to show more "details". Unfortunately i couldn't figure out how to create an other one :? If you know it, don't wait to enlighten me :(

I hope this small tutorial will help you to use terrains in future.

Have a nice one ... + MFG
WolfgangS
WolfgangS' projects http://www.schliess.net
Quotation of the month:
<p3hicy>oder ich hol mir so eine geile aus asien
<p3hicy>die ständig poppen will
<p3hicy>'n brötchen pro tag reicht doch
<p3hicy>die essen eh' nich so viel
memdee
New User
New User
Posts: 5
Joined: Sun Jan 04, 2004 7:06 pm
Location: Hamburg, Germany
Contact:

Post by memdee »

Well, thanks for that.
But why does this not work when I try it as you described?
Here are my two images:
Image
(test_terrain.png , 257x257, the terrain)

Image
(test_tex.jpg , 256x256, the texture)

the relating code:

Code: Select all

CreateMaterial(0, LoadTexture(0, "test_tex.jpg"))
CreateTerrain("data\test_terrain.png", MaterialID(0), 4,0.6,4, 1)

Why does this not work?! I don't get it...

So long
Doobrey
Enthusiast
Enthusiast
Posts: 218
Joined: Sat Apr 26, 2003 4:47 am
Location: Dullsville..population: me
Contact:

Post by Doobrey »

memdee wrote: the relating code:

Code: Select all

CreateMaterial(0, LoadTexture(0, "test_tex.jpg"))
CreateTerrain("data\test_terrain.png", MaterialID(0), 4,0.6,4, 1)

Why does this not work?! I don't get it...
Two things spring to mind..
1. You`re using png and jpg, did you use UseJPEGImageDecoder() at the start of your code?
2. The file for the terrain has a dir in the filename, whereas the texture file doesn`t..maybe it`s looking in the wrong place for the texture?

Which of the two lines of code fail? ( either will give result as 0 if they fail)
memdee
New User
New User
Posts: 5
Joined: Sun Jan 04, 2004 7:06 pm
Location: Hamburg, Germany
Contact:

Post by memdee »

It's line two.
There's nothing wrong with the directories. My test_terrain.png and test_tex.jpg are in the same dir as the terrain.png and terrain_texture.jpg from the example (which work fine).

For loading the texture I'm using this code:

Code: Select all

Add3DArchive("Data\"          , #PB_3DArchive_FileSystem) 
CreateMaterial  (0, LoadTexture(0, "test_tex.jpg"))
As you can see, this is the same code as in the example (and I don't need UseJPEGImageDecoder() because of the Add3DArchive()).
Can't anyone help :cry: ?
WolfgangS
Enthusiast
Enthusiast
Posts: 174
Joined: Fri Apr 25, 2003 3:30 pm

Post by WolfgangS »

@memdee

Both the texture and the terrain must have the same size. Resize to test_tex.jpg to 257*257 ;)
Then you take your painting programm again and resize the just saved bitmap texture to the right size (256*256 -> 257*257 usw.)
MFG
WolfgangS
WolfgangS' projects http://www.schliess.net
Quotation of the month:
<p3hicy>oder ich hol mir so eine geile aus asien
<p3hicy>die ständig poppen will
<p3hicy>'n brötchen pro tag reicht doch
<p3hicy>die essen eh' nich so viel
memdee
New User
New User
Posts: 5
Joined: Sun Jan 04, 2004 7:06 pm
Location: Hamburg, Germany
Contact:

Post by memdee »

Doesn't work. Now the terrain and the texture are the same size (257x257) - it doesn't work anyway :x
WolfgangS
Enthusiast
Enthusiast
Posts: 174
Joined: Fri Apr 25, 2003 3:30 pm

Post by WolfgangS »

memdee wrote:Doesn't work. Now the terrain and the texture are the same size (257x257) - it doesn't work anyway :x
Are you 100% sure the heighmap is in 8 bit depth ? (try to save it as .bmp ...)

MFG
WolfgangS
WolfgangS' projects http://www.schliess.net
Quotation of the month:
<p3hicy>oder ich hol mir so eine geile aus asien
<p3hicy>die ständig poppen will
<p3hicy>'n brötchen pro tag reicht doch
<p3hicy>die essen eh' nich so viel
memdee
New User
New User
Posts: 5
Joined: Sun Jan 04, 2004 7:06 pm
Location: Hamburg, Germany
Contact:

Post by memdee »

Yes, I am!
I saved it again as 8-bit-bitmap and converted it back to png...just doesn't work :roll:
Just says "Couldn't create terrain"...

€dit: But it's strange...the terrain.png from the example is 32 bit depth
WolfgangS
Enthusiast
Enthusiast
Posts: 174
Joined: Fri Apr 25, 2003 3:30 pm

Post by WolfgangS »

memdee wrote:blablabla
Then show me your complete code and the pictures stored as zip at your own webspace.

MFG
WolfgangS
WolfgangS' projects http://www.schliess.net
Quotation of the month:
<p3hicy>oder ich hol mir so eine geile aus asien
<p3hicy>die ständig poppen will
<p3hicy>'n brötchen pro tag reicht doch
<p3hicy>die essen eh' nich so viel
Polo
Addict
Addict
Posts: 2422
Joined: Tue May 06, 2003 5:07 pm
Location: UK

Post by Polo »

I think the terrain must be 256x256 instead of 257x257 and the texture too.
Polo
Addict
Addict
Posts: 2422
Joined: Tue May 06, 2003 5:07 pm
Location: UK

Post by Polo »

Sorry, I have said a mistake.
I just tried, and of course it works, but you have too :

For the terrain texture : You must put it in 257x257, and in 256colors (PNG)

For the texture : You must put it in 257x257

And after doing that, it works !
memdee
New User
New User
Posts: 5
Joined: Sun Jan 04, 2004 7:06 pm
Location: Hamburg, Germany
Contact:

Post by memdee »

uhm...I don't know what I exactly did...but it seems to work now.
Well....thx for your help.

Wolfgang: http://memdee.progamerz.com/engine_source.zip
Johan_Haegg
User
User
Posts: 60
Joined: Wed Apr 30, 2003 2:25 pm
Location: Västerås
Contact:

Post by Johan_Haegg »

Heightmap: 257x257x8bit (WinXP says 32bit)
http://www.wavelsoftware.com/pic2pic.htm <- converts nicely
Texture: 1024x1024 works good
CreateTerrain("Terrain\" + Terrain.s + ".png", MaterialID(LastMaterial()), 4, 0.6, 4, Screen\Precision) ;with this code, not directly in the demo however.
Post Reply