From 2494f2cf8d828722280a7a0f82b9fc368cde6a11 Mon Sep 17 00:00:00 2001 From: justin Date: Wed, 12 Apr 2023 18:42:31 +0100 Subject: [PATCH] Work on VertexAttribute usage --- src/Preload/Render.hs | 4 ++-- src/Shader/Compile.hs | 39 ++++++++++----------------------------- 2 files changed, 12 insertions(+), 31 deletions(-) diff --git a/src/Preload/Render.hs b/src/Preload/Render.hs index 803ebb099..475de4e7d 100644 --- a/src/Preload/Render.hs +++ b/src/Preload/Render.hs @@ -69,7 +69,7 @@ preloadRender = do -- setup shape geometry/cap VBO and two VAOs putStrLn "Setup shape VBO, VAO, EBO" shVBO <- setupVBO nShapeVerxComp - shPosVAO <- setupVAOUsingVBO [4] shVBO + shPosVAO <- setupVAOUsingVBO [VertexAttribute 4 GL_FLOAT GL_FALSE 0] shVBO shEBO <- setupEBO shPosVAO -- note the shape shader vao is distinct from the position vao, but they -- share an EBO @@ -78,7 +78,7 @@ preloadRender = do --setup silhouette edge VAO putStrLn "Setup silhouette VAO, EBO" - shedgevao <- setupVAOUsingVBO [4] shVBO + shedgevao <- setupVAOUsingVBO [VertexAttribute 4 GL_FLOAT GL_FALSE 0] shVBO shedgeebo <- setupEBO shedgevao putStrLn "Setup wall/windows VBO, VAO, EBO, shader" diff --git a/src/Shader/Compile.hs b/src/Shader/Compile.hs index 23127ac77..f0937640b 100644 --- a/src/Shader/Compile.hs +++ b/src/Shader/Compile.hs @@ -83,7 +83,7 @@ makeShaderUsingVBO :: VBO -> IO Shader makeShaderUsingVBO s shaderlist sizes pm vbo = do - vao <- setupVAOUsingVBO sizes vbo + vao <- setupVAOUsingVBO (toFloatVAs sizes) vbo makeShaderUsingVAO s shaderlist pm vao setupVBO :: Int -> IO VBO @@ -196,20 +196,19 @@ setupVAOvbo' :: [Int] -> Int -> GLuint -> IO VAO setupVAOvbo' sizes strd vbo = do vaoname <- mglCreate glCreateVertexArrays glBindVertexArray vaoname - setupVertexAttribs' vbo vaoname sizes strd + setupVertexAttribs vbo vaoname (toFloatVAs sizes) (fromIntegral strd * fromIntegral floatSize) return $ VAO { _vaoName = vaoname } -setupVAOUsingVBO :: [Int] -> VBO -> IO VAO -setupVAOUsingVBO sizes vbo = do +setupVAOUsingVBO :: [VertexAttribute] -> VBO -> IO VAO +setupVAOUsingVBO vas vbo = do let strd = vbo ^. vboVertexSize - when (strd < sum sizes) $ - error "Tried to assign vertex attributes to VBO with too small a stride." vaoname <- mglCreate glCreateVertexArrays glBindVertexArray vaoname - setupVertexAttribs vbo vaoname sizes strd + setupVertexAttribs (vbo ^. vboName) vaoname vas + (fromIntegral strd * fromIntegral floatSize) return VAO{_vaoName = vaoname} setupEBO :: VAO -> IO EBO @@ -245,13 +244,9 @@ toFloatVAs = go 0 setupVBOSized :: Int -> GLuint -> [Int] -> IO VBO setupVBOSized ndraw vao sizes = do - --vboName <- genObjectName - --bindBuffer ArrayBuffer $= Just vboName vboname <- mglCreate glCreateBuffers glVertexArrayVertexBuffer vao 0 vboname 0 (fromIntegral $ floatSize * strd) zipWithM_ (setupVertexAttribPointer vao) [0..] $ toFloatVAs sizes --- forM_ (zip3 [0 ..] sizes offs) $ \(loc, siz, off) -> do --- setupVertexAttribPointer vao loc siz off thePtr <- mallocArray (strd * ndraw) -- Allocate space glNamedBufferData @@ -267,25 +262,11 @@ setupVBOSized ndraw vao sizes = do } where strd = sum sizes - offs = scanl (+) 0 sizes -setupVertexAttribs :: VBO -> GLuint -> [Int] -> Int -> IO () -setupVertexAttribs vbo vao sizes strd = do - glVertexArrayVertexBuffer vao 0 (_vboName vbo) 0 (fromIntegral $ floatSize * strd) - zipWithM_ (setupVertexAttribPointer vao) [0..] $ toFloatVAs sizes - --forM_ (zip3 [0 ..] sizes offs) $ \(loc, siz, off) -> do - -- setupVertexAttribPointer vao loc siz off - where - offs = scanl (+) 0 sizes - -setupVertexAttribs' :: GLuint -> GLuint -> [Int] -> Int -> IO () -setupVertexAttribs' vbo vao sizes strd = do - glVertexArrayVertexBuffer vao 0 vbo 0 (fromIntegral $ floatSize * strd) - zipWithM_ (setupVertexAttribPointer vao) [0..] $ toFloatVAs sizes - --forM_ (zip3 [0 ..] sizes offs) $ \(loc, siz, off) -> do - -- setupVertexAttribPointer vao loc siz off - where - offs = scanl (+) 0 sizes +setupVertexAttribs :: GLuint -> GLuint -> [VertexAttribute] -> GLsizei -> IO () +setupVertexAttribs vbo vao vas strd = do + glVertexArrayVertexBuffer vao 0 vbo 0 strd + zipWithM_ (setupVertexAttribPointer vao) [0..] vas -- | Assumes the correct VBO is bound setupVertexAttribPointer ::