Need some help for a 3D map editor

Just starting out? Need help? Post your questions and find answers here.
Octavius
User
User
Posts: 15
Joined: Tue May 20, 2008 11:17 am
Location: France

Need some help for a 3D map editor

Post by Octavius »

Good day every body. Please, excuse my writing english, I'm french.

I've already made some code, but I'm blocked right now.

The principle of my program is :
1) Opening a picture in which grey pixels indicate height ;
2) Creating tables of vertices and triangles for the mesh.

My program works fine with pictures of 128*128 pixels. The problem is that I can't exceed that limit (the map I want to modelize is 591*379 pixels).

I think this should be due to the fact that over 16384 pixels the maximum number of vertices or triangles is reached. How can I reach that limit ?

(PS: width and height are #X and #Y)
(PS2: sorry, comments are in french)

Code: Select all


;-Initializations

;{
  #Sprite=0
  #Camera=0
  #Terrain=0
  #X=128
  #Y=128
Enumeration
EndEnumeration
;}

;{

;Instructions pour le debug
EnableExplicit
UseTGAImageDecoder()
UsePNGImageDecoder()
UseTIFFImageDecoder()

;Déclarations de fonctions
Declare Error(Text$)
Declare Check()
Declare.l ReverseRGB(Color.l)
Declare CreateWorld()
Declare Keyboard()
Declare Mouse()
Declare Camera(dA.f,dB.f,dR.f)

;Environnement général
If Not InitMouse() Or Not InitKeyboard() Or Not InitEngine3D() Or Not InitSprite() : Error("Impossible d'initialiser le programme.") : EndIf

;Les structures
Structure vtx : X.f : Y.f : Z.f : Color.l : EndStructure
Structure trgl : V1.w : V2.w : V3.w : EndStructure

;Variables
Global Path$,i.l,j.l,k.l
Global Dim Files.s(12)
Global Dim Vertices.vtx(#X*#Y)
Global Dim Triangles.trgl((#X-1)*(#Y-1)*2)

;Choix du mod
Path$=PathRequester("Choisissez le répertoire d'un mod pour le modifier :",GetCurrentDirectory())
If Path$="" : Error("Vous n'avez choisi aucun mod.") : EndIf

;Liste des fichiers traités
Restore Files : For i=0 To 11 : Read Files(i) : Next i
;}

;Vérification des fichiers
Check()

;Ouverture de l'écran
ExamineDesktops() : OpenScreen(DesktopWidth(0),DesktopHeight(0),DesktopDepth(0),"Geomod")

;Texture blanche 64*64
CreateTexture(0,64,64) : If StartDrawing(TextureOutput(0)) : Box(0,0,64,64,$FFFFFF) : StopDrawing() : EndIf

;Matière
CreateMaterial(0,TextureID(0)) : MaterialAmbientColor(0,#PB_Material_AmbientColors)

CreateWorld()
EntityLocate(0,0,0,0)

;Caméra
AmbientColor($FFFFFF) : CreateCamera(#Camera,0,0,100,100) : CameraBackColor(#Camera,$00FF00) : CameraLocate(#Camera,0,0,300)

Procedure Error(Text$)
  MessageRequester("Fin du programme !",Text$) : End
EndProcedure

Procedure Check()
For i=0 To 11
  If FileSize(Path$+Files(i))<0
    Error("Le fichier "+Files(i)+" est introuvable.")
  EndIf
Next i
EndProcedure

Procedure.l ReverseRGB(Color.l)
  ProcedureReturn RGB(Blue(Color),Green(Color),Red(Color))
EndProcedure

Procedure CreateWorld()
  Protected Color.l
  
  ;On charge l'image
  LoadSprite(#Sprite,"map_heights.png")
  ;On l'affiche
  DisplaySprite(#Sprite,0,0)
  ;On remplit le tableau de vertex en prélevant les couleurs de l'image point par point
  StartDrawing(ScreenOutput())
  For i=0 To #X-1
    For j=0 To #Y-1
      k=j+#Y*i
      Color=Point(i,#Y-j)
      Vertices(k)\X=i
      Vertices(k)\Y=j
      Vertices(k)\Z=Red(Color)/10
      Vertices(k)\Color=ReverseRGB(Color)
    Next j
  Next i
  StopDrawing()
  
  ;On remplit le tableau de triangles
  For i=0 To #X-2
    For j=0 To #Y-2
      k=j+i*(#Y-1)
      Triangles(2*k)\V1=j+1+i*#Y
      Triangles(2*k)\V2=j+i*#Y
      Triangles(2*k)\V3=j+(i+1)*#Y
      Triangles(2*k+1)\V1=j+1+i*#Y
      Triangles(2*k+1)\V2=j+(i+1)*#Y
      Triangles(2*k+1)\V3=j+1+(i+1)*#Y
    Next j
  Next i
  
  ;Création du terrain
  CreateMesh(#Terrain,#X*#Y)
  
  ;On remplit les données du terrain avec les deux tableaux de vertex et de triangles
  SetMeshData(#Terrain,#PB_Mesh_Vertex|#PB_Mesh_Color,@Vertices(),#X*#Y)
  SetMeshData(#Terrain,#PB_Mesh_Face,@Triangles(),(#X-1)*(#Y-1)*2)
  
  ;Création de l'entité du terrain
  CreateEntity(0,MeshID(#Terrain),MaterialID(0))
EndProcedure

Procedure Keyboard()
  ExamineKeyboard()
  If KeyboardPushed(#PB_Key_Escape) : End
  ElseIf KeyboardPushed(#PB_Key_Up) : MoveCamera(#Camera,0,1,0)
  ElseIf KeyboardPushed(#PB_Key_Down) : MoveCamera(#Camera,0,-1,0)
  ElseIf KeyboardPushed(#PB_Key_Left) : MoveCamera(#Camera,-1,0,0)
  ElseIf KeyboardPushed(#PB_Key_Right) : MoveCamera(#Camera,1,0,0) : EndIf
EndProcedure

Procedure Mouse()
  ExamineMouse()
;   If MouseButton(1)
; ;    Camera(MouseDeltaX()/1000,MouseDeltaY()/1000,0)
;   ElseIf MouseButton(2)
;     
;   Else
    Camera(0,0,MouseWheel())
;   EndIf
EndProcedure

Procedure Camera(dA.f,dB.f,dR.f)
  Protected X.f,Y.f,Z.f,Rh.f
  ;Calcul des coordonnées
;   ACam+dA
;   BCam+dB
;   Y=RCam*Sin(BCam)
;   Rh=RCam*Cos(BCam)
;   X=Rh*Cos(ACam)
;   Z=Rh*Sin(ACam)
;   ;Placer la caméra au bon endroit !
;   CameraLocate(#Camera,X,Y,Z)
;   RotateCamera(#Camera,dA*180/#PI,dB*180/#PI,0)
  MoveCamera(#Camera,0,0,10*dR)
EndProcedure

;**********************************************************************************************************
;-Main lopp
;**********************************************************************************************************

Repeat
  
  ClearScreen(0)
  
  Keyboard()
  Mouse()
  
  RenderWorld()
  
;   StartDrawing(ScreenOutput())
;   For i=0 To 31
;     For j=0 To 31
;       k=j+32*i
;       Plot(Vertices(k)\X,Vertices(k)\Y,Vertices(k)\Color)
;     Next j
;   Next i
;   StopDrawing()
  
  FlipBuffers()
  
ForEver

;-Data
;{
DataSection

Files:

Data.s "data\text\imperial_campaign_regions_and_settlement_names.txt.strings.bin"
Data.s "data\world\maps\base\descr_regions.txt"
Data.s "data\world\maps\base\descr_sounds_music_types.txt"
Data.s "data\world\maps\base\map_climates.tga"
Data.s "data\world\maps\base\map_features.tga"
Data.s "data\world\maps\base\map_ground_types.tga"
Data.s "data\world\maps\base\map_heights.tga"
Data.s "data\world\maps\base\map_regions.tga"
Data.s "data\world\maps\base\map_roughness.tga"
Data.s "data\world\maps\campaign\imperial_campaign\descr_mercenaries.txt"
Data.s "data\world\maps\campaign\imperial_campaign\descr_strat.txt"
Data.s "data\world\maps\campaign\imperial_campaign\descr_win_conditions.txt"

EndDataSection
;}

Here is the map :
Image
User avatar
Rook Zimbabwe
Addict
Addict
Posts: 4322
Joined: Tue Jan 02, 2007 8:16 pm
Location: Cypress TX
Contact:

Post by Rook Zimbabwe »

Syntax

DisplaySprite(#Sprite, x, y)
Description

Display the #Sprite at the specified position on the current screen. There is no transparent color. This function is clipped, so it's perfectly legal to display the sprite outside of the screen.
After I cut away all your external program files nonsense the sprite displays the block of North America, Cuba and the North East part of South America... The mountains are bumped... on a green screen background. I can control the scale of the psrite with the mouse wheel to a certain extent. I can make it smaller, but not larger... larger zooms in.

I can only assume this is not what you intended to do.

Are you trying to make some kind of MAP flyover? Like a looking glass?

You need to load the map as a background and then clip whatever portion of the sprite is above it as the new sprite image... it will be slow.
Binarily speaking... it takes 10 to Tango!!!

Image
http://www.bluemesapc.com/
Octavius
User
User
Posts: 15
Joined: Tue May 20, 2008 11:17 am
Location: France

Post by Octavius »

Rook Zimbabwe wrote: After I cut away all your external program files nonsense the sprite displays the block of North America, Cuba and the North East part of South America... The mountains are bumped... on a green screen background. I can control the scale of the psrite with the mouse wheel to a certain extent. I can make it smaller, but not larger... larger zooms in.

I can only assume this is not what you intended to do.

Are you trying to make some kind of MAP flyover? Like a looking glass?

You need to load the map as a background and then clip whatever portion of the sprite is above it as the new sprite image... it will be slow.
What I want is : 2D Map => 3D Terrain.
So I succeeded in making the mountains bumped as you have seen on a small part of the map, but I failed in converting the full map in a 3D terrain.

Here are some screenshots, found on the web, showing the kind of software I'm trying to implement :

Image

Image
User avatar
Rook Zimbabwe
Addict
Addict
Posts: 4322
Joined: Tue Jan 02, 2007 8:16 pm
Location: Cypress TX
Contact:

Post by Rook Zimbabwe »

So basically you just want a heightmap?

http://www.purebasic.fr/english/viewtop ... =heightmap
Binarily speaking... it takes 10 to Tango!!!

Image
http://www.bluemesapc.com/
Octavius
User
User
Posts: 15
Joined: Tue May 20, 2008 11:17 am
Location: France

Post by Octavius »

The PB function CreateTerrain() is not appropriate : my map and texture files are not squares and are too big.
Post Reply