index buffer failing

This commit is contained in:
nek0 2020-05-17 12:26:57 +02:00
parent 4acc1dd84e
commit 23e806a8e7

View file

@ -67,17 +67,21 @@ main = do
bo <- GL.genObjectName
GL.bindVertexArrayObject $= Just bo
-- create and bind buffer for a triangle
-- define vertices (positions of the triangle corners) as List of Floats
let vertexPositions =
[ (-0.5), (-0.5) -- 0
, 0.5 , (-0.5) -- 1
, 0.5 , 0.5 -- 2
, (-0.5), 0.5 -- 3
] :: [Float]
-- create draw order indices
indices = [0, 1, 2, 2, 3, 0] :: [Word]
-- create and bind buffer for vertices
buf <- GL.genObjectName
GL.bindBuffer GL.ArrayBuffer $= Just buf
-- define vertices (positions of the triangle corners) as List of Floats
let vertexPositions =
[ (-0.5), (-0.5)
, 0 , 0.5
, 0.5 , (-0.5)
] :: [Float]
-- put vertices into the buffer
-- turn the list into a pointer
withArray vertexPositions $ \ptr ->
@ -106,6 +110,23 @@ main = do
)
GL.vertexAttribArray (GL.AttribLocation 0) $= GL.Enabled
-- create and bind buffer index buffer
ibo <- GL.genObjectName
GL.bindBuffer GL.ElementArrayBuffer $= Just ibo
-- put indices into index buffer
-- turn the list into a pointer
withArray indices $ \ptr -> do
-- Feed the data to the buffer
GL.bufferData GL.ElementArrayBuffer $=
-- how much bytes of memory we are going to write (as an Int32)
( fromIntegral $ length indices * sizeOf (undefined :: Word)
-- The pointer to the data
, ptr
-- The data's usage
, GL.StaticDraw
)
-- -- SHADERS
-- read in shaders from source file
@ -114,6 +135,8 @@ main = do
sp <- createShaderProgram vertSrc fragSrc
GL.currentProgram $= Just sp
err <- get GL.errors
print $ "pre-loop errors: " <> show err
-- -- EVENTING AND DRAWING
@ -138,7 +161,10 @@ main = do
GL.clear [GL.ColorBuffer]
-- tha actual drawing happens here
GL.drawArrays GL.Triangles 0 3
void $ get GL.errors
GL.drawElements GL.Triangles 6 GL.UnsignedInt nullPtr
err <- get GL.errors
when (not $ null err) (print $ "loop errors: " <> show err)
-- make GL finish things up
-- GL.flush