Advertisement
Guest User

sphere.factor

a guest
Jun 22nd, 2021
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.59 KB | None | 0 0
  1. USING: accessors alien.c-types alien.data arrays calendar combinators images.loader
  2. kernel literals locals math math.constants math.functions math.matrices multiline namespaces
  3. opengl opengl.gl3 opengl.capabilities opengl.shaders opengl.textures sequences timers
  4. ui ui.gadgets ui.gadgets.worlds ui.pixel-formats ;
  5. QUALIFIED-WITH: alien.c-types c
  6. IN: sphere
  7.  
  8. CONSTANT: N 50 ! latitude bands number
  9. CONSTANT: M 50 ! longitude bands number
  10. CONSTANT: distance -5.0
  11. CONSTANT: FOV $[ 2.0 sqrt 1 + ] ! cotangens(pi/8)
  12. SYMBOL: indexNumber
  13.  
  14. STRING: vertex-shader
  15. #version 330 core
  16. layout (location = 0) in vec3 position;
  17. out vec3 texCoords;
  18. uniform mat4 matrix;
  19. void main()
  20. {
  21. gl_Position = matrix * vec4(position.x, position.y, position.z, 1.0f);
  22. texCoords = position;
  23. }
  24. ;
  25.  
  26. STRING: fragment-shader
  27. #version 330 core
  28. in vec3 texCoords;
  29. out vec4 color;
  30. uniform samplerCube ourTexture;
  31. void main()
  32. {
  33. color = texture(ourTexture, texCoords);
  34. }
  35. ;
  36.  
  37. ! The vocab opengl.textures is outdated!
  38. :: tex-image ( image bitmap -- )
  39. image image-format :> ( internal-format format type )
  40. GL_TEXTURE_CUBE_MAP_POSITIVE_X 0 internal-format
  41. image dim>> first2 0
  42. format type bitmap glTexImage2D
  43. GL_TEXTURE_CUBE_MAP_NEGATIVE_X 0 internal-format
  44. image dim>> first2 0
  45. format type bitmap glTexImage2D
  46. GL_TEXTURE_CUBE_MAP_POSITIVE_Y 0 internal-format
  47. image dim>> first2 0
  48. format type bitmap glTexImage2D
  49. GL_TEXTURE_CUBE_MAP_NEGATIVE_Y 0 internal-format
  50. image dim>> first2 0
  51. format type bitmap glTexImage2D
  52. GL_TEXTURE_CUBE_MAP_POSITIVE_Z 0 internal-format
  53. image dim>> first2 0
  54. format type bitmap glTexImage2D
  55. GL_TEXTURE_CUBE_MAP_NEGATIVE_Z 0 internal-format
  56. image dim>> first2 0
  57. format type bitmap glTexImage2D ;
  58.  
  59. : make-texture ( image -- id )
  60. gen-texture [
  61. GL_TEXTURE_CUBE_MAP swap glBindTexture
  62. dup bitmap>> tex-image
  63. GL_TEXTURE_CUBE_MAP glGenerateMipmap
  64. GL_TEXTURE_CUBE_MAP GL_TEXTURE_MIN_FILTER GL_LINEAR_MIPMAP_LINEAR glTexParameteri
  65. GL_TEXTURE_CUBE_MAP GL_TEXTURE_MAG_FILTER GL_LINEAR glTexParameteri
  66. GL_TEXTURE_CUBE_MAP GL_TEXTURE_WRAP_S GL_REPEAT glTexParameteri
  67. GL_TEXTURE_CUBE_MAP GL_TEXTURE_WRAP_T GL_REPEAT glTexParameteri
  68. GL_TEXTURE_CUBE_MAP GL_TEXTURE_WRAP_R GL_REPEAT glTexParameteri
  69. ] keep ;
  70.  
  71. TUPLE: rotation-world < world
  72. angle t-matrix pt-matrix program vertex-buffer index-buffer vertex-array texture ;
  73.  
  74. : (program) ( -- program )
  75. vertex-shader fragment-shader <simple-gl-program> ;
  76.  
  77. :: triangle ( n m -- n array )
  78. n pi * N / :> phi
  79. phi cos :> cosPhi
  80. phi sin :> sinPhi
  81. m 2 * pi * M / :> theta
  82. theta sin :> sinTheta
  83. theta cos :> cosTheta
  84. n
  85. cosTheta sinPhi *
  86. cosPhi
  87. sinTheta sinPhi *
  88. 3array ;
  89.  
  90. : (vertex-buffer) ( -- vertex-buffer )
  91. N 1 + <iota> [ M 1 + <iota> [ triangle ] map concat nip ] map concat
  92. c:float >c-array underlying>>
  93. GL_ARRAY_BUFFER swap GL_STATIC_DRAW <gl-buffer> ;
  94.  
  95. :: triangles ( n m -- n array )
  96. n M 1 + * m + :> firstIndex
  97. firstIndex M + 1 + :> secondIndex
  98. n
  99. firstIndex secondIndex firstIndex 1 + 3array
  100. secondIndex secondIndex 1 + firstIndex 1 + 3array
  101. append ;
  102.  
  103. : (index-buffer) ( -- index-buffer )
  104. N <iota> [ M <iota> [ triangles ] map concat nip ] map concat
  105. dup length indexNumber set
  106. c:uint >c-array underlying>>
  107. GL_ELEMENT_ARRAY_BUFFER swap GL_STATIC_DRAW <gl-buffer> ;
  108.  
  109. ! The function with-array-element-buffers from opengl does not work with VAO! Never unbind element-buffer!
  110.  
  111. : (vertex-array) ( vertex-buffer index-buffer -- vertex-array )
  112. gen-vertex-array [
  113. [
  114. GL_ELEMENT_ARRAY_BUFFER swap glBindBuffer
  115.  
  116. GL_ARRAY_BUFFER swap
  117. [
  118. 0 3 GL_FLOAT GL_FALSE c:float heap-size 3 * 0 buffer-offset glVertexAttribPointer
  119. ] with-gl-buffer
  120.  
  121. 0 glEnableVertexAttribArray
  122. ]
  123. with-vertex-array ] keep ;
  124.  
  125. :: perspective-matrix ( xy-dim near far -- matrix )
  126. xy-dim first2 :> ( x y )
  127. FOV 640 * x / :> xf
  128. FOV 640 * y / :> yf
  129. near far + near far - /f :> zf
  130. 2 near far * * near far - /f :> wf
  131. {
  132. { xf 0.0 0.0 0.0 }
  133. { 0.0 yf 0.0 0.0 }
  134. { 0.0 0.0 zf wf }
  135. { 0.0 0.0 -1.0 0.0 }
  136. } ;
  137.  
  138. : (r-matrix) ( angle -- matrix )
  139. 360.0 / 2 * pi *
  140. { 0.0 1.0 0.0 1.0 } swap rotation-matrix4 ;
  141.  
  142. : (t-matrix) ( -- matrix )
  143. 0.0 0.0 distance 3array translation-matrix4 ;
  144.  
  145. : (p-matrix) ( xy-dim -- matrix )
  146. 0.1 500.0 perspective-matrix ;
  147.  
  148. : increase ( angle -- angle )
  149. 1.0 + dup 360.0 > [ 360.0 - ] when ;
  150.  
  151. M: rotation-world begin-world
  152. "3.3" require-gl-version
  153. GL_DEPTH_TEST glEnable
  154. 1.0 1.0 1.0 1.0 glClearColor
  155. 0.0 >>angle
  156. (program) >>program
  157. (vertex-buffer) >>vertex-buffer
  158. (index-buffer) >>index-buffer
  159. (t-matrix) >>t-matrix
  160. dup
  161. [ vertex-buffer>> ] [ index-buffer>> ] bi
  162. (vertex-array) >>vertex-array
  163. "vocab:sphere/1.jpg" load-image make-texture >>texture
  164. [ [ increase ] change-angle relayout ] curry 25 milliseconds every drop ;
  165.  
  166. M: rotation-world resize-world
  167. dup
  168. [ dim>> (p-matrix) ] [ t-matrix>> ] bi m. >>pt-matrix
  169. dim>> 0 0 rot first2 glViewport ;
  170.  
  171. M: rotation-world end-world
  172. {
  173. [ program>> [ delete-gl-program ] when* ]
  174. [ vertex-buffer>> [ delete-gl-buffer ] when* ]
  175. [ index-buffer>> [ delete-gl-buffer ] when* ]
  176. [ vertex-array>> [ delete-vertex-array ] when* ]
  177. [ texture>> [ delete-texture ] when* ]
  178. } cleave ;
  179.  
  180. M: rotation-world draw-world*
  181. GL_DEPTH_BUFFER_BIT GL_COLOR_BUFFER_BIT bitor glClear
  182. dup
  183. GL_TEXTURE_CUBE_MAP swap texture>> glBindTexture
  184. dup
  185. vertex-array>>
  186. [
  187. dup
  188. program>>
  189. [
  190. "matrix" glGetUniformLocation
  191. swap [ pt-matrix>> ] [ angle>> (r-matrix) ] bi m.
  192. { 0 1 2 3 } swap cols concat c:float >c-array
  193. 1 GL_FALSE rot glUniformMatrix4fv
  194.  
  195. GL_TRIANGLES indexNumber get GL_UNSIGNED_INT 0 buffer-offset glDrawElements
  196. ] with-gl-program
  197. ] with-vertex-array ;
  198.  
  199. MAIN-WINDOW: rotation-window {
  200. { world-class rotation-world }
  201. { title "Rotation" }
  202. { pixel-format-attributes {
  203. windowed
  204. double-buffered
  205. T{ depth-bits { value 16 } }
  206. } }
  207. { pref-dim { 640 640 } }
  208. } ;
  209.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement