7.1.camera_circle.bmx 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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 = "Camera Circle"
  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. ' configure global opengl state
  45. glEnable(GL_DEPTH_TEST)
  46. ' build and compile our shader program
  47. Local ourShader:TShader = New TShader("7.1.camera.vs", "7.1.camera.fs")
  48. ' set up vertex data (and buffer(s)) and configure vertex attributes
  49. Local vertices:Float [] = [..
  50. -0.5, -0.5, -0.5, 0.0, 0.0, ..
  51. 0.5, -0.5, -0.5, 1.0, 0.0, ..
  52. 0.5, 0.5, -0.5, 1.0, 1.0, ..
  53. 0.5, 0.5, -0.5, 1.0, 1.0, ..
  54. -0.5, 0.5, -0.5, 0.0, 1.0, ..
  55. -0.5, -0.5, -0.5, 0.0, 0.0, ..
  56. ..
  57. -0.5, -0.5, 0.5, 0.0, 0.0, ..
  58. 0.5, -0.5, 0.5, 1.0, 0.0, ..
  59. 0.5, 0.5, 0.5, 1.0, 1.0, ..
  60. 0.5, 0.5, 0.5, 1.0, 1.0, ..
  61. -0.5, 0.5, 0.5, 0.0, 1.0, ..
  62. -0.5, -0.5, 0.5, 0.0, 0.0, ..
  63. ..
  64. -0.5, 0.5, 0.5, 1.0, 0.0, ..
  65. -0.5, 0.5, -0.5, 1.0, 1.0, ..
  66. -0.5, -0.5, -0.5, 0.0, 1.0, ..
  67. -0.5, -0.5, -0.5, 0.0, 1.0, ..
  68. -0.5, -0.5, 0.5, 0.0, 0.0, ..
  69. -0.5, 0.5, 0.5, 1.0, 0.0, ..
  70. ..
  71. 0.5, 0.5, 0.5, 1.0, 0.0, ..
  72. 0.5, 0.5, -0.5, 1.0, 1.0, ..
  73. 0.5, -0.5, -0.5, 0.0, 1.0, ..
  74. 0.5, -0.5, -0.5, 0.0, 1.0, ..
  75. 0.5, -0.5, 0.5, 0.0, 0.0, ..
  76. 0.5, 0.5, 0.5, 1.0, 0.0, ..
  77. ..
  78. -0.5, -0.5, -0.5, 0.0, 1.0, ..
  79. 0.5, -0.5, -0.5, 1.0, 1.0, ..
  80. 0.5, -0.5, 0.5, 1.0, 0.0, ..
  81. 0.5, -0.5, 0.5, 1.0, 0.0, ..
  82. -0.5, -0.5, 0.5, 0.0, 0.0, ..
  83. -0.5, -0.5, -0.5, 0.0, 1.0, ..
  84. ..
  85. -0.5, 0.5, -0.5, 0.0, 1.0, ..
  86. 0.5, 0.5, -0.5, 1.0, 1.0, ..
  87. 0.5, 0.5, 0.5, 1.0, 0.0, ..
  88. 0.5, 0.5, 0.5, 1.0, 0.0, ..
  89. -0.5, 0.5, 0.5, 0.0, 0.0, ..
  90. -0.5, 0.5, -0.5, 0.0, 1.0]
  91. ' world space positions of our cubes
  92. Local cubePositions:SVec3F[] = [ ..
  93. New SVec3F( 0.0, 0.0, 0.0), ..
  94. New SVec3F( 2.0, 5.0, -15.0), ..
  95. New SVec3F(-1.5, -2.2, -2.5), ..
  96. New SVec3F(-3.8, -2.0, -12.3), ..
  97. New SVec3F( 2.4, -0.4, -3.5), ..
  98. New SVec3F(-1.7, 3.0, -7.5), ..
  99. New SVec3F( 1.3, -2.0, -2.5), ..
  100. New SVec3F( 1.5, 2.0, -2.5), ..
  101. New SVec3F( 1.5, 0.2, -1.5), ..
  102. New SVec3F(-1.3, 1.0, -1.5) ..
  103. ]
  104. Local VBO:UInt
  105. Local VAO:UInt
  106. glGenVertexArrays (1, Varptr VAO)
  107. glGenBuffers (1, Varptr VBO)
  108. ' bind the Vertex Array Object first, then bind and set vertex buffer(s), and then configure vertex attributes(s).
  109. glBindVertexArray (VAO)
  110. glBindBuffer (GL_ARRAY_BUFFER, VBO)
  111. glBufferData (GL_ARRAY_BUFFER, Int(vertices.length * SizeOf (0:Float)), vertices, GL_STATIC_DRAW)
  112. ' position attribute
  113. glVertexAttribPointer (0, 3, GL_FLOAT, GL_FALSE, Int(5 * SizeOf (0:Float)), 0:Byte Ptr)
  114. glEnableVertexAttribArray (0)
  115. Local attribute_offset:Int = 3 * SizeOf (0:Float)
  116. ' texture coord attribute
  117. glVertexAttribPointer (1, 2, GL_FLOAT, GL_FALSE, Int(5 * SizeOf (0:Float)), Byte Ptr (attribute_offset))
  118. glEnableVertexAttribArray (1)
  119. ' load and create a texture
  120. ' -------------------------
  121. Local texture1:UInt
  122. Local texture2:UInt
  123. ' texture 1
  124. ' ---------
  125. glGenTextures(1, Varptr texture1)
  126. glBindTexture(GL_TEXTURE_2D, texture1)
  127. ' set the texture wrapping parameters
  128. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT)
  129. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT)
  130. ' set texture filtering parameters
  131. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
  132. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
  133. ' load image, create texture and generate mipmaps
  134. Local pixmap:TPixmap = LoadPixmap(PATH_PREFIX + "../resources/textures/container.jpg")
  135. If pixmap Then
  136. pixmap = YFlipPixmap(pixmap)
  137. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, pixmap.width, pixmap.height, 0, GL_RGB, GL_UNSIGNED_BYTE, pixmap.pixels)
  138. glGenerateMipmap(GL_TEXTURE_2D)
  139. Else
  140. Print "Failed to load texture"
  141. End
  142. End If
  143. ' texture 2
  144. ' ---------
  145. glGenTextures(1, Varptr texture2)
  146. glBindTexture(GL_TEXTURE_2D, texture2)
  147. ' set the texture wrapping parameters
  148. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT)
  149. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT)
  150. ' set texture filtering parameters
  151. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
  152. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
  153. ' load image, create texture and generate mipmaps
  154. pixmap = LoadPixmap(PATH_PREFIX + "../resources/textures/awesomeface.png")
  155. If pixmap Then
  156. pixmap = YFlipPixmap(pixmap)
  157. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, pixmap.width, pixmap.height, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixmap.pixels)
  158. glGenerateMipmap(GL_TEXTURE_2D)
  159. Else
  160. Print "Failed to load texture"
  161. End
  162. End If
  163. pixmap = Null
  164. ' tell opengl for each sampler to which texture unit it belongs to (only has to be done once)
  165. ourShader.use()
  166. ourShader.setInt("texture1", 0)
  167. ourShader.setInt("texture2", 1)
  168. ' pass projection matrix to shader (as projection matrix rarely changes there's no need to do this per frame)
  169. Local projection:SMat4F = SMat4F.Perspective(45.0, SCR_WIDTH, SCR_HEIGHT, 0.1, 100.0)
  170. ourShader.setMat4("projection", projection)
  171. Local angle:Float = MilliSecs() / 1000.0
  172. ' render loop
  173. ' -----------
  174. While Not window.ShouldClose ()
  175. angle :+ 0.1
  176. ' input
  177. ' -----
  178. ProcessInput (window)
  179. ' render
  180. ' ------
  181. glClearColor (0.2, 0.3, 0.3, 1.0)
  182. glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) ' also clear the depth buffer now!
  183. ' bind textures on corresponding texture units
  184. glActiveTexture(GL_TEXTURE0)
  185. glBindTexture(GL_TEXTURE_2D, texture1)
  186. glActiveTexture(GL_TEXTURE1)
  187. glBindTexture(GL_TEXTURE_2D, texture2)
  188. ' activate shader
  189. ourShader.use()
  190. ' create transformations
  191. Local view:SMat4F = SMat4F.Identity()
  192. Local radius:Float = 10.0
  193. Local camX:Float = Sin(angle) * radius
  194. Local camZ:Float = Cos(angle) * radius
  195. view = SMat4F.LookAt(New SVec3F(camX, 0.0, camZ), New SVec3F(0.0, 0.0, 0.0), New SVec3F(0.0, 1.0, 0.0))
  196. ourShader.setMat4("view", view)
  197. ' render boxes
  198. glBindVertexArray (VAO)
  199. For Local i:Int = 0 Until 10
  200. ' calculate the model matrix for each object and pass it to shader before drawing
  201. Local model:SMat4F = SMat4F.Identity()
  202. model = model.Translate(cubePositions[i])
  203. Local angle:Float = 20.0 * i
  204. model = model.Rotate(New SVec3F(1.0, 0.3, 0.5), angle)
  205. ourShader.setMat4("model", model)
  206. glDrawArrays(GL_TRIANGLES, 0, 36)
  207. Next
  208. ' glfw: swap buffers and poll IO events (keys pressed/released, mouse moved etc.)
  209. window.SwapBuffers ()
  210. PollSystem ()
  211. Wend
  212. ' optional: de-allocate all resources once they've outlived their purpose
  213. glDeleteVertexArrays (1, Varptr VAO)
  214. glDeleteBuffers (1, Varptr VBO)
  215. End