Skip to content
Snippets Groups Projects
Commit be910b5d authored by Christian Mattes's avatar Christian Mattes
Browse files

Added VRMesh loading from obj-files

parent 942ef177
No related branches found
No related tags found
No related merge requests found
......@@ -3,6 +3,14 @@
#include <iostream>
#include <zlib.h>
#include <assimp/Importer.hpp>
#include <assimp/mesh.h>
#include <assimp/postprocess.h>
#include <assimp/scene.h>
#include <assimp/vector3.h>
#include <lodepng/lodepng.h>
namespace lava {
namespace vr {
......@@ -122,5 +130,74 @@ VRMesh VRMesh::readFromFile(const std::string &filename) {
return result;
}
VRMesh VRMesh::readFromObj(const std::string &objfile,
const std::string &texture) {
VRMesh result;
Assimp::Importer importer;
const aiScene *importScene =
importer.ReadFile(objfile, AI_SCENE_FLAGS_NON_VERBOSE_FORMAT |
aiProcess_JoinIdenticalVertices);
if (importScene == NULL) {
std::cout << "VRMesh::readFromObj(...): no scene loaded." << std::endl
<< "Perhaps invalid filepath given? Filepath is:" << std::endl
<< objfile << std::endl;
assert(0);
}
if (importScene->HasMeshes()) {
aiMesh *theMesh = importScene->mMeshes[0];
auto uv = theMesh->mTextureCoords[0];
for (unsigned int i = 0; i < theMesh->mNumVertices; ++i) {
Vertex v;
v.position.x = theMesh->mVertices[i].x;
v.position.y = theMesh->mVertices[i].y;
v.position.z = theMesh->mVertices[i].z;
if (uv) {
v.texCoord.x = uv[i].x;
v.texCoord.y = 1.0 - uv[i].y;
}
v.layer = 0.0f;
result.vertices.push_back(v);
}
for (unsigned int i_f = 0; i_f < theMesh->mNumFaces; ++i_f) {
assert(theMesh->mFaces[i_f].mNumIndices == 3 &&
"Only triangles for now please");
for (unsigned int i_i = 0; i_i < 3; ++i_i) {
uint32_t currentVertexIndex =
(uint32_t)theMesh->mFaces[i_f].mIndices[i_i];
result.indices.push_back(currentVertexIndex);
}
}
if (!texture.empty()) {
result.colorTexture.meta.depth = 1;
result.colorTexture.meta.mipLayers = 1;
result.colorTexture.meta.bytes_per_pixel = 4;
lodepng::decode(result.colorTexture.data,
result.colorTexture.meta.width,
result.colorTexture.meta.height, texture, LCT_RGBA);
} else {
result.colorTexture.meta.width = 1;
result.colorTexture.meta.height = 1;
result.colorTexture.meta.depth = 1;
result.colorTexture.meta.mipLayers = 1;
result.colorTexture.meta.bytes_per_pixel = 4;
result.colorTexture.data = {0xff, 0xff, 0xff, 0xff};
}
} else {
std::cout
<< "Loading VRMesh from .obj-file failed (no mesh could be loaded)."
<< std::endl;
assert(0);
}
return result;
}
} // namespace vr
} // namespace lava
......@@ -50,6 +50,7 @@ struct VRMesh {
#endif
static VRMesh readFromFile(const std::string &filename);
static VRMesh readFromObj(const std::string &objfile, const std::string& texture = "");
struct OffsetHelper {
size_t width, height, depth, bytes_per_pixel, mip_layers;
......
......@@ -98,7 +98,9 @@ void VRMeshRenderer::upload(const VRMesh &mesh) {
auto meta = mesh.colorTexture.meta;
mTexture = lava::texture2DArray(meta.width, meta.height, meta.depth,
vk::Format::eBc7SrgbBlock)
meta.bytes_per_pixel == 1
? vk::Format::eBc7SrgbBlock
: vk::Format::eR8G8B8A8Srgb)
.setMipLevels(meta.mipLayers)
.create(device);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment