Advertisement
Guest User

Untitled

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