summaryrefslogtreecommitdiff
path: root/examples/HelloOBJ.hs
diff options
context:
space:
mode:
Diffstat (limited to 'examples/HelloOBJ.hs')
-rw-r--r--examples/HelloOBJ.hs18
1 files changed, 11 insertions, 7 deletions
diff --git a/examples/HelloOBJ.hs b/examples/HelloOBJ.hs
index 10bf248..8e409a5 100644
--- a/examples/HelloOBJ.hs
+++ b/examples/HelloOBJ.hs
@@ -63,7 +63,7 @@ loadOBJToGPU fname = loadOBJ fname >>= \case
63 gpuSubModels <- forM subModels $ \(mesh,mat) -> LambdaCubeGL.uploadMeshToGPU mesh >>= \a -> return (a,mat) 63 gpuSubModels <- forM subModels $ \(mesh,mat) -> LambdaCubeGL.uploadMeshToGPU mesh >>= \a -> return (a,mat)
64 return $ Right (gpuSubModels,mtlLib) 64 return $ Right (gpuSubModels,mtlLib)
65 65
66uploadMtlLib :: MtlLib -> IO (Map Text TextureData) 66uploadMtlLib :: MtlLib -> IO (Map Text (ObjMaterial,TextureData))
67uploadMtlLib mtlLib = do 67uploadMtlLib mtlLib = do
68 -- collect used textures 68 -- collect used textures
69 let usedTextures = nub . concatMap (maybeToList . mtl_map_Kd) $ Map.elems mtlLib 69 let usedTextures = nub . concatMap (maybeToList . mtl_map_Kd) $ Map.elems mtlLib
@@ -76,15 +76,16 @@ uploadMtlLib mtlLib = do
76 Right img -> LambdaCubeGL.uploadTexture2DToGPU img 76 Right img -> LambdaCubeGL.uploadTexture2DToGPU img
77 whiteTex <- LambdaCubeGL.uploadTexture2DToGPU whiteImage 77 whiteTex <- LambdaCubeGL.uploadTexture2DToGPU whiteImage
78 -- pair textures and materials 78 -- pair textures and materials
79 return $ maybe whiteTex (fromMaybe checkerTex . flip Map.lookup textureLib) . mtl_map_Kd <$> mtlLib 79 return $ (\a -> (a, maybe whiteTex (fromMaybe checkerTex . flip Map.lookup textureLib) . mtl_map_Kd $ a)) <$> mtlLib
80 80
81addOBJToObjectArray :: GLStorage -> String -> [(GPUMesh, Maybe Text)] -> Map Text TextureData -> IO [LambdaCubeGL.Object] 81addOBJToObjectArray :: GLStorage -> String -> [(GPUMesh, Maybe Text)] -> Map Text (ObjMaterial,TextureData) -> IO [LambdaCubeGL.Object]
82addOBJToObjectArray storage slotName objMesh mtlLib = forM objMesh $ \(mesh,mat) -> do 82addOBJToObjectArray storage slotName objMesh mtlLib = forM objMesh $ \(mesh,mat) -> do
83 obj <- LambdaCubeGL.addMeshToObjectArray storage slotName ["diffuseTexture"] mesh -- diffuseTexture value can change on each model 83 obj <- LambdaCubeGL.addMeshToObjectArray storage slotName ["diffuseTexture","diffuseColor"] mesh -- diffuseTexture and diffuseColor values can change on each model
84 case mat >>= flip Map.lookup mtlLib of 84 case mat >>= flip Map.lookup mtlLib of
85 Nothing -> return () 85 Nothing -> return ()
86 Just t -> LambdaCubeGL.updateObjectUniforms obj $ do 86 Just (ObjMaterial{..},t) -> LambdaCubeGL.updateObjectUniforms obj $ do
87 "diffuseTexture" @= return t -- set model's diffuse texture 87 "diffuseTexture" @= return t -- set model's diffuse texture
88 "diffuseColor" @= let (r,g,b) = mtl_Kd in return (V4 r g b mtl_Tr)
88 return obj 89 return obj
89 90
90main :: IO () 91main :: IO ()
@@ -102,10 +103,13 @@ main = do
102 defUniforms $ do 103 defUniforms $ do
103 "time" @: Float 104 "time" @: Float
104 "diffuseTexture" @: FTexture2D 105 "diffuseTexture" @: FTexture2D
106 "diffuseColor" @: V4F
105 107
106 storage <- LambdaCubeGL.allocStorage inputSchema 108 storage <- LambdaCubeGL.allocStorage inputSchema
107 109
108 objName <- head . (++ ["cube.obj"]) <$> getArgs 110 objName <- getArgs >>= \case
111 [] -> fail "missing .obj argument"
112 a -> return $ head a
109 -- load OBJ geometry and material descriptions 113 -- load OBJ geometry and material descriptions
110 Right (objMesh,mtlLib) <- loadOBJToGPU objName 114 Right (objMesh,mtlLib) <- loadOBJToGPU objName
111 -- load materials textures 115 -- load materials textures