Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- '$gui
- '$stop_scan
- #include once "fbgfx.bi"
- #include once "GL/gl.bi"
- #include once "GL/glu.bi"
- #include once "createtex.bi"
- using FB
- screenres 800, 600, 32, , GFX_OPENGL 'or GFX_MULTISAMPLE
- '' set up our viewport
- glMatrixMode(GL_PROJECTION)
- glLoadIdentity()
- glViewport(0, 0, 800, 600)
- glLoadIdentity ()
- gluPerspective( 45, 800 / 600, 0.1, 100.0 )
- '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_NICEST)
- glShadeModel( GL_FLAT )
- glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST)
- 'glEnable GL_COLOR_MATERIAL
- 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)
- 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
- 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
- declare function gfxLoadBMPAsTexture ( file as string ) 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)
- 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 any ptr tex = ImageCreate(256, 128, RGB( 64,64,64 ) )
- line tex, (0,0)-(256, 128), 0, bf
- dim as integer ps = 8
- ' for i as integer = 0 to 15
- ' for j as integer = 0 to 5
- ' dim as integer x1 = i*16*4-1
- ' dim as integer y1 = j*16*4-1
- ' for x as integer = 0 to 15
- ' for y as integer = 0 to 15
- ' if f(i,j).c(x,y).a > 0 then
- ' line tex, (x1+x*ps-1, y1+y*ps-1)-step(ps, ps), rgba(f(i,j).c(x,y).r, f(i,j).c(x,y).g, f(i,j).c(x,y).b, f(i,j).c(x,y).a), bf
- ' line tex, (x1+x*ps-1, y1+y*ps-1)-step(ps, ps), RGBA(64,64,64,255), b
- ' end if
- ' next y
- ' next x
- ' next j
- ' next i
- for i as integer = 0 to 15
- for j as integer = 0 to 5
- dim as integer x1 = i*16
- dim as integer y1 = j*16
- for x as integer = 0 to 15
- for y as integer = 0 to 15
- if f(i,j).c(x,y).a > 0 then
- pset tex, (x1+x, y1+y), rgba(f(i,j).c(x,y).r, f(i,j).c(x,y).g, f(i,j).c(x,y).b, f(i,j).c(x,y).a)
- 'line tex, (x1+x*3, y1+y*3)-step(2, 2), rgba(f(i,j).c(x,y).r * 0.75, f(i,j).c(x,y).g* 0.75, f(i,j).c(x,y).b* 0.75, f(i,j).c(x,y).a), b
- end if
- next y
- next x
- next j
- next i
- bsave "test.bmp", tex
- dim as uinteger th = gfxLoadBufferAsTexture(tex)
- ImageDestroy tex
- glNewList(l, GL_COMPILE)
- glEnable (GL_TEXTURE_2D)
- glTranslatef(-0.4, -0.4, -0.4)
- glBegin(GL_QUADS)
- glBindTexture(GL_TEXTURE_2D, th)
- 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
- for x as integer = bx1 to bx2
- for y as integer = by1 to by2
- cf.c(x,y).a = 0
- next y
- next x
- 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
- 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)
- 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)
- 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)
- 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)
- end if
- dim as integer tbx = k*16, tby = fi*16
- dim as double dec = 0'0.0001 / 1024.0
- glTexCoord2d(cdbl(tbx+bx1)/256.0, cdbl(tby+by1)/128.0): glVertex3d(p(0).x, p(0).y, p(0).z)
- glTexCoord2d(cdbl(tbx+bx2+1)/256.0-dec, cdbl(tby+by1)/128.0): glVertex3d(p(1).x, p(1).y, p(1).z)
- glTexCoord2d(cdbl(tbx+bx2+1)/256.0-dec, cdbl(tby+by2+1)/128.0-dec): glVertex3d(p(3).x, p(3).y, p(3).z)
- glTexCoord2d(cdbl(tbx+bx1)/256.0, cdbl(tby+by2+1)/128.0-dec): glVertex3d(p(2).x, p(2).y, p(2).z)
- loop
- next fi
- next k
- 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_REPEAT)
- glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT)
- glTexParameteri GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST 'GL_LINEAR
- glTexParameteri GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST 'GL_LINEAR
- glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE)
- glTexImage2D GL_TEXTURE_2D, 0, GL_RGBA8, temp->width, temp->height, 0, GL_BGRA, GL_UNSIGNED_BYTE, Buffer + SizeOf (fb.image)
- 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