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)
{
//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
uniform mat4 fg_ProjectionMatrix;
uniform mat4 fg_ModelMatrix;
//vertex-data
//in vec4 fg_Color;
in vec4 fg_Color;
in vec3 fg_VertexIn;
//in vec3 fg_Normal;
//output-data for later stages
//smooth out vec4 fg_SmoothColor;
smooth out vec4 fg_SmoothColor;
void main()
{
//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;
fg_SmoothColor = fg_Color;
// + 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
-- mutable because shaders may be changed in the future.
, shdrVertexIndex :: !GL.AttribLocation
, shdrColorIndex :: !GL.AttribLocation
, shdrProjMatIndex :: !GL.UniformLocation
, shdrModelMatIndex :: !GL.UniformLocation
-- the map
, stateMap :: !GL.BufferObject
, mapVert :: !GL.NumArrayIndices
@ -105,7 +107,7 @@ main = do
--generate map vertices
(mapBuffer, vert) <- getMapBufferObject
(vi, pi) <- initShader
(ci, vi, pi, mi) <- initShader
let zDistClosest = 10
zDistFarthest = zDistClosest + 20
@ -135,7 +137,9 @@ main = do
, stateDragStartXAngle = 0
, stateDragStartYAngle = 0
, shdrVertexIndex = vi
, shdrColorIndex = ci
, shdrProjMatIndex = pi
, shdrModelMatIndex = mi
, stateMap = mapBuffer
, mapVert = vert
, stateFrustum = frust
@ -222,6 +226,7 @@ run = do
processEvents
-- update State
{-
state <- get
if stateDragging state
then do
@ -243,6 +248,7 @@ run = do
{ stateXAngle = stateXAngle state + (2 * kxrot) + (2 * jxrot)
, stateYAngle = stateYAngle state + (2 * kyrot) + (2 * jyrot)
}
-}
{-
--modify the state with all that happened in mt time.
mt <- liftIO GLFW.getTime
@ -373,12 +379,16 @@ draw = do
let xa = stateXAngle state
ya = stateYAngle state
za = stateZAngle state
(GL.UniformLocation proj) = shdrProjMatIndex state
(GL.UniformLocation proj) = shdrProjMatIndex state
(GL.UniformLocation mmat) = shdrModelMatIndex state
vi = shdrVertexIndex state
ci = shdrColorIndex state
numVert = mapVert state
map' = stateMap state
frust = stateFrustum state
liftIO $ do
--(vi,GL.UniformLocation proj) <- initShader
GL.clearColor GL.$= GL.Color4 0.5 0.1 1 1
GL.clear [GL.ColorBuffer]
let fov = 90
s = recip (tan $ fov * 0.5 * pi / 180)
@ -391,7 +401,15 @@ draw = do
, 0, 0, -((f*n)/(f-n)), 0
]
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.vertexAttribPointer ci GL.$= fgColorIndex
GL.vertexAttribPointer vi GL.$= fgVertexIndex
GL.drawArrays GL.Triangles 0 numVert

View File

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

View File

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