added normals to shader
This commit is contained in:
parent
db4d55b811
commit
2193a0e7fd
@ -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);
|
||||
|
29
src/Main.hs
29
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"
|
||||
|
@ -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.."
|
||||
|
@ -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
|
||||
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user