source.bmx 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. SuperStrict
  2. Import BRL.Graphics
  3. Import BRL.Pixmap
  4. Import Pub.Glew
  5. Import Pub.OpenGL
  6. Import BRL.SystemDefault
  7. Private
  8. Incbin "gldrawtextfont.bin"
  9. Extern
  10. Function bbGLGraphicsShareContexts()
  11. Function bbGLGraphicsGraphicsModes:Int( buf:Byte Ptr,size:Int )
  12. Function bbGLGraphicsAttachGraphics:Byte Ptr( widget:Byte Ptr,flags:Long )
  13. Function bbGLGraphicsCreateGraphics:Byte Ptr( width:Int,height:Int,depth:Int,hertz:Int,flags:Long,x:Int,y:Int )
  14. Function bbGLGraphicsGetSettings( context:Byte Ptr,width:Int Var,height:Int Var,depth:Int Var,hertz:Int Var,flags:Long Var )
  15. Function bbGLGraphicsClose( context:Byte Ptr )
  16. Function bbGLGraphicsSetGraphics( context:Byte Ptr )
  17. Function bbGLGraphicsFlip( sync:Int )
  18. End Extern
  19. Public
  20. Type TGLGraphics Extends TGraphics
  21. Method Driver:TGLGraphicsDriver() Override
  22. Assert _context
  23. Return GLGraphicsDriver()
  24. End Method
  25. Method GetSettings( width:Int Var,height:Int Var,depth:Int Var,hertz:Int Var,flags:Long Var, x:Int Var, y:Int Var ) Override
  26. Assert _context
  27. Local w:Int,h:Int,d:Int,r:Int,f:Long,xp:Int,yp:Int
  28. bbGLGraphicsGetSettings _context,w,h,d,r,f
  29. width=w
  30. height=h
  31. depth=d
  32. hertz=r
  33. flags=f
  34. x=-1
  35. y=-1
  36. End Method
  37. Method Close() Override
  38. If Not _context Return
  39. bbGLGraphicsClose( _context )
  40. _context=0
  41. End Method
  42. Method Resize(width:Int, height:Int) Override
  43. End Method
  44. Method Position(x:Int, y:Int) Override
  45. End Method
  46. Field _context:Byte Ptr
  47. End Type
  48. Type TGLGraphicsDriver Extends TGraphicsDriver
  49. Method GraphicsModes:TGraphicsMode[]() Override
  50. Local buf:Int[1024*4]
  51. Local count:Int=bbGLGraphicsGraphicsModes( buf,1024 )
  52. Local modes:TGraphicsMode[count],p:Int Ptr=buf
  53. For Local i:Int=0 Until count
  54. Local t:TGraphicsMode=New TGraphicsMode
  55. t.width=p[0]
  56. t.height=p[1]
  57. t.depth=p[2]
  58. t.hertz=p[3]
  59. modes[i]=t
  60. p:+4
  61. Next
  62. Return modes
  63. End Method
  64. Method AttachGraphics:TGLGraphics( widget:Byte Ptr,flags:Long ) Override
  65. Local t:TGLGraphics=New TGLGraphics
  66. t._context=bbGLGraphicsAttachGraphics( widget,flags )
  67. Return t
  68. End Method
  69. Method CreateGraphics:TGLGraphics( width:Int,height:Int,depth:Int,hertz:Int,flags:Long,x:Int,y:Int ) Override
  70. Local t:TGLGraphics=New TGLGraphics
  71. t._context=bbGLGraphicsCreateGraphics( width,height,depth,hertz,flags,x,y )
  72. Return t
  73. End Method
  74. Method SetGraphics( g:TGraphics ) Override
  75. Local context:Byte Ptr
  76. Local t:TGLGraphics=TGLGraphics( g )
  77. If t context=t._context
  78. bbGLGraphicsSetGraphics context
  79. End Method
  80. Method Flip:Int( sync:Int) Override
  81. bbGLGraphicsFlip sync
  82. End Method
  83. End Type
  84. Rem
  85. bbdoc: Get OpenGL graphics driver
  86. returns: An OpenGL graphics driver
  87. about:
  88. The returned driver can be used with #SetGraphicsDriver
  89. End Rem
  90. Function GLGraphicsDriver:TGLGraphicsDriver()
  91. Global _driver:TGLGraphicsDriver=New TGLGraphicsDriver
  92. Return _driver
  93. End Function
  94. Rem
  95. bbdoc: Create OpenGL graphics
  96. returns: An OpenGL graphics object
  97. about:
  98. This is a convenience function that allows you to easily create an OpenGL graphics context.
  99. End Rem
  100. Function GLGraphics:TGraphics( width:Int,height:Int,depth:Int=0,hertz:Int=60,flags:Long=GRAPHICS_BACKBUFFER|GRAPHICS_DEPTHBUFFER )
  101. SetGraphicsDriver GLGraphicsDriver()
  102. Return Graphics( width,height,depth,hertz,flags )
  103. End Function
  104. SetGraphicsDriver GLGraphicsDriver()
  105. '----- Helper Functions -----
  106. Private
  107. Global fontTex:Int
  108. Global fontSeq:Int
  109. Global ortho_mv![16],ortho_pj![16]
  110. Function BeginOrtho()
  111. Local vp:Int[4]
  112. glPushAttrib GL_ENABLE_BIT|GL_TEXTURE_BIT|GL_TRANSFORM_BIT
  113. glGetIntegerv GL_VIEWPORT,vp
  114. glGetDoublev GL_MODELVIEW_MATRIX,ortho_mv
  115. glGetDoublev GL_PROJECTION_MATRIX,ortho_pj
  116. glMatrixMode GL_MODELVIEW
  117. glLoadIdentity
  118. glMatrixMode GL_PROJECTION
  119. glLoadIdentity
  120. glOrtho 0,vp[2],vp[3],0,-1,1
  121. glDisable GL_CULL_FACE
  122. glDisable GL_ALPHA_TEST
  123. glDisable GL_DEPTH_TEST
  124. End Function
  125. Function EndOrtho()
  126. glMatrixMode GL_PROJECTION
  127. glLoadMatrixd ortho_pj
  128. glMatrixMode GL_MODELVIEW
  129. glLoadMatrixd ortho_mv
  130. glPopAttrib
  131. End Function
  132. Public
  133. Rem
  134. bbdoc: Helper function to calculate nearest valid texture size
  135. about: This functions rounds @width and @height up to the nearest valid texture size
  136. End Rem
  137. Function GLAdjustTexSize( width:Int Var,height:Int Var )
  138. Function Pow2Size:Int( n:Int )
  139. Local t:Int=1
  140. While t<n
  141. t:*2
  142. Wend
  143. Return t
  144. End Function
  145. width=Pow2Size( width )
  146. height=Pow2Size( height )
  147. Repeat
  148. Local t:Int
  149. glTexImage2D GL_PROXY_TEXTURE_2D,0,4,width,height,0,GL_RGBA,GL_UNSIGNED_BYTE,Null
  150. glGetTexLevelParameteriv GL_PROXY_TEXTURE_2D,0,GL_TEXTURE_WIDTH,Varptr t
  151. If t Return
  152. If width=1 And height=1 RuntimeError "Unable to calculate tex size"
  153. If width>1 width:/2
  154. If height>1 height:/2
  155. Forever
  156. End Function
  157. Rem
  158. bbdoc: Helper function to create a texture from a pixmap
  159. returns: Integer GL Texture name
  160. about: @pixmap is resized to a valid texture size before conversion.
  161. end rem
  162. Function GLTexFromPixmap:Int( pixmap:TPixmap,mipmap:Int=True )
  163. If pixmap.format<>PF_RGBA8888 pixmap=pixmap.Convert( PF_RGBA8888 )
  164. Local width:Int=pixmap.width,height:Int=pixmap.height
  165. GLAdjustTexSize width,height
  166. If width<>pixmap.width Or height<>pixmap.height pixmap=ResizePixmap( pixmap,width,height )
  167. Local old_name:Int,old_row_len:Int
  168. glGetIntegerv GL_TEXTURE_BINDING_2D,Varptr old_name
  169. glGetIntegerv GL_UNPACK_ROW_LENGTH,Varptr old_row_len
  170. Local name:Int
  171. glGenTextures 1,Varptr name
  172. glBindtexture GL_TEXTURE_2D,name
  173. Local mip_level:Int
  174. Repeat
  175. glPixelStorei GL_UNPACK_ROW_LENGTH,pixmap.pitch/BytesPerPixel[pixmap.format]
  176. glTexImage2D GL_TEXTURE_2D,mip_level,GL_RGBA8,width,height,0,GL_RGBA,GL_UNSIGNED_BYTE,pixmap.pixels
  177. If Not mipmap Exit
  178. If width=1 And height=1 Exit
  179. If width>1 width:/2
  180. If height>1 height:/2
  181. pixmap=ResizePixmap( pixmap,width,height )
  182. mip_level:+1
  183. Forever
  184. glBindTexture GL_TEXTURE_2D,old_name
  185. glPixelStorei GL_UNPACK_ROW_LENGTH,old_row_len
  186. Return name
  187. End Function
  188. Rem
  189. bbdoc:Helper function to output a simple rectangle
  190. about:
  191. Draws a rectangle relative to top-left of current viewport.
  192. End Rem
  193. Function GLDrawRect( x:Int,y:Int,width:Int,height:Int )
  194. BeginOrtho
  195. glBegin GL_QUADS
  196. glVertex2i x,y
  197. glVertex2i x+width,y
  198. glVertex2i x+width,y+height
  199. glVertex2i x,y+height
  200. glEnd
  201. EndOrtho
  202. End Function
  203. Rem
  204. bbdoc: Helper function to output some simple 8x16 font text
  205. about:
  206. Draws text relative to top-left of current viewport.<br/>
  207. <br/>
  208. The font used is an internal fixed point 8x16 font.<br/>
  209. <br/>
  210. This function is intended for debugging purposes only - performance is unlikely to be stellar.
  211. End Rem
  212. Function GLDrawText( Text$,x:Int,y:Int )
  213. ' If fontSeq<>graphicsSeq
  214. If Not fontTex
  215. Local pixmap:TPixmap=TPixmap.Create( 1024,16,PF_RGBA8888 )
  216. Local p:Byte Ptr=IncbinPtr( "gldrawtextfont.bin" )
  217. For Local y:Int=0 Until 16
  218. For Local x:Int=0 Until 96
  219. Local b:Int=p[x]
  220. For Local n:Int=0 Until 8
  221. If b & (1 Shl n)
  222. pixmap.WritePixel x*8+n,y,~0
  223. Else
  224. pixmap.WritePixel x*8+n,y,0
  225. EndIf
  226. Next
  227. Next
  228. p:+96
  229. Next
  230. fontTex=GLTexFromPixmap( pixmap )
  231. fontSeq=graphicsSeq
  232. EndIf
  233. BeginOrtho
  234. glEnable GL_TEXTURE_2D
  235. glBindTexture GL_TEXTURE_2D,fontTex
  236. For Local i:Int=0 Until Text.length
  237. Local c:Int=Text[i]-32
  238. If c>=0 And c<96
  239. Const adv#=8/1024.0
  240. Local t#=c*adv;
  241. glBegin GL_QUADS
  242. glTexcoord2f t,0
  243. glVertex2f x,y
  244. glTexcoord2f t+adv,0
  245. glVertex2f x+8,y
  246. glTexcoord2f t+adv,1
  247. glVertex2f x+8,y+16
  248. glTexcoord2f t,1
  249. glVertex2f x,y+16
  250. glEnd
  251. EndIf
  252. x:+8
  253. Next
  254. EndOrtho
  255. End Function
  256. Rem
  257. bbdoc: Helper function to draw a pixmap to a gl context
  258. about:
  259. Draws the pixmap relative to top-left of current viewport.<br/>
  260. <br/>
  261. This function is intended for debugging purposes only - performance is unlikely to be stellar.
  262. End Rem
  263. Function GLDrawPixmap( pixmap:TPixmap,x:Int,y:Int )
  264. BeginOrtho
  265. Local t:TPixmap=YFlipPixmap(pixmap)
  266. If t.format<>PF_RGBA8888 t=ConvertPixmap( t,PF_RGBA8888 )
  267. glRasterPos2i 0,0
  268. glBitmap 0,0,0,0,x,-y-t.height,Null
  269. glDrawPixels t.width,t.height,GL_RGBA,GL_UNSIGNED_BYTE,t.pixels
  270. EndOrtho
  271. End Function
  272. Rem
  273. bbdoc: Enable OpenGL context sharing
  274. about:
  275. Calling #GLShareContexts will cause all opengl graphics contexts created to
  276. shared displaylists, textures, shaders etc.
  277. This should be called before any opengl contexts are created.
  278. End Rem
  279. Function GLShareContexts()
  280. bbGLGraphicsShareContexts
  281. End Function