Voxel Graphics Library

User projects written in or related to FreeBASIC.
gothon
Posts: 200
Joined: Apr 11, 2011 22:22

Voxel Graphics Library

Postby gothon » Jun 23, 2012 4:01

I am currently working on a simple graphics programming library for voxels. I'm still working on writting the documentation. However, I have 5 simple working examples uploaded now. If anyone wants to test it out and make some voxel programs of their own they can look at the examples and download the files from here:

http://vast3d.com/VoxelGFX/
VANYA
Posts: 1181
Joined: Oct 24, 2010 15:16
Location: Ярославль
Contact:

Re: Voxel Graphics Library

Postby VANYA » Jun 23, 2012 4:32

Hi gothon!

Getting a good
Lachie Dazdarian
Posts: 2222
Joined: May 31, 2005 9:59
Location: Croatia
Contact:

Re: Voxel Graphics Library

Postby Lachie Dazdarian » Jun 23, 2012 20:49

Wow gothon! This is wicked! Loved the drawing on the sphere example. Looking forward to the documentation, and I'm hoping to see some height map-terrain generator. :)
Lachie Dazdarian
Posts: 2222
Joined: May 31, 2005 9:59
Location: Croatia
Contact:

Re: Voxel Graphics Library

Postby Lachie Dazdarian » Jul 09, 2012 20:58

Any news/progress on this?
gothon
Posts: 200
Joined: Apr 11, 2011 22:22

Re: Voxel Graphics Library

Postby gothon » Aug 11, 2012 10:17

Sorry for taking so long to reply. I now have the documentation written and I have hidden the internal structures of the library behind a namespace to prevent name conflicts with the user program. I will post on further updates as I get done with them.
Lachie Dazdarian
Posts: 2222
Joined: May 31, 2005 9:59
Location: Croatia
Contact:

Re: Voxel Graphics Library

Postby Lachie Dazdarian » Sep 15, 2012 21:50

Any news?
gothon
Posts: 200
Joined: Apr 11, 2011 22:22

Re: Voxel Graphics Library

Postby gothon » Sep 22, 2012 9:06

Yes, I have fixed some bugs and added a few functions.

You can now create multiple drawing contexts and hence multiple perspectives using VoxNewContext, and VoxSetContext with the Vox_Context type.
Using 'VOXEL_SCREEN' you can reference the default 'screen' volume associated with a context. Which is different from passing '0' as I did in the examples.
If you ever have to reinitialize opengl (ie changing screen modes), you can now reload all the volume textures by calling VoxReloadVolumes.
You can now draw triangles by passing 3 points to VoxTriangle, 2 points to VoxTriangleTo, or 1 point to VoxTriangleFanTo and VoxTriangleStripTo.

I am currently working on a drawing program based on the library. It is not yet ready for me to release a demo, but I'll share some screenshots:
ImageImage
gothon
Posts: 200
Joined: Apr 11, 2011 22:22

Re: Voxel Graphics Library

Postby gothon » Oct 25, 2012 10:45

I now have a usable drawing program based on VoxelGFX. Some of the interface is crude at the moment, but it will serve as a good launching point.

Precompiled (windows) http://vast3d.com/VoxelGFX/bin/CubeDraw.zip
Source Code http://github.com/gothon/CubeDraw

It will load any .png file you pass to its command line (eg by drag/dropping onto the .exe file icon), and automatically save back to the original file on program exit. By default it will create a 32x32x32 image and save it to Untitled.png.

Rotation can be accomplished via the right mouse button.
Panning is done through the middle mouse button, and zooming via the mouse wheel.
Rotation, panning, and zooming are centered on the selected voxel. When no voxel is selected rotation is around the yellow cross-haired 'center' point which is initially the center of the volume, but will move with you as you move around. Panning uses the side of the voxel you click on as the panning plane in addition to voxel itself.

The program features 3 clickable grids which can be moved by dragging the arrows pointing from the corners, and re-sized using the wedge behind the arrows. After re-sizing the grids you can press 'enter' to make the volume re-size to match.

The button bar contains 7 buttons, which can be clicked on or activated by pressing 1-7:
1. Allows color selection via a 3D RGB cube. (click one of the corner cubes to get transparent color)
2. Allows you to pick colors from the volume (not from the display)
3. Allows you to toggle Building/Painting. In the first mode you can place voxels on top of each other to build up a model; in the second you can change the color of surface voxels without placing new ones.
4. Allows you to place individual voxels.
5. Allows you to draw lines by selecting 2 points.
6. Allows you to draw triangles by selecting 3 points.
7. Allows you to select a clipping plane, which will hide part of the volume so that you can edit the inside of a closed object.

Image
gothon
Posts: 200
Joined: Apr 11, 2011 22:22

Re: Voxel Graphics Library

Postby gothon » Dec 23, 2012 13:02

In addition to some bug fixes in VoxBlit, Text support is now available in VoxelGFX. I also added a 'VoxStep(x,y,z)' function that works like the 'STEP' keyword in FBGFX in that it returns the sum of its parameter(s) with the voxel graphics cursor.

There is no built in font in VoxelGFX, but you can make one rather easily. The 'VoxNewFont' function will take a Volume as the source for the glyphs, the character size, and the number of characters as required parameters. It will automatically interpret the characters sequentially in the order it finds them iterating over the volume. However, you can also specify a portion of the volume to iterate over, or even specify each character's individual position, width, and display width by passing the address of buffers containing this data.

The following program builds an ASCII font using FBGFX to generate the characters.

Code: Select all

#Include "VoxelGFX.bi"

#Include "fbgfx.bi"
#Include "GL/gl.bi"

Function BuildASCIIFont() As Vox_Font
    'Fill an FBGFX Image with FBGFX font characters
    Dim As Any Ptr Pixels, IP = ImageCreate(128, 128, 0, 32)
    Dim As Integer X, Y, I = 0, ByPP, Pitch, Size
    For Y = 0 To 127 Step 8
        For X = 0 To 127 Step 8
            Draw String IP, (X, Y), Chr(I)
            I += 1
        Next X
    Next Y
    'Copy the Image buffer to a Voxel Volume
    Var FontVol = VoxNewVolume(128, 128, 1)
    If 0 <> ImageInfo(IP, , , ByPP, Pitch, Pixels, Size) Then Beep: End -1
    If ByPP <> 4 Then Beep: End -1
   
    VoxVolumeLock
    Dim As UInteger Ptr Row, Voxels = VoxVolumePtr
    I = 0
    For Y = 0 To 127
        If (127-Y)* Pitch >= Size Then Beep: End -1
        Row = Cast(UInteger Ptr, Pixels + (127-Y)* Pitch)
        For X = 0 To 127
            Voxels[I] = Row[X]
            I += 1
        Next X
    Next Y
    VoxVolumeUnlock
    ImageDestroy IP
   
    'Build a Voxel Font and return it to the caller
    Return VoxNewFont(FontVol, Vec3I(8,8,1), 256, , , , , , , VOXEL_FONT_FLIP_Y)
End Function

'Initialize Graphics display
ScreenRes 800, 600, 32, , FB.GFX_OPENGL
VoxInit @ScreenGLProc, VOXEL_SCREEN64
VoxScreenRes Vec3I(64, 64, 64),  RGB(125, 176, 253)

'Build our ASCII Font from FBGFX
Var Font = BuildASCIIFont

'Draw Stuff
VoxSetVolume VOXEL_SCREEN

VoxSetColor RGB(0, 255, 0)
For I As Integer = 1 To 20
    VoxBlitRightRotate Int(Rnd*3+1)
    VoxBlitText Vec3I(1 + Rnd*60,1+Rnd*60,1+Rnd*55), Chr(Int(Rnd*256))
Next I

VoxSetColor RGB(0, 0, 255)
VoxSetBlitDefault
VoxBlitText Vec3I(10,20,62), "Hello"

'Run message loop
Dim As Double PrevT = Timer, dT = 1
Do
    dT = (Timer - PrevT)
    PrevT = Timer
    'Read the Keyboard
    If MultiKey(FB.SC_RIGHT) Then VoxScreenTurnRight dT
    If MultiKey(FB.SC_LEFT) Then VoxScreenTurnRight -dT
    If MultiKey(FB.SC_DOWN) Then VoxScreenTurnDown dT
    If MultiKey(FB.SC_UP) Then VoxScreenTurnDown -dT
   
    'Render to the Display
    VoxRender 800, 600
    glRotated -45, 1,1,0
    VoxRenderText "BASIC += 1"
    Flip
Loop Until InKey = Chr(255) & "k" Or MultiKey(FB.SC_ESCAPE)


I was able to download a free to use Unicode Bitmap font from http://unifoundry.com/unifont.html and converted it to a couple of files I zipped here: (using the uncalled function provided in the next program)
http://vast3d.com/VoxelGFX/Examples/Unifont.zip

The next program loads these files and is able to display over 50,000 glyphs corresponding to the printable code points of the first plane of the Unicode standard. Each glyph is either 8x16x1 or 16x16x1.

Code: Select all

#Include "VoxelGFX.bi"

#Include "fbgfx.bi"
#Include "GL/gl.bi"

Function ParseHexFont() As Vox_Font
    Dim As Integer X, Y, Z, CharCode, CharWidth, I, J, K
    Const CharMax = 65533 '63446 Actual Glyphs, 52637 Unique
    ReDim CharPosn(CharMax) As Vec3I
    ReDim CharW(CharMax) As Integer
    ReDim CharDat(CharMax) As String
    Dim As String S, LineData
    Var CharSize = Vec3I(0,16, 1)
   
    Open "unifont-5.1.20080820.hex" For Input As #1
        Do Until EOF(1)
            Line Input #1, S
            CharCode = ValInt("&H" & Left(S, 4))
            S = Right(S, Len(S)-5)
            CharDat(CharCode) = S
           
            If Len(S) = 64 Then
                CharWidth = 16 '16x16
               Else
                If Len(S) <> 32 Then Error 150
                CharWidth = 8 '8x16
            End If
            CharW(CharCode) = CharWidth
           
            For I = 0 To CharCode-1
                If CharDat(I) = S Then Exit For
            Next I
            If I = CharCode Then
                If X + CharWidth > 512 Then 'Advance to the next line
                    X = 0
                    Y += CharSize.Y
                    If Y + CharSize.Y > 512 Then Y = 0: Z += CharSize.Z 'Next page
                End If
                CharPosn(CharCode) = Vec3I(X, Y, Z)
                X += CharWidth
               Else
                CharPosn(CharCode) = CharPosn(I)
            End If
        Loop
    Close #1
   
    Var Src = VoxNewVolume(512, 512, Z+1)
    For I = 0 To CharMax
        CharWidth = CharW(I)
        For J = 0 To 15
            LineData = Bin(ValInt("&H1" & Mid(CharDat(I), 1+J*CharWidth\4, CharWidth\4)))
            LineData = Right(LineData, Len(LineData)-1)
            For K = 0 To Len(LineData)-1
                If Mid(LineData, K+1, 1) = "1" Then
                    VSet CharPosn(I)+Vec3I(K, 15 - J, 0), &HFFFFFFFF
                End If
            Next K
        Next J
    Next I
       
    VoxSaveFile("unifont-5.1.20080820.png", Src)
    Open "unifont-5.1.20080820.dat" For Binary As #1
        Put #1, , CharPosn()
        Put #1, , CharW()
    Close #1
    Return VoxNewFont(Src, CharSize, CharMax+1, @CharPosn(0), @CharW(0))
End Function

Function LoadUniFont() As Vox_Font
    Const CharMax = 65533 '63446 Actual Glyphs, 52637 Unique
    ReDim CharPosn(CharMax) As Vec3I
    ReDim CharWidth(CharMax) As Integer
    Var CharSize = Vec3I(0,16, 1)
    Var Src = VoxLoadFile("unifont-5.1.20080820.png",, Volume_Dynamic)
    Var F = FreeFile
    Open "unifont-5.1.20080820.dat" For Binary As #F
        Get #F, , CharPosn()
        Get #F, , CharWidth()
    Close #F
    Return VoxNewFont(Src, CharSize, CharMax+1, @CharPosn(0), @CharWidth(0))
End Function

Sub DrawCubeEdges(V1 As Vec3I, V2 As Vec3I)
    VoxVolumeLock
    VoxLine Vec3I(V1.X, V1.Y, V1.Z), Vec3I(V1.X, V1.Y, V2.Z)
    VoxLine Vec3I(V1.X, V2.Y, V1.Z), Vec3I(V1.X, V2.Y, V2.Z)
    VoxLine Vec3I(V2.X, V1.Y, V1.Z), Vec3I(V2.X, V1.Y, V2.Z)
    VoxLine Vec3I(V2.X, V2.Y, V1.Z), Vec3I(V2.X, V2.Y, V2.Z)
   
    VoxLine Vec3I(V1.X, V1.Y, V1.Z), Vec3I(V1.X, V2.Y, V1.Z)
    VoxLine Vec3I(V1.X, V1.Y, V2.Z), Vec3I(V1.X, V2.Y, V2.Z)
    VoxLine Vec3I(V2.X, V1.Y, V1.Z), Vec3I(V2.X, V2.Y, V1.Z)
    VoxLine Vec3I(V2.X, V1.Y, V2.Z), Vec3I(V2.X, V2.Y, V2.Z)
   
    VoxLine Vec3I(V1.X, V1.Y, V1.Z), Vec3I(V2.X, V1.Y, V1.Z)
    VoxLine Vec3I(V1.X, V1.Y, V2.Z), Vec3I(V2.X, V1.Y, V2.Z)
    VoxLine Vec3I(V1.X, V2.Y, V1.Z), Vec3I(V2.X, V2.Y, V1.Z)
    VoxLine Vec3I(V1.X, V2.Y, V2.Z), Vec3I(V2.X, V2.Y, V2.Z)
    VoxVolumeUnlock
End Sub

'Initialize Graphics display
ScreenRes 800, 600, 32, , FB.GFX_OPENGL
VoxInit @ScreenGLProc, VOXEL_SCREEN64
VoxScreenRes Vec3I(64, 64, 64),  RGB(125, 176, 253)

'Load our Unicode Font
Var Font = LoadUniFont

'Draw Stuff
VoxSetVolume VOXEL_SCREEN

VoxSetColor RGB(0, 255, 0)
For I As Integer = 1 To 20
    VoxBlitRightRotate Int(Rnd*3+1)
    VoxBlitText Vec3I(1 + Rnd*60,1+Rnd*60,1+Rnd*55), WChr(Rnd*50000)
Next I

VoxSetColor RGB(0, 0, 255)
VoxSetBlitDefault
VoxBlitText Vec3I(10,20,62), "Привет"
VoxBlitText Vec3I(10,36,63), WChr(10037, 9731, 10037)
VoxBlitText Vec3I(10,36,62), WChr(10037, 9731, 10037)
VoxBlitText Vec3I(10,36,61), WChr(10037, 9731, 10037)

VoxSetColor RGB(255, 0, 0)
DrawCubeEdges Vec3I(0,0,0), Vec3I(63, 63, 63)

'Run message loop
Dim As Double PrevT = Timer, dT = 1
Do
    dT = (Timer - PrevT)
    PrevT = Timer
    'Read the Keyboard
    If MultiKey(FB.SC_RIGHT) Then VoxScreenTurnRight dT
    If MultiKey(FB.SC_LEFT) Then VoxScreenTurnRight -dT
    If MultiKey(FB.SC_DOWN) Then VoxScreenTurnDown dT
    If MultiKey(FB.SC_UP) Then VoxScreenTurnDown -dT
   
    'Render to the Display
    VoxRender 800, 600
    glRotated -45, 1,1,0
    VoxRenderText "BASIC " & WChr(10866) & " 1"
    Flip
Loop Until InKey = Chr(255) & "k" Or MultiKey(FB.SC_ESCAPE)


True volume fonts that have a depth (or Z size) greater than 1 are also supported, though these are harder to find. :-P
badmrbox
Posts: 658
Joined: Oct 27, 2005 14:40
Location: Sweden
Contact:

Re: Voxel Graphics Library

Postby badmrbox » Feb 16, 2014 23:43

how in the world do I get this to work?
gothon
Posts: 200
Joined: Apr 11, 2011 22:22

Re: Voxel Graphics Library

Postby gothon » Feb 17, 2014 0:05

badmrbox wrote:how in the world do I get this to work?


If you are running windows you can set up VoxelGFX by:

1) Download the zip file from the linked webpage, eg: http://vast3d.com/VoxelGFX/bin/VoxelGFX.zip

2) Extract the linker (.a) files and the header file (VoxelGFX.bi), and place them in a suitable folder. This can be same folder as your program. Or you can place the linker files in %FreeBasic%\lib\win32, and the header file in %FreeBasic%\inc where %FreeBasic% is where you installed FreeBasic.

3) Write a program using #Include "VoxelGFX.bi", eg test one of the example programs at the webpage.

I have not tested it on Linux however there isn't any OS dependent code in it (besides library dependencies, eg OpenGl), so if you download the source code from github and compile it (to generate the .a linker file), it might just work (fingers crossed). If not, I can probably fix it, since I have virtualized Ubuntu running under VirtualBox now.

If you have any other questions, feel free to ask.
badmrbox
Posts: 658
Joined: Oct 27, 2005 14:40
Location: Sweden
Contact:

Re: Voxel Graphics Library

Postby badmrbox » Feb 17, 2014 0:26

I'm running win7, using Freebasic 0.24 and have done as you described. I get alot of undefined errors like
C:\FreeBASIC\Freebasic0.24\lib\win32/libVoxelGFX.a(VoxelGFX.o):fake:(.text+0x1de4): undefined reference to `wglGetProcAddress@4'

What am I doing wrong?
gothon
Posts: 200
Joined: Apr 11, 2011 22:22

Re: Voxel Graphics Library

Postby gothon » Feb 17, 2014 1:01

badmrbox wrote:I'm running win7, using Freebasic 0.24 and have done as you described. I get alot of undefined errors like
C:\FreeBASIC\Freebasic0.24\lib\win32/libVoxelGFX.a(VoxelGFX.o):fake:(.text+0x1de4): undefined reference to `wglGetProcAddress@4'

What am I doing wrong?


Unfortunetely there does seem to be a compatibility issue between Freebasic 0.24 and 0.9X. You can either update FreeBasic or compile VoxelGFX.a using 0.24. The version in the zip file was compiled using 0.90. I can upload the files for 0.24 if you want them.
badmrbox
Posts: 658
Joined: Oct 27, 2005 14:40
Location: Sweden
Contact:

Re: Voxel Graphics Library

Postby badmrbox » Feb 17, 2014 1:24

I would be grateful if you could do that :).
gothon
Posts: 200
Joined: Apr 11, 2011 22:22

Re: Voxel Graphics Library

Postby gothon » Feb 17, 2014 1:36

Actually, fbc 0.24 it seems to work with the newer .a file, if I add explicit references to all of the dependencies of VoxelGFX to the program.

Code: Select all

#Include "GL\gl.bi"
#Include "GL\glu.bi"
#IncLib "fbpngs

However, this code seems to need libfbpng.a in addition to libfbpngs.a.

@badmrbox
I have upload the 0.24 compiled VoxelGFX files to http://vast3d.com/VoxelGFX/bin/VoxelGFX_fbc024.zip

Return to “Projects”

Who is online

Users browsing this forum: No registered users and 2 guests