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)
{
//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;
//vertex-data
in vec4 fg_Color;
//in vec4 fg_Color;
in vec3 fg_VertexIn;
in vec3 fg_Normal;
//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;
//fg_SmoothColor = fg_Color + 0.001* fg_Normal.xyzx;
gl_Position = fg_ProjectionMatrix * fg_Vertex;
}

View File

@ -44,8 +44,6 @@ data State = State
, stateFrustum :: [GL.GLfloat]
-- pointer to bindings for locations inside the compiled shader
-- mutable because shaders may be changed in the future.
, shdrColorIndex :: !GL.AttribLocation
, shdrNormalIndex :: !GL.AttribLocation
, shdrVertexIndex :: !GL.AttribLocation
, shdrProjMatIndex :: !GL.UniformLocation
-- the map
@ -105,7 +103,7 @@ main = do
--generate map vertices
(mapBuffer, vert) <- getMapBufferObject
(ci, ni, vi, pi) <- initShader
(vi, pi) <- initShader
let zDistClosest = 10
zDistFarthest = zDistClosest + 20
@ -134,8 +132,6 @@ main = do
, stateDragStartY = 0
, stateDragStartXAngle = 0
, stateDragStartYAngle = 0
, shdrColorIndex = ci
, shdrNormalIndex = ni
, shdrVertexIndex = vi
, shdrProjMatIndex = pi
, stateMap = mapBuffer
@ -378,8 +374,6 @@ draw = do
ya = stateYAngle state
za = stateZAngle state
(GL.UniformLocation proj) = shdrProjMatIndex state
ci = shdrColorIndex state
ni = shdrNormalIndex state
vi = shdrVertexIndex state
numVert = mapVert state
map' = stateMap state
@ -387,8 +381,6 @@ draw = do
liftIO $ do
lookAtUniformMatrix4fv (0.0,0.0,0.0) (0, 15, 0) up frust proj 1
GL.bindBuffer GL.ArrayBuffer GL.$= Just map'
GL.vertexAttribPointer ci GL.$= fgColorIndex
GL.vertexAttribPointer ni GL.$= fgNormalIndex
GL.vertexAttribPointer vi GL.$= fgVertexIndex
GL.drawArrays GL.Triangles 0 numVert

View File

@ -47,9 +47,7 @@ lineHeight :: GLfloat
lineHeight = 0.8660254
numComponents :: Int
numComponents = 4 --color
+3 --normal
+3 --vertex
numComponents = 3
mapStride :: Stride
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 = do
map' <- testmap
map' <- return $ generateTriangles map'
map' <- return $ generateCube --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]
bo <- genObjectName -- create a new buffer
bindBuffer ArrayBuffer $= Just bo -- bind 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"
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 _ = []
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 map' =
let ((xl,yl),(xh,yh)) = bounds map' in

View File

@ -33,7 +33,7 @@ initBuffer varray =
checkError "initBuffer"
return bufferObject
initShader :: IO (AttribLocation, AttribLocation, AttribLocation, UniformLocation)
initShader :: IO (AttribLocation, UniformLocation)
initShader = do
! vertexSource <- B.readFile vertexShaderFile
! fragmentSource <- B.readFile fragmentShaderFile
@ -49,20 +49,12 @@ initShader = do
projectionMatrixIndex <- get (uniformLocation program "fg_ProjectionMatrix")
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")
vertexAttribArray vertexIndex $= Enabled
checkError "vertexInd"
checkError "initShader"
return (colorIndex, normalIndex, vertexIndex, projectionMatrixIndex)
return (vertexIndex, projectionMatrixIndex)
initRendering :: IO ()
initRendering = do