Attach EBOs to VAOs using DSA

This commit is contained in:
2023-03-09 13:55:52 +00:00
parent f14d9bef60
commit 3e2b51b5e9
3 changed files with 101 additions and 93 deletions
+34 -28
View File
@@ -1,38 +1,32 @@
module Dodge.Render.Shadow where module Dodge.Render.Shadow where
import Shader.Parameters
import Shader.ExtraPrimitive
import Graphics.Rendering.OpenGL ( ($=), bindBuffer
, BufferTarget (..), bufferSubData, TransferDirection (..)
)
import Graphics.GL.Core43
import Geometry
import Linear.V3 (cross)
--import Data.Vector.Unboxed as UV --import Data.Vector.Unboxed as UV
import qualified Data.Vector as V
import Shader.Data
import Foreign
import Shape.Data
import Data.Preload.Render
--import Dodge.Data.Universe --import Dodge.Data.Universe
import Control.Lens import Control.Lens
import Data.Preload.Render
import qualified Data.Vector as V
import qualified Data.Vector.Fusion.Stream.Monadic as VFSM import qualified Data.Vector.Fusion.Stream.Monadic as VFSM
import Foreign
import Geometry
import Graphics.GL.Core45
import Linear.V3 (cross)
import Shader.Data
import Shader.ExtraPrimitive
import Shader.Parameters
import Shape.Data
drawCPUShadows :: RenderData -> Shape -> Point3 -> Float -> IO () drawCPUShadows :: RenderData -> Shape -> Point3 -> Float -> IO ()
drawCPUShadows pdata s pos rad = do drawCPUShadows pdata s pos rad = do
let theshad = pdata ^. positionalBlankShader let theshad = pdata ^. positionalBlankShader
theptr = _vboPtr $ _vaoVBO $ _shadVAO' theshad theptr = _vboPtr $ _vaoVBO $ _shadVAO' theshad
i <- VFSM.foldlM' (pokeShapeShad pos rad theptr) 0 $ VFSM.fromList s i <- VFSM.foldlM' (pokeShapeShad pos rad theptr) 0 $ VFSM.fromList s
glBindBuffer GL_ARRAY_BUFFER (theshad ^. shadVAO' . vaoVBO . vboName) glBufferSubData
bufferSubData (theshad ^. shadVAO' . vaoVBO . vboName)
ArrayBuffer
WriteToBuffer
0 0
(fromIntegral $ floatSize * i) (fromIntegral $ floatSize * i)
(theshad ^. shadVAO' . vaoVBO . vboPtr) (theshad ^. shadVAO' . vaoVBO . vboPtr)
--currentProgram $= theshad ^? shadProg
glUseProgram (theshad ^. shadProg') glUseProgram (theshad ^. shadProg')
--bindVertexArrayObject $= Just (_vaoName $ _shadVAO' theshad)
glBindVertexArray . _vaoName $ _shadVAO' theshad glBindVertexArray . _vaoName $ _shadVAO' theshad
glDrawArrays glDrawArrays
(marshalEPrimitiveMode $ _shadPrim' theshad) (marshalEPrimitiveMode $ _shadPrim' theshad)
@@ -46,8 +40,14 @@ pokeShapeShad pos r theptr i so = do
is = memoTopPrismEdges V.! (so ^. shType . prismSize - 2) is = memoTopPrismEdges V.! (so ^. shType . prismSize - 2)
V.foldM' (pokeShadEdge pos r theptr vs) i is V.foldM' (pokeShadEdge pos r theptr vs) i is
pokeShadEdge :: Point3 -> Float -> Ptr Float -> V.Vector Point3 pokeShadEdge ::
-> Int -> (Int,Int,Int,Int) -> IO Int Point3 ->
Float ->
Ptr Float ->
V.Vector Point3 ->
Int ->
(Int, Int, Int, Int) ->
IO Int
pokeShadEdge pos _ ptr vxs i (a, b, x, y) = do pokeShadEdge pos _ ptr vxs i (a, b, x, y) = do
let p0 = vxs V.! a let p0 = vxs V.! a
p1 = vxs V.! b p1 = vxs V.! b
@@ -67,12 +67,15 @@ pokeShadEdge pos _ ptr vxs i (a,b,x,y) = do
if dotV3 n0 lightdir > 0 if dotV3 n0 lightdir > 0
then then
pokeV3 ptr p0 i >>= pokeV3 ptr p1 >>= pokeV3 ptr p2 pokeV3 ptr p0 i >>= pokeV3 ptr p1 >>= pokeV3 ptr p2
>>= pokeV3 ptr p0 >>= pokeV3 ptr p2 >>= pokeV3 ptr p3 >>= pokeV3 ptr p0
>>= pokeV3 ptr p2
>>= pokeV3 ptr p3
else else
pokeV3 ptr p1 i >>= pokeV3 ptr p0 >>= pokeV3 ptr p3 pokeV3 ptr p1 i >>= pokeV3 ptr p0 >>= pokeV3 ptr p3
>>= pokeV3 ptr p1 >>= pokeV3 ptr p3 >>= pokeV3 ptr p2 >>= pokeV3 ptr p1
else >>= pokeV3 ptr p3
return i >>= pokeV3 ptr p2
else return i
pokeV3 :: Ptr Float -> Point3 -> Int -> IO Int pokeV3 :: Ptr Float -> Point3 -> Int -> IO Int
pokeV3 ptr (V3 x y z) i = do pokeV3 ptr (V3 x y z) i = do
@@ -82,13 +85,16 @@ pokeV3 ptr (V3 x y z) i = do
return (i + 1) return (i + 1)
memoTopPrismEdges :: V.Vector (V.Vector (Int, Int, Int, Int)) memoTopPrismEdges :: V.Vector (V.Vector (Int, Int, Int, Int))
memoTopPrismEdges = V.generate 10 memoTopPrismEdges =
$ V.fromList . topPrismEdges . (+ 2) V.generate 10 $
V.fromList . topPrismEdges . (+ 2)
topPrismEdges :: Int -> [(Int, Int, Int, Int)] topPrismEdges :: Int -> [(Int, Int, Int, Int)]
topPrismEdges n = concatMap f [0 .. n -1] topPrismEdges n = concatMap f [0 .. n -1]
where where
f i = map h f i =
map
h
[ (0, 2, 1, 4) [ (0, 2, 1, 4)
, (0, 1, -2, 3) , (0, 1, -2, 3)
, (1, 3, -1, 2) , (1, 3, -1, 2)
+23 -29
View File
@@ -35,24 +35,23 @@ preloadRender = do
wpVBOname <- mglCreateSingle glCreateBuffers wpVBOname <- mglCreateSingle glCreateBuffers
wpVBOptr <- mallocArray (8 * numDrawableWalls) wpVBOptr <- mallocArray (8 * numDrawableWalls)
glBindBuffer GL_ARRAY_BUFFER wpVBOname glBindBuffer GL_ARRAY_BUFFER wpVBOname
bufferData ArrayBuffer glNamedBufferData wpVBOname
$= ( fromIntegral $ floatSize * numDrawableWalls * 8 ( fromIntegral $ floatSize * numDrawableWalls * 8)
, nullPtr nullPtr
, StreamDraw GL_STREAM_DRAW
)
let wpVBO = VBO{_vboName = wpVBOname, _vboPtr = wpVBOptr, _vboAttribSizes = [4, 4], _vboStride = 8} let wpVBO = VBO{_vboName = wpVBOname, _vboPtr = wpVBOptr, _vboAttribSizes = [4, 4], _vboStride = 8}
-- wpVAOname <- genObjectName -- wpVAOname <- genObjectName
-- bindVertexArrayObject $= Just wpVAOname -- bindVertexArrayObject $= Just wpVAOname
wpVAOname <- mglCreateSingle glCreateVertexArrays wpVAOname <- mglCreateSingle glCreateVertexArrays
glBindVertexArray wpVAOname glBindVertexArray wpVAOname
glBindBuffer GL_ARRAY_BUFFER wpVBOname glBindBuffer GL_ARRAY_BUFFER wpVBOname
setupVertexAttribPointer 0 4 8 0 setupVertexAttribPointer wpVAOname wpVBOname 0 4 8 0
--wpColVAOname <- genObjectName --wpColVAOname <- genObjectName
--bindVertexArrayObject $= Just wpColVAOname --bindVertexArrayObject $= Just wpColVAOname
wpColVAOname <- mglCreateSingle glCreateVertexArrays wpColVAOname <- mglCreateSingle glCreateVertexArrays
glBindVertexArray wpColVAOname glBindVertexArray wpColVAOname
setupVertexAttribPointer 0 4 8 0 setupVertexAttribPointer wpColVAOname wpVBOname 0 4 8 0
setupVertexAttribPointer 1 4 8 4 setupVertexAttribPointer wpColVAOname wpVBOname 1 4 8 4
let wpVAO = VAO{_vaoName = wpVAOname, _vaoVBO = wpVBO} let wpVAO = VAO{_vaoName = wpVAOname, _vaoVBO = wpVBO}
wpColVAO = VAO{_vaoName = wpColVAOname, _vaoVBO = wpVBO} wpColVAO = VAO{_vaoName = wpColVAOname, _vaoVBO = wpVBO}
-- setup window points VBO, VAOs and shaders -- setup window points VBO, VAOs and shaders
@@ -69,19 +68,12 @@ preloadRender = do
--bindVertexArrayObject $= Just winColVAOname --bindVertexArrayObject $= Just winColVAOname
winColVAOname <- mglCreateSingle glCreateVertexArrays winColVAOname <- mglCreateSingle glCreateVertexArrays
glBindVertexArray winColVAOname glBindVertexArray winColVAOname
setupVertexAttribPointer 0 4 8 0 setupVertexAttribPointer winColVAOname winVBOname 0 4 8 0
setupVertexAttribPointer 1 4 8 4 setupVertexAttribPointer winColVAOname winVBOname 1 4 8 4
let winColVAO = VAO{_vaoName = winColVAOname, _vaoVBO = winVBO} let winColVAO = VAO{_vaoName = winColVAOname, _vaoVBO = winVBO}
-- setup shape geometry/cap VBO and two VAOs -- setup shape geometry/cap VBO and two VAOs
--shEBOname <- genObjectName
shEBOptr <- mallocArray numDrawableElements shEBOptr <- mallocArray numDrawableElements
shEBOname <- mglCreateSingle glCreateBuffers shEBOname <- mglCreateSingle glCreateBuffers
--bindBuffer ElementArrayBuffer $= Just shEBOname
--bufferData ElementArrayBuffer
-- $= ( fromIntegral $ glushortSize * numDrawableElements
-- , nullPtr
-- , StreamDraw
-- )
glNamedBufferData glNamedBufferData
shEBOname shEBOname
( fromIntegral $ glushortSize * numDrawableElements) ( fromIntegral $ glushortSize * numDrawableElements)
@@ -91,29 +83,30 @@ preloadRender = do
shVBOname <- mglCreateSingle glCreateBuffers shVBOname <- mglCreateSingle glCreateBuffers
shVBOptr <- mallocArray (7 * numDrawableElements) shVBOptr <- mallocArray (7 * numDrawableElements)
glBindBuffer GL_ARRAY_BUFFER shVBOname glBindBuffer GL_ARRAY_BUFFER shVBOname
bufferData ArrayBuffer glNamedBufferData shVBOname
$= ( fromIntegral $ floatSize * numDrawableElements * 7 ( fromIntegral $ floatSize * numDrawableElements * 7)
, nullPtr nullPtr
, StreamDraw GL_STREAM_DRAW
)
let shVBO = VBO{_vboName = shVBOname, _vboPtr = shVBOptr, _vboAttribSizes = [4, 4], _vboStride = 8} let shVBO = VBO{_vboName = shVBOname, _vboPtr = shVBOptr, _vboAttribSizes = [4, 4], _vboStride = 8}
--shPosColVAOname <- genObjectName --shPosColVAOname <- genObjectName
--bindVertexArrayObject $= Just shPosColVAOname --bindVertexArrayObject $= Just shPosColVAOname
shPosColVAOname <- mglCreateSingle glCreateVertexArrays shPosColVAOname <- mglCreateSingle glCreateVertexArrays
glBindVertexArray shPosColVAOname glBindVertexArray shPosColVAOname
glBindBuffer GL_ARRAY_BUFFER shVBOname glBindBuffer GL_ARRAY_BUFFER shVBOname
setupVertexAttribPointer 0 3 7 0 setupVertexAttribPointer shPosColVAOname shVBOname 0 3 7 0
setupVertexAttribPointer 1 4 7 3 setupVertexAttribPointer shPosColVAOname shVBOname 1 4 7 3
--bindBuffer ElementArrayBuffer $= Just shEBOname --bindBuffer ElementArrayBuffer $= Just shEBOname
glBindBuffer GL_ELEMENT_ARRAY_BUFFER shEBOname --glBindBuffer GL_ELEMENT_ARRAY_BUFFER shEBOname
glVertexArrayElementBuffer shPosColVAOname shEBOname
--shPosVAOname <- genObjectName --shPosVAOname <- genObjectName
--bindVertexArrayObject $= Just shPosVAOname --bindVertexArrayObject $= Just shPosVAOname
shPosVAOname <- mglCreateSingle glCreateVertexArrays shPosVAOname <- mglCreateSingle glCreateVertexArrays
glBindVertexArray shPosVAOname glBindVertexArray shPosVAOname
glBindBuffer GL_ARRAY_BUFFER shVBOname glBindBuffer GL_ARRAY_BUFFER shVBOname
setupVertexAttribPointer 0 3 7 0 setupVertexAttribPointer shPosVAOname shVBOname 0 3 7 0
--bindBuffer ElementArrayBuffer $= Just shEBOname --bindBuffer ElementArrayBuffer $= Just shEBOname
glBindBuffer GL_ELEMENT_ARRAY_BUFFER shEBOname --glBindBuffer GL_ELEMENT_ARRAY_BUFFER shEBOname
glVertexArrayElementBuffer shPosVAOname shEBOname
let shPosColVAO = VAO{_vaoName = shPosColVAOname, _vaoVBO = shVBO} let shPosColVAO = VAO{_vaoName = shPosColVAOname, _vaoVBO = shVBO}
shPosVAO = VAO{_vaoName = shPosVAOname, _vaoVBO = shVBO} shPosVAO = VAO{_vaoName = shPosVAOname, _vaoVBO = shVBO}
--setup silhouette edge VAO --setup silhouette edge VAO
@@ -122,12 +115,13 @@ preloadRender = do
shEdgeVAOname <- mglCreateSingle glCreateVertexArrays shEdgeVAOname <- mglCreateSingle glCreateVertexArrays
glBindVertexArray shEdgeVAOname glBindVertexArray shEdgeVAOname
glBindBuffer GL_ARRAY_BUFFER shVBOname glBindBuffer GL_ARRAY_BUFFER shVBOname
setupVertexAttribPointer 0 3 7 0 setupVertexAttribPointer shEdgeVAOname shVBOname 0 3 7 0
--setup ebo for silhouette edges --setup ebo for silhouette edges
silEBOname <- mglCreateSingle glCreateBuffers silEBOname <- mglCreateSingle glCreateBuffers
silEBOptr <- mallocArray numDrawableElements silEBOptr <- mallocArray numDrawableElements
-- it may be important to bind this while the correct VAO is bound -- it may be important to bind this while the correct VAO is bound
glBindBuffer GL_ELEMENT_ARRAY_BUFFER silEBOname --bindBuffer ElementArrayBuffer $= Just silEBOname --glBindBuffer GL_ELEMENT_ARRAY_BUFFER silEBOname --bindBuffer ElementArrayBuffer $= Just silEBOname
glVertexArrayElementBuffer shEdgeVAOname silEBOname
--bindBuffer ElementArrayBuffer $= Just silEBOname --bindBuffer ElementArrayBuffer $= Just silEBOname
glNamedBufferData glNamedBufferData
silEBOname silEBOname
+20 -12
View File
@@ -14,7 +14,13 @@ import Foreign
import Foreign.C.String import Foreign.C.String
import GLHelp import GLHelp
import Graphics.GL.Core45 import Graphics.GL.Core45
import Graphics.Rendering.OpenGL hiding (Point, imageHeight, scale, translate) import Graphics.Rendering.OpenGL (vertexAttribPointer, AttribLocation (..), ($=)
, IntegerHandling (..)
, VertexArrayDescriptor (..)
, DataType (..)
, vertexAttribArray
, Capability (..)
)
import Shader.Data import Shader.Data
import Shader.Parameters import Shader.Parameters
@@ -132,28 +138,27 @@ setupVAOSized :: Int -> [Int] -> IO VAO
setupVAOSized ndraw sizes = do setupVAOSized ndraw sizes = do
vaoname <- mglCreateSingle glCreateVertexArrays vaoname <- mglCreateSingle glCreateVertexArrays
glBindVertexArray vaoname glBindVertexArray vaoname
theVBO <- setupVBOSized ndraw sizes theVBO <- setupVBOSized ndraw vaoname sizes
return $ return $
VAO VAO
{ _vaoName = vaoname { _vaoName = vaoname
, _vaoVBO = theVBO , _vaoVBO = theVBO
} }
setupVBOSized :: Int -> [Int] -> IO VBO setupVBOSized :: Int -> GLuint -> [Int] -> IO VBO
setupVBOSized ndraw sizes = do setupVBOSized ndraw vao sizes = do
--vboName <- genObjectName --vboName <- genObjectName
--bindBuffer ArrayBuffer $= Just vboName --bindBuffer ArrayBuffer $= Just vboName
vboname <- mglCreateSingle glCreateBuffers vboname <- mglCreateSingle glCreateBuffers
glBindBuffer GL_ARRAY_BUFFER vboname glBindBuffer GL_ARRAY_BUFFER vboname
forM_ (zip3 [0 ..] sizes offs) $ \(loc, siz, off) -> do forM_ (zip3 [0 ..] sizes offs) $ \(loc, siz, off) -> do
setupVertexAttribPointer loc siz strd off setupVertexAttribPointer vao vboname loc siz strd off
thePtr <- mallocArray (strd * ndraw) thePtr <- mallocArray (strd * ndraw)
-- Allocate space -- Allocate space
bufferData ArrayBuffer glNamedBufferData vboname
$= ( fromIntegral $ floatSize * ndraw * strd ( fromIntegral $ floatSize * ndraw * strd)
, nullPtr nullPtr
, StreamDraw GL_STREAM_DRAW
)
return $ return $
VBO VBO
{ _vboName = vboname { _vboName = vboname
@@ -167,7 +172,10 @@ setupVBOSized ndraw sizes = do
-- | Assumes the correct VBO is bound -- | Assumes the correct VBO is bound
setupVertexAttribPointer :: setupVertexAttribPointer ::
-- | Atrib location GLuint ->
-- | vao name
GLuint ->
-- | vbo name
Int -> Int ->
-- | Size -- | Size
Int -> Int ->
@@ -176,7 +184,7 @@ setupVertexAttribPointer ::
-- | Offset -- | Offset
Int -> Int ->
IO () IO ()
setupVertexAttribPointer loc siz strd off = do setupVertexAttribPointer vao vbo loc siz strd off = do
vertexAttribPointer (AttribLocation (fi loc)) vertexAttribPointer (AttribLocation (fi loc))
$= (ToFloat, VertexArrayDescriptor (fi' siz) Float (fi'' $ floatSize * strd) (bufferOffset off)) $= (ToFloat, VertexArrayDescriptor (fi' siz) Float (fi'' $ floatSize * strd) (bufferOffset off))
vertexAttribArray (AttribLocation (fi loc)) $= Enabled vertexAttribArray (AttribLocation (fi loc)) $= Enabled