glgraphics.bmx 9.2 KB

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