Created
July 12, 2015 19:50
-
-
Save jdavidberger/32531d2e041a6bc393c3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Color exposing (..) | |
import Graphics.Collage exposing (..) | |
import Graphics.Element exposing (..) | |
import Mouse | |
import List exposing (..) | |
import Math.Vector3 exposing (..) | |
import Math.Vector4 exposing (..) | |
import Math.Matrix4 exposing (..) | |
import Time exposing (..) | |
import WebGL exposing (..) | |
type alias Vertex = { position : Vec3, color : Vec3, transparency: Float } | |
points : RenderableType Vertex | |
points = | |
Points [ | |
Vertex (vec3 0 0 0) (vec3 1 0 0) (1.0), | |
Vertex (vec3 1 1 0) (vec3 0 0 1) (1.0), | |
Vertex (vec3 1 -1 0) (vec3 1 0 1) (1.0) | |
] | |
triFan = | |
TriangleFan [ | |
Vertex (vec3 0 0 0) (vec3 1 0 1) (1.0), | |
Vertex (vec3 1 1 0) (vec3 1 1 0) (1.0), | |
Vertex (vec3 1.5 0 0) (vec3 1 0 0) (1.0), | |
Vertex (vec3 1 -1 0) (vec3 0 0 1) (1.0), | |
Vertex (vec3 0 -1 0) (vec3 0 0 1) (1.0) | |
] | |
triStrip = | |
TriangleStrip [ | |
Vertex (vec3 0 0 0) (vec3 1 0 1) (1.0), | |
Vertex (vec3 1 1 0) (vec3 1 1 0) (1.0), | |
Vertex (vec3 1 -1 0) (vec3 0 0 1) (1.0), | |
Vertex (vec3 1.5 0 0) (vec3 1 0 0) (1.0), | |
Vertex (vec3 1.5 -1.5 0) (vec3 1 0 0) (1.0)] | |
lineStrip = | |
LineStrip [ | |
Vertex (vec3 0 0 0) (vec3 1 0 0) (0.0), | |
Vertex (vec3 1 1 0) (vec3 0 0 1) (1.0), | |
Vertex (vec3 1 -1 0) (vec3 1 0 1) (0.0) ] | |
lines = | |
Lines [ | |
( Vertex (vec3 0 0 0) (vec3 1 0 0) 0.9, Vertex (vec3 1 1 0) (vec3 0 1 0) 0.9) | |
,( Vertex (vec3 0 0 0) (vec3 0 1 0) 0.9, Vertex (vec3 1 -1 0) (vec3 0 0 1) 0.9) | |
] | |
lineLoop = | |
LineLoop [ | |
Vertex (vec3 0 0 0) (vec3 1 0 0) (0.5), | |
Vertex (vec3 1 1 0) (vec3 0 0 1) (1.0), | |
Vertex (vec3 1 -1 0) (vec3 1 0 1) (0.5) ] | |
tri = | |
Triangle [ ( Vertex (vec3 0 0 0) (vec3 1 0 0) 0.9 | |
, Vertex (vec3 1 1 0) (vec3 0 1 0) 0.9 | |
, Vertex (vec3 1 -1 0) (vec3 0 0 1) 0.9 | |
) | |
] | |
main = | |
Signal.map view (Signal.foldp (+) 0 (fps 30)) | |
entities = [ lineStrip, tri, lineLoop, triFan, triStrip, lines, points ] | |
makeEntity : Float -> Int -> RenderableType Vertex -> Entity | |
makeEntity t n e = entity vertexShader fragmentShader e (slot t n) | |
slot t n = | |
{ perspective = ortho ( (t / 1000) + ( (toFloat n) * 3.14 / 4) ) } | |
view t = | |
webgl (400,400) (indexedMap (makeEntity t) entities) | |
ortho : Float -> Mat4 | |
ortho t = | |
mul (makeOrtho -2 2 2 -2 10 0.01) | |
(makeLookAt (vec3 (4 * cos t) 0 (4 * sin t)) (vec3 0 0 0) (vec3 0 1 0)) | |
-- Shaders | |
vertexShader : Shader { attr | position:Vec3, color:Vec3, transparency: Float } { unif | perspective:Mat4 } { vcolor:Vec4 } | |
vertexShader = [glsl| | |
attribute vec3 position; | |
attribute vec3 color; | |
attribute float transparency; | |
uniform mat4 perspective; | |
varying vec4 vcolor; | |
void main () { | |
gl_PointSize = 5.0; | |
gl_Position = perspective * vec4(position, 1.0); | |
vcolor = vec4(color.rgb, transparency); | |
} | |
|] | |
fragmentShader : Shader {} u { vcolor:Vec4 } | |
fragmentShader = [glsl| | |
precision mediump float; | |
varying vec4 vcolor; | |
void main () { | |
gl_FragColor = vcolor; | |
} | |
|] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment