4.1.textures.bmx 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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.StandardIO
  10. Local app_name:String = "Textures"
  11. Const SCR_WIDTH:UInt = 800
  12. Const SCR_HEIGHT:UInt = 600
  13. Type TGameWindow Extends TGLFWWindow
  14. ' whenever the window size changed (by OS or user resize) this callback method executes
  15. Method OnFrameBufferSize (width:Int, height:Int)
  16. ' make sure the viewport matches the new window dimensions; note that width and
  17. ' height will be significantly larger than specified on retina displays.
  18. glViewport (0, 0, width, height)
  19. EndMethod
  20. EndType
  21. ' process all input: query GLFW whether relevant keys are pressed/released this frame and react accordingly
  22. Function ProcessInput (window:TGLFWWindow)
  23. If window.IsKeyDown (GLFW_KEY_ESCAPE)
  24. window.SetShouldClose (True)
  25. EndIf
  26. EndFunction
  27. ' glfw: initialize and configure
  28. TGLFWWindow.Hint (GLFW_CONTEXT_VERSION_MAJOR, 3)
  29. TGLFWWindow.Hint (GLFW_CONTEXT_VERSION_MINOR, 3)
  30. TGLFWWindow.Hint (GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE)
  31. ?MacOS ' Ewww...
  32. TGLFWWindow.Hint (GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE)
  33. ?
  34. ' glfw window creation
  35. Local window:TGLFWWindow = New TGameWindow.Create (SCR_WIDTH, SCR_HEIGHT, app_name)
  36. If Not window
  37. Print "Failed to create GLFW window!"
  38. End
  39. EndIf
  40. window.MakeContextCurrent ()
  41. ' glad: load all OpenGL function pointers
  42. gladLoadGL (glfwGetProcAddress)
  43. ' build and compile our shader program
  44. Local ourShader:TShader = New TShader("4.1.texture.vs", "4.1.texture.fs")
  45. ' set up vertex data (and buffer(s)) and configure vertex attributes
  46. Local vertices:Float [] = [..
  47. 0.5, 0.5, 0.0, 1.0, 0.0, 0.0, 1.0, 1.0, ..
  48. 0.5, -0.5, 0.0, 0.0, 1.0, 0.0, 1.0, 0.0, ..
  49. -0.5, -0.5, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, ..
  50. -0.5, 0.5, 0.0, 1.0, 1.0, 0.0, 0.0, 1.0]
  51. Local indices:Int[] = [..
  52. 0, 1, 3, ..
  53. 1, 2, 3 ]
  54. Local VBO:UInt
  55. Local VAO:UInt
  56. Local EBO:UInt
  57. glGenVertexArrays (1, Varptr VAO)
  58. glGenBuffers (1, Varptr VBO)
  59. glGenBuffers (1, Varptr EBO)
  60. ' bind the Vertex Array Object first, then bind and set vertex buffer(s), and then configure vertex attributes(s).
  61. glBindVertexArray (VAO)
  62. glBindBuffer (GL_ARRAY_BUFFER, VBO)
  63. glBufferData (GL_ARRAY_BUFFER, Int(vertices.length * SizeOf (0:Float)), vertices, GL_STATIC_DRAW)
  64. glBindBuffer (GL_ELEMENT_ARRAY_BUFFER, EBO)
  65. glBufferData (GL_ELEMENT_ARRAY_BUFFER, Int(indices.length * SizeOf (0:Int)), indices, GL_STATIC_DRAW)
  66. ' position attribute
  67. glVertexAttribPointer (0, 3, GL_FLOAT, GL_FALSE, Int(8 * SizeOf (0:Float)), 0:Byte Ptr)
  68. glEnableVertexAttribArray (0)
  69. Local attribute_offset:Int = 3 * SizeOf (0:Float)
  70. ' color attribute
  71. glVertexAttribPointer (1, 3, GL_FLOAT, GL_FALSE, Int(8 * SizeOf (0:Float)), Byte Ptr (attribute_offset))
  72. glEnableVertexAttribArray (1)
  73. attribute_offset:Int = 6 * SizeOf (0:Float)
  74. ' texture coord attribute
  75. glVertexAttribPointer (2, 2, GL_FLOAT, GL_FALSE, Int(8 * SizeOf (0:Float)), Byte Ptr (attribute_offset))
  76. glEnableVertexAttribArray (2)
  77. ' load and create a texture
  78. ' -------------------------
  79. Local texture:UInt
  80. glGenTextures(1, Varptr texture)
  81. glBindTexture(GL_TEXTURE_2D, texture)
  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. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, pixmap.width, pixmap.height, 0, GL_RGB, GL_UNSIGNED_BYTE, pixmap.pixels)
  92. glGenerateMipmap(GL_TEXTURE_2D)
  93. Else
  94. Print "Failed to load texture"
  95. End
  96. End If
  97. pixmap = Null
  98. ' render loop
  99. ' -----------
  100. While Not window.ShouldClose ()
  101. ' input
  102. ' -----
  103. ProcessInput (window)
  104. ' render
  105. ' ------
  106. glClearColor (0.2, 0.3, 0.3, 1.0)
  107. glClear (GL_COLOR_BUFFER_BIT)
  108. ' bind Texture
  109. glBindTexture(GL_TEXTURE_2D, texture)
  110. ' render container
  111. ourShader.use()
  112. glBindVertexArray (VAO)
  113. glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0)
  114. ' glfw: swap buffers and poll IO events (keys pressed/released, mouse moved etc.)
  115. window.SwapBuffers ()
  116. PollSystem ()
  117. Wend
  118. ' optional: de-allocate all resources once they've outlived their purpose
  119. glDeleteVertexArrays (1, Varptr VAO)
  120. glDeleteBuffers (1, Varptr VBO)
  121. glDeleteBuffers (1, Varptr EBO)
  122. End