Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- '$stop_scan
- #include once "fbgfx.bi"
- #include once "gl/gl.bi"
- #include once "gl/glu.bi"
- using FB
- screenres 480, 480, 32, , GFX_OPENGL or GFX_MULTISAMPLE
- '' set up our viewport
- glMatrixMode(GL_PROJECTION)
- glLoadIdentity()
- glViewport(0, 0, 480, 480)
- glLoadIdentity ()
- glFrustum (-0.125/2.0, 0.125/2.0, -0.125/2.0, 0.125/2.0, 0.1, 100.0)
- '' set up color properties
- glClearColor(.125, .125, .125, 0)
- '' set up rendering
- glMatrixMode(GL_MODELVIEW)
- glLoadIdentity()
- glEnable (GL_DEPTH_TEST)
- glEnable (GL_BLEND)
- glEnable (GL_LINE_SMOOTH)
- glHint(GL_LINE_SMOOTH_HINT, GL_FASTEST)
- glHint(GL_POLYGON_SMOOTH_HINT, GL_FASTEST)
- 'glPolygonMode( GL_FRONT_AND_BACK, GL_LINE )
- glShadeModel( GL_FLAT )
- dim as GLfloat fogColor(0 to 3)
- fogColor(0) = 0.5
- fogColor(1) = 0.5
- fogColor(2) = 0.5
- fogColor(3) = 1.0
- glFogi(GL_FOG_MODE, GL_EXP)
- glFogfv(GL_FOG_COLOR, @fogColor(0))
- glFogf(GL_FOG_DENSITY, 0.15)
- glHint(GL_FOG_HINT, GL_DONT_CARE)
- glFogf(GL_FOG_START, 1.0)
- glFogf(GL_FOG_END, 5.0)
- glEnable(GL_FOG)
- glEnable(GL_CULL_FACE)
- type lvoxel
- as byte x, y, z
- as uinteger c
- end type
- type vclr
- r as ubyte
- g as ubyte
- b as ubyte
- a as ubyte
- end type
- type vec3
- x as double
- y as double
- z as double
- end type
- type opface
- c(0 to 15, 0 to 15) as vclr
- end type
- type opfacer
- x1 as integer
- y1 as integer
- x2 as integer
- y2 as integer
- tx as integer
- ty as integer
- k as integer
- fi as integer
- end type
- dim as vclr vtest(0 to 15, 0 to 15, 0 to 15)
- const vsize = 0.05
- const pi = 3.141592
- dim as vec3 camp, camup
- dim as double cama
- camp.x = -32 * vsize
- camp.z = -16 * vsize
- camup.z = -1.0
- cama = 0.0
- dim as GLuint vlist
- declare sub gen_vox ( v() as vclr, l as GLuint )
- declare sub gen_vox_opt ( v() as vclr, l as GLuint )
- declare sub ren_vox ( l as GLuint, x as double, y as double, z as double )
- declare sub ren_vox_rot ( l as GLuint, x as double, y as double, z as double, a0 as double, a1 as double )
- declare sub load_lvox ( filename as string, v() as vclr )
- declare function gfxLoadBufferAsTexture( buffer as any ptr ) As UInteger
- load_lvox("voxel.lst", vtest())
- vlist = GLGenLists(1)
- gen_vox_opt(vtest(), vlist)
- dim as double curT = timer(), lastT = timer()
- do
- if multikey(SC_LEFT) then cama -= (pi / 60.0) / 1.0
- if multikey(SC_RIGHT) then cama += (pi / 60.0) / 1.0
- if multikey(SC_UP) then
- camp.x += cos(cama) * ((vsize * 16.0) / 60.0) * 4.0
- camp.y += sin(cama) * ((vsize * 16.0) / 60.0) * 4.0
- end if
- if multikey(SC_DOWN) then
- camp.x -= cos(cama) * ((vsize * 16.0) / 60.0) * 4.0
- camp.y -= sin(cama) * ((vsize * 16.0) / 60.0) * 4.0
- end if
- glFinish()
- '' begin a new matrix for the polygon set so we can isolate our rendering
- '' and not affect other matrices.
- glPushMatrix()
- 'glTranslatef(0.0, 0.0, -2.0)
- 'glRotatef(curT*100.0, 1.0, 1.0, 2.0)
- gluLookAt(camp.x, camp.y, camp.z, camp.x + cos(cama), camp.y + sin(cama), camp.z, camup.x, camup.y, camup.z)
- for i as integer = 0 to 24
- for j as integer = 0 to 24 step 2
- ren_vox(vlist, cdbl(i), cdbl(j), 0.0)
- next j
- for j as integer = 1 to 24 step 2
- ren_vox_rot(vlist, cdbl(i), cdbl(j), 0.0, 180.0, 0.0)
- next j
- next i
- ren_vox_rot(vlist, 0.0, 1.0, 1.0, 180.0, 180.0)
- ren_vox_rot(vlist, 0.0, 0.0, 1.0, 0.0, 180.0)
- glPopMatrix()
- '' flip our screen
- 'glFlush()
- flip()
- glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT)
- 'sleep(16, 1)
- while 1.0/(timer-curT) > 120.0
- sleep 1
- wend
- '' render important information
- 'open cons for output as #1
- ' print #1, 1/(curT - lastT)
- 'xclose #1
- lastT = curT
- curT = timer()
- loop until multikey(1)
- sub ren_vox ( l as GLuint, x as double, y as double, z as double )
- glPushMatrix()
- glTranslatef(x*16.0*vsize, y*16.0*vsize, -z*16.0*vsize)
- glCallList(l)
- glPopMatrix()
- end sub
- sub ren_vox_rot ( l as GLuint, x as double, y as double, z as double, a0 as double, a1 as double )
- glPushMatrix()
- glTranslatef(x*16.0*vsize, y*16.0*vsize, -z*16.0*vsize)
- glRotatef(a0, 0.0, 0.0, -1.0)
- glRotatef(a1, 0.0, 1.0, 0.0)
- glCallList(l)
- glPopMatrix()
- end sub
- function mvec3( x as double, y as double, z as double) as vec3
- dim as vec3 ret
- ret.x = x
- ret.y = y
- ret.z = z
- return ret
- end function
- sub gen_vox_opt ( v() as vclr, l as GLuint )
- dim as opface f(0 to 15, 0 to 5)
- dim as opfacer fa(0 to (16*16*16*6)-1)
- dim as integer fac = 0
- for i as integer = 0 to 15
- for j as integer = 0 to 15
- for k as integer = 0 to 15
- if v(i,j,k).a > 0 then
- if k = 0 orelse v(i,j,k-1).a = 0 then f(k, 0).c(i,j) = v(i,j,k)
- if k = 15 orelse v(i,j,k+1).a = 0 then f(k, 1).c(i,j) = v(i,j,k)
- if j = 0 orelse v(i,j-1,k).a = 0 then f(j, 2).c(i,k) = v(i,j,k)
- if j = 15 orelse v(i,j+1,k).a = 0 then f(j, 3).c(i,k) = v(i,j,k)
- if i = 0 orelse v(i-1,j,k).a = 0 then f(i, 4).c(j,k) = v(i,j,k)
- if i = 15 orelse v(i+1,j,k).a = 0 then f(i, 5).c(j,k) = v(i,j,k)
- end if
- next k
- next j
- next i
- glEnable (GL_TEXTURE_2D)
- dim as integer texw = 1024, texh = 512
- ' ### Mambazo Edit ###
- dim as uinteger max_x = 0
- ' #####################
- dim as any ptr tex = ImageCreate(texw, texh)
- line (0,0)-step(texw, texh), rgba(0,0,0,255), bf
- for k as integer = 0 to 15
- for fi as integer = 0 to 5
- dim as opface cf = f(k, fi)
- do
- dim as integer bx1=-1, by1, bx2, by2
- for x1 as integer = 0 to 15
- for y1 as integer = 0 to 15
- if cf.c(x1,y1).a > 0 then
- dim as integer x2 = x1, y2 = y1, any1 = 1
- while x2 < 16 and y2 < 16 and any1 = 1
- dim as integer cany
- any1 = 0
- x2 += 1
- cany = 1
- for i as integer = y1 to y2
- if cf.c(x2, i).a = 0 then
- cany = 0
- x2 -= 1
- exit for
- end if
- next i
- if cany = 1 then any1 = 1
- y2 += 1
- cany = 1
- for i as integer = x1 to x2
- if cf.c(i, y2).a = 0 then
- cany = 0
- y2 -= 1
- exit for
- end if
- next i
- if cany = 1 then any1 = 1
- wend
- if x2 = 16 then x2 = 15
- if y2 = 16 then y2 = 15
- if bx1 = -1 orelse ((x2-x1+1)*(y2-y1+1) > (bx2-bx1+1)*(by2-by1+1)) then
- bx1 = x1: by1 = y1
- bx2 = x2: by2 = y2
- end if
- end if
- next y1
- next x1
- if bx1 = -1 then exit do
- fa(fac).x1 = bx1
- fa(fac).y1 = by1
- fa(fac).x2 = bx2
- fa(fac).y2 = by2
- fa(fac).fi = fi
- fa(fac).k = k
- fac += 1
- for i as integer = bx1 to bx2
- for j as integer = by1 to by2
- cf.c(i,j).a = 0
- next j
- next i
- loop
- next fi
- next k
- for i as integer = 0 to fac-1
- for j as integer = i to fac-1
- if (fa(j).x2 - fa(j).x1) > (fa(i).x2 - fa(i).x1) then
- swap fa(i), fa(j)
- end if
- next j
- next i
- dim as integer ctx = 0, cty = 0, ctw = 0
- for i as integer = 0 to fac-1
- dim as opface cf = f(fa(i).k, fa(i).fi)
- dim as integer bx1, by1, bx2, by2
- bx1 = fa(i).x1
- by1 = fa(i).y1
- bx2 = fa(i).x2
- by2 = fa(i).y2
- dim as integer tw = (bx2 - bx1 + 1) * 8 + 2, th = (by2 - by1 + 1) * 8 + 2
- if (cty + th) >= texh then
- ctx += ctw
- ctw = 0
- cty = 0
- end if
- fa(i).tx = ctx
- fa(i).ty = cty
- open cons for output as #1
- print #1, ctx, cty
- close #1
- for x as integer = bx1 to bx2
- for y as integer = by1 to by2
- dim as vclr c = cf.c(x,y)
- line tex, ( ctx+(x-bx1)*8, cty+(y-by1)*8 ) - step ( 9, 9 ), rgba(c.r*0.75, c.g*0.75, c.b*0.75, c.a), bf
- ' ### Mambazo Edit ###
- if( ctx+(x-bx1)*8 + 10 ) > max_x then max_x = ( ctx+(x-bx1)*8 + 10 )
- ' ####################
- next y
- next x
- for x as integer = bx1 to bx2
- for y as integer = by1 to by2
- dim as vclr c = cf.c(x,y)
- line tex, ( ctx+(x-bx1)*8, cty+(y-by1)*8 ) - step ( 8, 8 ), rgba(c.r*0.75, c.g*0.75, c.b*0.75, c.a), bf
- next y
- next x
- for x as integer = bx1 to bx2
- for y as integer = by1 to by2
- dim as vclr c = cf.c(x,y)
- line tex, ( ctx+(x-bx1)*8+1, cty+(y-by1)*8+1 ) - step ( 6, 6 ), rgba(c.r, c.g, c.b, c.a), bf
- cf.c(x,y).a = 0
- next y
- next x
- cty += th
- if tw > ctw then ctw = tw
- next i
- ' ### Mambazo Edit ###
- ' find power of 2 greater or equal to max_x
- dim as integer pot = 16
- while pot < max_x
- pot *= 2
- wend
- ' ####################
- dim as fb.image ptr compact = ImageCreate( pot, texh )
- put compact, ( 0, 0 ), tex,(0,0)-(pot,texh), PSET
- bsave "thistex.bmp", tex
- bsave "thiscompact.bmp", compact
- dim as uinteger th = gfxLoadBufferAsTexture(compact)
- ImageDestroy tex
- ImageDestroy compact
- glNewList(l, GL_COMPILE)
- glEnable (GL_TEXTURE_2D)
- glTranslatef(-0.4, -0.4, -0.4)
- glBegin(GL_QUADS)
- glBindTexture(GL_TEXTURE_2D, th)
- for i as integer = 0 to fac-1
- dim as integer fi = fa(i).fi, k = fa(i).k
- dim as integer bx1, by1, bx2, by2
- bx1 = fa(i).x1
- by1 = fa(i).y1
- bx2 = fa(i).x2
- by2 = fa(i).y2
- dim as vec3 p(4)
- dim as double a1,b1,c1, a2,b2,c2
- dim as double zm = 16.0 * vsize
- a1 = cdbl(bx1) * vsize: a2 = cdbl(bx2+1) * vsize
- b1 = cdbl(by1) * vsize: b2 = cdbl(by2+1) * vsize
- c1 = cdbl(k) * vsize: c2 = cdbl(k+1) * vsize
- dim as integer rmid = 0
- if fi = 0 then
- p(0) = mvec3(a1, b1, zm - c1)
- p(1) = mvec3(a2, b1, zm - c1)
- p(2) = mvec3(a1, b2, zm - c1)
- p(3) = mvec3(a2, b2, zm - c1)
- rmid = 1
- elseif fi = 1 then
- p(0) = mvec3(a1, b1, zm - c2)
- p(1) = mvec3(a2, b1, zm - c2)
- p(2) = mvec3(a1, b2, zm - c2)
- p(3) = mvec3(a2, b2, zm - c2)
- rmid = 1
- elseif fi = 2 then
- p(0) = mvec3(a1, c1, zm - b1)
- p(1) = mvec3(a2, c1, zm - b1)
- p(2) = mvec3(a1, c1, zm - b2)
- p(3) = mvec3(a2, c1, zm - b2)
- rmid = 1
- elseif fi = 3 then
- p(0) = mvec3(a1, c2, zm - b1)
- p(1) = mvec3(a2, c2, zm - b1)
- p(2) = mvec3(a1, c2, zm - b2)
- p(3) = mvec3(a2, c2, zm - b2)
- elseif fi = 4 then
- p(0) = mvec3(c1, a1, zm - b1)
- p(1) = mvec3(c1, a2, zm - b1)
- p(2) = mvec3(c1, a1, zm - b2)
- p(3) = mvec3(c1, a2, zm - b2)
- elseif fi = 5 then
- p(0) = mvec3(c2, a1, zm - b1)
- p(1) = mvec3(c2, a2, zm - b1)
- p(2) = mvec3(c2, a1, zm - b2)
- p(3) = mvec3(c2, a2, zm - b2)
- rmid = 1
- end if
- dim as integer tx1 = fa(i).tx, ty1 = fa(i).ty
- 'open cons for output as #1
- ' print #1, tx1, ty1
- 'close #1
- dim as integer tx2 = tx1 + (bx2-bx1+1) * 8
- dim as integer ty2 = ty1 + (by2-by1+1) * 8
- dim as double dw = 0.5/cdbl(pot), dh = 0.5/cdbl(texh)
- dim as double tw = cdbl(pot), th = cdbl(texh)
- if rmid = 1 then
- glTexCoord2d(cdbl(tx1)/tw+dw, cdbl(ty1)/th+dh): glVertex3d(p(0).x, p(0).y, p(0).z)
- glTexCoord2d(cdbl(tx1)/tw+dw, cdbl(ty2)/th+dh): glVertex3d(p(2).x, p(2).y, p(2).z)
- glTexCoord2d(cdbl(tx2)/tw+dw, cdbl(ty2)/th+dh): glVertex3d(p(3).x, p(3).y, p(3).z)
- glTexCoord2d(cdbl(tx2)/tw+dw, cdbl(ty1)/th+dh): glVertex3d(p(1).x, p(1).y, p(1).z)
- else
- glTexCoord2d(cdbl(tx1)/tw+dw, cdbl(ty1)/th+dh): glVertex3d(p(0).x, p(0).y, p(0).z)
- glTexCoord2d(cdbl(tx2)/tw+dw, cdbl(ty1)/th+dh): glVertex3d(p(1).x, p(1).y, p(1).z)
- glTexCoord2d(cdbl(tx2)/tw+dw, cdbl(ty2)/th+dh): glVertex3d(p(3).x, p(3).y, p(3).z)
- glTexCoord2d(cdbl(tx1)/tw+dw, cdbl(ty2)/th+dh): glVertex3d(p(2).x, p(2).y, p(2).z)
- end if
- next i
- glEnd()
- glDisable (GL_TEXTURE_2D)
- glEndList()
- end sub
- sub gen_vox ( v() as vclr, l as GLuint )
- glNewList(l, GL_COMPILE)
- glTranslatef(-0.4, -0.4, -0.4)
- glBegin(GL_QUADS)
- for i as integer = 0 to ubound(v, 0)
- for j as integer = 0 to ubound(v, 1)
- for k as integer = 0 to ubound(v, 2)
- if v(i,j,k).a > 0 then
- dim as double x, y, z
- x = cdbl(i) * vsize
- y = cdbl(j) * vsize
- z = cdbl(ubound(v, 2)+1) * vsize - cdbl(k) * vsize
- glColor3ub(v(i,j,k).r, v(i,j,k).g, v(i,j,k).b)
- if k = 0 orelse v(i,j,k-1).a = 0 then
- glVertex3f(x, y, z)
- glVertex3f(x+vsize, y, z)
- glVertex3f(x+vsize, y+vsize, z)
- glVertex3f(x, y+vsize, z)
- end if
- if k = ubound(v,2) orelse v(i,j,k+1).a = 0 then
- glVertex3f(x, y, z-vsize)
- glVertex3f(x+vsize, y, z-vsize)
- glVertex3f(x+vsize, y+vsize, z-vsize)
- glVertex3f(x, y+vsize, z-vsize)
- end if
- if j = 0 orelse v(i,j-1,k).a = 0 then
- glVertex3f(x, y, z)
- glVertex3f(x+vsize, y, z)
- glVertex3f(x+vsize, y, z-vsize)
- glVertex3f(x, y, z-vsize)
- end if
- if j = ubound(v,1) orelse v(i,j+1,k).a = 0 then
- glVertex3f(x, y+vsize, z)
- glVertex3f(x+vsize, y+vsize, z)
- glVertex3f(x+vsize, y+vsize, z-vsize)
- glVertex3f(x, y+vsize, z-vsize)
- end if
- if i = 0 orelse v(i-1,j,k).a = 0 then
- glVertex3f(x, y, z)
- glVertex3f(x, y+vsize, z)
- glVertex3f(x, y+vsize, z-vsize)
- glVertex3f(x, y, z-vsize)
- end if
- if i = ubound(v,0) orelse v(i+1,j,k).a = 0 then
- glVertex3f(x+vsize, y, z)
- glVertex3f(x+vsize, y+vsize, z)
- glVertex3f(x+vsize, y+vsize, z-vsize)
- glVertex3f(x+vsize, y, z-vsize)
- end if
- end if
- next k
- next j
- next i
- glEnd()
- glEndList()
- end sub
- sub load_lvox ( filename as string, v() as vclr )
- dim as uinteger nv
- dim as lvoxel vx
- open filename for binary as #1
- get #1 ,, nv
- for i as integer = 0 to nv - 1
- get #1 ,, vx
- v(vx.x, vx.y, vx.z).r = (vx.c shr 16) and 255
- v(vx.x, vx.y, vx.z).g = (vx.c shr 8) and 255
- v(vx.x, vx.y, vx.z).b = (vx.c shr 0) and 255
- v(vx.x, vx.y, vx.z).a = 255
- next i
- close #1
- end sub
- Function gfxLoadBufferAsTexture( buffer as any ptr ) As UInteger
- Dim As UInteger handle
- ' get one new texture handle, put it in Handle
- glGenTextures(1, @Handle)
- glBindTexture(GL_TEXTURE_2D, Handle)
- dim as fb.image ptr temp = buffer
- glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE)
- glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE)
- glTexParameteri GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR
- glTexParameteri GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR
- 'glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE)
- gluBuild2DMipmaps GL_TEXTURE_2D, GL_RGBA8, temp->width, temp->height, GL_BGRA, GL_UNSIGNED_BYTE, Buffer + SizeOf (fb.image)
- 'glTexImage2D GL_TEXTURE_2D, 0, GL_RGBA8, temp->width, temp->height, 0, GL_BGRA, GL_UNSIGNED_BYTE, Buffer + SizeOf (fb.image)
- 'glBindTexture (GL_TEXTURE_2D, 0)
- return handle
- End Function
- Function gfxLoadBMPAsTexture(file As String) As UInteger
- If file = "" Then Return 0
- Dim As UInteger handle
- Dim As Integer ff = FreeFile
- Dim As Integer W, H
- Open file For Binary As #ff
- Get #ff, 19, W
- Get #ff, 23, H
- Close #ff
- ' Ensure we're in texture mode
- glEnable (GL_TEXTURE_2D)
- Dim as any ptr buffer = ImageCreate(w,h)
- BLoad file, buffer
- ' get one new texture handle, put it in Handle
- handle = gfxLoadBufferAsTexture(buffer)
- ImageDestroy buffer
- return handle
- End Function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement