Permalink
Browse files

Made Geometry loading to the GPU as stable as possible by using

withMappedBuffer and copying everything directly to the GPU.

This should also increase the efficiency of loading data.

Currently geometric entites are assumed to be stored as:
[[vertices],[colours]] and the offsets reflect this. Since this
works, we now know how to load contiguous and composite data
to the GPU and use it effectively.

Later we will require something like this:
[[vertices],[texcoords],[normals]] using the same methodology.

Signed-off-by: Edward Tate <cmalune@gmail.com>
  • Loading branch information...
1 parent e97faf5 commit 58bcf12e5d85b00183163eac2bbed19bcde35b41 Edward Tate committed Jul 24, 2011
Showing with 22 additions and 10 deletions.
  1. +0 −3 Main.hs
  2. +17 −4 Resurrection/GL/VertexBuffer.hs
  3. +4 −2 Resurrection/Geometry.hs
  4. +1 −1 Resurrection/Geometry/Triangle.hs
View
@@ -120,9 +120,6 @@ main = do
newIORef 0.0 <*>
newIORef 0.0
- GL.lighting $= GL.Enabled
- GL.ambient (GL.Light 0) $= GL.Color4 (0.75 :: GL.GLfloat) (0.75 :: GL.GLfloat) (0.75 :: GL.GLfloat) (0.75 :: GL.GLfloat)
- GL.light (GL.Light 0) $= GL.Enabled
GL.clearColor $= GL.Color4 0.5 0.5 0.8 0.5
GLUT.displayCallback $= render windowState worldState font
@@ -10,25 +10,38 @@ import Graphics.Rendering.OpenGL
import Foreign.ForeignPtr
import Foreign.Ptr
import Foreign.Storable
+import Foreign.Marshal.Array
+import Foreign.Marshal.Utils
import Data.Array.Storable
------------------------------------------------------------------------------
-- |
-makeBuffer target elts =
+makeBuffer target elts =
makeBufferLen target (length elts) elts
------------------------------------------------------------------------------
-- |
+allocateBufferSpace target len size = do
+ let n = fromIntegral $ len * size
+ bufferData target $= (n, offset0, StaticDraw)
+
+
+------------------------------------------------------------------------------
+-- |
makeBufferLen target len elts =
do [buffer] <- genObjectNames 1
bindBuffer target $= Just buffer
- let n = fromIntegral $ len * sizeOf (head elts)
+ allocateBufferSpace target len $ sizeOf (head elts)
arr <- newListArray (0, len - 1) elts
- withStorableArray arr $ \ ptr ->
- bufferData target $= (n, ptr, StaticDraw)
+ withStorableArray arr $ \ ptr -> do
+ withMappedBuffer target WriteOnly (mapData ptr len) handleError
return buffer
+ where mapData fromPtr len' toPtr = do
+ copyArray toPtr fromPtr (fromIntegral len')
+ handleError err = do
+ putStrLn (show err)
------------------------------------------------------------------------------
@@ -33,8 +33,10 @@ bindGeometry g =
vertexAttribArray (geometryVertexAttrib g) $= Enabled
vertexAttribPointer (geometryColourAttrib g) $= (ToFloat, colourVad)
vertexAttribArray (geometryColourAttrib g) $= Enabled
- where vertexOffset = offset0
- colourOffset = offsetPtr (0 + (geometryNVertices g))
+ where len = fromIntegral $ geometryNVertices g
+ vertexOffset = offset0
+ colourOffset = offsetPtr (fromIntegral $ (sizeOf (undefined::GLfloat)) * len)
+
------------------------------------------------------------------------------
@@ -28,7 +28,7 @@ triangleColours =
------------------------------------------------------------------------------
-- |
triangleIndices :: [GLuint]
-triangleIndices = [0..3]
+triangleIndices = [0..2]
------------------------------------------------------------------------------

0 comments on commit 58bcf12

Please sign in to comment.