abstract buffer creation and arrays

This commit is contained in:
mtgmonkey
2025-11-27 20:59:55 +01:00
parent bc5e556371
commit 1efda4fa44
2 changed files with 54 additions and 67 deletions

View File

@@ -5,7 +5,7 @@
pkgs, pkgs,
... ...
}: let }: let
ghcExeOptions = "-Wall -Widentities -Wincomplete-record-updates -Wincomplete-uni-patterns -Wmissing-export-lists -Wmissing-home-modules -Wpartial-fields -Wredundant-constraints -threaded -rtsopts -with-rtsopts=-N"; ghcExeOptions = "-O -Wall -Widentities -Wincomplete-record-updates -Wincomplete-uni-patterns -Wmissing-export-lists -Wmissing-home-modules -Wpartial-fields -Wredundant-constraints -threaded -rtsopts -with-rtsopts=-N";
ghcPackages = p: [ ghcPackages = p: [
p.bytestring p.bytestring
p.data-default p.data-default

109
src/IO.hs
View File

@@ -21,12 +21,15 @@ import Model
-- VIEW -- -- VIEW --
backgroundColor :: GL.Color4 GL.GLfloat
backgroundColor = GL.Color4 0 0 0 1
view :: GLFW.Window -> Descriptor -> Model -> IO () view :: GLFW.Window -> Descriptor -> Model -> IO ()
view window descriptor@(Descriptor vertices firstIndex numVertices) model = do view window descriptor@(Descriptor vertices firstIndex numVertices) model = do
GL.clearColor $= GL.Color4 1 0 1 1 GL.clearColor $= backgroundColor
GL.clear [GL.ColorBuffer] GL.clear [GL.ColorBuffer]
GL.bindVertexArrayObject $= Just vertices GL.bindVertexArrayObject $= Just vertices
GL.drawArrays GL.Triangles firstIndex numVertices GL.drawArrays GL.TriangleStrip firstIndex numVertices
GLFW.swapBuffers window GLFW.swapBuffers window
GLFW.pollEvents GLFW.pollEvents
@@ -43,82 +46,66 @@ data Descriptor =
bufferOffset :: Integral a => a -> Ptr b bufferOffset :: Integral a => a -> Ptr b
bufferOffset integral = plusPtr nullPtr $ fromIntegral integral bufferOffset integral = plusPtr nullPtr $ fromIntegral integral
initResources :: IO Descriptor verticesArray :: [GL.Vertex2 GL.GLfloat]
initResources = do
-- vertices array
verticesGLArray <- GL.genObjectName
GL.bindVertexArrayObject $= Just verticesGLArray
let
verticesArray = verticesArray =
[ GL.Vertex2 0.9 0.9 [ GL.Vertex2 0.9 0.9
, GL.Vertex2 0.9 (-0.85) , GL.Vertex2 0.5 0.9
, GL.Vertex2 (-0.85) 0.9 , GL.Vertex2 0.9 0.5
, GL.Vertex2 (-0.9) (-0.9) , GL.Vertex2 0.5 0.5
, GL.Vertex2 (-0.9) 0.85 , GL.Vertex2 0.9 0.0
, GL.Vertex2 0.85 (-0.9) , GL.Vertex2 0.5 0.0
] :: [GL.Vertex2 GL.GLfloat] ]
numVertices = length verticesArray
vertexBuffer <- GL.genObjectName
GL.bindBuffer GL.ArrayBuffer $= Just vertexBuffer
withArray verticesArray $ \ptr -> do
let
size
= fromIntegral
$ (*)
numVertices
$ sizeOf
$ case viaNonEmpty head verticesArray of
Nothing -> (GL.Vertex2 0 0) :: (GL.Vertex2 GL.GLfloat)
Just a -> a
GL.bufferData GL.ArrayBuffer $= (size, ptr, GL.StaticDraw)
let
firstIndex = 0
vPosition = GL.AttribLocation 0
GL.vertexAttribPointer vPosition $=
(GL.ToFloat, GL.VertexArrayDescriptor 2 GL.Float 0 (bufferOffset firstIndex))
GL.vertexAttribArray vPosition $= GL.Enabled
-- colors array generateRGBA :: Int -> [GL.Color4 GL.GLfloat]
generateRGBA i =
take i $ cycle rgba
let rgba :: [GL.Color4 GL.GLfloat]
rgba = rgba =
[ GL.Color4 1.0 0.0 0.0 1.0 [ GL.Color4 1.0 0.0 0.0 1.0
, GL.Color4 0.0 1.0 0.0 1.0 , GL.Color4 0.0 1.0 0.0 1.0
, GL.Color4 0.0 0.0 1.0 1.0 , GL.Color4 0.0 0.0 1.0 1.0
, GL.Color4 1.0 0.0 0.0 1.0 ]
, GL.Color4 0.0 1.0 0.0 1.0
, GL.Color4 0.0 0.0 1.0 1.0 sizeOfArray :: (Storable a, Num b) => [a] -> b
] :: [GL.Color4 GL.GLfloat] sizeOfArray [] = 0
colorBuffer <- GL.genObjectName sizeOfArray (x:xs) = fromIntegral $ (*) ( 1 + length xs) $ sizeOf x
GL.bindBuffer GL.ArrayBuffer $= Just colorBuffer
withArray rgba $ \ptr -> do makeBuffer :: Storable (a GL.GLfloat) => [a GL.GLfloat] -> GL.NumComponents -> GL.GLuint -> IO ()
let makeBuffer array numComponents i = do
size let vPosition = GL.AttribLocation i
= fromIntegral buffer <- GL.genObjectName
$ (*) GL.bindBuffer GL.ArrayBuffer $= Just buffer
numVertices withArray array $ \ptr ->
$ sizeOf GL.bufferData GL.ArrayBuffer $= (sizeOfArray array, ptr, GL.StaticDraw)
$ case viaNonEmpty head rgba of
Nothing ->
(GL.Color4 0.0 0.0 0.0 0.0) :: (GL.Color4 GL.GLfloat)
Just a ->
a
GL.bufferData GL.ArrayBuffer $= (size, ptr, GL.StaticDraw)
let
firstIndex = 0
vPosition = GL.AttribLocation 1
GL.vertexAttribPointer vPosition $= GL.vertexAttribPointer vPosition $=
(GL.ToFloat, GL.VertexArrayDescriptor 4 GL.Float 0 (bufferOffset firstIndex)) (GL.ToFloat, GL.VertexArrayDescriptor numComponents GL.Float 0 (bufferOffset 0))
GL.vertexAttribArray vPosition $= GL.Enabled GL.vertexAttribArray vPosition $= GL.Enabled
initResources :: IO Descriptor
initResources = do
-- vertices array
verticesGLArray <- GL.genObjectName
GL.bindVertexArrayObject $= Just verticesGLArray
let verticesDescriptor = Descriptor
verticesGLArray
0
$ fromIntegral
$ length verticesArray
makeBuffer verticesArray 2 0
makeBuffer (generateRGBA $ length verticesArray) 4 1
-- load shaders
program <- loadShaders program <- loadShaders
[ ShaderInfo GL.VertexShader (FileSource "vert.glsl") [ ShaderInfo GL.VertexShader (FileSource "vert.glsl")
, ShaderInfo GL.FragmentShader (FileSource "frag.glsl") , ShaderInfo GL.FragmentShader (FileSource "frag.glsl")
] ]
GL.currentProgram $= Just program GL.currentProgram $= Just program
return $ Descriptor verticesGLArray firstIndex $ fromIntegral numVertices -- return descriptor for vertices array
return verticesDescriptor
-- INPUT -- -- INPUT --