flickering triangle (rendering cube)

This commit is contained in:
tpajenka 2014-01-04 02:53:12 +01:00
parent a2c1effb48
commit 33b1ec5534
5 changed files with 56 additions and 29 deletions

View File

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

View File

@ -4,17 +4,17 @@
uniform mat4 fg_ProjectionMatrix; uniform mat4 fg_ProjectionMatrix;
//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 + 0.001* fg_Normal.xyzx;
gl_Position = fg_ProjectionMatrix * fg_Vertex; gl_Position = fg_ProjectionMatrix * fg_Vertex;
} }

View File

@ -44,8 +44,6 @@ data State = State
, stateFrustum :: [GL.GLfloat] , 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
, shdrNormalIndex :: !GL.AttribLocation
, shdrVertexIndex :: !GL.AttribLocation , shdrVertexIndex :: !GL.AttribLocation
, shdrProjMatIndex :: !GL.UniformLocation , shdrProjMatIndex :: !GL.UniformLocation
-- the map -- the map
@ -105,7 +103,7 @@ main = do
--generate map vertices --generate map vertices
(mapBuffer, vert) <- getMapBufferObject (mapBuffer, vert) <- getMapBufferObject
(ci, ni, vi, pi) <- initShader (vi, pi) <- initShader
let zDistClosest = 10 let zDistClosest = 10
zDistFarthest = zDistClosest + 20 zDistFarthest = zDistClosest + 20
@ -134,8 +132,6 @@ main = do
, stateDragStartY = 0 , stateDragStartY = 0
, stateDragStartXAngle = 0 , stateDragStartXAngle = 0
, stateDragStartYAngle = 0 , stateDragStartYAngle = 0
, shdrColorIndex = ci
, shdrNormalIndex = ni
, shdrVertexIndex = vi , shdrVertexIndex = vi
, shdrProjMatIndex = pi , shdrProjMatIndex = pi
, stateMap = mapBuffer , stateMap = mapBuffer
@ -378,8 +374,6 @@ draw = do
ya = stateYAngle state ya = stateYAngle state
za = stateZAngle state za = stateZAngle state
(GL.UniformLocation proj) = shdrProjMatIndex state (GL.UniformLocation proj) = shdrProjMatIndex state
ci = shdrColorIndex state
ni = shdrNormalIndex state
vi = shdrVertexIndex state vi = shdrVertexIndex state
numVert = mapVert state numVert = mapVert state
map' = stateMap state map' = stateMap state
@ -387,8 +381,6 @@ draw = do
liftIO $ do liftIO $ do
lookAtUniformMatrix4fv (0.0,0.0,0.0) (0, 15, 0) up frust proj 1 lookAtUniformMatrix4fv (0.0,0.0,0.0) (0, 15, 0) 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 ni GL.$= fgNormalIndex
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,9 +47,7 @@ lineHeight :: GLfloat
lineHeight = 0.8660254 lineHeight = 0.8660254
numComponents :: Int numComponents :: Int
numComponents = 4 --color numComponents = 3
+3 --normal
+3 --vertex
mapStride :: Stride mapStride :: Stride
mapStride = fromIntegral (sizeOf (0.0 :: GLfloat)) * fromIntegral numComponents mapStride = fromIntegral (sizeOf (0.0 :: GLfloat)) * fromIntegral numComponents
@ -73,14 +71,14 @@ fgVertexIndex = (ToFloat, mapVertexArrayDescriptor 3 7) --vertex after normal
getMapBufferObject :: IO (BufferObject, NumArrayIndices) getMapBufferObject :: IO (BufferObject, NumArrayIndices)
getMapBufferObject = do getMapBufferObject = do
map' <- testmap map' <- testmap
map' <- return $ generateTriangles map' map' <- return $ generateCube --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]
bo <- genObjectName -- create a new buffer bo <- genObjectName -- create a new buffer
bindBuffer ArrayBuffer $= Just bo -- bind buffer bindBuffer ArrayBuffer $= Just bo -- bind buffer
withArray map' $ \buffer -> withArray map' $ \buffer ->
bufferData ArrayBuffer $= (fromIntegral (sizeOf(P.head map')), buffer, StaticDraw) bufferData ArrayBuffer $= (fromIntegral (P.length map' * sizeOf(P.head map')), buffer, StaticDraw)
checkError "initBuffer" checkError "initBuffer"
return (bo,len) return (bo,len)
@ -88,6 +86,51 @@ prettyMap :: [GLfloat] -> [(GLfloat,GLfloat,GLfloat,GLfloat,GLfloat,GLfloat,GLfl
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 (a:b:c:d:x:y:z:u:v:w:ms) = (a,b,c,d,x,y,z,u,v,w):(prettyMap ms)
prettyMap _ = [] prettyMap _ = []
generateCube :: [GLfloat]
generateCube = [ -- lower plane
-0.3,-0.3,-0.3,
0.3,-0.3,0.3,
0.3,-0.3,-0.3,
-0.3,-0.3,-0.3,
-0.3,-0.3,0.3,
0.3,-0.3,0.3,
-- upper plane
-0.3,0.3,-0.3,
0.3,0.3,0.3,
0.3,0.3,-0.3,
-0.3,0.3,-0.3,
-0.3,0.3,0.3,
0.3,0.3,0.3,
-- left plane
-0.3,-0.3,-0.3,
-0.3,0.3,0.3,
-0.3,-0.3,0.3,
-0.3,-0.3,-0.3,
-0.3,0.3,0.3,
-0.3,0.3,-0.3,
-- right plane
0.3,-0.3,-0.3,
0.3,0.3,0.3,
0.3,-0.3,0.3,
0.3,-0.3,-0.3,
0.3,0.3,0.3,
0.3,0.3,-0.3,
-- front plane
-0.3,-0.3,-0.3,
0.3,0.3,-0.3,
0.3,-0.3,-0.3,
-0.3,-0.3,-0.3,
0.3,0.3,-0.3,
-0.3,0.3,-0.3,
-- back plane
-0.3,-0.3,0.3,
0.3,0.3,0.3,
0.3,-0.3,0.3,
-0.3,-0.3,0.3,
0.3,0.3,0.3,
-0.3,0.3,0.3
]
generateTriangles :: PlayMap -> [GLfloat] generateTriangles :: PlayMap -> [GLfloat]
generateTriangles map' = generateTriangles map' =
let ((xl,yl),(xh,yh)) = bounds map' in let ((xl,yl),(xh,yh)) = bounds map' in

View File

@ -33,7 +33,7 @@ initBuffer varray =
checkError "initBuffer" checkError "initBuffer"
return bufferObject return bufferObject
initShader :: IO (AttribLocation, AttribLocation, AttribLocation, UniformLocation) initShader :: IO (AttribLocation, UniformLocation)
initShader = do initShader = do
! vertexSource <- B.readFile vertexShaderFile ! vertexSource <- B.readFile vertexShaderFile
! fragmentSource <- B.readFile fragmentShaderFile ! fragmentSource <- B.readFile fragmentShaderFile
@ -49,20 +49,12 @@ initShader = do
projectionMatrixIndex <- get (uniformLocation program "fg_ProjectionMatrix") projectionMatrixIndex <- get (uniformLocation program "fg_ProjectionMatrix")
checkError "projMat" checkError "projMat"
colorIndex <- get (attribLocation program "fg_Color")
vertexAttribArray colorIndex $= Enabled
checkError "colorInd"
normalIndex <- get (attribLocation program "fg_Normal")
vertexAttribArray normalIndex $= Enabled
checkError "normalInd"
vertexIndex <- get (attribLocation program "fg_VertexIn") vertexIndex <- get (attribLocation program "fg_VertexIn")
vertexAttribArray vertexIndex $= Enabled vertexAttribArray vertexIndex $= Enabled
checkError "vertexInd" checkError "vertexInd"
checkError "initShader" checkError "initShader"
return (colorIndex, normalIndex, vertexIndex, projectionMatrixIndex) return (vertexIndex, projectionMatrixIndex)
initRendering :: IO () initRendering :: IO ()
initRendering = do initRendering = do