Advertisement
Isti115

elm-webgl-triangles

Mar 22nd, 2017
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.83 KB | None | 0 0
  1. module Triangles exposing (..)
  2.  
  3. import WebGL as GL exposing (..)
  4. import Math.Vector2 exposing (Vec2, vec2)
  5. import Math.Vector3 exposing (Vec3, vec3)
  6. import Math.Matrix4 exposing (..)
  7. import Html exposing (..)
  8.  
  9. type alias Model =
  10. { rotation : Float
  11. }
  12.  
  13. type alias Vertex =
  14. { position : Vec2
  15. , color : Vec3
  16. }
  17.  
  18. type alias Varying =
  19. { vColor : Vec3
  20. }
  21.  
  22. update : msg -> Model -> Model
  23. update e m = {m | rotation = m.rotation + 0.1}
  24.  
  25. --main : Program Never {} {}
  26. main =
  27. Html.beginnerProgram
  28. { model =
  29. Model 1.0
  30. , view = view
  31. , update = update
  32. }
  33.  
  34.  
  35. view : Model -> Html msg
  36. view m =
  37. GL.toHtml
  38. []
  39. [ boxEntity m.rotation ]
  40.  
  41.  
  42. boxEntity : Float -> Entity
  43. boxEntity f =
  44. GL.entity
  45. vertexShader
  46. fragmentShader
  47. boxMesh
  48. { rotation = makeRotate f (vec3 1 1 1) }
  49.  
  50.  
  51. vertexShader : Shader { position : Vec2, color : Vec3 } { rotation : Mat4 } { vColor : Vec3 }
  52. vertexShader =
  53. [glsl|
  54.  
  55. precision mediump float;
  56. attribute vec2 position;
  57. attribute vec3 color;
  58. uniform mat4 rotation;
  59. varying vec3 vColor;
  60.  
  61. void main () {
  62. gl_Position = rotation * vec4(position, 0.0, 1.0);
  63. vColor = color;
  64. }
  65.  
  66. |]
  67.  
  68.  
  69. fragmentShader : Shader {} { rotation : Mat4 } Varying
  70. fragmentShader =
  71. [glsl|
  72.  
  73. precision mediump float;
  74. varying vec3 vColor;
  75.  
  76. void main () {
  77. gl_FragColor = vec4(vColor, 1.);
  78. }
  79.  
  80. |]
  81.  
  82.  
  83. boxMesh : Mesh Vertex
  84. boxMesh =
  85. GL.triangles
  86. [ ( (Vertex (vec2 -1 1) (vec3 1 0 0))
  87. , (Vertex (vec2 1 1) (vec3 0 1 0))
  88. , (Vertex (vec2 -1 -1) (vec3 0 0 1))
  89. )
  90. , ( (Vertex (vec2 -1 -1) (vec3 1 0 0))
  91. , (Vertex (vec2 1 -1) (vec3 0 1 0))
  92. , (Vertex (vec2 1 1) (vec3 0 0 1))
  93. )
  94. ]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement