Fixed memory read issues

Templated load function
This commit is contained in:
Xiao Qi 2023-02-25 19:18:35 +08:00
parent d94e81231c
commit bc8347a161
5 changed files with 36 additions and 48 deletions

View File

@ -16,7 +16,7 @@ typedef std::filesystem::path AssetPath;
enum class BUFFER_TARGET : int
{
ARRAY_BUFFER = 3496,
ARRAY_BUFFER = 34962,
ELEMENT_ARRAY_BUFFER = 34963
};

View File

@ -48,6 +48,8 @@ namespace SH_COMP
static inline void ProcessModel(ModelData const&, ModelRef asset) noexcept;
static inline void BuildHeaders(ModelRef asset) noexcept;
template<typename T>
static void FetchData(int accessorID, std::vector<T>& dst);
public:
static void LoadAndCompile(AssetPath path) noexcept;
};

View File

@ -24,6 +24,7 @@
#include <fstream>
#include <iostream>
#include <algorithm>
#include "tiny_gltf.h"
#include <map>
@ -82,54 +83,25 @@ namespace SH_COMP
try
{
//meshIn.vertexPosition = FetchData<SHVec3>(primitive.attributes.at(ATT_POSITION.data()));
//meshIn.vertexNormal = FetchData<SHVec3>(primitive.attributes.at(ATT_NORMAL.data()));
//meshIn.vertexTangent = FetchData<SHVec3>(primitive.attributes.at(ATT_TANGENT.data()));
//meshIn.texCoords = FetchData<SHVec2>(primitive.attributes.at(ATT_TEXCOORD.data()));
FetchData(primitive.attributes.at(ATT_POSITION.data()), meshIn.vertexPosition);
FetchData(primitive.attributes.at(ATT_NORMAL.data()), meshIn.vertexNormal);
FetchData(primitive.attributes.at(ATT_TEXCOORD.data()), meshIn.texCoords);
auto accessor = &(*accessors)[primitive.attributes.at(ATT_POSITION.data())];
auto view = &(*bufferViews)[accessor->bufferView];
meshIn.vertexPosition.resize(accessor->count);
std::memcpy(
meshIn.vertexPosition.data(),
buffer + view->byteOffset,
view->byteLength
);
std::vector<unsigned short> indices_ushort;
FetchData(primitive.indices, indices_ushort);
meshIn.indices.resize(indices_ushort.size());
std::ranges::copy(indices_ushort, meshIn.indices.begin());
accessor = &(*accessors)[primitive.indices];
view = &(*bufferViews)[accessor->bufferView];
meshIn.indices.resize(accessor->count);
std::memcpy(
meshIn.indices.data(),
buffer + view->byteOffset,
view->byteLength
);
accessor = &(*accessors)[primitive.attributes.at(ATT_NORMAL.data())];
view = &(*bufferViews)[accessor->bufferView];
meshIn.vertexNormal.resize(accessor->count);
std::memcpy(
meshIn.vertexNormal.data(),
buffer + view->byteOffset,
view->byteLength
);
accessor = &(*accessors)[primitive.attributes.at(ATT_TEXCOORD.data())];
view = &(*bufferViews)[accessor->bufferView];
meshIn.texCoords.resize(accessor->count);
std::memcpy(
meshIn.texCoords.data(),
buffer + view->byteOffset,
view->byteLength
);
accessor = &(*accessors)[primitive.attributes.at(ATT_TANGENT.data())];
view = &(*bufferViews)[accessor->bufferView];
meshIn.vertexTangent.resize(accessor->count);
std::memcpy(
meshIn.vertexTangent.data(),
buffer + view->byteOffset,
view->byteLength
std::vector<SHVec4> intermediate;
FetchData(primitive.attributes.at(ATT_TANGENT.data()), intermediate);
meshIn.vertexTangent.resize(intermediate.size());
std::ranges::transform(
intermediate,
meshIn.vertexTangent.begin(),
[](auto const& inTan)
{
return SHVec3{ inTan.x, inTan.y, inTan.z };
}
);
}
catch (std::out_of_range e)
@ -141,6 +113,20 @@ namespace SH_COMP
}
template <typename T>
void MeshCompiler::FetchData(int accessorID, std::vector<T>& dst)
{
auto const& accessor = (*accessors)[accessorID];
auto const& view = (*bufferViews)[accessor.bufferView];
dst.resize(3484);
std::cout << "buffer line\n";
std::memcpy(
dst.data(),
buffer + view.byteOffset,
view.byteLength
);
}
inline void MeshCompiler::BuildHeaders(ModelRef asset) noexcept
{
// Mesh Headers

View File

@ -57,7 +57,7 @@ int main(int argc, char* argv[])
}
#else
SH_COMP::MeshCompiler::LoadAndCompile("racoon.gltf");
SH_COMP::MeshCompiler::LoadAndCompile("racoon_tiny.gltf");
#endif
return 0;