Advertisement
Guest User

triangle.factor

a guest
May 29th, 2021
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.18 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: triangle
  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. TUPLE: triangle-world < world
  26. program vertex-buffer vertex-array ;
  27.  
  28. : (program) ( -- program )
  29. vertex-shader fragment-shader <simple-gl-program> ;
  30.  
  31. : (vertex-buffer) ( -- vertex-buffer )
  32. {
  33. -0.5 -0.5 0.0 ! Left
  34. 0.5 -0.5 0.0 ! Right
  35. 0.0 0.5 0.0 ! Top
  36. }
  37. c:float >c-array underlying>>
  38. GL_ARRAY_BUFFER swap GL_STATIC_DRAW <gl-buffer> ;
  39.  
  40. : (vertex-array) ( vertex-buffer -- vertex-array )
  41. gen-vertex-array [
  42. [
  43. GL_ARRAY_BUFFER swap
  44. [
  45. 0 3 GL_FLOAT GL_FALSE c:float heap-size 3 * 0 buffer-offset glVertexAttribPointer
  46. 0 glEnableVertexAttribArray
  47. ] with-gl-buffer
  48. ] with-vertex-array ] keep ;
  49.  
  50. M: triangle-world begin-world
  51. "3.3" require-gl-version
  52. GL_DEPTH_TEST glEnable
  53. 1.0 1.0 1.0 1.0 glClearColor
  54. (program) >>program
  55. (vertex-buffer) [ >>vertex-buffer ] keep
  56. (vertex-array) >>vertex-array
  57. drop ;
  58.  
  59. M: triangle-world end-world
  60. {
  61. [ program>> [ delete-gl-program ] when* ]
  62. [ vertex-buffer>> [ delete-gl-buffer ] when* ]
  63. [ vertex-array>> [ delete-vertex-array ] when* ]
  64. } cleave ;
  65.  
  66. M: triangle-world draw-world*
  67. GL_DEPTH_BUFFER_BIT GL_COLOR_BUFFER_BIT bitor glClear
  68. dup
  69. vertex-array>>
  70. [
  71. program>>
  72. [
  73. GL_TRIANGLES 0 3 glDrawArrays drop
  74. ] with-gl-program
  75. ] with-vertex-array
  76. ;
  77.  
  78. MAIN-WINDOW: triangle-window {
  79. { world-class triangle-world }
  80. { title "Triangle" }
  81. { pixel-format-attributes {
  82. windowed
  83. double-buffered
  84. T{ depth-bits { value 16 } }
  85. } }
  86. { pref-dim { 640 480 } }
  87. } ;
  88.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement