5.1.transformations.bmx 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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.JpgLoader
  9. Import BRL.PngLoader
  10. Import BRL.StandardIO
  11. Local app_name:String = "Transformations"
  12. Const SCR_WIDTH:UInt = 800
  13. Const SCR_HEIGHT:UInt = 600
  14. Type TGameWindow Extends TGLFWWindow
  15. ' whenever the window size changed (by OS or user resize) this callback method executes
  16. Method OnFrameBufferSize (width:Int, height:Int)
  17. ' make sure the viewport matches the new window dimensions; note that width and
  18. ' height will be significantly larger than specified on retina displays.
  19. glViewport (0, 0, width, height)
  20. EndMethod
  21. EndType
  22. ' process all input: query GLFW whether relevant keys are pressed/released this frame and react accordingly
  23. Function ProcessInput (window:TGLFWWindow)
  24. If window.IsKeyDown (GLFW_KEY_ESCAPE)
  25. window.SetShouldClose (True)
  26. EndIf
  27. EndFunction
  28. ' glfw: initialize and configure
  29. TGLFWWindow.Hint (GLFW_CONTEXT_VERSION_MAJOR, 3)
  30. TGLFWWindow.Hint (GLFW_CONTEXT_VERSION_MINOR, 3)
  31. TGLFWWindow.Hint (GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE)
  32. ?MacOS ' Ewww...
  33. TGLFWWindow.Hint (GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE)
  34. ?
  35. ' glfw window creation
  36. Local window:TGLFWWindow = New TGameWindow.Create (SCR_WIDTH, SCR_HEIGHT, app_name)
  37. If Not window
  38. Print "Failed to create GLFW window!"
  39. End
  40. EndIf
  41. window.MakeContextCurrent ()
  42. ' glad: load all OpenGL function pointers
  43. gladLoadGL (glfwGetProcAddress)
  44. ' build and compile our shader program
  45. Local ourShader:TShader = New TShader("5.1.transform.vs", "5.1.transform.fs")
  46. ' set up vertex data (and buffer(s)) and configure vertex attributes
  47. Local vertices:Float [] = [..
  48. 0.5, 0.5, 0.0, 1.0, 1.0, ..
  49. 0.5, -0.5, 0.0, 1.0, 0.0, ..
  50. -0.5, -0.5, 0.0, 0.0, 0.0, ..
  51. -0.5, 0.5, 0.0, 0.0, 1.0]
  52. Local indices:Int[] = [..
  53. 0, 1, 3, ..
  54. 1, 2, 3 ]
  55. Local VBO:UInt
  56. Local VAO:UInt
  57. Local EBO:UInt
  58. glGenVertexArrays (1, Varptr VAO)
  59. glGenBuffers (1, Varptr VBO)
  60. glGenBuffers (1, Varptr EBO)
  61. ' bind the Vertex Array Object first, then bind and set vertex buffer(s), and then configure vertex attributes(s).
  62. glBindVertexArray (VAO)
  63. glBindBuffer (GL_ARRAY_BUFFER, VBO)
  64. glBufferData (GL_ARRAY_BUFFER, Int(vertices.length * SizeOf (0:Float)), vertices, GL_STATIC_DRAW)
  65. glBindBuffer (GL_ELEMENT_ARRAY_BUFFER, EBO)
  66. glBufferData (GL_ELEMENT_ARRAY_BUFFER, Int(indices.length * SizeOf (0:Int)), indices, GL_STATIC_DRAW)
  67. ' position attribute
  68. glVertexAttribPointer (0, 3, GL_FLOAT, GL_FALSE, Int(5 * SizeOf (0:Float)), 0:Byte Ptr)
  69. glEnableVertexAttribArray (0)
  70. Local attribute_offset:Int = 3 * SizeOf (0:Float)
  71. ' texture coord attribute
  72. glVertexAttribPointer (1, 2, GL_FLOAT, GL_FALSE, Int(5 * SizeOf (0:Float)), Byte Ptr (attribute_offset))
  73. glEnableVertexAttribArray (1)
  74. ' load and create a texture
  75. ' -------------------------
  76. Local texture1:UInt
  77. Local texture2:UInt
  78. ' texture 1
  79. ' ---------
  80. glGenTextures(1, Varptr texture1)
  81. glBindTexture(GL_TEXTURE_2D, texture1)
  82. ' set the texture wrapping parameters
  83. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT)
  84. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT)
  85. ' set texture filtering parameters
  86. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
  87. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
  88. ' load image, create texture and generate mipmaps
  89. Local pixmap:TPixmap = LoadPixmap(PATH_PREFIX + "../resources/textures/container.jpg")
  90. If pixmap Then
  91. pixmap = YFlipPixmap(pixmap)
  92. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, pixmap.width, pixmap.height, 0, GL_RGB, GL_UNSIGNED_BYTE, pixmap.pixels)
  93. glGenerateMipmap(GL_TEXTURE_2D)
  94. Else
  95. Print "Failed to load texture"
  96. End
  97. End If
  98. ' texture 2
  99. ' ---------
  100. glGenTextures(1, Varptr texture2)
  101. glBindTexture(GL_TEXTURE_2D, texture2)
  102. ' set the texture wrapping parameters
  103. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT)
  104. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT)
  105. ' set texture filtering parameters
  106. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
  107. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
  108. ' load image, create texture and generate mipmaps
  109. pixmap = LoadPixmap(PATH_PREFIX + "../resources/textures/awesomeface.png")
  110. If pixmap Then
  111. pixmap = YFlipPixmap(pixmap)
  112. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, pixmap.width, pixmap.height, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixmap.pixels)
  113. glGenerateMipmap(GL_TEXTURE_2D)
  114. Else
  115. Print "Failed to load texture"
  116. End
  117. End If
  118. pixmap = Null
  119. ' tell opengl for each sampler to which texture unit it belongs to (only has to be done once)
  120. ourShader.use()
  121. ourShader.setInt("texture1", 0)
  122. ourShader.setInt("texture2", 1)
  123. Local angle:Double = 0
  124. ' render loop
  125. ' -----------
  126. While Not window.ShouldClose ()
  127. angle :+ 0.1
  128. ' input
  129. ' -----
  130. ProcessInput (window)
  131. ' render
  132. ' ------
  133. glClearColor (0.2, 0.3, 0.3, 1.0)
  134. glClear (GL_COLOR_BUFFER_BIT)
  135. ' bind textures on corresponding texture units
  136. glActiveTexture(GL_TEXTURE0)
  137. glBindTexture(GL_TEXTURE_2D, texture1)
  138. glActiveTexture(GL_TEXTURE1)
  139. glBindTexture(GL_TEXTURE_2D, texture2)
  140. ' create transformations
  141. Local transform:SMat4F = SMat4F.Identity()
  142. transform = transform.Translate(New SVec3F(0.5, -0.5, 0.0))
  143. transform = transform.Rotate(New SVec3F(0.0, 0.0, 1.0), angle)
  144. ' get matrix's uniform location and set matrix
  145. ourShader.use()
  146. Local transformLoc:Int = glGetUniformLocation(ourShader.Id, "transform")
  147. glUniformMatrix4fv(transformLoc, 1, GL_FALSE, Varptr transform.a)
  148. ' render container
  149. glBindVertexArray (VAO)
  150. glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0)
  151. ' glfw: swap buffers and poll IO events (keys pressed/released, mouse moved etc.)
  152. window.SwapBuffers ()
  153. PollSystem ()
  154. Wend
  155. ' optional: de-allocate all resources once they've outlived their purpose
  156. glDeleteVertexArrays (1, Varptr VAO)
  157. glDeleteBuffers (1, Varptr VBO)
  158. glDeleteBuffers (1, Varptr EBO)
  159. End