KVX file viewer as per the old build games - Duke Nukem 3D, Blood, Shadow Warrior etc.
Very pixelated stuff I might add, realy only usefull for weird retro games.
The attached file contains a few voxels and an editor by the original creator Ken Silverman and is available at his website:
http://advsys.net/ken/download.htm#slab6
a few more voxel packs can be found at http://moreplease.free.fr/HRP/Devan/voxeldownload.html
http://moreplease.free.fr/HRP/Devan/voxeldownload.html
However these extra packs might call for an upgrade to handle more than 256 colors (vow)
This is a very rough code, and I'd much appreciate some help with the coloring, as I have only been able to come up with two solutions for coloring - creating a mesh with FVF format 338 and coloring with the diffuse parameters, or as 274 and creating an image containing the palette. The later of these is much more colorful (and usefull) however I hate having to create an image just to color a pixelated model.
FVF 274:
sync on
ptr=KXF2OBJ("duke.kvx",find free object())
fade object ptr,500
position camera 0,25,-25
sync
do
//print screen fps()
control camera using arrowkeys 0,0.1,1
if scancode()=44 then position camera camera position x(),camera position y()-0.1,camera position z()
if scancode()=30 then position camera camera position x(),camera position y()+0.1,camera position z()
//rotate camera 0,wrap(joystick twist z()/100,0,360),0
//position camera joystick x()/-100,joystick z()/100,joystick y()/100
sync
loop
rem OPEN DATAFILE TO READ
function KXF2OBJ(kvx_file$,obj)
bank=find free bank(1,256)
make bank from file bank,kvx_file$
size=file size(kvx_file$)
numbytes=bank integer(bank,0)
xsiz=bank integer(bank,4)
ysiz=bank integer(bank,8)
zsiz=bank integer(bank,12)
xpivot=bank integer(bank,16)
ypivot=bank integer(bank,20)
zpivot=bank integer(bank,24)
palet=pal2image(get bank ptr(bank)+numbytes+4)
//errormessage str$(bank word(bank,24+xsiz*4)+28+bank word(bank,28+((xsiz+1)*4)+((xsiz-1)*(ysiz+1)*2)+2*(ysiz-1)+2))+"-"+str$(size-(bank word(bank,24+xsiz*4)+28+bank word(bank,28+((xsiz+1)*4)+((xsiz-1)*(ysiz+1)*2)+2*(ysiz-1)+2)))
memblock=find free memblock() : make memblock memblock,12+96*(xsiz*ysiz*zsiz*6) : v_ptr=get memblock ptr(memblock)
poke dword v_ptr,274 : poke dword v_ptr+4,32
// poke dword v_ptr,338 : poke dword v_ptr+4,36
for x=1 to xsiz
for layer=1 to ysiz
offset=bank word(bank,24+x*4)+28
tower_end=offset+bank word(bank,28+((xsiz+1)*4)+((x-1)*(ysiz+1)*2)+2*(layer-1)+2)
ptr=offset+bank word(bank,28+((xsiz+1)*4)+((x-1)*(ysiz+1)*2)+2*(layer-1))
while ptr<tower_end
z=bank byte(bank,ptr)
height=bank byte(bank,ptr+1)
face=bank byte(bank,ptr+2)
offset=size+bank byte(bank,ptr+3)*3
c=bank byte(bank,ptr+3)
if BIT GET(face,4)
write_pal(v_ptr,c,x,z+1,layer+1,x+1,z+1,layer+1,x+1,z+1,layer)
write_pal(v_ptr,c,x,z+1,layer,x,z+1,layer+1,x+1,z+1,layer)
endif
for pix=1 to height
offset=size+bank byte(bank,ptr+2+pix)*3
obj=find free object()
c=bank byte(bank,ptr+2+pix)
if BIT GET(face,0)
write_pal(v_ptr,c,x,z+pix,layer,x,z+pix+1,layer,x,z+pix,layer+1)
write_pal(v_ptr,c,x,z+pix+1,layer,x,z+pix+1,layer+1,x,z+pix,layer+1)
endif
if BIT GET(face,1)
write_pal(v_ptr,c,x+1,z+pix,layer,x+1,z+pix,layer+1,x+1,z+pix+1,layer)
write_pal(v_ptr,c,x+1,z+pix+1,layer+1,x+1,z+pix+1,layer,x+1,z+pix,layer+1)
endif
if BIT GET(face,2)
write_pal(v_ptr,c,x+1,z+pix+1,layer,x,z+pix+1,layer,x+1,z+pix,layer)
write_pal(v_ptr,c,x,z+pix,layer,x+1,z+pix,layer,x,z+pix+1,layer)
endif
if BIT GET(face,3)
write_pal(v_ptr,c,x,z+pix+1,layer+1,x+1,z+pix+1,layer+1,x+1,z+pix,layer+1)
write_pal(v_ptr,c,x,z+pix,layer+1,x,z+pix+1,layer+1,x+1,z+pix,layer+1)
endif
next pix
if BIT GET(face,5)
write_pal(v_ptr,c,x,z+height+1,layer+1,x+1,z+height+1,layer,x+1,z+height+1,layer+1)
write_pal(v_ptr,c,x,z+height+1,layer,x+1,z+height+1,layer,x,z+height+1,layer+1)
endif
ptr=ptr+2+pix
endwhile
next layer
next x
mesh=find free mesh() : make mesh from memblock mesh,memblock : delete memblock memblock
make object obj,mesh,palet : delete mesh mesh : delete bank bank
endfunction obj
function write_pal(ptr,pal,x1,y1,z1,x2,y2,z2,x3,y3,z3)
poke dword ptr+8,peek dword(ptr+8)+3 : ptr=ptr+32*(peek dword(ptr+8)-3)
poke float ptr+12,x1 : poke float ptr+16,-1*y1 : poke float ptr+20,z1
poke float ptr+36,0.003906*pal : poke float ptr+40,0
poke float ptr+44,x2 : poke float ptr+48,-1*y2 : poke float ptr+52,z2
poke float ptr+68,0.003906*pal : poke float ptr+72,1
poke float ptr+76,x3 : poke float ptr+80,-1*y3 : poke float ptr+84,z3
poke float ptr+100,0.003906*pal+0.002 : poke float ptr+104,1
endfunction
function pal2image(ptr)
memblock=find free memblock() : image=find free image()
make memblock memblock,14+256*4
write memblock dword memblock,0,256 : write memblock dword memblock,4,1 : write memblock dword memblock,8,32
for t=0 to 255 : write memblock byte memblock,14+t*4,peek byte(ptr+t*3) : write memblock byte memblock,13+t*4,peek byte(ptr+1+t*3) : write memblock byte memblock,12+t*4,peek byte(ptr+2+t*3) : next t
make image from memblock image,memblock : delete memblock memblock
endfunction image
FVF 338:
sync on
ptr=KXF2OBJ("duke.kvx",find free object())
position camera 0,25,-25
sync
do
//print screen fps()
getevent
control camera using arrowkeys 0,0.1,1
if scancode()=44 then position camera camera position x(),camera position y()-0.1,camera position z()
if scancode()=30 then position camera camera position x(),camera position y()+0.1,camera position z()
//rotate camera 0,wrap(joystick twist z()/100,0,360),0
//position camera joystick x()/-100,joystick z()/100,joystick y()/100
sync
loop
rem OPEN DATAFILE TO READ
function KXF2OBJ(kvx_file$,obj)
bank=find free bank(1,256)
make bank from file bank,kvx_file$
numbytes=bank integer(bank,0)
xsiz=bank integer(bank,4)
ysiz=bank integer(bank,8)
zsiz=bank integer(bank,12)
xpivot=bank integer(bank,16)
ypivot=bank integer(bank,20)
zpivot=bank integer(bank,24)
size=numbytes+772
//errormessage str$(bank word(bank,24+xsiz*4)+28+bank word(bank,28+((xsiz+1)*4)+((xsiz-1)*(ysiz+1)*2)+2*(ysiz-1)+2))+"-"+str$(size-(bank word(bank,24+xsiz*4)+28+bank word(bank,28+((xsiz+1)*4)+((xsiz-1)*(ysiz+1)*2)+2*(ysiz-1)+2)))
memblock=find free memblock() : make memblock memblock,12+108*(xsiz*ysiz*zsiz*6) : v_ptr=get memblock ptr(memblock)
//poke dword v_ptr,274 : poke dword v_ptr+4,32
poke dword v_ptr,338 : poke dword v_ptr+4,36
for x=1 to xsiz
for layer=1 to ysiz
offset=bank word(bank,24+x*4)+28
tower_end=offset+bank word(bank,28+((xsiz+1)*4)+((x-1)*(ysiz+1)*2)+2*(layer-1)+2)
ptr=offset+bank word(bank,28+((xsiz+1)*4)+((x-1)*(ysiz+1)*2)+2*(layer-1))
while ptr<tower_end
z=bank byte(bank,ptr)
height=bank byte(bank,ptr+1)
face=bank byte(bank,ptr+2)
offset=size+bank byte(bank,ptr+3)*3
if BIT GET(face,4)
write_vox(v_ptr,palette(bank byte(bank,offset-768),bank byte(bank,offset-767),bank byte(bank,offset-766)),x,z+1,layer+1,x+1,z+1,layer+1,x+1,z+1,layer)
write_vox(v_ptr,palette(bank byte(bank,offset-768),bank byte(bank,offset-767),bank byte(bank,offset-766)),x,z+1,layer,x,z+1,layer+1,x+1,z+1,layer)
endif
for pix=1 to height
offset=size+bank byte(bank,ptr+2+pix)*3
obj=find free object()
if BIT GET(face,0)
write_vox(v_ptr,palette(bank byte(bank,offset-768),bank byte(bank,offset-767),bank byte(bank,offset-766)),x,z+pix,layer,x,z+pix+1,layer,x,z+pix,layer+1)
write_vox(v_ptr,palette(bank byte(bank,offset-768),bank byte(bank,offset-767),bank byte(bank,offset-766)),x,z+pix+1,layer+1,x,z+pix,layer+1,x,z+pix+1,layer)
endif
if BIT GET(face,1)
write_vox(v_ptr,palette(bank byte(bank,offset-768),bank byte(bank,offset-767),bank byte(bank,offset-766)),x+1,z+pix,layer,x+1,z+pix,layer+1,x+1,z+pix+1,layer)
write_vox(v_ptr,palette(bank byte(bank,offset-768),bank byte(bank,offset-767),bank byte(bank,offset-766)),x+1,z+pix+1,layer+1,x+1,z+pix+1,layer,x+1,z+pix,layer+1)
endif
if BIT GET(face,2)
write_vox(v_ptr,palette(bank byte(bank,offset-768),bank byte(bank,offset-767),bank byte(bank,offset-766)),x,z+pix+1,layer,x+1,z+pix,layer,x+1,z+pix+1,layer)
write_vox(v_ptr,palette(bank byte(bank,offset-768),bank byte(bank,offset-767),bank byte(bank,offset-766)),x,z+pix,layer,x+1,z+pix,layer,x,z+pix+1,layer)
endif
if BIT GET(face,3)
write_vox(v_ptr,palette(bank byte(bank,offset-768),bank byte(bank,offset-767),bank byte(bank,offset-766)),x,z+pix+1,layer+1,x+1,z+pix+1,layer+1,x+1,z+pix,layer+1)
write_vox(v_ptr,palette(bank byte(bank,offset-768),bank byte(bank,offset-767),bank byte(bank,offset-766)),x,z+pix,layer+1,x,z+pix+1,layer+1,x+1,z+pix,layer+1)
endif
next pix
if BIT GET(face,5)
write_vox(v_ptr,palette(bank byte(bank,offset-768),bank byte(bank,offset-767),bank byte(bank,offset-766)),x,z+height+1,layer+1,x+1,z+height+1,layer,x+1,z+height+1,layer+1)
write_vox(v_ptr,palette(bank byte(bank,offset-768),bank byte(bank,offset-767),bank byte(bank,offset-766)),x,z+height+1,layer,x+1,z+height+1,layer,x,z+height+1,layer+1)
endif
ptr=ptr+2+pix
endwhile
next layer
next x
mesh=find free mesh()
make mesh from memblock mesh,memblock
delete memblock memblock
load image "C:\Users\o.nielsen\Desktop\bit.bmp",1
make object obj,mesh
delete bank bank
endfunction obj
function write_vox(ptr,color as dword,x1,y1,z1,x2,y2,z2,x3,y3,z3)
poke dword ptr+8,peek dword(ptr+8)+3 : ptr=ptr+36*(peek dword(ptr+8)-3)
poke float ptr+12,x1 : poke float ptr+16,-1*y1 : poke float ptr+20,z1
poke float ptr+24,10 : poke float ptr+28,10 : poke float ptr+32,0
poke dword ptr+36,color
poke float ptr+48,x2 : poke float ptr+52,-1*y2 : poke float ptr+56,z2
poke float ptr+60,10 : poke float ptr+64,10 : poke float ptr+68,0
poke dword ptr+72,color
poke float ptr+84,x3 : poke float ptr+88,-1*y3 : poke float ptr+92,z3
poke float ptr+96,10 : poke float ptr+100,10 : poke float ptr+104,0
poke dword ptr+108,color
endfunction
function palette(r,g,b)
color=rgb(128,r,g,b)
endfunction color