2.2.hello_triangle_indexed.bmx 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. ' https://learnopengl.com/
  2. SuperStrict
  3. Framework GLFW.GLFWWindow
  4. Import GLFW.GLFW
  5. Import GLFW.GLFWOpenGL
  6. Import GLFW.GLFWSystem
  7. Import BRL.StandardIO
  8. Local app_name:String = "Hello Triangle Indexed"
  9. Const SCR_WIDTH:UInt = 800
  10. Const SCR_HEIGHT:UInt = 600
  11. Type TGameWindow Extends TGLFWWindow
  12. ' glfw: whenever the window size changed (by OS or user resize) this callback method executes
  13. Method OnFrameBufferSize (width:Int, height:Int)
  14. ' make sure the viewport matches the new window dimensions; note that width and
  15. ' height will be significantly larger than specified on retina displays.
  16. glViewport (0, 0, width, height)
  17. EndMethod
  18. EndType
  19. ' process all input: query GLFW whether relevant keys are pressed/released this frame and react accordingly
  20. Function ProcessInput (window:TGLFWWindow)
  21. If window.IsKeyDown (GLFW_KEY_ESCAPE)
  22. window.SetShouldClose (True)
  23. EndIf
  24. EndFunction
  25. ' glfw: initialize and configure
  26. TGLFWWindow.Hint (GLFW_CONTEXT_VERSION_MAJOR, 3)
  27. TGLFWWindow.Hint (GLFW_CONTEXT_VERSION_MINOR, 3)
  28. TGLFWWindow.Hint (GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE)
  29. ?MacOS ' Ewww...
  30. TGLFWWindow.Hint (GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE)
  31. ?
  32. ' glfw window creation
  33. Local window:TGLFWWindow = New TGameWindow.Create (SCR_WIDTH, SCR_HEIGHT, app_name)
  34. If Not window
  35. Print "Failed to create GLFW window!"
  36. End
  37. EndIf
  38. window.MakeContextCurrent ()
  39. ' glad: load all OpenGL function pointers
  40. gladLoadGL (glfwGetProcAddress)
  41. Local vertexShaderSource:String
  42. vertexShaderSource :+ "#version 330 core~n"
  43. vertexShaderSource :+ "layout (location = 0) in vec3 aPos;~n"
  44. vertexShaderSource :+ "void main()~n"
  45. vertexShaderSource :+ "{~n"
  46. vertexShaderSource :+ " gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);~n"
  47. vertexShaderSource :+ "}~n"
  48. Local fragmentShaderSource:String
  49. fragmentShaderSource :+ "#version 330 core~n"
  50. fragmentShaderSource :+ "out vec4 FragColor;~n"
  51. fragmentShaderSource :+ "void main()~n"
  52. fragmentShaderSource :+ "{~n"
  53. fragmentShaderSource :+ " FragColor = vec4(1.0f, 0.5f, 0.2f, 1.0f);~n"
  54. fragmentShaderSource :+ "}~n"
  55. ' build and compile our shader program
  56. ' ------------------------------------
  57. ' vertex shader
  58. Local vertexShader:Int = glCreateShader (GL_VERTEX_SHADER)
  59. glShaderSource (vertexShader, 1, vertexShaderSource)
  60. glCompileShader (vertexShader)
  61. Local success:Int
  62. Local infoLog:String
  63. ' check for shader compile errors
  64. glGetShaderiv (vertexShader, GL_COMPILE_STATUS, Varptr success)
  65. If Not success
  66. infoLog = glGetShaderInfoLog (vertexShader)
  67. Print "Vertex shader compilation failed: " + infoLog
  68. EndIf
  69. ' fragment shader
  70. Local fragmentShader:Int = glCreateShader (GL_FRAGMENT_SHADER)
  71. glShaderSource (fragmentShader, 1, fragmentShaderSource)
  72. glCompileShader (fragmentShader)
  73. ' check for shader compile errors
  74. glGetShaderiv (fragmentShader, GL_COMPILE_STATUS, Varptr success)
  75. If Not success
  76. infoLog = glGetShaderInfoLog (fragmentShader)
  77. Print "Fragment shader compilation failed: " + infoLog
  78. EndIf
  79. ' link shaders
  80. Local shaderProgram:Int = glCreateProgram ()
  81. glAttachShader (shaderProgram, vertexShader)
  82. glAttachShader (shaderProgram, fragmentShader)
  83. glLinkProgram (shaderProgram)
  84. glGetProgramiv (shaderProgram, GL_LINK_STATUS, Varptr success)
  85. If Not success
  86. infoLog = glGetProgramInfoLog (shaderProgram)
  87. Print "Shader program linking failed: " + infoLog
  88. EndIf
  89. glDeleteShader (vertexShader)
  90. glDeleteShader (fragmentShader)
  91. ' set up vertex data (and buffer(s)) and configure vertex attributes
  92. Local vertices:Float [] = [..
  93. 0.5, 0.5, 0.0, ..
  94. 0.5, -0.5, 0.0, ..
  95. -0.5, -0.5, 0.0, ..
  96. -0.5, 0.5, 0.0]
  97. Local indices:UInt [] = [..
  98. 0, 1, 3, ..
  99. 1, 2, 3]
  100. Local VBO:UInt
  101. Local VAO:UInt
  102. Local EBO:UInt
  103. glGenVertexArrays (1, Varptr VAO)
  104. glGenBuffers (1, Varptr VBO)
  105. glGenBuffers (1, Varptr EBO)
  106. ' bind the Vertex Array Object first, then bind and set vertex buffer(s), and then configure vertex attributes(s).
  107. glBindVertexArray (VAO)
  108. glBindBuffer (GL_ARRAY_BUFFER, VBO)
  109. glBufferData (GL_ARRAY_BUFFER, Int(vertices.length * SizeOf (0:Float)), vertices, GL_STATIC_DRAW)
  110. glBindBuffer (GL_ELEMENT_ARRAY_BUFFER, EBO)
  111. glBufferData (GL_ELEMENT_ARRAY_BUFFER, Int(indices.length * SizeOf (0:Float)), indices, GL_STATIC_DRAW)
  112. glVertexAttribPointer (0, 3, GL_FLOAT, GL_FALSE, Int(3 * SizeOf (0:Float)), 0:Byte Ptr)
  113. glEnableVertexAttribArray (0)
  114. ' note that this is allowed, the call to glVertexAttribPointer registered VBO as the vertex attribute's bound vertex buffer object so afterwards we can safely unbind
  115. glBindBuffer (GL_ARRAY_BUFFER, 0)
  116. ' You can unbind the VAO afterwards so other VAO calls won't accidentally modify this VAO, but this rarely happens. Modifying other
  117. ' VAOs requires a call to glBindVertexArray anyways so we generally don't unbind VAOs (nor VBOs) when it's not directly necessary.
  118. glBindVertexArray (0)
  119. ' render loop
  120. ' -----------
  121. While Not window.ShouldClose ()
  122. ' input
  123. ' -----
  124. ProcessInput (window)
  125. ' render
  126. ' ------
  127. glClearColor (0.2, 0.3, 0.3, 1.0)
  128. glClear (GL_COLOR_BUFFER_BIT)
  129. ' draw our first triangle
  130. glUseProgram (shaderProgram)
  131. glBindVertexArray (VAO) ' seeing as we only have a single VAO there's no need to bind it every time, but we'll do so to keep things a bit more organized
  132. glDrawElements (GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0)
  133. ' swap buffers and poll IO events (keys pressed/released, mouse moved etc.)
  134. window.SwapBuffers ()
  135. PollSystem ()
  136. Wend
  137. ' optional: de-allocate all resources once they've outlived their purpose
  138. glDeleteVertexArrays (1, Varptr VAO)
  139. glDeleteBuffers (1, Varptr VBO)
  140. glDeleteBuffers (1, Varptr EBO)
  141. End