Guest User

Untitled

a guest
Aug 2nd, 2018
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #define FBEXT_NO_LIBJPG
  2. #define fbext_NoBuiltinInstanciations() 1
  3. '#define FBEXT_BUILD_NO_GFX_LOADERS 1
  4.  
  5.  
  6. #include once "fbgfx.bi"
  7. '#include once "ext/graphics/png.bi"
  8. #include once "ext/graphics/image.bi"
  9. #include once "ext/graphics/sprite.bi"
  10. #include once "ext/graphics/manip.bi"
  11.  
  12. #include once "ext/math/vectors.bi"
  13. #include once "ext/math/matrix.bi"
  14. #include once "GL/gl.bi"
  15. #include once "GL/glu.bi"
  16.  
  17. using ext.math
  18. using ext.gfx
  19.  
  20. type triangle_struct
  21.  
  22.     as integer point_id(2)
  23.     as uinteger tex_id
  24.  
  25. end type
  26.  
  27. type model_struct
  28.  
  29.     as uinteger max_vertices
  30.     as vector3d ptr vertex
  31.  
  32.     as uinteger max_triangles
  33.     as triangle_struct ptr triangle
  34.  
  35. end type
  36.  
  37. declare function load_texture( byref filename as string, texture as gluint, byref force_clamp as integer ) as integer
  38. declare function outline( byref filename as string, byval skcol as uinteger ) as integer
  39. declare function outline_2verts( byref filename as string, byval skcol as uinteger, byref verts as vector2d ptr ) as uinteger
  40. declare function make_ccw( byref vrets as vector2d ptr, byref max_verts as uinteger ) as uinteger
  41.  
  42.  
  43.  
  44. dim as integer scr_w = 640, scr_h = 480
  45.  
  46. screenres scr_w, scr_h, 32
  47.  
  48.  
  49.  
  50. dim as vector2d ptr tverts
  51. dim as uinteger max_tverts = outline_2verts( "res/img/madscience.png", &hffff00ff, tverts )
  52. print "finish outline!"
  53.  
  54.  
  55.  
  56. make_ccw( tverts, max_tverts)
  57.  
  58. print "finish make_ccw!"
  59.  
  60. sleep
  61.  
  62.  
  63.  
  64. screenres scr_w, scr_h, 32, 0, FB.GFX_OPENGL
  65.  
  66. screencontrol FB.SET_GL_DEPTH_BITS, 24
  67.  
  68. glViewport(0, 0, scr_w, scr_h)
  69. glMatrixMode(GL_PROJECTION)
  70. glLoadIdentity()
  71. gluPerspective( 60, scr_w/scr_h, .01, 10000 )
  72.  
  73. glDepthFunc( GL_LEQUAL )
  74. glEnable( GL_COLOR_MATERIAL )
  75. glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST )
  76. glPolygonMode ( GL_FRONT, GL_FILL )
  77. glPolygonMode ( GL_BACK, GL_LINE )
  78. glEnable( GL_LIGHTING )
  79. glEnable( GL_LIGHT0 )
  80. glEnable( GL_CULL_FACE )
  81.  
  82.  
  83. dim as vector3d campos=vector3d(0,0,3), camlook=vector3d(0,0,0)
  84.  
  85.  
  86.  
  87.  
  88. dim as gluint texture
  89. load_texture( "res/img/madscience.png", texture, 0 )
  90.  
  91.  
  92. do
  93.  
  94.  
  95.  
  96.     if multikey( FB.SC_LEFT ) then
  97.         campos.x-=1.1
  98.     end if
  99.  
  100.     if multikey( FB.SC_RIGHT ) then
  101.         campos.x+=1.1
  102.     end if
  103.    
  104.     if multikey( FB.SC_UP ) then
  105.         campos.z+=1.1
  106.     end if
  107.  
  108.     if multikey( FB.SC_DOWN ) then
  109.         campos.z-=1.1
  110.     end if
  111.    
  112.  
  113.     glClear( GL_DEPTH_BUFFER_BIT or GL_COLOR_BUFFER_BIT )
  114.     glMatrixMode( GL_MODELVIEW )
  115.     glLoadIdentity()
  116.     gluLookat( campos.x, campos.y, campos.z, camlook.x, camlook.y, camlook.z, 0,1,0 )
  117.  
  118.  
  119.     glColor3f(1,1,1)
  120.  
  121.  
  122.     glrotatef(timer*50,7,2,3)
  123.  
  124.     glBegin(GL_QUADS)
  125.     glNormal3f(0f,0f,1f)
  126.  
  127.     gltexcoord2f(0f,1f)
  128.     glVertex3f(-.5,.5,0f)
  129.  
  130.     gltexcoord2f(0f,0f)
  131.     glVertex3f(-.5,-.5,0f)
  132.  
  133.     gltexcoord2f(1f,0f)
  134.     glVertex3f(.5,-.5,0f)
  135.  
  136.     gltexcoord2f(1f,1f)
  137.     glVertex3f(.5,.5,0f)
  138.  
  139.     glEnd
  140.    
  141.    
  142.     'if max_tverts>0 then
  143.     '  
  144.     '   glDisable( GL_CULL_FACE )
  145.     '   glDisable( GL_LIGHTING )   
  146.     '   glDisable( GL_TEXTURE_2D )
  147.     '   glColor3f(1,1,1)
  148.     '  
  149.     '   glBegin( GL_POLYGON )
  150.     '   for i as integer = 0 to max_tverts-1
  151.     '       glvertex3f( tverts[i].x, tverts[i].y, 0 )
  152.     '   next
  153.     '   glEnd()
  154.     '  
  155.     '  
  156.     '   glEnable( GL_TEXTURE_2D )
  157.     '  
  158.     'end if
  159.    
  160.  
  161.     flip
  162.    
  163.     print #1, campos
  164.    
  165.  
  166. loop until multikey(FB.SC_ESCAPE)
  167.  
  168.  
  169.  
  170.  
  171.  
  172.  
  173. deallocate( tverts )
  174. close #1
  175.  
  176.  
  177. function make_ccw( byref verts as vector2d ptr, byref max_verts as uinteger ) as uinteger
  178.    
  179.     dim as double angle(max_verts-1)
  180.    
  181.    
  182.     dim as vector2d center
  183.    
  184.     for i as integer = 0 to max_verts-1
  185.         center+=verts[i]
  186.     next
  187.    
  188.     center/=max_verts
  189.    
  190.    
  191.     for i as integer = 0 to max_verts-1
  192.    
  193.         angle(i) = atan2(center.y-verts[i].y, center.x-verts[i].x)'center.AngleBetween(verts[i])
  194.        
  195.     next
  196.    
  197.    
  198.     for i as integer = 0 to max_verts-1
  199.        
  200.         for i2 as integer = 0 to max_verts-1
  201.            
  202.             if i2<>i then
  203.                
  204.                 if angle(i2)<angle(i) then
  205.                     swap verts[i], verts[i2]
  206.                     swap angle(i), angle(i2)
  207.                 end if
  208.                
  209.             end if
  210.            
  211.         next
  212.        
  213.     next
  214.    
  215.    
  216.     for i as integer = 0 to max_verts-1
  217.         pset( (verts[i].x*342)+(342/2), (verts[i].y*320)+(320/2) )
  218.     next
  219.    
  220.    
  221.     return 0
  222.    
  223. end function
  224.  
  225.  
  226.  
  227.  
  228. function outline( byref filename as string, byval skcol as uinteger = &hffff00ff ) as integer
  229.    
  230.     'draws only the outline of a fb.image ptr
  231.    
  232.     dim as ext.gfx.image ptr temp = ext.gfx.loadimage( filename )
  233.    
  234.     'if temp = 0 then
  235.     '   return 0
  236.     'end if
  237.    
  238.     dim mySprite as ext.gfx.Sprite
  239.     mySprite.Init( 1 )
  240.     mySprite.SetImage(0, *temp)
  241.     var bw = mysprite.getcolmap(0)
  242.    
  243.     put(0,0),temp,alpha,16
  244.    
  245.     for y1 as integer = 0 To temp->height - 2
  246.         For x1 as integer = 0 To temp->width - 2
  247.            
  248.             dim as uinteger dif
  249.             dim as uinteger col1 = point(x1,y1,bw)
  250.             dim as ubyte r1 = (col1 shr 24) and 255
  251.             dim as ubyte g1 = (col1 shr 16) and 255
  252.             dim as ubyte b1 = (col1 shr 8) and 255
  253.  
  254.             for y2 as integer = 0 To 1
  255.                 for x2 as integer = 0 To 1
  256.                    
  257.                     dim as uinteger col2 = point(x1+x2,y1+y2, bw)
  258.                    
  259.                     if dif <> skcol then
  260.                         dif+=abs(r1-((col2 shr 24) and 255)) + abs(g1-((col2 shr 16) and 255)) + abs(b1-((col2 shr 8 ) and 255))
  261.                     end if
  262.                    
  263.                 next
  264.             next
  265.            
  266.             if dif > 0 then
  267.                 pset (x1,y1), rgba( dif, dif, dif, dif )
  268.             end if
  269.            
  270.         next
  271.     next
  272.    
  273.     'imagedestroy( temp )
  274.    
  275.     return 0
  276.    
  277. end function
  278.  
  279.  
  280. function outline_2verts( byref filename as string, byval skcol as uinteger, byref verts as vector2d ptr ) as uinteger
  281.    
  282.     'draws only the outline of a fb.image ptr  
  283.    
  284.     dim as uinteger vcnt
  285.    
  286.     dim as ext.gfx.image ptr temp = ext.gfx.loadimage( filename )
  287.    
  288.     'if temp = 0 then
  289.     '   return 0
  290.     'end if
  291.    
  292.    
  293.     dim mySprite as Sprite
  294.     mySprite.Init( 1 )
  295.     mySprite.SetImage(0, *temp)
  296.    
  297.     var bw = mysprite.getcolmap(0)
  298.    
  299.     put(0,0),temp,alpha,16
  300.    
  301.     for y1 as integer = 0 To temp->height - 2
  302.         For x1 as integer = 0 To temp->width - 2
  303.            
  304.             dim as uinteger dif
  305.             dim as uinteger col1 = point(x1,y1,bw)
  306.             dim as ubyte r1 = (col1 shr 24) and 255
  307.             dim as ubyte g1 = (col1 shr 16) and 255
  308.             dim as ubyte b1 = (col1 shr 8) and 255
  309.  
  310.             for y2 as integer = 0 To 1
  311.                 for x2 as integer = 0 To 1
  312.                    
  313.                     dim as uinteger col2 = point(x1+x2,y1+y2, bw)
  314.                    
  315.                     if dif <> skcol then
  316.                         dif+=abs(r1-((col2 shr 24) and 255)) + abs(g1-((col2 shr 16) and 255)) + abs(b1-((col2 shr 8 ) and 255))
  317.                     end if
  318.                    
  319.                 next
  320.             next
  321.            
  322.             if dif > 0 then
  323.                 pset (x1,y1), rgba( dif, dif, dif, dif )
  324.                
  325.                 verts = reallocate( verts, (vcnt+1)*sizeof(vector2d) )
  326.                 verts[vcnt] = vector2d(x1-(temp->width/2) ,y1-(temp->height/2) )
  327.                 verts[vcnt]/=vector2d(temp->width, temp->height)
  328.                
  329.                
  330.                 'pset( (verts[vcnt].x)+(temp->width/2), (verts[vcnt].y)+(temp->height/2)), rgba(dif, dif, dif, dif)
  331.                
  332.                 'print #1, verts[vcnt]
  333.                
  334.                 vcnt+=1
  335.             end if
  336.            
  337.         next
  338.     next
  339.    
  340.     'imagedestroy( temp )
  341.    
  342.    
  343.     print #1, vcnt
  344.    
  345.     return vcnt
  346.  
  347. end function
  348.  
  349.  
  350.  
  351. function load_texture( byref filename as string, texture as gluint, byref force_clamp as integer ) as integer
  352.  
  353.     glenable( GL_TEXTURE_2D )
  354.     glbindtexture( GL_TEXTURE_2D, texture )
  355.  
  356.     dim SprWidth as integer
  357.     dim SprHeight as integer
  358.    
  359.     dim as ext.gfx.image ptr temp = ext.gfx.loadimage( filename)', ext.gfx.TARGET_OPENGL )
  360.    
  361.     'if temp = 0 then
  362.     '   return 0
  363.     'end if
  364.    
  365.     sprWidth = temp->width
  366.     sprHeight = temp->height
  367.    
  368.     glteximage2d( GL_TEXTURE_2D, 0, GL_RGBA, SprWidth, SprHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, temp->pixels )
  369.  
  370.     glubuild2dmipmaps( GL_TEXTURE_2D, GL_RGBA, SprWidth, SprHeight, GL_RGBA, GL_UNSIGNED_BYTE, temp->pixels )
  371.  
  372.     if force_clamp then
  373.         gltexparameterf GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE
  374.         gltexparameterf GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE
  375.     else
  376.         gltexparameterf GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT
  377.         gltexparameterf GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT
  378.     end if
  379.  
  380.     gltexparameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_LINEAR )
  381.     gltexparameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR )
  382.     gltexenvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE)
  383.    
  384.     return 0
  385.  
  386. end function
Add Comment
Please, Sign In to add comment