Dark Bit Factory & Gravity

PROGRAMMING => General coding questions => Topic started by: va!n on August 19, 2011

Title: Trying to find out, how to code a voxel engine
Post by: va!n on August 19, 2011
I am a big fan of voxel engines as seen in some amiga demos and where you can fly arround. I dont know how to code such a great voxel engine and so i thought about it and tried something myself with following result, which doesnt looks bad. But now i have reached the point, where i dont know how to continue nor if my solution is the right way or not.

Code: [Select]
; ************************************************************
; *
; * Trying to find out, how to code a voxel engine. v0.1
; * Thorsten Will (aka 'Mr.Vain / Secretly!')
; * PureBasic - 19-Aug-2011
; *
; ************************************************************

  InitSprite()
 
  OpenWindow(0, 0, 0, 800, 600, "Trying to find out, how to code a voxel engine - Mr.Vain/Secretly!", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  OpenWindowedScreen(WindowID(0), 0, 0, 800, 600, 0, 0, 0)
 
  Dim MapData.l(512,512)

  ;// -------- Load Map and save it into array
 
  LoadSprite(0,"d:\map.bmp") 
 
  StartDrawing(SpriteOutput(0))
      For x = 0 To 500-1
          For y = 0 To 500-1
              MapData(x,y) = Point(x,y)         
          Next
      Next
  StopDrawing() 
 
  ;// -------- Mainloop (drawing is very slow)
         
  Repeat
     Event = WindowEvent()
     ClearScreen(RGB(0, 0, 0))       
       
     StartDrawing(ScreenOutput())         
     
     For y = 0 To 500-1
       For x = 0 To 500-1
           value = MapData(x,y)
           Line(x,300-Red(value)+y,1, Red(value)+y,value)
          ;LineXY(x,300-Red(value)+y,x, Red(value)+y,value)
       Next
     Next
     
     StopDrawing()     
     
     FlipBuffers()
  Until Event = #PB_Event_CloseWindow

Attachement: Map.png (the used map) ... VoxelTest.png (my first generated result)
Title: Re: Trying to find out, how to code a voxel engine
Post by: hellfire on August 19, 2011
First of all this is not "voxel" because there's no actual volume involved (you're just drawing vertical lines). The technique is called wave surfing (http://www.flipcode.com/archives/Realtime_Voxel_Landscape_Engines-Part_2_Rendering_the_Landscapes_Structure.shtml).

To make this more efficient you would start drawing lines at the bottom (front) and go up to the top (back).
Actually you start a fair bit below the bottom because some of these lines can still reach into the screen.
Store the current height of a drawn vertical line for each coloumn.
If you process the next row you only draw that part of the line which goes beyond the already drawn height.
This way you can skip many lines which are hidden behind already drawn mountains.

To get perspective you need to scale the coloumn which you pick from your height-map, eg:
For the top-most row use a range of -500..500 for x,
for the bottom row use a range of -200..+200 for x.
-> the mountains get bigger to the front.
This is basically perspective texture mapping but much easier as the depth is constant for each row.

Finally scale your height along y,
so you're simply drawing longer lines at the bottom and shorter lines at the top.

Another thing you might want to add is rotation.
To do that you basically include a classic rotozoomer before picking values from your height-map.
Title: Re: Trying to find out, how to code a voxel engine
Post by: va!n on August 28, 2011
Thanks for the info and for the link. Seems not really so easy as i thought.
I will try to follow and understand the link.