TRIANGLEEEEESS!!!!11!!

finally.
This commit is contained in:
Nicole Dresselhaus 2014-01-04 14:09:42 +01:00
parent 47de89ca39
commit fd4d2a0d14
5 changed files with 59 additions and 27 deletions

View File

@ -9,5 +9,5 @@ out vec4 fg_FragColor;
void main(void) void main(void)
{ {
//copy-shader //copy-shader
fg_FragColor = vec4(0.5,0.5,0.5,1.0);//fg_SmoothColor; fg_FragColor = fg_SmoothColor;
} }

View File

@ -2,19 +2,21 @@
//constant projection matrix //constant projection matrix
uniform mat4 fg_ProjectionMatrix; uniform mat4 fg_ProjectionMatrix;
uniform mat4 fg_ModelMatrix;
//vertex-data //vertex-data
//in vec4 fg_Color; in vec4 fg_Color;
in vec3 fg_VertexIn; in vec3 fg_VertexIn;
//in vec3 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()
{ {
//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);
//fg_SmoothColor = fg_Color + 0.001* fg_Normal.xyzx; fg_SmoothColor = fg_Color;
gl_Position = fg_ProjectionMatrix * fg_Vertex; // + 0.001* fg_Normal.xyzx;
gl_Position = fg_ProjectionMatrix * fg_ModelMatrix * fg_Vertex;
} }

View File

@ -47,7 +47,9 @@ data State = State
-- 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.
, shdrVertexIndex :: !GL.AttribLocation , shdrVertexIndex :: !GL.AttribLocation
, shdrColorIndex :: !GL.AttribLocation
, shdrProjMatIndex :: !GL.UniformLocation , shdrProjMatIndex :: !GL.UniformLocation
, shdrModelMatIndex :: !GL.UniformLocation
-- the map -- the map
, stateMap :: !GL.BufferObject , stateMap :: !GL.BufferObject
, mapVert :: !GL.NumArrayIndices , mapVert :: !GL.NumArrayIndices
@ -105,7 +107,7 @@ main = do
--generate map vertices --generate map vertices
(mapBuffer, vert) <- getMapBufferObject (mapBuffer, vert) <- getMapBufferObject
(vi, pi) <- initShader (ci, vi, pi, mi) <- initShader
let zDistClosest = 10 let zDistClosest = 10
zDistFarthest = zDistClosest + 20 zDistFarthest = zDistClosest + 20
@ -135,7 +137,9 @@ main = do
, stateDragStartXAngle = 0 , stateDragStartXAngle = 0
, stateDragStartYAngle = 0 , stateDragStartYAngle = 0
, shdrVertexIndex = vi , shdrVertexIndex = vi
, shdrColorIndex = ci
, shdrProjMatIndex = pi , shdrProjMatIndex = pi
, shdrModelMatIndex = mi
, stateMap = mapBuffer , stateMap = mapBuffer
, mapVert = vert , mapVert = vert
, stateFrustum = frust , stateFrustum = frust
@ -222,6 +226,7 @@ run = do
processEvents processEvents
-- update State -- update State
{-
state <- get state <- get
if stateDragging state if stateDragging state
then do then do
@ -243,6 +248,7 @@ run = do
{ stateXAngle = stateXAngle state + (2 * kxrot) + (2 * jxrot) { stateXAngle = stateXAngle state + (2 * kxrot) + (2 * jxrot)
, stateYAngle = stateYAngle state + (2 * kyrot) + (2 * jyrot) , stateYAngle = stateYAngle state + (2 * kyrot) + (2 * jyrot)
} }
-}
{- {-
--modify the state with all that happened in mt time. --modify the state with all that happened in mt time.
mt <- liftIO GLFW.getTime mt <- liftIO GLFW.getTime
@ -373,12 +379,16 @@ draw = do
let xa = stateXAngle state let xa = stateXAngle state
ya = stateYAngle state ya = stateYAngle state
za = stateZAngle state za = stateZAngle state
(GL.UniformLocation proj) = shdrProjMatIndex state (GL.UniformLocation proj) = shdrProjMatIndex state
(GL.UniformLocation mmat) = shdrModelMatIndex state
vi = shdrVertexIndex state vi = shdrVertexIndex state
ci = shdrColorIndex state
numVert = mapVert state numVert = mapVert state
map' = stateMap state map' = stateMap state
frust = stateFrustum state frust = stateFrustum state
liftIO $ do liftIO $ do
--(vi,GL.UniformLocation proj) <- initShader
GL.clearColor GL.$= GL.Color4 0.5 0.1 1 1
GL.clear [GL.ColorBuffer] GL.clear [GL.ColorBuffer]
let fov = 90 let fov = 90
s = recip (tan $ fov * 0.5 * pi / 180) s = recip (tan $ fov * 0.5 * pi / 180)
@ -391,7 +401,15 @@ draw = do
, 0, 0, -((f*n)/(f-n)), 0 , 0, 0, -((f*n)/(f-n)), 0
] ]
V.unsafeWith perspective $ \ptr -> GL.glUniformMatrix4fv proj 1 0 ptr V.unsafeWith perspective $ \ptr -> GL.glUniformMatrix4fv proj 1 0 ptr
let model = V.fromList [
1, 0, 0, 0
, 0, 0, 1, 0
, 0, 1, 0, 0
,-1, -1, -5, 1
]
V.unsafeWith model $ \ptr -> GL.glUniformMatrix4fv mmat 1 0 ptr
GL.bindBuffer GL.ArrayBuffer GL.$= Just map' GL.bindBuffer GL.ArrayBuffer GL.$= Just map'
GL.vertexAttribPointer ci GL.$= fgColorIndex
GL.vertexAttribPointer vi GL.$= fgVertexIndex GL.vertexAttribPointer vi GL.$= fgVertexIndex
GL.drawArrays GL.Triangles 0 numVert GL.drawArrays GL.Triangles 0 numVert

View File

@ -47,31 +47,31 @@ lineHeight :: GLfloat
lineHeight = 0.8660254 lineHeight = 0.8660254
numComponents :: Int numComponents :: Int
numComponents = 3 numComponents = 7
mapStride :: Stride mapStride :: Stride
mapStride = fromIntegral (sizeOf (0.0 :: GLfloat)) * fromIntegral numComponents mapStride = fromIntegral (sizeOf (0.0 :: GLfloat) * numComponents)
bufferObjectPtr :: Integral a => a -> Ptr b bufferObjectPtr :: Integral a => a -> Ptr GLfloat
bufferObjectPtr = plusPtr (nullPtr :: Ptr GLchar) . fromIntegral bufferObjectPtr = plusPtr (nullPtr :: Ptr GLfloat) . fromIntegral
mapVertexArrayDescriptor :: NumComponents -> NumComponents -> VertexArrayDescriptor a mapVertexArrayDescriptor :: NumComponents -> NumComponents -> VertexArrayDescriptor GLfloat
mapVertexArrayDescriptor count' offset = mapVertexArrayDescriptor count' offset =
VertexArrayDescriptor count' Float mapStride (bufferObjectPtr (fromIntegral numComponents * offset)) VertexArrayDescriptor count' Float mapStride (bufferObjectPtr offset ) --(fromIntegral numComponents * offset))
fgColorIndex :: (IntegerHandling, VertexArrayDescriptor a) fgColorIndex :: (IntegerHandling, VertexArrayDescriptor GLfloat)
fgColorIndex = (ToFloat, mapVertexArrayDescriptor 4 0) --color first fgColorIndex = (ToFloat, mapVertexArrayDescriptor 4 3) --color first
fgNormalIndex :: (IntegerHandling, VertexArrayDescriptor a) fgNormalIndex :: (IntegerHandling, VertexArrayDescriptor GLfloat)
fgNormalIndex = (ToFloat, mapVertexArrayDescriptor 3 4) --normal after color fgNormalIndex = (ToFloat, mapVertexArrayDescriptor 3 4) --normal after color
fgVertexIndex :: (IntegerHandling, VertexArrayDescriptor a) fgVertexIndex :: (IntegerHandling, VertexArrayDescriptor GLfloat)
fgVertexIndex = (ToFloat, mapVertexArrayDescriptor 3 7) --vertex after normal fgVertexIndex = (ToFloat, mapVertexArrayDescriptor 3 0) --vertex after normal
getMapBufferObject :: IO (BufferObject, NumArrayIndices) getMapBufferObject :: IO (BufferObject, NumArrayIndices)
getMapBufferObject = do getMapBufferObject = do
map' <- testmap map' <- testmap
map' <- return $ generateCube --generateTriangles map' map' <- return $ P.map (*1) (generateTriangles map')
putStrLn $ P.unlines $ P.map show (prettyMap 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
putStrLn $ P.unwords ["num verts",show len] putStrLn $ P.unwords ["num verts",show len]
@ -177,9 +177,9 @@ lookupVertex map' x y =
--TODO: calculate normals correctly! --TODO: calculate normals correctly!
in in
[ [
cr, cg, cb, 1.0, -- RGBA Color vx, vy, vz, -- 3 Vertex
nx, ny, nz, -- 3 Normal cr, cg, cb, 1.0 -- RGBA Color
vx, vy, vz -- 3 Vertex --nx, ny, nz, -- 3 Normal
] ]
heightLookup :: PlayMap -> (Int,Int) -> GLfloat heightLookup :: PlayMap -> (Int,Int) -> GLfloat
@ -202,7 +202,7 @@ coordLookup (x,z) y =
if even x then if even x then
(fromIntegral $ x `div` 2, y, fromIntegral (2 * z) * lineHeight) (fromIntegral $ x `div` 2, y, fromIntegral (2 * z) * lineHeight)
else else
(fromIntegral (x `div` 2) / 2.0, y, fromIntegral (2 * z + 1) * lineHeight) (fromIntegral (x `div` 2) + 0.5, y, fromIntegral (2 * z + 1) * lineHeight)
-- if writing in ASCII-Format transpose so i,j -> y,x -- if writing in ASCII-Format transpose so i,j -> y,x
@ -233,14 +233,15 @@ testMapTemplate = T.transpose [
testMapTemplate2 :: [Text] testMapTemplate2 :: [Text]
testMapTemplate2 = T.transpose [ testMapTemplate2 = T.transpose [
"~~~~~~" "~~~~~~~~~~~~",
"~SSSSSSSSSS~"
] ]
testmap :: IO PlayMap testmap :: IO PlayMap
testmap = do testmap = do
g <- getStdGen g <- getStdGen
rawMap <- return $ parseTemplate (randoms g) (T.concat testMapTemplate2) rawMap <- return $ parseTemplate (randoms g) (T.concat testMapTemplate2)
return $ listArray ((0,0),(5,0)) rawMap return $ listArray ((0,0),(9,1)) rawMap
parseTemplate :: [Int] -> Text -> [MapEntry] parseTemplate :: [Int] -> Text -> [MapEntry]

View File

@ -33,7 +33,7 @@ initBuffer varray =
checkError "initBuffer" checkError "initBuffer"
return bufferObject return bufferObject
initShader :: IO (AttribLocation, UniformLocation) initShader :: IO (AttribLocation, AttribLocation, UniformLocation, UniformLocation)
initShader = do initShader = do
! vertexSource <- B.readFile vertexShaderFile ! vertexSource <- B.readFile vertexShaderFile
! fragmentSource <- B.readFile fragmentShaderFile ! fragmentSource <- B.readFile fragmentShaderFile
@ -49,12 +49,23 @@ initShader = do
projectionMatrixIndex <- get (uniformLocation program "fg_ProjectionMatrix") projectionMatrixIndex <- get (uniformLocation program "fg_ProjectionMatrix")
checkError "projMat" checkError "projMat"
modelMatrixIndex <- get (uniformLocation program "fg_ModelMatrix")
checkError "modelMat"
att <- get (activeAttribs program)
vertexIndex <- get (attribLocation program "fg_VertexIn") vertexIndex <- get (attribLocation program "fg_VertexIn")
vertexAttribArray vertexIndex $= Enabled vertexAttribArray vertexIndex $= Enabled
checkError "vertexInd" checkError "vertexInd"
colorIndex <- get (attribLocation program "fg_Color")
vertexAttribArray colorIndex $= Enabled
checkError "colorInd"
putStrLn $ unlines $ "Attributes: ":map show att
checkError "initShader" checkError "initShader"
return (vertexIndex, projectionMatrixIndex) return (colorIndex, vertexIndex, projectionMatrixIndex, modelMatrixIndex)
initRendering :: IO () initRendering :: IO ()
initRendering = do initRendering = do