3.3_shaders_class.bmx 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. ' https://learnopengl.com/
  2. SuperStrict
  3. Framework GLFW.GLFWWindow
  4. Import "../shader.bmx"
  5. Import GLFW.GLFW
  6. Import GLFW.GLFWOpenGL
  7. Import GLFW.GLFWSystem
  8. Import BRL.StandardIO
  9. Local app_name:String = "Shaders Class"
  10. Const SCR_WIDTH:UInt = 800
  11. Const SCR_HEIGHT:UInt = 600
  12. Type TGameWindow Extends TGLFWWindow
  13. ' whenever the window size changed (by OS or user resize) this callback method executes
  14. Method OnFrameBufferSize (width:Int, height:Int)
  15. ' make sure the viewport matches the new window dimensions; note that width and
  16. ' height will be significantly larger than specified on retina displays.
  17. glViewport (0, 0, width, height)
  18. EndMethod
  19. EndType
  20. ' process all input: query GLFW whether relevant keys are pressed/released this frame and react accordingly
  21. Function ProcessInput (window:TGLFWWindow)
  22. If window.IsKeyDown (GLFW_KEY_ESCAPE)
  23. window.SetShouldClose (True)
  24. EndIf
  25. EndFunction
  26. ' glfw: initialize and configure
  27. TGLFWWindow.Hint (GLFW_CONTEXT_VERSION_MAJOR, 3)
  28. TGLFWWindow.Hint (GLFW_CONTEXT_VERSION_MINOR, 3)
  29. TGLFWWindow.Hint (GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE)
  30. ?MacOS ' Ewww...
  31. TGLFWWindow.Hint (GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE)
  32. ?
  33. ' glfw window creation
  34. Local window:TGLFWWindow = New TGameWindow.Create (SCR_WIDTH, SCR_HEIGHT, app_name)
  35. If Not window
  36. Print "Failed to create GLFW window!"
  37. End
  38. EndIf
  39. window.MakeContextCurrent ()
  40. ' glad: load all OpenGL function pointers
  41. gladLoadGL (glfwGetProcAddress)
  42. ' build and compile our shader program
  43. Local ourShader:TShader = New TShader("3.3.shader.vs", "3.3.shader.fs")
  44. ' set up vertex data (and buffer(s)) and configure vertex attributes
  45. Local vertices:Float [] = [..
  46. 0.5, -0.5, 0.0, 1.0, 0.0, 0.0, ..
  47. -0.5, -0.5, 0.0, 0.0, 1.0, 0.0, ..
  48. 0.0, 0.5, 0.0, 0.0, 0.0, 1.0]
  49. Local VBO:UInt
  50. Local VAO:UInt
  51. glGenVertexArrays (1, Varptr VAO)
  52. glGenBuffers (1, Varptr VBO)
  53. ' bind the Vertex Array Object first, then bind and set vertex buffer(s), and then configure vertex attributes(s).
  54. glBindVertexArray (VAO)
  55. glBindBuffer (GL_ARRAY_BUFFER, VBO)
  56. glBufferData (GL_ARRAY_BUFFER, Int(vertices.length * SizeOf (0:Float)), vertices, GL_STATIC_DRAW)
  57. ' position attribute
  58. glVertexAttribPointer (0, 3, GL_FLOAT, GL_FALSE, Int(6 * SizeOf (0:Float)), 0:Byte Ptr)
  59. glEnableVertexAttribArray (0)
  60. Local attribute_offset:Int = 3 * SizeOf (0:Float)
  61. ' color attribute
  62. glVertexAttribPointer (1, 3, GL_FLOAT, GL_FALSE, Int(6 * SizeOf (0:Float)), Byte Ptr (attribute_offset))
  63. glEnableVertexAttribArray (1)
  64. ' render loop
  65. ' -----------
  66. While Not window.ShouldClose ()
  67. ' input
  68. ' -----
  69. ProcessInput (window)
  70. ' render
  71. ' ------
  72. glClearColor (0.2, 0.3, 0.3, 1.0)
  73. glClear (GL_COLOR_BUFFER_BIT)
  74. ' render the triangle
  75. ourShader.use()
  76. glBindVertexArray (VAO)
  77. glDrawArrays (GL_TRIANGLES, 0, 3)
  78. ' glfw: swap buffers and poll IO events (keys pressed/released, mouse moved etc.)
  79. window.SwapBuffers ()
  80. PollSystem ()
  81. Wend
  82. ' optional: de-allocate all resources once they've outlived their purpose
  83. glDeleteVertexArrays (1, Varptr VAO)
  84. glDeleteBuffers (1, Varptr VBO)
  85. End