Skip to content

Commit d0f1612

Browse files
authored
Merge pull request #11 from lamdera/fix-glsl-extensions
Upgrade glsl parsing package and add test
2 parents 750b2b3 + 3d61f8b commit d0f1612

File tree

7 files changed

+192
-2
lines changed

7 files changed

+192
-2
lines changed

.ghci

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
:set -fbyte-code
2+
:set -fobject-code
3+
:def rr const $ return $ unlines [":r","Test.target"]

stack.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ extra-deps:
1515
- 'vendor/elm-format'
1616
# windows pinning for build issues
1717
- ansi-terminal-0.11
18+
- git: https://github.com/noteed/language-glsl.git
19+
commit: 99c3aae76363b127679da640a6f00d61d2203830
1820
flags:
1921
regex-posix:
2022
_regex-posix-clib: true

stack.yaml.lock

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,17 @@ packages:
4646
size: 1461
4747
original:
4848
hackage: ansi-terminal-0.11
49+
- completed:
50+
name: language-glsl
51+
version: 0.4.0.0
52+
git: https://github.com/noteed/language-glsl.git
53+
pantry-tree:
54+
size: 1066
55+
sha256: 155d01f395cc7df3c511df6ff35835b9d54f88a629a58098af7b54a4eeff8e63
56+
commit: 99c3aae76363b127679da640a6f00d61d2203830
57+
original:
58+
git: https://github.com/noteed/language-glsl.git
59+
commit: 99c3aae76363b127679da640a6f00d61d2203830
4960
snapshots:
5061
- completed:
5162
sha256: 9313df78f49519315342f4c51ffc5da12659d3735f8ac3c54a1fb98ff874474e

test/Test.hs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import qualified Test.Check
1919
import qualified Test.Wire
2020
import qualified Test.Ext.ElmPages.Check
2121
import qualified Test.TypeHashes
22+
import qualified Test.WebGL
2223

2324
import qualified Lamdera.Evergreen.TestMigrationHarness
2425
import qualified Lamdera.Evergreen.TestMigrationGenerator
@@ -122,8 +123,8 @@ previewProject = do
122123
Dir.withCurrentDirectory p $ Lamdera.CLI.Deploy.run () ()
123124

124125

125-
target =
126-
Test.Check.checkWithParams "/Users/mario/dev/projects/lamdera-dashboard"
126+
--target =
127+
-- Test.Check.checkWithParams "/Users/mario/dev/projects/lamdera-dashboard"
127128
-- Test.Check.checkWithParams "/Users/mario/lamdera/test/sheep-game" "sheep-game"
128129

129130
-- target = buildTestHarnessToProductionJs
@@ -277,6 +278,7 @@ liveReloadLive = do
277278
-- "src/Bytes/Encode.elm"
278279
-- "withDebug"
279280

281+
target = EasyTest.run Test.WebGL.suite
280282

281283
all =
282284
EasyTest.run allTests
@@ -299,4 +301,5 @@ allTests =
299301
, scope "Test.Check -> " $ Test.Check.suite
300302
, scope "Lamdera.Evergreen.TestMigrationHarness -> " $ Lamdera.Evergreen.TestMigrationHarness.suite
301303
, scope "Lamdera.Evergreen.TestMigrationGenerator -> " $ Lamdera.Evergreen.TestMigrationGenerator.suite
304+
, scope "Test.WebGL -> " $ Test.WebGL.suite
302305
]

test/Test/WebGL.hs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
{-# LANGUAGE OverloadedStrings #-}
2+
3+
module Test.WebGL where
4+
5+
import qualified System.Directory as Dir
6+
import System.FilePath ((</>))
7+
import Data.Text as T
8+
9+
import Lamdera
10+
import EasyTest
11+
12+
import qualified Init
13+
import qualified Lamdera.CLI.Login
14+
import qualified Lamdera.AppConfig
15+
import qualified Lamdera.Update
16+
import qualified Lamdera.Compile
17+
import qualified Lamdera.Evergreen.Snapshot
18+
import qualified Lamdera.Relative
19+
import Test.Helpers
20+
import Test.Check
21+
22+
-- import qualified Lamdera.CLI.Check
23+
-- import qualified Lamdera.CLI.Reset
24+
-- import qualified Lamdera.CLI.Live
25+
-- import qualified Lamdera.ReverseProxy
26+
-- import Test.Wire
27+
import qualified Ext.Common
28+
29+
30+
suite :: Test ()
31+
suite = tests
32+
[ scope "make Elm app containing extension directive in shader" $ do
33+
project <- io $ Lamdera.Relative.findDir "test/scenario-webgl-extensions"
34+
35+
_ <- io $ rmdir (project </> "elm-stuff")
36+
37+
actual <- catchOutput $ Lamdera.Compile.makeDev project [ "src/Triangle.elm" ]
38+
39+
expectTextContains actual "Success! Compiled 1 module."
40+
]
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"type": "application",
3+
"source-directories": [
4+
"src"
5+
],
6+
"elm-version": "0.19.1",
7+
"dependencies": {
8+
"direct": {
9+
"elm/browser": "1.0.0",
10+
"elm/core": "1.0.0",
11+
"elm/html": "1.0.0",
12+
"elm/json": "1.0.0",
13+
"elm-explorations/linear-algebra": "1.0.0",
14+
"elm-explorations/webgl": "1.1.3"
15+
},
16+
"indirect": {
17+
"elm/time": "1.0.0",
18+
"elm/url": "1.0.0",
19+
"elm/virtual-dom": "1.0.0"
20+
}
21+
},
22+
"test-dependencies": {
23+
"direct": {},
24+
"indirect": {}
25+
}
26+
}
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
module Triangle exposing (main)
2+
3+
{-
4+
Rotating triangle, that is a "hello world" of the WebGL
5+
-}
6+
7+
import Browser
8+
import Browser.Events exposing (onAnimationFrameDelta)
9+
import Html exposing (Html)
10+
import Html.Attributes exposing (width, height, style)
11+
import WebGL exposing (Mesh, Shader)
12+
import Math.Matrix4 as Mat4 exposing (Mat4)
13+
import Math.Vector3 as Vec3 exposing (vec3, Vec3)
14+
import Json.Decode exposing (Value)
15+
16+
17+
main : Program Value Float Float
18+
main =
19+
Browser.element
20+
{ init = \_ -> ( 0, Cmd.none )
21+
, view = view
22+
, subscriptions = (\_ -> onAnimationFrameDelta Basics.identity)
23+
, update = (\elapsed currentTime -> ( elapsed + currentTime, Cmd.none ))
24+
}
25+
26+
27+
view : Float -> Html msg
28+
view t =
29+
WebGL.toHtml
30+
[ width 400
31+
, height 400
32+
, style "display" "block"
33+
]
34+
[ WebGL.entity
35+
vertexShader
36+
fragmentShader
37+
mesh
38+
{ perspective = perspective (t / 1000) }
39+
]
40+
41+
42+
perspective : Float -> Mat4
43+
perspective t =
44+
Mat4.mul
45+
(Mat4.makePerspective 45 1 0.01 100)
46+
(Mat4.makeLookAt (vec3 (4 * cos t) 0 (4 * sin t)) (vec3 0 0 0) (vec3 0 1 0))
47+
48+
49+
50+
-- Mesh
51+
52+
53+
type alias Vertex =
54+
{ position : Vec3
55+
, color : Vec3
56+
}
57+
58+
59+
mesh : Mesh Vertex
60+
mesh =
61+
WebGL.triangles
62+
[ ( Vertex (vec3 0 0 0) (vec3 1 0 0)
63+
, Vertex (vec3 1 1 0) (vec3 0 1 0)
64+
, Vertex (vec3 1 -1 0) (vec3 0 0 1)
65+
)
66+
]
67+
68+
69+
70+
-- Shaders
71+
72+
73+
type alias Uniforms =
74+
{ perspective : Mat4 }
75+
76+
77+
vertexShader : Shader Vertex Uniforms { vcolor : Vec3 }
78+
vertexShader =
79+
[glsl|
80+
81+
attribute vec3 position;
82+
attribute vec3 color;
83+
uniform mat4 perspective;
84+
varying vec3 vcolor;
85+
86+
void main () {
87+
gl_Position = perspective * vec4(position, 1.0);
88+
vcolor = color;
89+
}
90+
91+
|]
92+
93+
94+
fragmentShader : Shader {} Uniforms { vcolor : Vec3 }
95+
fragmentShader =
96+
[glsl|
97+
#extension EXT_frag_depth : enable
98+
precision mediump float;
99+
varying vec3 vcolor;
100+
101+
void main () {
102+
gl_FragColor = vec4(vcolor, 1.0);
103+
}
104+
105+
|]

0 commit comments

Comments
 (0)