Advertisement
Guest User

Untitled

a guest
Jul 18th, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. '$gui
  2. '$stop_scan
  3. #include once "fbgfx.bi"
  4. #include once "GL/gl.bi"
  5. #include once "GL/glu.bi"
  6. #include once "createtex.bi"
  7.  
  8. using FB
  9.  
  10. screenres 800, 600, 32, , GFX_OPENGL 'or GFX_MULTISAMPLE
  11.  
  12. '' set up our viewport
  13. glMatrixMode(GL_PROJECTION)
  14. glLoadIdentity()
  15. glViewport(0, 0, 800, 600)
  16. glLoadIdentity ()
  17. gluPerspective( 45, 800 / 600, 0.1, 100.0 )
  18. 'glFrustum (-0.125/2.0, 0.125/2.0, -0.125/2.0, 0.125/2.0, 0.1, 100.0)
  19.  
  20. '' set up color properties
  21. glClearColor(.125, .125, .125, 0)
  22.  
  23. '' set up rendering
  24. glMatrixMode(GL_MODELVIEW)
  25. glLoadIdentity()
  26.  
  27. glEnable (GL_DEPTH_TEST)
  28. glEnable (GL_BLEND)
  29. glEnable (GL_LINE_SMOOTH)
  30. glHint(GL_LINE_SMOOTH_HINT, GL_FASTEST)
  31. glHint(GL_POLYGON_SMOOTH_HINT, GL_NICEST)
  32.  
  33. glShadeModel( GL_FLAT )
  34.  
  35. glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST)
  36. 'glEnable GL_COLOR_MATERIAL
  37.  
  38. dim as GLfloat fogColor(0 to 3)
  39. fogColor(0) = 0.5
  40. fogColor(1) = 0.5
  41. fogColor(2) = 0.5
  42. fogColor(3) = 1.0
  43.  
  44. glFogi(GL_FOG_MODE, GL_EXP)
  45. glFogfv(GL_FOG_COLOR, @fogColor(0))
  46. glFogf(GL_FOG_DENSITY, 0.15)
  47. glHint(GL_FOG_HINT, GL_DONT_CARE)
  48. glFogf(GL_FOG_START, 1.0)
  49. glFogf(GL_FOG_END, 5.0)
  50. glEnable(GL_FOG)   
  51.    
  52. type lvoxel
  53.    as byte x, y, z
  54.    as uinteger c
  55. end type
  56.  
  57. type vclr
  58.    r as ubyte
  59.    g as ubyte
  60.    b as ubyte
  61.    a as ubyte
  62. end type
  63.  
  64. type vec3
  65.    x as double
  66.    y as double
  67.    z as double
  68. end type
  69.  
  70. type opface
  71.    c(0 to 15, 0 to 15) as vclr
  72. end type
  73.  
  74. dim as vclr vtest(0 to 15, 0 to 15, 0 to 15)
  75. const vsize = 0.05
  76. const pi = 3.141592
  77. dim as vec3 camp, camup
  78. dim as double cama
  79.  
  80. camp.x = -32 * vsize
  81. camp.z = -16 * vsize
  82. camup.z = -1.0
  83.  
  84. cama = 0.0
  85.  
  86. dim as GLuint vlist
  87.  
  88. declare sub gen_vox ( v() as vclr, l as GLuint )
  89. declare sub gen_vox_opt ( v() as vclr, l as GLuint )
  90. declare sub ren_vox ( l as GLuint, x as double, y as double, z as double )
  91. declare sub ren_vox_rot ( l as GLuint, x as double, y as double, z as double, a0 as double, a1 as double )
  92. declare sub load_lvox ( filename as string, v() as vclr )
  93. declare function gfxLoadBufferAsTexture ( buffer as any ptr ) As uinteger
  94. declare function gfxLoadBMPAsTexture ( file as string ) As uinteger
  95.  
  96. load_lvox("voxel.lst", vtest())
  97. vlist = GLGenLists(1)
  98. gen_vox_opt(vtest(), vlist)
  99.  
  100. dim as double curT = timer(), lastT = timer()
  101. do
  102.  
  103.         if multikey(SC_LEFT) then cama -= (pi / 60.0) / 1.0
  104.         if multikey(SC_RIGHT) then cama += (pi / 60.0) / 1.0
  105.  
  106.         if multikey(SC_UP) then
  107.             camp.x += cos(cama) * ((vsize * 16.0) / 60.0) * 4.0
  108.             camp.y += sin(cama) * ((vsize * 16.0) / 60.0) * 4.0
  109.         end if
  110.  
  111.         if multikey(SC_DOWN) then
  112.             camp.x -= cos(cama) * ((vsize * 16.0) / 60.0) * 4.0
  113.             camp.y -= sin(cama) * ((vsize * 16.0) / 60.0) * 4.0
  114.         end if
  115.  
  116.         glFinish()
  117.         '' begin a new matrix for the polygon set so we can isolate our rendering
  118.         '' and not affect other matrices.
  119.  
  120.  
  121.         glPushMatrix()
  122.  
  123.             'glTranslatef(0.0, 0.0, -2.0)
  124.             'glRotatef(curT*100.0, 1.0, 1.0, 2.0)
  125.  
  126.             gluLookAt(camp.x, camp.y, camp.z, camp.x + cos(cama), camp.y + sin(cama), camp.z, camup.x, camup.y, camup.z)
  127.            
  128.             for i as integer = 0 to 24
  129.                 for j as integer = 0 to 24 step 2
  130.                     ren_vox(vlist, cdbl(i), cdbl(j), 0.0)
  131.                 next j
  132.                 for j as integer = 1 to 24 step 2
  133.                     ren_vox_rot(vlist, cdbl(i), cdbl(j), 0.0, 180.0, 0.0)
  134.                 next j
  135.             next i
  136.             ren_vox_rot(vlist, 0.0, 1.0, 1.0, 180.0, 180.0)
  137.             ren_vox_rot(vlist, 0.0, 0.0, 1.0, 0.0, 180.0)
  138.  
  139.         glPopMatrix()
  140.        
  141.  
  142.         '' flip our screen
  143.         'glFlush()
  144.         flip()
  145.         glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT)
  146.        
  147.         'sleep(16, 1)
  148.  
  149.         while 1.0/(timer-curT) > 120.0
  150.             sleep 1
  151.         wend
  152.  
  153.         '' render important information
  154.         'open cons for output as #1
  155.         '        print #1, 1/(curT - lastT)
  156.         'xclose #1
  157.  
  158.         lastT = curT
  159.         curT = timer()
  160.        
  161. loop until multikey(1)
  162.  
  163. sub ren_vox ( l as GLuint, x as double, y as double, z as double )
  164.  
  165.     glPushMatrix()
  166.            
  167.         glTranslatef(x*16.0*vsize, y*16.0*vsize, -z*16.0*vsize)
  168.         glCallList(l)
  169.  
  170.     glPopMatrix()
  171.  
  172. end sub
  173.  
  174. sub ren_vox_rot ( l as GLuint, x as double, y as double, z as double, a0 as double, a1 as double )
  175.  
  176.     glPushMatrix()
  177.            
  178.         glTranslatef(x*16.0*vsize, y*16.0*vsize, -z*16.0*vsize)
  179.         glRotatef(a0, 0.0, 0.0, -1.0)
  180.         glRotatef(a1, 0.0, 1.0, 0.0)
  181.         glCallList(l)
  182.  
  183.     glPopMatrix()
  184.  
  185. end sub
  186.  
  187. function mvec3( x as double, y as double, z as double) as vec3
  188.     dim as vec3 ret
  189.     ret.x = x
  190.     ret.y = y
  191.     ret.z = z
  192.     return ret
  193. end function
  194.  
  195. sub gen_vox_opt ( v() as vclr, l as GLuint )
  196.  
  197.     dim as opface f(0 to 15, 0 to 5)
  198.  
  199.     for i as integer = 0 to 15
  200.         for j as integer = 0 to 15
  201.             for k as integer = 0 to 15
  202.  
  203.                 if v(i,j,k).a > 0 then
  204.  
  205.                     if k = 0  orelse v(i,j,k-1).a = 0 then f(k, 0).c(i,j) = v(i,j,k)
  206.                     if k = 15 orelse v(i,j,k+1).a = 0 then f(k, 1).c(i,j) = v(i,j,k)
  207.                     if j = 0  orelse v(i,j-1,k).a = 0 then f(j, 2).c(i,k) = v(i,j,k)
  208.                     if j = 15 orelse v(i,j+1,k).a = 0 then f(j, 3).c(i,k) = v(i,j,k)
  209.                     if i = 0  orelse v(i-1,j,k).a = 0 then f(i, 4).c(j,k) = v(i,j,k)
  210.                     if i = 15 orelse v(i+1,j,k).a = 0 then f(i, 5).c(j,k) = v(i,j,k)
  211.  
  212.                 end if
  213.  
  214.             next k
  215.         next j
  216.     next i
  217.    
  218.     glEnable (GL_TEXTURE_2D)
  219.        
  220.     dim as any ptr tex = ImageCreate(256, 128, RGB( 64,64,64 ) )
  221.     line tex, (0,0)-(256, 128), 0, bf
  222.    
  223.     dim as integer ps = 8
  224.    
  225. '    for i as integer = 0 to 15
  226. '        for j as integer = 0 to 5
  227. '            dim as integer x1 = i*16*4-1
  228. '            dim as integer y1 = j*16*4-1
  229. '            for x as integer = 0 to 15
  230. '                for y as integer = 0 to 15
  231. '                    if f(i,j).c(x,y).a > 0 then
  232. '                        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
  233. '                        line tex, (x1+x*ps-1, y1+y*ps-1)-step(ps, ps), RGBA(64,64,64,255), b
  234. '                    end if
  235. '                next y
  236. '            next x
  237. '        next j
  238. '    next i
  239.  
  240.     for i as integer = 0 to 15
  241.         for j as integer = 0 to 5
  242.             dim as integer x1 = i*16
  243.             dim as integer y1 = j*16
  244.             for x as integer = 0 to 15
  245.                 for y as integer = 0 to 15
  246.                     if f(i,j).c(x,y).a > 0 then
  247.                         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)
  248.                         '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
  249.                     end if
  250.                 next y
  251.             next x
  252.         next j
  253.     next i
  254.  
  255.    bsave "test.bmp", tex
  256.  
  257.     dim as uinteger th = gfxLoadBufferAsTexture(tex)
  258.     ImageDestroy tex
  259.  
  260.     glNewList(l, GL_COMPILE)
  261.     glEnable (GL_TEXTURE_2D)
  262.     glTranslatef(-0.4, -0.4, -0.4)
  263.     glBegin(GL_QUADS)
  264.     glBindTexture(GL_TEXTURE_2D, th)
  265.  
  266.     for k as integer = 0 to 15
  267.         for fi as integer = 0 to 5
  268.            
  269.             dim as opface cf = f(k, fi)
  270.  
  271.             do
  272.  
  273.                 dim as integer bx1=-1, by1, bx2, by2
  274.  
  275.                 for x1 as integer = 0 to 15
  276.                     for y1 as integer = 0 to 15
  277.                         if cf.c(x1,y1).a > 0 then
  278.  
  279.                             dim as integer x2 = x1, y2 = y1, any1 = 1
  280.  
  281.                             while x2 < 16 and y2 < 16 and any1 = 1
  282.  
  283.                                 dim as integer cany
  284.                                 any1 = 0
  285.  
  286.                                 x2 += 1
  287.                                 cany = 1
  288.                                 for i as integer = y1 to y2
  289.                                     if cf.c(x2, i).a = 0 then
  290.                                         cany = 0
  291.                                         x2 -= 1
  292.                                         exit for
  293.                                     end if
  294.                                 next i
  295.                                 if cany = 1 then any1 = 1
  296.  
  297.                                 y2 += 1
  298.                                 cany = 1
  299.                                 for i as integer = x1 to x2
  300.                                     if cf.c(i, y2).a = 0 then
  301.                                         cany = 0
  302.                                         y2 -= 1
  303.                                         exit for
  304.                                     end if
  305.                                 next i
  306.                                 if cany = 1 then any1 = 1
  307.  
  308.                             wend
  309.  
  310.                             if x2 = 16 then x2 = 15
  311.                             if y2 = 16 then y2 = 15
  312.  
  313.                             if bx1 = -1 orelse ((x2-x1+1)*(y2-y1+1) > (bx2-bx1+1)*(by2-by1+1)) then
  314.                                 bx1 = x1: by1 = y1
  315.                                 bx2 = x2: by2 = y2
  316.                             end if
  317.  
  318.                         end if
  319.                     next y1
  320.                 next x1
  321.  
  322.                 if bx1 = -1 then exit do
  323.  
  324.                 for x as integer = bx1 to bx2
  325.                     for y as integer = by1 to by2
  326.                         cf.c(x,y).a = 0
  327.                     next y
  328.                 next x
  329.  
  330.                 dim as vec3 p(4)
  331.  
  332.                 dim as double a1,b1,c1, a2,b2,c2
  333.                 dim as double zm = 16.0 * vsize
  334.  
  335.                 a1 = cdbl(bx1) * vsize: a2 = cdbl(bx2+1) * vsize
  336.                 b1 = cdbl(by1) * vsize: b2 = cdbl(by2+1) * vsize
  337.                 c1 = cdbl(k)   * vsize: c2 = cdbl(k+1)   * vsize
  338.  
  339.                 if fi = 0 then
  340.                     p(0) = mvec3(a1, b1, zm - c1)
  341.                     p(1) = mvec3(a2, b1, zm - c1)
  342.                     p(2) = mvec3(a1, b2, zm - c1)
  343.                     p(3) = mvec3(a2, b2, zm - c1)
  344.                 elseif fi = 1 then
  345.                     p(0) = mvec3(a1, b1, zm - c2)
  346.                     p(1) = mvec3(a2, b1, zm - c2)
  347.                     p(2) = mvec3(a1, b2, zm - c2)
  348.                     p(3) = mvec3(a2, b2, zm - c2)
  349.                 elseif fi = 2 then
  350.                     p(0) = mvec3(a1, c1, zm - b1)
  351.                     p(1) = mvec3(a2, c1, zm - b1)
  352.                     p(2) = mvec3(a1, c1, zm - b2)
  353.                     p(3) = mvec3(a2, c1, zm - b2)
  354.                 elseif fi = 3 then
  355.                     p(0) = mvec3(a1, c2, zm - b1)
  356.                     p(1) = mvec3(a2, c2, zm - b1)
  357.                     p(2) = mvec3(a1, c2, zm - b2)
  358.                     p(3) = mvec3(a2, c2, zm - b2)
  359.                 elseif fi = 4 then
  360.                     p(0) = mvec3(c1, a1, zm - b1)
  361.                     p(1) = mvec3(c1, a2, zm - b1)
  362.                     p(2) = mvec3(c1, a1, zm - b2)
  363.                     p(3) = mvec3(c1, a2, zm - b2)
  364.                 elseif fi = 5 then
  365.                     p(0) = mvec3(c2, a1, zm - b1)
  366.                     p(1) = mvec3(c2, a2, zm - b1)
  367.                     p(2) = mvec3(c2, a1, zm - b2)
  368.                     p(3) = mvec3(c2, a2, zm - b2)
  369.                 end if
  370.  
  371.                 dim as integer tbx = k*16, tby = fi*16
  372.                 dim as double dec = 0'0.0001 / 1024.0
  373.  
  374.                 glTexCoord2d(cdbl(tbx+bx1)/256.0, cdbl(tby+by1)/128.0): glVertex3d(p(0).x, p(0).y, p(0).z)
  375.                 glTexCoord2d(cdbl(tbx+bx2+1)/256.0-dec, cdbl(tby+by1)/128.0): glVertex3d(p(1).x, p(1).y, p(1).z)
  376.                 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)
  377.                 glTexCoord2d(cdbl(tbx+bx1)/256.0, cdbl(tby+by2+1)/128.0-dec): glVertex3d(p(2).x, p(2).y, p(2).z)
  378.                
  379.             loop
  380.  
  381.         next fi
  382.     next k
  383.  
  384.     glEnd()
  385.     glDisable (GL_TEXTURE_2D)
  386.     glEndList()
  387.  
  388. end sub
  389.  
  390. sub gen_vox ( v() as vclr, l as GLuint )
  391.  
  392.     glNewList(l, GL_COMPILE)
  393.  
  394.     glTranslatef(-0.4, -0.4, -0.4)
  395.  
  396.     glBegin(GL_QUADS)
  397.  
  398.     for i as integer = 0 to ubound(v, 0)
  399.         for j as integer = 0 to ubound(v, 1)
  400.             for k as integer = 0 to ubound(v, 2)
  401.  
  402.                 if v(i,j,k).a > 0 then
  403.  
  404.                     dim as double x, y, z
  405.                     x = cdbl(i) * vsize
  406.                     y = cdbl(j) * vsize
  407.                     z = cdbl(ubound(v, 2)+1) * vsize - cdbl(k) * vsize
  408.  
  409.                     glColor3ub(v(i,j,k).r, v(i,j,k).g, v(i,j,k).b)
  410.  
  411.                     if k = 0 orelse v(i,j,k-1).a = 0 then
  412.  
  413.                         glVertex3f(x, y, z)
  414.                         glVertex3f(x+vsize, y, z)
  415.                         glVertex3f(x+vsize, y+vsize, z)
  416.                         glVertex3f(x, y+vsize, z)
  417.  
  418.                     end if
  419.  
  420.                     if k = ubound(v,2) orelse v(i,j,k+1).a = 0 then
  421.  
  422.                         glVertex3f(x, y, z-vsize)
  423.                         glVertex3f(x+vsize, y, z-vsize)
  424.                         glVertex3f(x+vsize, y+vsize, z-vsize)
  425.                         glVertex3f(x, y+vsize, z-vsize)
  426.  
  427.                     end if
  428.  
  429.                     if j = 0 orelse v(i,j-1,k).a = 0 then
  430.  
  431.                         glVertex3f(x, y, z)
  432.                         glVertex3f(x+vsize, y, z)
  433.                         glVertex3f(x+vsize, y, z-vsize)
  434.                         glVertex3f(x, y, z-vsize)
  435.  
  436.                     end if
  437.  
  438.                     if j = ubound(v,1) orelse v(i,j+1,k).a = 0 then
  439.  
  440.                         glVertex3f(x, y+vsize, z)
  441.                         glVertex3f(x+vsize, y+vsize, z)
  442.                         glVertex3f(x+vsize, y+vsize, z-vsize)
  443.                         glVertex3f(x, y+vsize, z-vsize)
  444.  
  445.                     end if
  446.  
  447.                     if i = 0 orelse v(i-1,j,k).a = 0 then
  448.  
  449.                         glVertex3f(x, y, z)
  450.                         glVertex3f(x, y+vsize, z)
  451.                         glVertex3f(x, y+vsize, z-vsize)
  452.                         glVertex3f(x, y, z-vsize)
  453.  
  454.                     end if
  455.  
  456.                     if i = ubound(v,0) orelse v(i+1,j,k).a = 0 then
  457.  
  458.                         glVertex3f(x+vsize, y, z)
  459.                         glVertex3f(x+vsize, y+vsize, z)
  460.                         glVertex3f(x+vsize, y+vsize, z-vsize)
  461.                         glVertex3f(x+vsize, y, z-vsize)
  462.  
  463.                     end if
  464.  
  465.                 end if
  466.  
  467.             next k
  468.         next j
  469.     next i
  470.  
  471.     glEnd()
  472.  
  473.     glEndList()
  474.    
  475. end sub
  476.  
  477. sub load_lvox ( filename as string, v() as vclr )
  478.  
  479.    dim as uinteger nv
  480.    dim as lvoxel vx
  481.  
  482.    open filename for binary as #1
  483.  
  484.       get #1 ,, nv
  485.      
  486.       for i as integer = 0 to nv - 1
  487.          get #1 ,, vx
  488.          v(vx.x, vx.y, vx.z).r = (vx.c shr 16) and 255
  489.          v(vx.x, vx.y, vx.z).g = (vx.c shr 8) and 255
  490.          v(vx.x, vx.y, vx.z).b = (vx.c shr 0) and 255
  491.          v(vx.x, vx.y, vx.z).a = 255
  492.       next i
  493.  
  494.    close #1
  495.  
  496. end sub
  497.  
  498. Function gfxLoadBufferAsTexture( buffer as any ptr ) As UInteger
  499.         Dim As UInteger handle
  500.         ' get one new texture handle, put it in Handle              
  501.         glGenTextures(1, @Handle)
  502.         glBindTexture(GL_TEXTURE_2D, Handle)
  503.         dim as fb.image ptr temp = buffer
  504.    
  505.         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT)
  506.         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT)
  507.         glTexParameteri GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST 'GL_LINEAR
  508.         glTexParameteri GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST 'GL_LINEAR
  509.         glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE)
  510.                
  511.         glTexImage2D GL_TEXTURE_2D, 0, GL_RGBA8, temp->width, temp->height, 0, GL_BGRA, GL_UNSIGNED_BYTE, Buffer + SizeOf (fb.image)
  512.        
  513.         return handle
  514. End Function
  515.  
  516. Function gfxLoadBMPAsTexture(file As String) As UInteger
  517.         If file = "" Then Return 0
  518.         Dim As UInteger handle
  519.         Dim As Integer ff = FreeFile
  520.         Dim As Integer W, H
  521.         Open file For Binary As #ff
  522.         Get #ff, 19, W
  523.         Get #ff, 23, H
  524.         Close #ff
  525.        
  526.         ' Ensure we're in texture mode
  527.         glEnable (GL_TEXTURE_2D)
  528.        
  529.         Dim as any ptr buffer = ImageCreate(w,h)
  530.         BLoad file, buffer
  531.        
  532.         ' get one new texture handle, put it in Handle              
  533.         handle = gfxLoadBufferAsTexture(buffer)
  534.         ImageDestroy buffer
  535.         return handle
  536. End Function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement