load mesh from OBJ file

This commit is contained in:
nek0 2022-12-17 07:39:45 +01:00
parent 597870d957
commit c1d59c12f4
4 changed files with 190932 additions and 2 deletions

1
.gitattributes vendored Normal file
View File

@ -0,0 +1 @@
assets/ filter=lfs diff=lfs merge=lfs -text

190890
assets/cat_mask_scaled.obj Normal file

File diff suppressed because it is too large Load Diff

View File

@ -80,6 +80,8 @@ initVulkan window = do
allocator <- initAllocator vulkanPhysicalDevice vulkanLogicalDevice vulkanInstance
mesh <- uploadMesh loadMeshes allocator
maskMesh <- loadFromObj "./assets/cat_mask_scaled.obj" allocator
dimensions <- liftIO $ SDL.windowInitialSize <$> SDL.getWindowConfig window
(swapchain, surfaceFormat) <-
createSwapchain vulkanSurface surfaceFormats dimensions vulkanLogicalDevice
@ -147,5 +149,6 @@ initVulkan window = do
inFlightFence
imageAvailableSemaphore
renderFinishedSemaphore
mesh
maskMesh
maskMesh
allocator

View File

@ -71,5 +71,41 @@ uploadMesh vertices allocator = do
loadFromObj
:: (MonadResource m, MonadFail m)
=> FilePath
-> VMA.Allocator
-> m Mesh
loadFromObj filepath = undefined
loadFromObj filepath vma = do
eitherObj <- liftIO $ fromFile filepath
case eitherObj of
Left err ->
error ("loadFromObj: loading mesh data: " <> err)
Right obj -> do
let vertices = digestFaces (objFaces obj) (objLocations obj)
uploadMesh vertices vma
where
digestFaces :: V.Vector (Element Face) -> V.Vector Location -> V.Vector Vertex
digestFaces faces locations =
V.foldl
(\acc (Element _ _ _ _ (Face fa fb fc others)) ->
if not (null others)
then
error "loadFromObj: Non-triangular face detected"
else
let newVertices = V.map
(\faceIndex ->
let position = faceIndexToPosition faceIndex locations
normal = V3 0 0 0
color = V4 1 1 0 1
in
Vertex position normal color
)
(V.fromList [ fa, fb, fc ])
in
acc V.++ newVertices
)
V.empty
faces
faceIndexToPosition :: FaceIndex -> V.Vector Location -> V3 Float
faceIndexToPosition index locations =
let Location x y z _ = locations V.! (faceLocIndex index - 1)
in
V3 x y z