Generator of mountains and rocks

Just starting out? Need help? Post your questions and find answers here.
User avatar
SPH
Enthusiast
Enthusiast
Posts: 561
Joined: Tue Jan 04, 2011 6:21 pm

Generator of mountains and rocks

Post by SPH »

Hi,

I appeal to your memory (and your knowledge) to know if you have any tips for drawing a mountain with its rocks, its cracks, etc ... all generated in polygons (or other).

Thx 8)

!i!i!i!i!i!i!i!i!i!
!i!i!i!i!i!i!
!i!i!i!
//// Informations ////
Portable LENOVO ideapad 110-17ACL 64 bits
Version de PB : 6.12LTS - 64 bits
BarryG
Addict
Addict
Posts: 4121
Joined: Thu Apr 18, 2019 8:17 am

Re: Generator of mountains and rocks

Post by BarryG »

pf shadoko is the king of landscape coding in these forums. See his code here -> viewtopic.php?f=36&t=74714

Or see his ocean code with a rocky island here -> viewtopic.php?f=36&t=76401
User avatar
falsam
Enthusiast
Enthusiast
Posts: 632
Joined: Wed Sep 21, 2011 9:11 am
Location: France
Contact:

Re: Generator of mountains and rocks

Post by falsam »

2D OpenGL. Only Polygons. Mountains, Sky & Moon :mrgreen:

Code: Select all

EnableExplicit

Global Scene, ww = 1024, wh = 600

; Plan de l'application
Declare Start()
Declare Render()

Declare Sky(Gadget, Color, Stars.i = 100, StarColor = $FFFFFFFF , OpacityMin.f=0.5, SizeMin = 1, SizeMax = 3)
Declare DrawMoon(Gadget, x.f, y.f, Width.f, Height.f, Fullness.f = 1, Color.i = $FFFFFFFF, Angle.f = 0)
Declare Mountain(Gadget, x.f, y.f, Width.f, Ranges.s, Color.i = $FFFD47FF)

Declare Exit()

Start()


;-
Procedure Start()    
  OpenWindow(#PB_Any, 0, 0, ww, wh, "OpenGL : Création d'une scene", #PB_Window_SystemMenu|#PB_Window_ScreenCentered)
  
  ; Fenetre openGl
  Scene = OpenGLGadget(#PB_Any, 0, 0, ww, wh, #PB_OpenGL_Keyboard)
  
  ; Activation textures et transparence
  glEnable_(#GL_BLEND)
  glBlendFunc_(#GL_SRC_ALPHA, #GL_ONE_MINUS_SRC_ALPHA)
  
  ; Couleur d'arriere plan
  ; Chaque composant RGBA est divisé par 255
  glClearColor_(0, 0, 0, 1.0) 
  
  ; Sélectionner la pile des matrices de projection 
  glMatrixMode_(#GL_PROJECTION)
  
  ; Ré-initialise la matrice de projection 
  ; et assure ainsi qu’une seule projection soit effectuée.
  glLoadIdentity_()
  
  ; glOrtho_(gauche, droit, bas, haut, proche, loin);
  ;glOrtho_(0, ww, wh, 0, -1, 1)
  gluOrtho2D_(0, ww, wh, 0)
  glDisable_(#GL_DEPTH_TEST)
  
  glMatrixMode_(#GL_MODELVIEW);
  glLoadIdentity_()
  
  ; Déclencheur  
  BindEvent(#PB_Event_CloseWindow, @Exit())
  
  ; Boucle de rendu visuel
  Render()
EndProcedure

; Visualisation du résultat
Procedure Render()    
  Repeat
    
    ; Efface le frame buffer et le Z-buffer
    glClear_(#GL_COLOR_BUFFER_BIT | #GL_DEPTH_BUFFER_BIT)
    
    
    ; Ciel
    Sky(Scene, RGBA(36, 69, 68, 255), 100, RGBA(255, 255, 255, 255), 0.1)
    
    ; Lune 
    DrawMoon(Scene, 200, 150, 50, 50 , -0.8, RGBA(255, 255, 255, 255), 180)
    
    ; Relief
    Mountain(Scene, 0, 0, ww, "[100, 50, 60, 125, 130, 200, 250, 280, 300, 225, 200, 125, 300, 200, 210]", RGBA(21, 21, 21, 255))
    Mountain(Scene, 0, 0, ww-200, "[100, 10, 30, 125, 80, 90, 100, 50, 80, 65, 60, 55, 40, 20, 0]", RGBA(0, 0, 0, 255))
    Mountain(Scene, 0, 0, ww, "[10, 15, 20, 40, 35, 30, 35, 40, 35, 30, 0]", RGBA(67, 5, 59, 255))
    
    ; FlipBuffers
    SetGadgetAttribute(Scene, #PB_OpenGL_FlipBuffers, #True)
  Until WindowEvent() = #PB_Event_CloseWindow
EndProcedure

; Dessin d'une lune
Procedure DrawMoon(Gadget, x.f, y.f, Width.f, Height.f, Fullness.f = 1, Color.i = $FFFFFFFF, Angle.f = 0)
  Protected Finesse.f = 0.1
  Protected n.f = Finesse 
  
  Protected SinAngle.f
  Protected CosAngle.f
  
  Protected x0.f
  Protected y0.f
  
  SetGadgetAttribute(Gadget, #PB_OpenGL_SetContext, #True)

  glMatrixMode_(#GL_MODELVIEW)    
  glPushMatrix_();   
  
  ; Location
  glTranslatef_(x, y, 0)
  
  ; Rotate
  glRotatef_(Angle, 0, 0, 1)
  
  ; Draw Moon
  glBegin_(#GL_QUAD_STRIP)
  glColor4f_(Red(Color)/255, Green(Color)/255, Blue(Color)/255, Alpha(Color)/255)
  
  While n < #PI 
    SinAngle.f = Sin(n)
    CosAngle.f = Cos(n)
    
    x0 = (Width * SinAngle) 
    y0 = (Height * CosAngle) 
    
    glVertex2f_(x0, y0);
    glVertex2f_(-Fullness * x0, y0)
    n + Finesse
  Wend    
  glEnd_()
  
  glPopMatrix_()
EndProcedure

; Dessin d'un ciel étoilé
Procedure Sky(Gadget, Color, Stars.i = 100, StarColor = $FFFFFFFF , OpacityMin.f=0.5, SizeMin = 1, SizeMax = 3)
  Structure Star
    x.f
    y.f
  EndStructure
  
  Protected w = GadgetWidth(Gadget)
  Protected h = GadgetHeight(Gadget)
  Static Dim Stars.Star(100), Flag.b
  
  Protected n
  
  ; Initialisation des étoiles 
  If Flag = 0
    Flag = 1
    For n = 0 To Stars
      Stars(n)\x = Random(w)
      Stars(n)\y = Random(h)  
    Next
  EndIf
  
  ; Dessin du ciel
  SetGadgetAttribute(Gadget, #PB_OpenGL_SetContext, #True)
  glMatrixMode_(#GL_MODELVIEW)  
  glLoadIdentity_()
  glPushMatrix_()
  
  ; Couleur du ciel
  glClearColor_(Red(Color)/255, Green(Color)/255, Blue(Color)/255, Alpha(Color)/255) 
  
  ; Dessin des étoiles
  For n = 1 To Stars
    glPointSize_(Random(SizeMax, SizeMin)) ; Valeur decimal 
    glColor4f_(Red(StarColor)/255, Green(StarColor)/255, Blue(StarColor)/255, (Random(10, OpacityMin * 10))/10)
    glBegin_(#GL_POINTS)
    glVertex2f_(Stars(n)\x, Stars(n)\y)
    glEnd_()
  Next  
  glPopMatrix_()
EndProcedure

; Dessin d'un relief montagneux
Procedure Mountain(Gadget, x.f, y.f, Width.f, Ranges.s, Color.i = $FFFD47FF)
  Protected gh = GadgetHeight(Gadget)
  Protected Dim Ranges(0), n, s, i.i, x0.f
  
  ParseJSON(0, Ranges)
  ExtractJSONArray(JSONValue(0), Ranges())
  
  ; Nombre de points à dessiner 
  s = ArraySize(Ranges())
  
  ; Intervalle entre chaque point
  i = (Width / s) 
  
  ; Dessin du ciel
  SetGadgetAttribute(Gadget, #PB_OpenGL_SetContext, #True)
  glMatrixMode_(#GL_MODELVIEW)  
  glLoadIdentity_()
  glPushMatrix_()
  
  glBegin_(#GL_POLYGON)
  glColor4f_(Red(Color)/255, Green(Color)/255, Red(Color)/255, Alpha(Color)/255)
  
  glVertex2f_(x, wh-y)
  For n = 0 To s
    glVertex2f_(x0, gh-y-Ranges(n))
    x0 + i
  Next 
  glVertex2f_(width, gh-y)  
  glEnd_()
  glPopMatrix_()
EndProcedure

Procedure Exit()  
  End
EndProcedure

➽ Windows 11 64-bit - PB 6.21 x64 - AMD Ryzen 7 - NVIDIA GeForce GTX 1650 Ti

Sorry for my bad english and the Dunning–Kruger effect 🤪
User avatar
Fangbeast
PureBasic Protozoa
PureBasic Protozoa
Posts: 4789
Joined: Fri Apr 25, 2003 3:08 pm
Location: Not Sydney!!! (Bad water, no goats)

Re: Generator of mountains and rocks

Post by Fangbeast »

/me falls off the chair laughing
Amateur Radio/VK3HAF, (D-STAR/DMR and more), Arduino, ESP32, Coding, Crochet
User avatar
falsam
Enthusiast
Enthusiast
Posts: 632
Joined: Wed Sep 21, 2011 9:11 am
Location: France
Contact:

Re: Generator of mountains and rocks

Post by falsam »

I was inspired with this link http://pcg.worldof.xyz/ ;)

➽ Windows 11 64-bit - PB 6.21 x64 - AMD Ryzen 7 - NVIDIA GeForce GTX 1650 Ti

Sorry for my bad english and the Dunning–Kruger effect 🤪
Olli
Addict
Addict
Posts: 1194
Joined: Wed May 27, 2020 12:26 pm

Re: Generator of mountains and rocks

Post by Olli »

Code: Select all

Delay(100000000000000000000)
ScreenshootImage
Post Reply