added normals to shader

This commit is contained in:
Nicole Dresselhaus 2014-01-06 21:13:58 +01:00
parent db4d55b811
commit 2193a0e7fd
5 changed files with 42 additions and 11 deletions

View File

@ -15,13 +15,13 @@ smooth out vec4 fg_SmoothColor;
void main() 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 //transform vec3 into vec4, setting w to 1
vec4 fg_Vertex = vec4(fg_VertexIn, 1.0); vec4 fg_Vertex = vec4(fg_VertexIn, 1.0);
vec4 light = vec4(1.0,1.0,1.0,1.0); vec4 light = vec4(1.0,1.0,1.0,1.0);
vec4 dark = vec4(0.0,0.0,0.0,1.0); vec4 dark = vec4(0.0,0.0,0.0,1.0);
//direction to sun from origin //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); float costheta = dot(normalize(fg_Normal), lightDir);

View File

@ -12,7 +12,7 @@ import Control.Monad.RWS.Strict (RWST, ask, asks,
evalRWST, get, liftIO, evalRWST, get, liftIO,
modify, put) modify, put)
import Control.Monad.Trans.Maybe (MaybeT (..), runMaybeT) import Control.Monad.Trans.Maybe (MaybeT (..), runMaybeT)
import Data.Distributive (distribute) import Data.Distributive (distribute, collect)
import Data.List (intercalate) import Data.List (intercalate)
import Data.Maybe (catMaybes) import Data.Maybe (catMaybes)
import Foreign (Ptr, castPtr, with) import Foreign (Ptr, castPtr, with)
@ -30,6 +30,8 @@ import Render.Misc (checkError,
lookAt, up) lookAt, up)
import Render.Render (initRendering, import Render.Render (initRendering,
initShader) initShader)
import Control.Lens ((^.),transposeOf)
import Data.Traversable (traverse)
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
@ -66,6 +68,7 @@ data State = State
, shdrProjMatIndex :: !GL.UniformLocation , shdrProjMatIndex :: !GL.UniformLocation
, shdrViewMatIndex :: !GL.UniformLocation , shdrViewMatIndex :: !GL.UniformLocation
, shdrModelMatIndex :: !GL.UniformLocation , shdrModelMatIndex :: !GL.UniformLocation
, shdrNormalMatIndex :: !GL.UniformLocation
--- the map --- the map
, stateMap :: !GL.BufferObject , stateMap :: !GL.BufferObject
, mapVert :: !GL.NumArrayIndices , mapVert :: !GL.NumArrayIndices
@ -124,7 +127,7 @@ main = do
initRendering initRendering
--generate map vertices --generate map vertices
(mapBuffer, vert) <- getMapBufferObject (mapBuffer, vert) <- getMapBufferObject
(ci, ni, vi, pri, vii, mi) <- initShader (ci, ni, vi, pri, vii, mi, nmi) <- initShader
let zDistClosest = 10 let zDistClosest = 10
zDistFarthest = zDistClosest + 20 zDistFarthest = zDistClosest + 20
@ -159,6 +162,7 @@ main = do
, shdrProjMatIndex = pri , shdrProjMatIndex = pri
, shdrViewMatIndex = vii , shdrViewMatIndex = vii
, shdrModelMatIndex = mi , shdrModelMatIndex = mi
, shdrNormalMatIndex = nmi
, stateMap = mapBuffer , stateMap = mapBuffer
, mapVert = vert , mapVert = vert
, stateFrustum = frust , stateFrustum = frust
@ -426,6 +430,7 @@ draw = do
let xa = stateXAngle state let xa = stateXAngle state
ya = stateYAngle state ya = stateYAngle state
(GL.UniformLocation proj) = shdrProjMatIndex state (GL.UniformLocation proj) = shdrProjMatIndex state
(GL.UniformLocation nmat) = shdrNormalMatIndex state
(GL.UniformLocation vmat) = shdrViewMatIndex state (GL.UniformLocation vmat) = shdrViewMatIndex state
vi = shdrVertexIndex state vi = shdrVertexIndex state
ni = shdrNormalIndex state ni = shdrNormalIndex state
@ -438,16 +443,29 @@ draw = do
zDist = stateZDist state zDist = stateZDist state
liftIO $ do liftIO $ do
--(vi,GL.UniformLocation proj) <- initShader --(vi,GL.UniformLocation proj) <- initShader
GL.clearColor GL.$= GL.Color4 0.5 0.1 1 1
GL.clear [GL.ColorBuffer, GL.DepthBuffer] GL.clear [GL.ColorBuffer, GL.DepthBuffer]
checkError "foo"
--set up projection (= copy from state) --set up projection (= copy from state)
with (distribute $ frust) $ \ptr -> with (distribute $ frust) $ \ptr ->
glUniformMatrix4fv proj 1 0 (castPtr (ptr :: Ptr (M44 CFloat))) glUniformMatrix4fv proj 1 0 (castPtr (ptr :: Ptr (M44 CFloat)))
checkError "foo"
--set up camera --set up camera
let ! cam = getCam (camX,camY) zDist xa ya let ! cam = getCam (camX,camY) zDist xa ya
with (distribute $ cam) $ \ptr -> with (distribute $ cam) $ \ptr ->
glUniformMatrix4fv vmat 1 0 (castPtr (ptr :: Ptr (M44 CFloat))) 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.bindBuffer GL.ArrayBuffer GL.$= Just map'
GL.vertexAttribPointer ci GL.$= fgColorIndex GL.vertexAttribPointer ci GL.$= fgColorIndex
@ -456,6 +474,7 @@ draw = do
GL.vertexAttribArray ni GL.$= GL.Enabled GL.vertexAttribArray ni GL.$= GL.Enabled
GL.vertexAttribPointer vi GL.$= fgVertexIndex GL.vertexAttribPointer vi GL.$= fgVertexIndex
GL.vertexAttribArray vi GL.$= GL.Enabled GL.vertexAttribArray vi GL.$= GL.Enabled
checkError "beforeDraw"
GL.drawArrays GL.Triangles 0 numVert GL.drawArrays GL.Triangles 0 numVert
checkError "draw" checkError "draw"

View File

@ -297,8 +297,8 @@ parseTemplate (r:rs) t =
(case T.head t of (case T.head t of
'~' -> (0, Water) '~' -> (0, Water)
'S' -> (0, Sand) 'S' -> (0, Sand)
'G' -> (fromIntegral (r `mod` 3)/2.0,Grass) 'G' -> (fromIntegral (r `mod` 10)/10.0,Grass)
'M' -> (fromIntegral (r `mod` 3 + 2)/2.0, Mountain) 'M' -> (fromIntegral ((r `mod` 10) + 20)/10.0, Mountain)
_ -> error "invalid template format for map" _ -> error "invalid template format for map"
):parseTemplate rs (T.tail t) ):parseTemplate rs (T.tail t)
parseTemplate [] _ = error "out of randoms.." parseTemplate [] _ = error "out of randoms.."

View File

@ -13,6 +13,7 @@ import Graphics.Rendering.OpenGL.Raw.Core31
import System.IO (hPutStrLn, stderr) import System.IO (hPutStrLn, stderr)
import Linear import Linear
up :: V3 CFloat up :: V3 CFloat
up = V3 0 1 0 up = V3 0 1 0

View File

@ -34,7 +34,15 @@ initBuffer varray =
checkError "initBuffer" checkError "initBuffer"
return bufferObject 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 initShader = do
! vertexSource <- B.readFile vertexShaderFile ! vertexSource <- B.readFile vertexShaderFile
! fragmentSource <- B.readFile fragmentShaderFile ! fragmentSource <- B.readFile fragmentShaderFile
@ -56,6 +64,9 @@ initShader = do
modelMatrixIndex <- get (uniformLocation program "fg_ModelMatrix") modelMatrixIndex <- get (uniformLocation program "fg_ModelMatrix")
checkError "modelMat" checkError "modelMat"
normalMatrixIndex <- get (uniformLocation program "fg_NormalMatrix")
checkError "normalMat"
vertexIndex <- get (attribLocation program "fg_VertexIn") vertexIndex <- get (attribLocation program "fg_VertexIn")
vertexAttribArray vertexIndex $= Enabled vertexAttribArray vertexIndex $= Enabled
checkError "vertexInd" checkError "vertexInd"
@ -74,7 +85,7 @@ initShader = do
putStrLn $ unlines $ ["Indices: ", show (colorIndex, normalIndex, vertexIndex)] putStrLn $ unlines $ ["Indices: ", show (colorIndex, normalIndex, vertexIndex)]
checkError "initShader" checkError "initShader"
return (colorIndex, normalIndex, vertexIndex, projectionMatrixIndex, viewMatrixIndex, modelMatrixIndex) return (colorIndex, normalIndex, vertexIndex, projectionMatrixIndex, viewMatrixIndex, modelMatrixIndex, normalMatrixIndex)
initRendering :: IO () initRendering :: IO ()
initRendering = do initRendering = do