Guest User

Untitled

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