Skip to content

Instantly share code, notes, and snippets.

@TheSeamau5
Created December 5, 2014 06:03
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TheSeamau5/1e958ff93e5975a591e5 to your computer and use it in GitHub Desktop.
Save TheSeamau5/1e958ff93e5975a591e5 to your computer and use it in GitHub Desktop.
Simple Rotating Cube in Elm
import Graphics.WebGL (..)
import Math.Vector3 (..)
import Math.Matrix4 (..)
type Attribute = {position : Vec3}
type Uniform = { rotationMatrix : Mat4}
type Varying = {}
mapMesh : (a -> b) -> [Triangle a] -> [Triangle b]
mapMesh = map << mapTriangle
square : Vec3 -> Float -> [Triangle Attribute]
square center size =
let hs = size / 2
ftl = center `add` vec3 -hs hs -hs
ftr = center `add` vec3 hs hs -hs
fbr = center `add` vec3 hs -hs -hs
fbl = center `add` vec3 -hs -hs -hs
btl = center `add` vec3 -hs hs hs
btr = center `add` vec3 hs hs hs
bbr = center `add` vec3 hs -hs hs
bbl = center `add` vec3 -hs -hs hs
in mapMesh Attribute [
(btl, ftl, ftr),(ftr, btr, btl) -- TOP
,(ftl, fbl, fbr),(fbr, ftr, ftl) -- FRONT
,(fbl, fbr, bbr),(bbr, bbl, fbl) -- BOTTOM
,(btr, bbr, bbl),(bbl, btl, btr) -- BACK
,(ftl, fbl, bbl),(bbl, btl, ftl) -- LEFT
,(ftr, fbr, bbr),(bbr, btr, ftr) -- RIGHT
]
vertexShader : Shader Attribute Uniform Varying
vertexShader = [glsl|
attribute vec3 position;
uniform mat4 rotationMatrix;
void main (){
gl_Position = rotationMatrix * vec4(position,1.0);
}
|]
fragmentShader : Shader {} Uniform Varying
fragmentShader = [glsl|
precision mediump float;
void main (){
gl_FragColor = vec4(1.0,0.4,0.2,1.0);
}
|]
test : Float -> [Entity]
test angle = [entity vertexShader fragmentShader (square (vec3 0 0 0) 1) (Uniform <| testMatrix angle)]
testMatrix : Float -> Mat4
testMatrix angle = makeRotate angle (vec3 0 1 1)
scene : [Entity] -> Element
scene = webgl (400,400)
testSignal = (*) 0.001 <~ foldp (+) 0 (fps 60)
main : Signal Element
main = scene << test <~ testSignal
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment