Advertisement
Guest User

rotation.factor

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