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
35 floorDrawables : List Drawable
38 -- The compiler seems to have trouble if I just directly say makeProgram.
40 makeColourTextureProgram : WebGL2RenderingContext -> JSIO S3D.GLProgram.ColourTexture.Program
41 makeColourTextureProgram = makeProgram
43 makeSimpleColourProgram : WebGL2RenderingContext -> JSIO S3D.GLProgram.SimpleColour.Program
44 makeSimpleColourProgram = makeProgram
46 theTexturePixels : List Bits8
49 [ (if mod u 2 == mod v 2 then [255, 255, 255, 255] else [128, 128, 128, 255])
54 makeTheTexture : WebGL2RenderingContext -> Bits32 -> JSIO ()
55 makeTheTexture gl textureUnit =
56 do texture <- unMaybe "createTexture" $ createTexture gl
57 activeTexture gl textureUnit
58 bindTexture gl TEXTURE_2D (Just texture)
59 texParameteri gl TEXTURE_2D TEXTURE_WRAP_S (cast CLAMP_TO_EDGE)
60 texParameteri gl TEXTURE_2D TEXTURE_WRAP_T (cast CLAMP_TO_EDGE)
61 texParameteri gl TEXTURE_2D TEXTURE_MIN_FILTER (cast NEAREST)
62 texParameteri gl TEXTURE_2D TEXTURE_MAG_FILTER (cast NEAREST)
63 pixels <- makeUInt8Array !(fromListIO theTexturePixels)
65 let internalFormat = RGBA
66 let sourceFormat = RGBA
67 let imageDataType = UNSIGNED_BYTE
69 texImage2D gl TEXTURE_2D detailLevel (cast internalFormat)
70 8 8 border sourceFormat imageDataType
71 (Just (S (S (S (Z pixels)))))
73 swapXZ : Matrix 4 4 Double
82 mainLoop : Context -> PhysicalState -> Clock Monotonic -> JSIO ()
83 mainLoop context physicalState time =
84 do let gl = context.ui.gl
85 (physicalState', viewMatrix) <-
86 stepState context.obstacles time context.ui.state physicalState
87 let drawables = if !(floorEnabled context.ui)
88 then context.drawables ++ context.floorDrawables
89 else context.drawables
90 drawUniverse gl viewMatrix drawables
91 requestAnimationFrame (runJS . mainLoop context physicalState')
94 mainAfterLoad : JSIO ()
96 do ui <- S3D.WebUI.init
99 colourTextureProgram <- makeColourTextureProgram gl
100 simpleColourProgram <- makeSimpleColourProgram gl
102 drawables <- compileFigure gl colourTextureProgram simpleColourProgram
105 compileFigure gl colourTextureProgram simpleColourProgram
108 makeTheTexture gl TEXTURE0
109 useProgram gl (Just colourTextureProgram.program)
110 uniform1i gl (Just colourTextureProgram.textureLocation) 0
115 , drawables = drawables
116 , floorDrawables = floorDrawables
117 , obstacles = scene.obstacles
119 requestAnimationFrame (runJS . mainLoop context initialState)
124 do Web.Raw.Html.GlobalEventHandlers.onload !window ?> mainAfterLoad