From 2193a0e7fda62e5ca684508c38284a932f67b5c8 Mon Sep 17 00:00:00 2001 From: Stefan Dresselhaus Date: Mon, 6 Jan 2014 21:13:58 +0100 Subject: [PATCH] added normals to shader --- shaders/vertex.shader | 4 ++-- src/Main.hs | 29 ++++++++++++++++++++++++----- src/Map/Map.hs | 4 ++-- src/Render/Misc.hs | 1 + src/Render/Render.hs | 15 +++++++++++++-- 5 files changed, 42 insertions(+), 11 deletions(-) diff --git a/shaders/vertex.shader b/shaders/vertex.shader index 46b623a..324aa46 100644 --- a/shaders/vertex.shader +++ b/shaders/vertex.shader @@ -15,13 +15,13 @@ smooth out vec4 fg_SmoothColor; void main() { - vec3 fg_Normal = fg_NormalIn; //vec3(0,1,0); + vec3 fg_Normal = fg_NormalMatrix * fg_NormalIn; //vec3(0,1,0); //transform vec3 into vec4, setting w to 1 vec4 fg_Vertex = vec4(fg_VertexIn, 1.0); vec4 light = vec4(1.0,1.0,1.0,1.0); vec4 dark = vec4(0.0,0.0,0.0,1.0); //direction to sun from origin - vec3 lightDir = normalize(vec3(5.0,5.0,1.0)); + vec3 lightDir = normalize(fg_ViewMatrix * vec4(5.0,5.0,1.0,0.0)).xyz; float costheta = dot(normalize(fg_Normal), lightDir); diff --git a/src/Main.hs b/src/Main.hs index f4150f5..55e0915 100644 --- a/src/Main.hs +++ b/src/Main.hs @@ -12,7 +12,7 @@ import Control.Monad.RWS.Strict (RWST, ask, asks, evalRWST, get, liftIO, modify, put) import Control.Monad.Trans.Maybe (MaybeT (..), runMaybeT) -import Data.Distributive (distribute) +import Data.Distributive (distribute, collect) import Data.List (intercalate) import Data.Maybe (catMaybes) import Foreign (Ptr, castPtr, with) @@ -30,6 +30,8 @@ import Render.Misc (checkError, lookAt, up) import Render.Render (initRendering, initShader) +import Control.Lens ((^.),transposeOf) +import Data.Traversable (traverse) -------------------------------------------------------------------------------- @@ -66,6 +68,7 @@ data State = State , shdrProjMatIndex :: !GL.UniformLocation , shdrViewMatIndex :: !GL.UniformLocation , shdrModelMatIndex :: !GL.UniformLocation + , shdrNormalMatIndex :: !GL.UniformLocation --- the map , stateMap :: !GL.BufferObject , mapVert :: !GL.NumArrayIndices @@ -124,7 +127,7 @@ main = do initRendering --generate map vertices (mapBuffer, vert) <- getMapBufferObject - (ci, ni, vi, pri, vii, mi) <- initShader + (ci, ni, vi, pri, vii, mi, nmi) <- initShader let zDistClosest = 10 zDistFarthest = zDistClosest + 20 @@ -159,6 +162,7 @@ main = do , shdrProjMatIndex = pri , shdrViewMatIndex = vii , shdrModelMatIndex = mi + , shdrNormalMatIndex = nmi , stateMap = mapBuffer , mapVert = vert , stateFrustum = frust @@ -425,8 +429,9 @@ draw = do state <- get let xa = stateXAngle state ya = stateYAngle state - (GL.UniformLocation proj) = shdrProjMatIndex state - (GL.UniformLocation vmat) = shdrViewMatIndex state + (GL.UniformLocation proj) = shdrProjMatIndex state + (GL.UniformLocation nmat) = shdrNormalMatIndex state + (GL.UniformLocation vmat) = shdrViewMatIndex state vi = shdrVertexIndex state ni = shdrNormalIndex state ci = shdrColorIndex state @@ -438,16 +443,29 @@ draw = do zDist = stateZDist state liftIO $ do --(vi,GL.UniformLocation proj) <- initShader - GL.clearColor GL.$= GL.Color4 0.5 0.1 1 1 GL.clear [GL.ColorBuffer, GL.DepthBuffer] + checkError "foo" --set up projection (= copy from state) with (distribute $ frust) $ \ptr -> glUniformMatrix4fv proj 1 0 (castPtr (ptr :: Ptr (M44 CFloat))) + checkError "foo" --set up camera let ! cam = getCam (camX,camY) zDist xa ya with (distribute $ cam) $ \ptr -> glUniformMatrix4fv vmat 1 0 (castPtr (ptr :: Ptr (M44 CFloat))) + checkError "foo" + + --set up normal--Mat transpose((model*camera)^-1) + let normal = (case inv33 ((fmap (^._xyz) cam) ^. _xyz) of + (Just a) -> a + Nothing -> eye3) :: M33 CFloat + nmap = (collect (fmap id) normal) :: M33 CFloat --transpose... + + with (distribute $ nmap) $ \ptr -> + glUniformMatrix3fv nmat 1 0 (castPtr (ptr :: Ptr (M33 CFloat))) + + checkError "nmat" GL.bindBuffer GL.ArrayBuffer GL.$= Just map' GL.vertexAttribPointer ci GL.$= fgColorIndex @@ -456,6 +474,7 @@ draw = do GL.vertexAttribArray ni GL.$= GL.Enabled GL.vertexAttribPointer vi GL.$= fgVertexIndex GL.vertexAttribArray vi GL.$= GL.Enabled + checkError "beforeDraw" GL.drawArrays GL.Triangles 0 numVert checkError "draw" diff --git a/src/Map/Map.hs b/src/Map/Map.hs index e48a678..49d73fc 100644 --- a/src/Map/Map.hs +++ b/src/Map/Map.hs @@ -297,8 +297,8 @@ parseTemplate (r:rs) t = (case T.head t of '~' -> (0, Water) 'S' -> (0, Sand) - 'G' -> (fromIntegral (r `mod` 3)/2.0,Grass) - 'M' -> (fromIntegral (r `mod` 3 + 2)/2.0, Mountain) + 'G' -> (fromIntegral (r `mod` 10)/10.0,Grass) + 'M' -> (fromIntegral ((r `mod` 10) + 20)/10.0, Mountain) _ -> error "invalid template format for map" ):parseTemplate rs (T.tail t) parseTemplate [] _ = error "out of randoms.." diff --git a/src/Render/Misc.hs b/src/Render/Misc.hs index 93cd96d..18f2e47 100644 --- a/src/Render/Misc.hs +++ b/src/Render/Misc.hs @@ -13,6 +13,7 @@ import Graphics.Rendering.OpenGL.Raw.Core31 import System.IO (hPutStrLn, stderr) import Linear + up :: V3 CFloat up = V3 0 1 0 diff --git a/src/Render/Render.hs b/src/Render/Render.hs index 98f7d18..c7e9d5b 100644 --- a/src/Render/Render.hs +++ b/src/Render/Render.hs @@ -34,7 +34,15 @@ initBuffer varray = checkError "initBuffer" return bufferObject -initShader :: IO (AttribLocation, AttribLocation, AttribLocation, UniformLocation, UniformLocation, UniformLocation) +initShader :: IO ( + AttribLocation -- ^ color + , AttribLocation -- ^ normal + , AttribLocation -- ^ vertex + , UniformLocation -- ^ ProjectionMat + , UniformLocation -- ^ ViewMat + , UniformLocation -- ^ ModelMat + , UniformLocation -- ^ NormalMat + ) initShader = do ! vertexSource <- B.readFile vertexShaderFile ! fragmentSource <- B.readFile fragmentShaderFile @@ -56,6 +64,9 @@ initShader = do modelMatrixIndex <- get (uniformLocation program "fg_ModelMatrix") checkError "modelMat" + normalMatrixIndex <- get (uniformLocation program "fg_NormalMatrix") + checkError "normalMat" + vertexIndex <- get (attribLocation program "fg_VertexIn") vertexAttribArray vertexIndex $= Enabled checkError "vertexInd" @@ -74,7 +85,7 @@ initShader = do putStrLn $ unlines $ ["Indices: ", show (colorIndex, normalIndex, vertexIndex)] checkError "initShader" - return (colorIndex, normalIndex, vertexIndex, projectionMatrixIndex, viewMatrixIndex, modelMatrixIndex) + return (colorIndex, normalIndex, vertexIndex, projectionMatrixIndex, viewMatrixIndex, modelMatrixIndex, normalMatrixIndex) initRendering :: IO () initRendering = do