make vao/vbo clearer, remove bs transform, simplify DisplayableObject

This commit is contained in:
mtgmonkey
2025-12-01 22:55:40 +01:00
parent 0cff550c23
commit ed61ff3868
3 changed files with 48 additions and 45 deletions

View File

@@ -39,9 +39,9 @@ view window objects model = do
displayObjects :: GLFW.Window -> DisplayableObjects -> IO (DisplayableObjects)
displayObjects _ [] = return []
displayObjects window ((DisplayableObject vertices firstIndex numVertices _ _ primitiveMode):objects) = do
GL.bindVertexArrayObject $= Just vertices
GL.drawArrays primitiveMode firstIndex numVertices
displayObjects window ((DisplayableObject vao numVertices _ primitiveMode):objects) = do
GL.bindVertexArrayObject $= Just vao
GL.drawArrays primitiveMode 0 numVertices
displayObjects window objects
-- INIT RENDERER --
@@ -59,6 +59,13 @@ verticesArray =
, GL.Vertex2 0.5 0.0
]
testArray :: [GL.Vertex3 GL.GLfloat]
testArray =
[ GL.Vertex3 0.5 1 0.5
, GL.Vertex3 (-0.5) (-0.5) (-0.5)
, GL.Vertex3 0 0 0
]
generateRGBA :: Int -> [GL.Color4 GL.GLfloat]
generateRGBA i =
take i $ cycle rgba
@@ -74,42 +81,53 @@ sizeOfArray :: (Storable a, Num b) => [a] -> b
sizeOfArray [] = 0
sizeOfArray (x:xs) = fromIntegral $ (*) ( 1 + length xs) $ sizeOf x
makeBuffer :: Storable (a GL.GLfloat) => [a GL.GLfloat] -> GL.NumComponents -> GL.GLuint -> IO ()
makeBuffer array numComponents attributeLocation = do
let vPosition = GL.AttribLocation attributeLocation
createVBO :: Storable (a GL.GLfloat) => [a GL.GLfloat] -> GL.NumComponents -> GL.AttribLocation -> IO GL.BufferObject
createVBO array numComponents attribLocation = do
buffer <- GL.genObjectName
GL.bindBuffer GL.ArrayBuffer $= Just buffer
withArray array $ \ptr ->
withArray
array
$ \ptr ->
GL.bufferData GL.ArrayBuffer $= (sizeOfArray array, ptr, GL.StaticDraw)
GL.vertexAttribPointer vPosition $=
(GL.ToFloat, GL.VertexArrayDescriptor numComponents GL.Float 0 (bufferOffset 0))
GL.vertexAttribArray vPosition $= GL.Enabled
initResources :: IO DisplayableObject
initResources = do
-- vertices array
verticesGLArray <- GL.genObjectName
GL.bindVertexArrayObject $= Just verticesGLArray
let verticesObject = DisplayableObject
verticesGLArray
GL.vertexAttribPointer attribLocation $=
( GL.ToFloat
, GL.VertexArrayDescriptor
numComponents
GL.Float
0
(fromIntegral $ length verticesArray)
2
(GL.AttribLocation 0)
GL.TriangleStrip
(bufferOffset 0)
)
GL.vertexAttribArray attribLocation $= GL.Enabled
return buffer
makeBuffer verticesArray 2 0
makeBuffer (generateRGBA $ length verticesArray) 4 1
createDisplayableObject :: Storable (a GL.GLfloat) => [a GL.GLfloat] -> GL.NumComponents -> GL.PrimitiveMode -> IO DisplayableObject
createDisplayableObject array numComponents primitiveMode = do
vao <- GL.genObjectName
GL.bindVertexArrayObject $= Just vao
vbo_0 <- createVBO array numComponents $ GL.AttribLocation 0
vbo_1 <- createVBO (generateRGBA $ length array) 4 $ GL.AttribLocation 1
return
(DisplayableObject
vao
(fromIntegral $ length array)
numComponents
primitiveMode
)
initResources :: IO DisplayableObjects
initResources = do
verticesObject <- createDisplayableObject verticesArray 2 GL.TriangleStrip
testObject <- createDisplayableObject testArray 3 GL.TriangleStrip
-- load shaders
program <- loadShaders
[ ShaderInfo GL.VertexShader (StringSource $ unpack $ GLSL.generateGLSL vertShader)
, ShaderInfo GL.FragmentShader (StringSource $ unpack $ GLSL.generateGLSL fragShader)
]
GL.currentProgram $= Just program
-- return descriptor for vertices array
return verticesObject
return [verticesObject, testObject]
-- INPUT --
@@ -137,8 +155,8 @@ openWindow
GLFW.setWindowCloseCallback window (Just shutdownWindow)
GLFW.setWindowSizeCallback window (Just resizeWindow)
GLFW.setKeyCallback window (Just keyPressed)
object <- initResources
return (window, [object])
objects <- initResources
return (window, objects)
shutdownWindow :: GLFW.WindowCloseCallback
shutdownWindow window =

View File

@@ -1,7 +1,7 @@
{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE OverloadedStrings #-}
module Transforms (displayableObjectFromTriangles) where
module Transforms (pointToVertex) where
-- IMPORTS --
@@ -13,18 +13,5 @@ import Relude
import IO
import Types
displayableObjectFromTriangles :: [Triangle] -> GL.GLuint -> IO DisplayableObject
displayableObjectFromTriangles triangles attribLocation = do
let array = [ pointToVertex p | (Triangle a b c) <- triangles, p <- [a, b, c] ]
glArray <- GL.genObjectName
GL.bindVertexArrayObject $= Just glArray
return $ DisplayableObject
glArray
0
(fromIntegral $ length array)
4
(GL.AttribLocation attribLocation)
GL.Triangles
pointToVertex :: Point -> GL.Vertex4 GL.GLfloat
pointToVertex (Point x y z k) = GL.Vertex4 x y z k

View File

@@ -18,10 +18,8 @@ type DisplayableObjects = [DisplayableObject]
data DisplayableObject =
DisplayableObject
GL.VertexArrayObject
GL.ArrayIndex
GL.NumArrayIndices
GL.NumComponents
GL.AttribLocation
GL.PrimitiveMode
-- model --