Procedural infinite heightmap generator

Post your FreeBASIC tips and tricks here. Please don’t post your code without including an explanation.
Z!re

Procedural infinite heightmap generator

Postby Z!re » Mar 05, 2008 20:02

[Content removed at author's request]
phycowelder
Posts: 74
Joined: Dec 19, 2007 6:55

Postby phycowelder » Mar 05, 2008 20:23

that looks sweet! ill have a gander soon after i get further with newton libs!
Dr_D
Posts: 2392
Joined: May 27, 2005 4:59
Contact:

Postby Dr_D » Mar 06, 2008 1:40

Pretty cool. Why didn't you do the multitexture thing with it? That would have made it really cool. ;)
Z!re

Postby Z!re » Mar 06, 2008 7:35

[Content removed at author's request]
Dr_D
Posts: 2392
Joined: May 27, 2005 4:59
Contact:

Postby Dr_D » Mar 06, 2008 7:40

You can always look at that thing I did. It uses regular old ARB_multitexture even, so it's compatible with more cards. ;)
Z!re

Postby Z!re » Mar 06, 2008 10:53

[Content removed at author's request]
Dr_D
Posts: 2392
Joined: May 27, 2005 4:59
Contact:

Postby Dr_D » Mar 06, 2008 19:34

I couldn't find the original thread. I must not have given it a significant name. :p

http://aapproj.phatcode.net/fbgfx/demos ... exture.zip
Z!re

Postby Z!re » Mar 06, 2008 21:06

[Content removed at author's request]
KristopherWindsor
Posts: 2428
Joined: Jul 19, 2006 19:17
Location: Sunnyvale, CA
Contact:

Postby KristopherWindsor » Mar 06, 2008 21:38

403 Error — Forbidden.
You were trying to access a download through a direct link (or hotlinking), which is NOT allowed in here! :(


(But the real warning was in bright colors. ;-))
Dr_D
Posts: 2392
Joined: May 27, 2005 4:59
Contact:

Postby Dr_D » Mar 06, 2008 21:46

Hotlinking not allowed? WTF?
D.J.Peters
Posts: 8019
Joined: May 28, 2005 3:28
Contact:

Postby D.J.Peters » Mar 07, 2008 0:03

Dr_D wrote:I couldn't find the original thread. I must not have given it a significant* name.

*OpenGL multi-texturing for terrains... :-)
http://www.freebasic.net/forum/viewtopic.php?t=6803

Joshy
JohnB
Posts: 236
Joined: Jul 22, 2005 3:53
Location: Minnesota Arizona

Question

Postby JohnB » Mar 08, 2008 10:39

Question?

Is the PIH.exe in the .rar file compiled using the WoM.bi in the .rar file? My conversion seems to run much slower than your .exe.

Code: Select all

#Include Once "GL/gl.bi"
#Include Once "GL/glu.bi"
#Include Once "GL/glext.bi"
#Include Once "SDL/SDL.bi"
#Include Once "SDL/SDL_mouse.bi"
#include Once "fbpng.bi"
#inclib "fbpng"

#Define PI 3.141592
#include once "vectors.bi"
#include once "wom.bi"
'#include once "textures.bi"

#ifndef FALSE
#define FALSE 0
#define TRUE (Not FALSE)
#EndIf

#ifndef NULL
#define NULL 0
#endif

#Define scr_width  1024
#Define scr_height  768
#Define scr_bpp      32

Declare Sub   doEvents()
Declare Function loadTextures() As Integer
Declare Function loadGLTexture(file_name As String, texture_num As GLuint) As Integer

Dim As Integer            errormsg
Dim As Integer            scr_flags
Dim As SDL_Surface Ptr   video

Dim Shared event As SDL_Event
Dim Shared As Integer done = 0

Dim Shared As GLfloat cursor_x = 0.0
Dim Shared As GLfloat cursor_y = 0.0
Dim Shared As Integer icursor_x = 0
Dim Shared As Integer icursor_y = 0

Dim Shared As GLuint texture(0 To 255)

Dim As Single fogColor(0 To 3)
Dim As Single lightAmbient(0 To 3)
fogColor(0) = 0.0
fogColor(1) = 0.5
fogColor(2) = 0.5
fogColor(3) = 1.0

errormsg = SDL_Init(SDL_INIT_VIDEO)
If (errormsg = 1) Then SDL_Quit : End
   
SDL_GL_SetAttribute SDL_GL_DOUBLEBUFFER, 1
SDL_GL_SetAttribute SDL_GL_DEPTH_SIZE, 32

'scr_flags = SDL_OPENGL Or SDL_FULLSCREEN
scr_flags = SDL_OPENGL

video = SDL_SetVideoMode( scr_width, scr_height, scr_bpp, scr_flags)

If (video = 0) Then SDL_Quit : End

glViewport 0, 0, scr_width, scr_height
glMatrixMode GL_PROJECTION
glLoadIdentity
gluPerspective 80/2, scr_width/scr_height, 0.1, 512.0   
glMatrixMode GL_MODELVIEW
glLoadIdentity
glClearDepth 1.0
glDepthFunc GL_LEQUAL
glHint GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST
glHint GL_LINE_SMOOTH_HINT, GL_NICEST
glHint GL_POLYGON_SMOOTH_HINT, GL_NICEST
glCullFace(GL_BACK)
glColor4f(1.0f,1.0f,1.0f,1.0f)
glBlendFunc(GL_SRC_ALPHA,GL_ONE)
glClearColor(fogColor(0), fogColor(1), fogColor(2), fogColor(3))
glShadeModel GL_SMOOTH
glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE)
glFogi(GL_FOG_MODE, GL_LINEAR)
glFogfv(GL_FOG_COLOR, @fogColor(0))
glFogf(GL_FOG_DENSITY, 0.06f)
glHint(GL_FOG_HINT, GL_NICEST)
glFogf(GL_FOG_START, 120)
glFogf(GL_FOG_END, 256)

glEnable GL_TEXTURE_2D
glEnable GL_DEPTH_TEST
glEnable GL_LINE_SMOOTH
glEnable GL_POLYGON_SMOOTH
glEnable GL_FOG
'glDisable GL_FOG
glEnable GL_COLOR_MATERIAL
glEnable GL_NORMALIZE
'glEnable GL_LIGHTING
'glEnable GL_LIGHT0

'
'Textures_Init
'
If loadTextures() = FALSE Then sdl_quit : End
'
Dim Shared As Double RenderGrid(0 To 128,0 To 128), tt, tbm, theta, phi, r, g, b, HMUL, ft
Dim Shared As Integer x, y, dummy
Dim Shared As __VECTOR__ Player, Map, Normal

Dim As Single LightSpc(0 To 3), LightDif(0 To 3), LightAmb(0 To 3), LightPos(0 To 2)

map.x = 32768
map.y = 32768


for y = 0 to 128
   for x = 0 to 128
      RenderGrid(x, y)=getHeight(x+map.x, y+map.y)
   next
next
HMUL = 10

SDL_EnableKeyRepeat(1, 1)
SDL_ShowCursor 0

Do While (done = 0)
   tt=timer
   If Timer - ft > 5 Then
      player.y -= 7.5*tbm
   End If
   
   doEvents()
   
   If player.x >= 1 Then
      map.x += 1
      player.x -= 1
      For y = 0 to 128
         For x = 1 to 128
            RenderGrid(x-1, y) = RenderGrid(x, y)
         Next
         RenderGrid(128, y) = getHeight(x+map.x, y+map.y)
      Next
   ElseIf player.x < 0 Then
      map.x -= 1
      player.x += 1
      For y = 0 to 128
         For x = 127 to 0 step -1
            RenderGrid(x+1, y) = RenderGrid(x, y)
         Next
         RenderGrid(0, y) = getHeight(x+map.x, y+map.y)
      Next
   End If
   If player.y >= 1 Then
      map.y += 1
      player.y -= 1
      For x = 0 to 128
         For y = 1 to 128
            RenderGrid(x, y-1) = RenderGrid(x, y)
         Next
         RenderGrid(x, 128) = getHeight(x+map.x, y+map.y)
      Next
   ElseIf player.y <= -1 Then
      map.y -= 1
      player.y += 1
      For x = 0 to 128
         For y = 127 to 0 step -1
            RenderGrid(x, y+1) = RenderGrid(x, y)
         Next
         RenderGrid(x, 0) = getHeight(x+map.x, y+map.y)
      Next
   End If
   
   glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT)
   
   
   glPushMatrix
   glTranslatef -128-player.x*4, 32+(player.y*1.5), -256-player.y*4
   glScalef 4, 4, 4
   glRotatef 20, 1, 0, 0
   glColor4f 1, 1, 1, 1
   For y = 0 to 64
      For x = 0 to 64
         r = (RenderGrid(x, y+1)+RenderGrid(x, y)+RenderGrid(x+1, y))/3
         g = (RenderGrid(x, y+1)+RenderGrid(x, y)+RenderGrid(x+1, y))/3
         b = (RenderGrid(x, y+1)+RenderGrid(x, y)+RenderGrid(x+1, y))/3
         glColor4f r, g, b, 1.0
         If r < 0.6 Then
            glBindTexture GL_TEXTURE_2D, texture(1)
         ElseIf r < 0.7 Then
            glColor4f 0.25+r, 0.25+g, 0.25+b, 1
            glBindTexture GL_TEXTURE_2D, texture(2)
         ElseIf r < 0.8 Then
            glBindTexture GL_TEXTURE_2D, texture(3)
         ElseIf r < 0.85 Then
            glBindTexture GL_TEXTURE_2D, texture(4)
         ElseIf r < 0.95 Then
            glBindTexture GL_TEXTURE_2D, texture(5)
         ElseIf r > 0.95 Then
            glBindTexture GL_TEXTURE_2D, texture(6)
         Else
            glBindTexture GL_TEXTURE_2D, 0
         End If
         
         glBegin GL_TRIANGLES
            glTexCoord2f( 0, 1 )
            glVertex3f( x, RenderGrid(x, y+1)*HMUL, y+1)
            glTexCoord2f( 0, 0 )
            glVertex3f( x, RenderGrid(x, y)*HMUL, y)
            glTexCoord2f( 1, 0 )
            glVertex3f( x+1, RenderGrid(x+1, y)*HMUL, y)
         glEnd
         
         r = ((RenderGrid(x, y+1)+RenderGrid(x+1, y)+RenderGrid(x+1, y+1))/3)
         g = ((RenderGrid(x, y+1)+RenderGrid(x+1, y)+RenderGrid(x+1, y+1))/3)
         b = ((RenderGrid(x, y+1)+RenderGrid(x+1, y)+RenderGrid(x+1, y+1))/3)
         glColor4f r, g, b, 1
         If r < 0.6 Then
            glBindTexture GL_TEXTURE_2D, Texture(1)
         ElseIf r < 0.7 Then
            glColor4f 0.25+r, 0.25+g, 0.25+b, 1
            glBindTexture GL_TEXTURE_2D, texture(2)
         ElseIf r < 0.8 Then
            glBindTexture GL_TEXTURE_2D, texture(3)
         ElseIf r < 0.85 Then
            glBindTexture GL_TEXTURE_2D, texture(4)
         ElseIf r < 0.95 Then
            glBindTexture GL_TEXTURE_2D, texture(5)
         ElseIf r > 0.95 Then
            glBindTexture GL_TEXTURE_2D, texture(6)
         Else
            glBindTexture GL_TEXTURE_2D, 0
         End If
         
         glBegin GL_TRIANGLES
            glTexCoord2f( 0, 1 )
            glVertex3f( x, RenderGrid(x, y+1)*HMUL, y+1)
            glTexCoord2f( 1, 0 )
            glVertex3f( x+1, RenderGrid(x+1, y)*HMUL, y)
            glTexCoord2f( 1, 1 )
            glVertex3f( x+1, RenderGrid(x+1, y+1)*HMUL, y+1)
         glEnd
      Next
   Next
   glPopMatrix
   
   glPushMatrix
   glTranslatef -512, 32, -256
   glScalef 4, 4, 4
   glRotatef 20, 1, 0, 0
   glColor4f 0.5, 0.5, 1, 1
   glBindTexture GL_TEXTURE_2D, texture(0)
   glEnable GL_BLEND
      glBegin GL_QUADS
         glTexCoord2f( 0, 0 )
         glVertex3f( 0, 0.5*HMUL, 0)
         glTexCoord2f( 1, 0 )
         glVertex3f( 256, 0.5*HMUL, 0)
         glTexCoord2f( 1, 1 )
         glVertex3f( 256, 0.5*HMUL, 64)
         glTexCoord2f( 0, 1 )
         glVertex3f( 0, 0.5*HMUL, 64)
      glEnd
   glDisable GL_BLEND
   glBindTexture GL_TEXTURE_2D, 0
   glPopMatrix
   
   SDL_GL_SwapBuffers
'   sleep 1
   tbm = (1/(timer-tt)/1000)
Loop

SDL_ShowCursor 1
SDL_Quit

' cleanup
glDeleteTextures 256, @texture(0)

Sub doEvents
   Do While (SDL_PollEvent(@event))
      If (event.type = SDL_QUIT_) Then done = 1
      If (event.type = SDL_MOUSEMOTION) Then
         ' not used in this example
         SDL_GetRelativeMouseState( @icursor_x, @icursor_y)
         cursor_x += icursor_x
         cursor_y -= icursor_y
      EndIf
      If (event.type = SDL_MOUSEBUTTONDOWN) Then
         Select Case SDL_GetMouseState(NULL, NULL)
            Case SDL_BUTTON(SDL_BUTTON_LEFT)
            Case SDL_BUTTON(SDL_BUTTON_MIDDLE)
            Case SDL_BUTTON(SDL_BUTTON_RIGHT)
            Case SDL_BUTTON(SDL_BUTTON_WHEELUP)
            Case SDL_BUTTON(SDL_BUTTON_WHEELDOWN)
         End Select         
      EndIf
      If (event.type = SDL_KEYDOWN) Then
         Select Case event.key.keysym.sym
            Case SDLK_ESCAPE
               done = 1
            Case SDLK_UP
            Case SDLK_DOWN
            Case SDLK_LEFT
            Case SDLK_RIGHT
            Case SDLK_PAGEUP
            Case SDLK_PAGEDOWN
            Case SDLK_KP2
            Case SDLK_KP8
            Case SDLK_KP6
            Case SDLK_KP4
            Case SDLK_KP1
            Case SDLK_KP3
            Case SDLK_SPACE
            Case SDLK_1
            Case SDLK_2
            Case SDLK_w
               player.y -= 5*tbm
               ft = timer
            Case SDLK_a
               player.x -= 5*tbm
               ft = timer
            Case SDLK_s
               player.y += 5*tbm
               ft = timer
            Case SDLK_d
               player.x += 5*tbm
               ft = timer
            Case SDLK_r
          End Select                       
       End If
   Loop
End Sub

'   if YaglKeyboard_isKeyPressed(asc("w")) then
'      player.y-=5*tbm
'      ft=timer
'   end if
'   if YaglKeyboard_isKeyPressed(asc("a")) then
'      player.x-=5*tbm
'      ft=timer
'   end if
'   if YaglKeyboard_isKeyPressed(asc("s")) then
'      player.y+=5*tbm
'      ft=timer
'   end if
'   if YaglKeyboard_isKeyPressed(asc("d")) then
'      player.x+=5*tbm
'      ft=timer
'   end if
   
Function loadTextures() As Integer
   Dim status As Integer = TRUE
   status AND= loadGLTexture(ExePath + "\Textures\water.png",0)
   status AND= loadGLTexture(ExePath + "\Textures\desert.png",1)
   status AND= loadGLTexture(ExePath + "\Textures\flatlands.png",2)
   status AND= loadGLTexture(ExePath + "\Textures\dense_forest.png",3)
   status AND= loadGLTexture(ExePath + "\Textures\barrens.png",4)
   status AND= loadGLTexture(ExePath + "\Textures\mountains.png",5)
   status AND= loadGLTexture(ExePath + "\Textures\ice.png",6)
   Return status   
End Function
       
Function loadGLTexture(file_name As String, texture_num As GLuint) As Integer
   Dim status As Integer = FALSE
   Dim As Any Ptr img
   Dim As Uinteger w, h
 
   img = png_load( file_name, PNG_TARGET_OPENGL )
   
   If img <> NULL Then
      png_dimensions( file_name, w, h )
      status = TRUE
       glGenTextures 1, @texture(texture_num)
      glBindTexture GL_TEXTURE_2D, texture(texture_num)
      glTexImage2D GL_TEXTURE_2D, 0, 4,w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, img
      glTexParameteri GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR
      glTexParameteri GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR
      Deallocate( img )
   End If
   Return status
End Function



JohnB
JohnB
Posts: 236
Joined: Jul 22, 2005 3:53
Location: Minnesota Arizona

Postby JohnB » Mar 08, 2008 10:40

P.S.

Landscape look good. I especially like the landscape under the water.

JohnB
Z!re

Re: Question

Postby Z!re » Mar 08, 2008 12:31

[Content removed at author's request]
JohnB
Posts: 236
Joined: Jul 22, 2005 3:53
Location: Minnesota Arizona

Postby JohnB » Mar 09, 2008 2:11

I don't know about you, but I find it kind of hard to compile a .bi file since FBC won't even accept it...


I am not compiling the WoM.bi, I am just using it as an include file when I compile the modified PIM.bas file. The modified PIH.bas file compiles and executes, I must be doing something right, and the resulting display look almost the same as the original PIH executable.

One of section of code that seem to executes very slowly is :

Code: Select all

for y = 0 to 128
   for x = 0 to 128
      RenderGrid(x, y)=getHeight(x+map.x, y+map.y)
   next
next


The code has nothing to do with SDL, it simply call routines defined in WoM.bi. SDL is simplely use to setup OpenGL. According to some text in the WoM.bi file :

Code: Select all

' Changes include: Removal of all lookup tables
' This makes getHeight slower, but it is now capable of generating infinite terrain
' No more resetNow since the lookup tables are gone
' Function clutter cleaned up


Was the PIH you supplied in the .rar compiled using the include file with the lookup table version of WoM or the WoM in the .rar file, or is PIH.exe of unknown origin? I am just curious as to why I have the differences in execution speeds, ie , fps.

Thanks

JohnB

Return to “Tips and Tricks”

Who is online

Users browsing this forum: MSN [Bot] and 1 guest