glutil.bmx 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. '
  2. ' BlitzMax port, 2015 Bruce A Henderson
  3. '
  4. ' Copyright (c) 2015 Mark Sibly
  5. '
  6. ' This software is provided 'as-is', without any express or implied
  7. ' warranty. In no event will the authors be held liable for any damages
  8. ' arising from the use of this software.
  9. '
  10. ' Permission is granted to anyone to use this software for any purpose,
  11. ' including commercial applications, and to alter it and redistribute it
  12. ' freely, subject to the following restrictions:
  13. '
  14. ' 1. The origin of this software must not be misrepresented; you must not
  15. ' claim that you wrote the original software. If you use this software
  16. ' in a product, an acknowledgement in the product documentation would be
  17. ' appreciated but is not required.
  18. ' 2. Altered source versions must be plainly marked as such, and must not be
  19. ' misrepresented as being the original software.
  20. ' 3. This notice may not be removed or altered from any source distribution.
  21. '
  22. SuperStrict
  23. Import brl.standardio
  24. ?opengles
  25. Import Pub.OpenGLES
  26. ?Not opengles
  27. Import Pub.Glew
  28. Import Pub.OpenGL
  29. ?
  30. Private
  31. Global tmpi:Int
  32. Public
  33. Function glCheck()
  34. Local err:Int=glGetError()
  35. If err=GL_NO_ERROR Return
  36. Throw "GL ERROR! err=" + err
  37. End Function
  38. Function glPushTexture2d( tex:Int )
  39. glGetIntegerv GL_TEXTURE_BINDING_2D, Varptr tmpi
  40. glBindTexture GL_TEXTURE_2D,tex
  41. End Function
  42. Function glPopTexture2d()
  43. glBindTexture GL_TEXTURE_2D, tmpi
  44. End Function
  45. Function glPushFramebuffer( framebuf:Int )
  46. glGetIntegerv GL_FRAMEBUFFER_BINDING, Varptr tmpi
  47. glBindFramebuffer GL_FRAMEBUFFER, framebuf
  48. End Function
  49. Function glPopFramebuffer()
  50. glBindFramebuffer GL_FRAMEBUFFER, tmpi
  51. End Function
  52. Function glCompile:Int( kind:Int,source:String )
  53. ?opengles
  54. source="precision mediump float;~n"+source
  55. ?
  56. Local shader:Int = glCreateShader( kind )
  57. Local s:Byte Ptr = source.ToCString()
  58. glShaderSource shader,1, Varptr s, Null
  59. MemFree(s)
  60. glCompileShader shader
  61. glGetShaderiv shader,GL_COMPILE_STATUS, Varptr tmpi
  62. If Not tmpi
  63. Local buf:Byte[1024]
  64. Local l:Int
  65. glGetShaderInfoLog( shader, 1024, Varptr l, buf)
  66. Print "Failed to compile fragment shader:"+ String.FromBytes(buf, l)
  67. Local LINES:String[]=source.Split( "~n" )
  68. For Local i:Int=0 Until LINES.Length
  69. Print (i+1)+":~t"+LINES[i]
  70. Next
  71. Throw "Compile fragment shader failed"
  72. EndIf
  73. Return shader
  74. End Function
  75. Function glLink( program:Int )
  76. glLinkProgram program
  77. glGetProgramiv program,GL_LINK_STATUS, Varptr tmpi
  78. If Not tmpi
  79. Local buf:Byte[1024]
  80. Local l:Int
  81. glGetProgramInfoLog( program, 1024, Varptr l, buf)
  82. Throw "Failed to link program:"+ String.FromBytes(buf, l)
  83. End If
  84. End Function