6 import S3D.AnimationFrame
11 import S3D.GLProgram.ColourTexture
12 import S3D.GLProgram.SimpleColour
14 import Math.LinearAlgebra
16 import S3D.PhysicalObject
17 import S3D.PhysicalState
18 import S3D.Scenes.House
19 import S3D.Transformable
29 -- Everything the main loop needs to operate.
33 drawables : List Drawable
36 -- The compiler seems to have trouble if I just directly say makeProgram.
38 makeColourTextureProgram : WebGL2RenderingContext -> JSIO S3D.GLProgram.ColourTexture.Program
39 makeColourTextureProgram = makeProgram
41 makeSimpleColourProgram : WebGL2RenderingContext -> JSIO S3D.GLProgram.SimpleColour.Program
42 makeSimpleColourProgram = makeProgram
44 theTexturePixels : List Bits8
47 [ (if mod u 2 == mod v 2 then [255, 255, 255, 255] else [128, 128, 128, 255])
52 makeTheTexture : WebGL2RenderingContext -> Bits32 -> JSIO ()
53 makeTheTexture gl textureUnit =
54 do texture <- unMaybe "createTexture" $ createTexture gl
55 activeTexture gl textureUnit
56 bindTexture gl TEXTURE_2D (Just texture)
57 texParameteri gl TEXTURE_2D TEXTURE_WRAP_S (cast CLAMP_TO_EDGE)
58 texParameteri gl TEXTURE_2D TEXTURE_WRAP_T (cast CLAMP_TO_EDGE)
59 texParameteri gl TEXTURE_2D TEXTURE_MIN_FILTER (cast NEAREST)
60 texParameteri gl TEXTURE_2D TEXTURE_MAG_FILTER (cast NEAREST)
61 pixels <- makeUInt8Array !(fromListIO theTexturePixels)
63 let internalFormat = RGBA
64 let sourceFormat = RGBA
65 let imageDataType = UNSIGNED_BYTE
67 texImage2D gl TEXTURE_2D detailLevel (cast internalFormat)
68 8 8 border sourceFormat imageDataType
69 (Just (S (S (S (Z pixels)))))
71 swapXZ : Matrix 4 4 Double
80 mainLoop : Context -> PhysicalState -> Clock Monotonic -> JSIO ()
81 mainLoop context physicalState time =
82 do let gl = context.ui.gl
83 (physicalState', viewMatrix) <-
84 stepState context.obstacles time context.ui.state physicalState
85 drawUniverse gl viewMatrix context.drawables
86 requestAnimationFrame (runJS . mainLoop context physicalState')
89 mainAfterLoad : JSIO ()
91 do ui <- S3D.WebUI.init
94 colourTextureProgram <- makeColourTextureProgram gl
95 simpleColourProgram <- makeSimpleColourProgram gl
97 drawables <- compileFigure gl colourTextureProgram simpleColourProgram
100 makeTheTexture gl TEXTURE0
101 useProgram gl (Just colourTextureProgram.program)
102 uniform1i gl (Just colourTextureProgram.textureLocation) 0
107 , drawables = drawables
108 , obstacles = scene.obstacles
110 requestAnimationFrame (runJS . mainLoop context initialState)
115 do Web.Raw.Html.GlobalEventHandlers.onload !window ?> mainAfterLoad