shaders now compile and link correctly

- map still invisible
- frustum defined
- shaders fixed
- attrib-link to shaders fixed
- lookat now generates a frustum-projected look-at matrix
- smaller test-map for debug
This commit is contained in:
Nicole Dresselhaus 2014-01-03 17:46:41 +01:00
parent e5193fc7c5
commit 7d201cf216
6 changed files with 110 additions and 27 deletions

View File

@ -1,4 +1,4 @@
#version 140 #version 330
//color from earlier stages //color from earlier stages
smooth in vec4 fg_SmoothColor; smooth in vec4 fg_SmoothColor;

View File

@ -1,18 +1,20 @@
#version 140 #version 330
//constant projection matrix //constant projection matrix
uniform mat4 fg_ProjectionMatrix; uniform mat4 fg_ProjectionMatrix;
//vertex-data //vertex-data
in vec4 fg_Color; in vec4 fg_Color;
in vec4 fg_Vertex; in vec3 fg_VertexIn;
in vec4 fg_Normal; in vec3 fg_Normal;
//output-data for later stages //output-data for later stages
smooth out vec4 fg_SmoothColor; smooth out vec4 fg_SmoothColor;
void main() void main()
{ {
fg_SmoothColor = fg_Color; //transform vec3 into vec4, setting w to 1
vec4 fg_Vertex = vec4(fg_VertexIn, 1.0);
fg_SmoothColor = fg_Color + 0.001* fg_Normal.xyzx;
gl_Position = fg_ProjectionMatrix * fg_Vertex; gl_Position = fg_ProjectionMatrix * fg_Vertex;
} }

View File

@ -15,7 +15,7 @@ import qualified Graphics.UI.GLFW as GLFW
import Map.Map import Map.Map
import Render.Render (initShader) import Render.Render (initShader)
import Render.Misc (up, lookAtUniformMatrix4fv) import Render.Misc (up, lookAtUniformMatrix4fv, createFrustum)
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
@ -23,8 +23,6 @@ import Render.Misc (up, lookAtUniformMatrix4fv)
data Env = Env data Env = Env
{ envEventsChan :: TQueue Event { envEventsChan :: TQueue Event
, envWindow :: !GLFW.Window , envWindow :: !GLFW.Window
, envMap :: !GL.BufferObject
, mapVert :: !GL.NumArrayIndices
, envZDistClosest :: !Double , envZDistClosest :: !Double
, envZDistFarthest :: !Double , envZDistFarthest :: !Double
} }
@ -43,12 +41,16 @@ data State = State
, stateDragStartY :: !Double , stateDragStartY :: !Double
, stateDragStartXAngle :: !Double , stateDragStartXAngle :: !Double
, stateDragStartYAngle :: !Double , stateDragStartYAngle :: !Double
, stateFrustum :: [GL.GLfloat]
-- pointer to bindings for locations inside the compiled shader -- pointer to bindings for locations inside the compiled shader
-- mutable because shaders may be changed in the future. -- mutable because shaders may be changed in the future.
, shdrColorIndex :: !GL.AttribLocation , shdrColorIndex :: !GL.AttribLocation
, shdrNormalIndex :: !GL.AttribLocation , shdrNormalIndex :: !GL.AttribLocation
, shdrVertexIndex :: !GL.AttribLocation , shdrVertexIndex :: !GL.AttribLocation
, shdrProjMatIndex :: !GL.UniformLocation , shdrProjMatIndex :: !GL.UniformLocation
-- the map
, stateMap :: !GL.BufferObject
, mapVert :: !GL.NumArrayIndices
} }
type Pioneer = RWST Env () State IO type Pioneer = RWST Env () State IO
@ -81,7 +83,7 @@ main = do
eventsChan <- newTQueueIO :: IO (TQueue Event) eventsChan <- newTQueueIO :: IO (TQueue Event)
withWindow width height "GLFW-b-demo" $ \win -> do withWindow width height "Pioneers" $ \win -> do
GLFW.setErrorCallback $ Just $ errorCallback eventsChan GLFW.setErrorCallback $ Just $ errorCallback eventsChan
GLFW.setWindowPosCallback win $ Just $ windowPosCallback eventsChan GLFW.setWindowPosCallback win $ Just $ windowPosCallback eventsChan
GLFW.setWindowSizeCallback win $ Just $ windowSizeCallback eventsChan GLFW.setWindowSizeCallback win $ Just $ windowSizeCallback eventsChan
@ -118,11 +120,14 @@ main = do
let zDistClosest = 10 let zDistClosest = 10
zDistFarthest = zDistClosest + 20 zDistFarthest = zDistClosest + 20
zDist = zDistClosest + ((zDistFarthest - zDistClosest) / 2) zDist = zDistClosest + ((zDistFarthest - zDistClosest) / 2)
fov = 90 --field of view
near = 1 --near plane
far = 100 --far plane
ratio = fromIntegral fbWidth / fromIntegral fbHeight
frust = createFrustum fov near far ratio
env = Env env = Env
{ envEventsChan = eventsChan { envEventsChan = eventsChan
, envWindow = win , envWindow = win
, envMap = mapBuffer
, mapVert = vert
, envZDistClosest = zDistClosest , envZDistClosest = zDistClosest
, envZDistFarthest = zDistFarthest , envZDistFarthest = zDistFarthest
} }
@ -143,6 +148,9 @@ main = do
, shdrNormalIndex = ni , shdrNormalIndex = ni
, shdrVertexIndex = vi , shdrVertexIndex = vi
, shdrProjMatIndex = pi , shdrProjMatIndex = pi
, stateMap = mapBuffer
, mapVert = vert
, stateFrustum = frust
} }
runDemo env state runDemo env state
@ -316,9 +324,9 @@ processEvent ev =
} }
(EventCursorPos _ x y) -> do (EventCursorPos _ x y) -> do
let x' = round x :: Int {-let x' = round x :: Int
y' = round y :: Int y' = round y :: Int
printEvent "cursor pos" [show x', show y'] printEvent "cursor pos" [show x', show y']-}
state <- get state <- get
when (stateMouseDown state && not (stateDragging state)) $ when (stateMouseDown state && not (stateDragging state)) $
put $ state put $ state
@ -395,10 +403,13 @@ draw = do
ci = shdrColorIndex state ci = shdrColorIndex state
ni = shdrNormalIndex state ni = shdrNormalIndex state
vi = shdrVertexIndex state vi = shdrVertexIndex state
numVert = mapVert env numVert = mapVert state
map' = envMap env map' = stateMap state
frust = stateFrustum state
liftIO $ do liftIO $ do
lookAtUniformMatrix4fv (0.0,0.0,0.0) (xa, ya, za) up proj 1 GL.clearColor GL.$= GL.Color4 0.5 0.2 1 1
GL.clear [GL.ColorBuffer, GL.DepthBuffer]
lookAtUniformMatrix4fv (0.0,0.0,0.0) (15, 15, 30) up frust proj 1
GL.bindBuffer GL.ArrayBuffer GL.$= Just map' GL.bindBuffer GL.ArrayBuffer GL.$= Just map'
GL.vertexAttribPointer ci GL.$= fgColorIndex GL.vertexAttribPointer ci GL.$= fgColorIndex
GL.vertexAttribPointer ni GL.$= fgNormalIndex GL.vertexAttribPointer ni GL.$= fgNormalIndex

View File

@ -44,6 +44,9 @@ numComponents = 4 --color
+3 --normal +3 --normal
+3 --vertex +3 --vertex
mapStride :: Stride
mapStride = fromIntegral (sizeOf (0.0 :: GLfloat)) * fromIntegral numComponents
bufferObjectPtr :: Integral a => a -> Ptr b bufferObjectPtr :: Integral a => a -> Ptr b
bufferObjectPtr = plusPtr (nullPtr :: Ptr GLchar) . fromIntegral bufferObjectPtr = plusPtr (nullPtr :: Ptr GLchar) . fromIntegral
@ -60,13 +63,11 @@ fgNormalIndex = (ToFloat, mapVertexArrayDescriptor 3 4) --normal after color
fgVertexIndex :: (IntegerHandling, VertexArrayDescriptor a) fgVertexIndex :: (IntegerHandling, VertexArrayDescriptor a)
fgVertexIndex = (ToFloat, mapVertexArrayDescriptor 3 7) --vertex after normal fgVertexIndex = (ToFloat, mapVertexArrayDescriptor 3 7) --vertex after normal
mapStride :: Stride
mapStride = fromIntegral (sizeOf (0.0 :: GLfloat)) * fromIntegral numComponents
getMapBufferObject :: IO (BufferObject, NumArrayIndices) getMapBufferObject :: IO (BufferObject, NumArrayIndices)
getMapBufferObject = do getMapBufferObject = do
map' <- testmap map' <- testmap
map' <- return $ generateTriangles map' map' <- return $ generateTriangles map'
putStrLn $ P.unlines $ P.map show (prettyMap map')
len <- return $ fromIntegral $ P.length map' `div` numComponents len <- return $ fromIntegral $ P.length map' `div` numComponents
bo <- genObjectName -- create a new buffer bo <- genObjectName -- create a new buffer
bindBuffer ArrayBuffer $= Just bo -- bind buffer bindBuffer ArrayBuffer $= Just bo -- bind buffer
@ -75,6 +76,9 @@ getMapBufferObject = do
checkError "initBuffer" checkError "initBuffer"
return (bo,len) return (bo,len)
prettyMap :: [GLfloat] -> [(GLfloat,GLfloat,GLfloat,GLfloat,GLfloat,GLfloat,GLfloat,GLfloat,GLfloat,GLfloat)]
prettyMap (a:b:c:d:x:y:z:u:v:w:ms) = (a,b,c,d,x,y,z,u,v,w):(prettyMap ms)
prettyMap _ = []
generateTriangles :: PlayMap -> [GLfloat] generateTriangles :: PlayMap -> [GLfloat]
generateTriangles map' = generateTriangles map' =
@ -176,11 +180,17 @@ testMapTemplate = T.transpose [
"~~~~~~~~~~~~~~~~~~~~" "~~~~~~~~~~~~~~~~~~~~"
] ]
testMapTemplate2 :: [Text]
testMapTemplate2 = T.transpose [
"~~~~~~",
"~~~~~~"
]
testmap :: IO PlayMap testmap :: IO PlayMap
testmap = do testmap = do
g <- getStdGen g <- getStdGen
rawMap <- return $ parseTemplate (randoms g) (T.concat testMapTemplate) rawMap <- return $ parseTemplate (randoms g) (T.concat testMapTemplate2)
return $ listArray ((0,0),(19,19)) rawMap return $ listArray ((0,0),(5,1)) rawMap
parseTemplate :: [Int] -> Text -> [MapEntry] parseTemplate :: [Int] -> Text -> [MapEntry]

View File

@ -57,16 +57,69 @@ createProgramUsing shaders = do
linkAndCheck program linkAndCheck program
return program return program
createFrustum :: Float -> Float -> Float -> Float -> [GLfloat]
createFrustum fov n f rat =
let s = recip (tan $ fov*0.5 * pi / 180) in
map (fromRational . toRational) [
rat*s,0,0,0,
0,rat*s,0,0,
0,0,-(f/(f-n)), -1,
0,0,-((f*n)/(f-n)), 1
]
lookAtUniformMatrix4fv :: (Double, Double, Double) --origin lookAtUniformMatrix4fv :: (Double, Double, Double) --origin
-> (Double, Double, Double) --camera-pos -> (Double, Double, Double) --camera-pos
-> (Double, Double, Double) --up -> (Double, Double, Double) --up
-> [GLfloat] --frustum
-> GLint -> GLsizei -> IO () --rest of GL-call -> GLint -> GLsizei -> IO () --rest of GL-call
lookAtUniformMatrix4fv o c u num size = allocaArray 16 $ \projMat -> lookAtUniformMatrix4fv o c u frust num size = allocaArray 16 $ \projMat ->
do do
pokeArray projMat $ lookAt o c u pokeArray projMat $ (lookAt o c u) >< frust
glUniformMatrix4fv num size 1 projMat glUniformMatrix4fv num size 1 projMat
-- generats 4x4-Projection-Matrix infixl 5 ><
(><) :: [GLfloat] -> [GLfloat] -> [GLfloat]
[ aa, ab, ac, ad,
ba, bb, bc, bd,
ca, cb, cc, cd,
da, db, dc, dd
] ><
[
xx, xy, xz, xw,
yx, yy, yz, yw,
zx, zy, zz, zw,
wx, wy, wz, ww
] = [
--first row
aa*xx + ab*yx + ac*zx + ad * wx,
aa*xy + ab*yy + ac*zy + ad * wy,
aa*xz + ab*yz + ac*zz + ad * wz,
aa*xw + ab*yw + ac*zw + ad * ww,
--second row
ba*xx + bb*yx + bc*zx + bd * wx,
ba*xy + bb*yy + bc*zy + bd * wy,
ba*xz + bb*yz + bc*zz + bd * wz,
ba*xw + bb*yw + bc*zw + bd * ww,
--third row
ca*xx + cb*yx + cc*zx + cd * wx,
ca*xy + cb*yy + cc*zy + cd * wy,
ca*xz + cb*yz + cc*zz + cd * wz,
ca*xw + cb*yw + cc*zw + cd * ww,
--fourth row
da*xx + db*yx + dc*zx + dd * wx,
da*xy + db*yy + dc*zy + dd * wy,
da*xz + db*yz + dc*zz + dd * wz,
da*xw + db*yw + dc*zw + dd * ww
]
_ >< _ = error "non-conformat matrix-multiplication"
-- generates 4x4-Projection-Matrix
lookAt :: (Double, Double, Double) -> (Double, Double, Double) -> (Double, Double, Double) -> [GLfloat] lookAt :: (Double, Double, Double) -> (Double, Double, Double) -> (Double, Double, Double) -> [GLfloat]
lookAt origin eye up = lookAt origin eye up =
map (fromRational . toRational) [ map (fromRational . toRational) [

View File

@ -38,21 +38,28 @@ initShader = do
! vertexSource <- B.readFile vertexShaderFile ! vertexSource <- B.readFile vertexShaderFile
! fragmentSource <- B.readFile fragmentShaderFile ! fragmentSource <- B.readFile fragmentShaderFile
vertexShader <- compileShaderSource VertexShader vertexSource vertexShader <- compileShaderSource VertexShader vertexSource
checkError "compile Vertex"
fragmentShader <- compileShaderSource FragmentShader fragmentSource fragmentShader <- compileShaderSource FragmentShader fragmentSource
checkError "compile Frag"
program <- createProgramUsing [vertexShader, fragmentShader] program <- createProgramUsing [vertexShader, fragmentShader]
checkError "compile Program"
currentProgram $= Just program currentProgram $= Just program
projectionMatrixIndex <- get (uniformLocation program "fg_ProjectionMatrix") projectionMatrixIndex <- get (uniformLocation program "fg_ProjectionMatrix")
checkError "projMat"
colorIndex <- get (attribLocation program "fg_Color") colorIndex <- get (attribLocation program "fg_Color")
vertexAttribArray colorIndex $= Enabled vertexAttribArray colorIndex $= Enabled
checkError "colorInd"
vertexIndex <- get (attribLocation program "fg_Vertex")
vertexAttribArray vertexIndex $= Enabled
normalIndex <- get (attribLocation program "fg_Normal") normalIndex <- get (attribLocation program "fg_Normal")
vertexAttribArray normalIndex $= Enabled vertexAttribArray normalIndex $= Enabled
checkError "normalInd"
vertexIndex <- get (attribLocation program "fg_VertexIn")
vertexAttribArray vertexIndex $= Enabled
checkError "vertexInd"
checkError "initShader" checkError "initShader"
return (colorIndex, normalIndex, vertexIndex, projectionMatrixIndex) return (colorIndex, normalIndex, vertexIndex, projectionMatrixIndex)