source.bmx 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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. Method ToString:String() Override
  84. Return "TGLGraphicsDriver"
  85. End Method
  86. End Type
  87. Rem
  88. bbdoc: Get OpenGL graphics driver
  89. returns: An OpenGL graphics driver
  90. about:
  91. The returned driver can be used with #SetGraphicsDriver
  92. End Rem
  93. Function GLGraphicsDriver:TGLGraphicsDriver()
  94. Global _driver:TGLGraphicsDriver=New TGLGraphicsDriver
  95. Return _driver
  96. End Function
  97. Rem
  98. bbdoc: Create OpenGL graphics
  99. returns: An OpenGL graphics object
  100. about:
  101. This is a convenience function that allows you to easily create an OpenGL graphics context.
  102. End Rem
  103. Function GLGraphics:TGraphics( width:Int,height:Int,depth:Int=0,hertz:Int=60,flags:Long=GRAPHICS_BACKBUFFER|GRAPHICS_DEPTHBUFFER )
  104. SetGraphicsDriver GLGraphicsDriver()
  105. Return Graphics( width,height,depth,hertz,flags )
  106. End Function
  107. SetGraphicsDriver GLGraphicsDriver()
  108. '----- Helper Functions -----
  109. Private
  110. Global fontTex:Int
  111. Global fontSeq:Int
  112. Global ortho_mv![16],ortho_pj![16]
  113. Function BeginOrtho()
  114. Local vp:Int[4]
  115. glPushAttrib GL_ENABLE_BIT|GL_TEXTURE_BIT|GL_TRANSFORM_BIT
  116. glGetIntegerv GL_VIEWPORT,vp
  117. glGetDoublev GL_MODELVIEW_MATRIX,ortho_mv
  118. glGetDoublev GL_PROJECTION_MATRIX,ortho_pj
  119. glMatrixMode GL_MODELVIEW
  120. glLoadIdentity
  121. glMatrixMode GL_PROJECTION
  122. glLoadIdentity
  123. glOrtho 0,vp[2],vp[3],0,-1,1
  124. glDisable GL_CULL_FACE
  125. glDisable GL_ALPHA_TEST
  126. glDisable GL_DEPTH_TEST
  127. End Function
  128. Function EndOrtho()
  129. glMatrixMode GL_PROJECTION
  130. glLoadMatrixd ortho_pj
  131. glMatrixMode GL_MODELVIEW
  132. glLoadMatrixd ortho_mv
  133. glPopAttrib
  134. End Function
  135. Public
  136. Rem
  137. bbdoc: Helper function to calculate nearest valid texture size
  138. about: This functions rounds @width and @height up to the nearest valid texture size
  139. End Rem
  140. Function GLAdjustTexSize( width:Int Var,height:Int Var )
  141. Function Pow2Size:Int( n:Int )
  142. Local t:Int=1
  143. While t<n
  144. t:*2
  145. Wend
  146. Return t
  147. End Function
  148. width=Pow2Size( width )
  149. height=Pow2Size( height )
  150. Repeat
  151. Local t:Int
  152. glTexImage2D GL_PROXY_TEXTURE_2D,0,4,width,height,0,GL_RGBA,GL_UNSIGNED_BYTE,Null
  153. glGetTexLevelParameteriv GL_PROXY_TEXTURE_2D,0,GL_TEXTURE_WIDTH,Varptr t
  154. If t Return
  155. If width=1 And height=1 RuntimeError "Unable to calculate tex size"
  156. If width>1 width:/2
  157. If height>1 height:/2
  158. Forever
  159. End Function
  160. Rem
  161. bbdoc: Helper function to create a texture from a pixmap
  162. returns: Integer GL Texture name
  163. about: @pixmap is resized to a valid texture size before conversion.
  164. end rem
  165. Function GLTexFromPixmap:Int( pixmap:TPixmap,mipmap:Int=True )
  166. If pixmap.format<>PF_RGBA8888 pixmap=pixmap.Convert( PF_RGBA8888 )
  167. Local width:Int=pixmap.width,height:Int=pixmap.height
  168. GLAdjustTexSize width,height
  169. If width<>pixmap.width Or height<>pixmap.height pixmap=ResizePixmap( pixmap,width,height )
  170. Local old_name:Int,old_row_len:Int
  171. glGetIntegerv GL_TEXTURE_BINDING_2D,Varptr old_name
  172. glGetIntegerv GL_UNPACK_ROW_LENGTH,Varptr old_row_len
  173. Local name:Int
  174. glGenTextures 1,Varptr name
  175. glBindtexture GL_TEXTURE_2D,name
  176. Local mip_level:Int
  177. Repeat
  178. glPixelStorei GL_UNPACK_ROW_LENGTH,pixmap.pitch/BytesPerPixel[pixmap.format]
  179. glTexImage2D GL_TEXTURE_2D,mip_level,GL_RGBA8,width,height,0,GL_RGBA,GL_UNSIGNED_BYTE,pixmap.pixels
  180. If Not mipmap Exit
  181. If width=1 And height=1 Exit
  182. If width>1 width:/2
  183. If height>1 height:/2
  184. pixmap=ResizePixmap( pixmap,width,height )
  185. mip_level:+1
  186. Forever
  187. glBindTexture GL_TEXTURE_2D,old_name
  188. glPixelStorei GL_UNPACK_ROW_LENGTH,old_row_len
  189. Return name
  190. End Function
  191. Rem
  192. bbdoc:Helper function to output a simple rectangle
  193. about:
  194. Draws a rectangle relative to top-left of current viewport.
  195. End Rem
  196. Function GLDrawRect( x:Int,y:Int,width:Int,height:Int )
  197. BeginOrtho
  198. glBegin GL_QUADS
  199. glVertex2i x,y
  200. glVertex2i x+width,y
  201. glVertex2i x+width,y+height
  202. glVertex2i x,y+height
  203. glEnd
  204. EndOrtho
  205. End Function
  206. Rem
  207. bbdoc: Helper function to output some simple 8x16 font text
  208. about:
  209. Draws text relative to top-left of current viewport.<br/>
  210. <br/>
  211. The font used is an internal fixed point 8x16 font.<br/>
  212. <br/>
  213. This function is intended for debugging purposes only - performance is unlikely to be stellar.
  214. End Rem
  215. Function GLDrawText( Text:String,x:Int,y:Int )
  216. ' If fontSeq<>graphicsSeq
  217. If Not fontTex
  218. Local pixmap:TPixmap=TPixmap.Create( 1024,16,PF_RGBA8888 )
  219. Local p:Byte Ptr=IncbinPtr( "gldrawtextfont.bin" )
  220. For Local y:Int=0 Until 16
  221. For Local x:Int=0 Until 96
  222. Local b:Int=p[x]
  223. For Local n:Int=0 Until 8
  224. If b & (1 Shl n)
  225. pixmap.WritePixel x*8+n,y,~0
  226. Else
  227. pixmap.WritePixel x*8+n,y,0
  228. EndIf
  229. Next
  230. Next
  231. p:+96
  232. Next
  233. fontTex=GLTexFromPixmap( pixmap )
  234. fontSeq=graphicsSeq
  235. EndIf
  236. BeginOrtho
  237. glEnable GL_TEXTURE_2D
  238. glBindTexture GL_TEXTURE_2D,fontTex
  239. For Local i:Int=0 Until Text.length
  240. Local c:Int=Text[i]-32
  241. If c>=0 And c<96
  242. Const adv#=8/1024.0
  243. Local t#=c*adv;
  244. glBegin GL_QUADS
  245. glTexcoord2f t,0
  246. glVertex2f x,y
  247. glTexcoord2f t+adv,0
  248. glVertex2f x+8,y
  249. glTexcoord2f t+adv,1
  250. glVertex2f x+8,y+16
  251. glTexcoord2f t,1
  252. glVertex2f x,y+16
  253. glEnd
  254. EndIf
  255. x:+8
  256. Next
  257. EndOrtho
  258. End Function
  259. Rem
  260. bbdoc: Helper function to draw a pixmap to a gl context
  261. about:
  262. Draws the pixmap relative to top-left of current viewport.<br/>
  263. <br/>
  264. This function is intended for debugging purposes only - performance is unlikely to be stellar.
  265. End Rem
  266. Function GLDrawPixmap( pixmap:TPixmap,x:Int,y:Int )
  267. BeginOrtho
  268. Local t:TPixmap=YFlipPixmap(pixmap)
  269. If t.format<>PF_RGBA8888 t=ConvertPixmap( t,PF_RGBA8888 )
  270. glRasterPos2i 0,0
  271. glBitmap 0,0,0,0,x,-y-t.height,Null
  272. glDrawPixels t.width,t.height,GL_RGBA,GL_UNSIGNED_BYTE,t.pixels
  273. EndOrtho
  274. End Function
  275. Rem
  276. bbdoc: Enable OpenGL context sharing
  277. about:
  278. Calling #GLShareContexts will cause all opengl graphics contexts created to
  279. shared displaylists, textures, shaders etc.
  280. This should be called before any opengl contexts are created.
  281. End Rem
  282. Function GLShareContexts()
  283. bbGLGraphicsShareContexts
  284. End Function