diff --git a/src/Importer/IQM/Parser.hs b/src/Importer/IQM/Parser.hs index 5112ece..a86fc40 100644 --- a/src/Importer/IQM/Parser.hs +++ b/src/Importer/IQM/Parser.hs @@ -33,7 +33,7 @@ import Foreign.Storable (sizeOf) import Prelude as P hiding (take, null) -import Render.Misc (printPtrAsFloatArray, printPtrAsUByteArray, printPtrAsWord32Array, withVBO) +import Render.Misc (printPtrAsFloatArray, printPtrAsUByteArray, printPtrAsWord32Array, withVBO, checkError) -- | helper-function for creating an integral out of [8-Bit Ints] _w8ToInt :: Integral a => a -> a -> a @@ -229,13 +229,16 @@ parseIQM a = let initBuffer :: AttribLocation -> IQMVertexArrayType -> [IQMVertexArray] -> IO () initBuffer l t vas = do - let (IQMVertexArray _ _ _ num _ dat) = case filter (\(IQMVertexArray ty _ _ _ _ _) -> ty == t) vas of + -- find array with type t, otherwise abort hard. + let (IQMVertexArray _ _ format num _ dat) = case filter (\(IQMVertexArray ty _ _ _ _ _) -> ty == t) vas of [b] -> b _ -> error $ "Current object does not support " ++ (show t) buf <- genObjectName + -- create buffer and write data withVBO buf (toBufferTargetfromVAType t) $ do -- copy data - bufferData (toBufferTargetfromVAType t) $= ((fromIntegral num),dat,StaticDraw) + bufferData (toBufferTargetfromVAType t) $= (fromIntegral num * (fromIntegral.vaSize) format,dat,StaticDraw) + checkError "bufferData vao" -- tell layout vertexAttribPointer l $= (ToFloat, VertexArrayDescriptor num Float 0 nullPtr) initBuffer (AttribLocation 0) IQMPosition va' @@ -251,7 +254,9 @@ parseIQM a = data' = skipDrop ((fromIntegral.ofs_triangles.bareheader) bare) byteLen f p <- mallocBytes byteLen unsafeUseAsCString data' (\s -> copyBytes p s byteLen) - bufferData ElementArrayBuffer $= (fromIntegral len*3, p, StaticDraw) + withVBO tbo ElementArrayBuffer $ do + bufferData ElementArrayBuffer $= (fromIntegral byteLen, p, StaticDraw) + checkError "bufferData tris" return $ castPtr p putStrLn "Triangles:" printPtrAsWord32Array tris ((*3).fromIntegral.num_triangles.bareheader $ bare) 3 diff --git a/src/Render/Render.hs b/src/Render/Render.hs index b7a16f1..b4fd059 100644 --- a/src/Render/Render.hs +++ b/src/Render/Render.hs @@ -290,11 +290,15 @@ renderIQM :: IQM -> L.V3 CFloat -> L.V3 CFloat -> IO () renderIQM m p@(L.V3 x y z) s@(L.V3 sx sy sz) = do withVAO (vertexArrayObject m) $ do vertexAttribArray (AttribLocation 0) $= Enabled + checkError "setting array to enabled" bindBuffer ElementArrayBuffer $= Just (triangleBufferObject m) + checkError "bindBuffer" let n = fromIntegral.num_triangles.header $ m --print $ concat ["drawing ", show n," triangles"] drawElements Triangles n UnsignedInt nullPtr - checkError "drawing model" + checkError "drawing model" + bindBuffer ElementArrayBuffer $= Nothing + checkError "unbind buffer" return () renderObject :: MapObject -> IO ()