Author Topic: Trying to find out, how to code a voxel engine  (Read 4245 times)

0 Members and 1 Guest are viewing this topic.

Offline va!n

  • Pentium
  • *****
  • Posts: 1435
  • Karma: 109
    • View Profile
    • http://www.secretly.de
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)
- hp EliteBook 8540p, 4 GB RAM, Windows 8.1 x64
- Asus P5Q, Intel Q8200, 6 GB DDR2, Radeon 4870, Windows 8.1 x64
http://www.secretly.de
Challenge Trophies Won:

Offline hellfire

  • Sponsor
  • Pentium
  • *******
  • Posts: 1294
  • Karma: 466
    • View Profile
    • my stuff
Re: Trying to find out, how to code a voxel engine
« Reply #1 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.

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.
« Last Edit: August 19, 2011 by hellfire »
Challenge Trophies Won:

Offline va!n

  • Pentium
  • *****
  • Posts: 1435
  • Karma: 109
    • View Profile
    • http://www.secretly.de
Re: Trying to find out, how to code a voxel engine
« Reply #2 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.
- hp EliteBook 8540p, 4 GB RAM, Windows 8.1 x64
- Asus P5Q, Intel Q8200, 6 GB DDR2, Radeon 4870, Windows 8.1 x64
http://www.secretly.de
Challenge Trophies Won: