Advertisement
Guest User

rectangle.factor

a guest
May 29th, 2021
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.88 KB | None | 0 0
  1. USING: accessors alien.c-types alien.data combinators kernel math
  2. multiline opengl opengl.gl3 opengl.capabilities opengl.shaders
  3. ui ui.gadgets.worlds ui.pixel-formats ;
  4. QUALIFIED-WITH: alien.c-types c
  5. IN: rectangle
  6.  
  7. STRING: vertex-shader
  8. #version 330 core
  9. layout (location = 0) in vec3 position;
  10. void main()
  11. {
  12. gl_Position = vec4(position.x, position.y, position.z, 1.0);
  13. }
  14. ;
  15.  
  16. STRING: fragment-shader
  17. #version 330 core
  18. out vec4 color;
  19. void main()
  20. {
  21. color = vec4(0.0f, 0.0f, 1.0f, 1.0f);
  22. }
  23. ;
  24.  
  25. ! The function with-array-element-buffers does not work with VAO. Never unbind index-buffer!
  26. : with-buffers ( vertex-buffer index-buffer quot -- )
  27. swap GL_ELEMENT_ARRAY_BUFFER swap glBindBuffer
  28. [ GL_ARRAY_BUFFER ] 2dip with-gl-buffer ; inline
  29.  
  30. TUPLE: rectangle-world < world
  31. program vertex-buffer index-buffer vertex-array ;
  32.  
  33. : (program) ( -- program )
  34. vertex-shader fragment-shader <simple-gl-program> ;
  35.  
  36. : (vertex-buffer) ( -- vertex-buffer )
  37. {
  38. 0.5 0.5 0.0 ! top right
  39. 0.5 -0.5 0.0 ! bottom right
  40. -0.5 -0.5 0.0 ! bottom left
  41. -0.5 0.5 0.0 ! top left
  42. }
  43. c:float >c-array underlying>>
  44. GL_ARRAY_BUFFER swap GL_STATIC_DRAW <gl-buffer> ;
  45.  
  46. : (index-buffer) ( -- index-buffer )
  47. {
  48. 0 1 3 ! first triangle
  49. 1 2 3 ! second triangle
  50. }
  51. c:uint >c-array underlying>>
  52. GL_ELEMENT_ARRAY_BUFFER swap GL_STATIC_DRAW <gl-buffer> ;
  53.  
  54. : (vertex-array) ( vertex-buffer index-buffer -- vertex-array )
  55. gen-vertex-array [
  56. [
  57. [
  58. 0 3 GL_FLOAT GL_FALSE c:float heap-size 3 * 0 buffer-offset glVertexAttribPointer
  59. 0 glEnableVertexAttribArray
  60. ] with-buffers
  61. ]
  62. with-vertex-array ] keep ;
  63.  
  64. M: rectangle-world begin-world
  65. "3.3" require-gl-version
  66. GL_DEPTH_TEST glEnable
  67. 1.0 1.0 1.0 1.0 glClearColor
  68. (program) >>program
  69. (vertex-buffer) >>vertex-buffer
  70. (index-buffer) >>index-buffer
  71. dup
  72. [ vertex-buffer>> ] [ index-buffer>> ] bi
  73. (vertex-array) >>vertex-array
  74. drop ;
  75.  
  76. M: rectangle-world end-world
  77. {
  78. [ program>> [ delete-gl-program ] when* ]
  79. [ vertex-buffer>> [ delete-gl-buffer ] when* ]
  80. [ index-buffer>> [ delete-gl-buffer ] when* ]
  81. [ vertex-array>> [ delete-vertex-array ] when* ]
  82. } cleave ;
  83.  
  84. M: rectangle-world draw-world*
  85. GL_DEPTH_BUFFER_BIT GL_COLOR_BUFFER_BIT bitor glClear
  86. dup
  87. vertex-array>>
  88. [
  89. program>>
  90. [
  91. GL_TRIANGLES 6 GL_UNSIGNED_INT 0 buffer-offset glDrawElements drop
  92. ] with-gl-program
  93. ] with-vertex-array
  94. ;
  95.  
  96. MAIN-WINDOW: rectangle-window {
  97. { world-class rectangle-world }
  98. { title "Rectangle" }
  99. { pixel-format-attributes {
  100. windowed
  101. double-buffered
  102. T{ depth-bits { value 16 } }
  103. } }
  104. { pref-dim { 640 480 } }
  105. } ;
  106.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement