Working on gltf loading and debug gui for gltf assets

simple_math_single_header
Martin Felis 2018-03-22 23:05:45 +01:00
parent a51c1936f2
commit ed18691b1a
13 changed files with 2909 additions and 15 deletions

2
.gitignore vendored
View File

@ -1,6 +1,8 @@
.*.swp .*.swp
build/* build/*
cmake-build-debug/**
cmake-build-release/**
tags tags
3rdparty/glfw/docs/** 3rdparty/glfw/docs/**

BIN
data/models/Cube/Cube.bin Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.8 KiB

193
data/models/Cube/Cube.gltf Normal file
View File

@ -0,0 +1,193 @@
{
"accessors" : [
{
"bufferView" : 0,
"byteOffset" : 0,
"componentType" : 5123,
"count" : 36,
"max" : [
35
],
"min" : [
0
],
"type" : "SCALAR"
},
{
"bufferView" : 1,
"byteOffset" : 0,
"componentType" : 5126,
"count" : 36,
"max" : [
1.000000,
1.000000,
1.000001
],
"min" : [
-1.000000,
-1.000000,
-1.000000
],
"type" : "VEC3"
},
{
"bufferView" : 2,
"byteOffset" : 0,
"componentType" : 5126,
"count" : 36,
"max" : [
1.000000,
1.000000,
1.000000
],
"min" : [
-1.000000,
-1.000000,
-1.000000
],
"type" : "VEC3"
},
{
"bufferView" : 3,
"byteOffset" : 0,
"componentType" : 5126,
"count" : 36,
"max" : [
1.000000,
-0.000000,
-0.000000,
1.000000
],
"min" : [
0.000000,
-0.000000,
-1.000000,
-1.000000
],
"type" : "VEC4"
},
{
"bufferView" : 4,
"byteOffset" : 0,
"componentType" : 5126,
"count" : 36,
"max" : [
1.000000,
1.000000
],
"min" : [
-1.000000,
-1.000000
],
"type" : "VEC2"
}
],
"asset" : {
"generator" : "VKTS glTF 2.0 exporter",
"version" : "2.0"
},
"bufferViews" : [
{
"buffer" : 0,
"byteLength" : 72,
"byteOffset" : 0,
"target" : 34963
},
{
"buffer" : 0,
"byteLength" : 432,
"byteOffset" : 72,
"target" : 34962
},
{
"buffer" : 0,
"byteLength" : 432,
"byteOffset" : 504,
"target" : 34962
},
{
"buffer" : 0,
"byteLength" : 576,
"byteOffset" : 936,
"target" : 34962
},
{
"buffer" : 0,
"byteLength" : 288,
"byteOffset" : 1512,
"target" : 34962
}
],
"buffers" : [
{
"byteLength" : 1800,
"uri" : "Cube.bin"
}
],
"images" : [
{
"uri" : "Cube_BaseColor.png"
},
{
"uri" : "Cube_MetallicRoughness.png"
}
],
"materials" : [
{
"name" : "Cube",
"pbrMetallicRoughness" : {
"baseColorTexture" : {
"index" : 0
},
"metallicRoughnessTexture" : {
"index" : 1
}
}
}
],
"meshes" : [
{
"name" : "Cube",
"primitives" : [
{
"attributes" : {
"NORMAL" : 2,
"POSITION" : 1,
"TANGENT" : 3,
"TEXCOORD_0" : 4
},
"indices" : 0,
"material" : 0,
"mode" : 4
}
]
}
],
"nodes" : [
{
"mesh" : 0,
"name" : "Cube"
}
],
"samplers" : [
{}
],
"scene" : 0,
"scenes" : [
{
"nodes" : [
0
]
}
],
"textures" : [
{
"sampler" : 0,
"source" : 0
},
{
"sampler" : 0,
"source" : 1
}
]
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 871 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 319 B

View File

@ -0,0 +1,4 @@
License: Donated by Norbert Nopper for glTF testing.
https://github.com/KhronosGroup/glTF-Sample-Models/tree/master/2.0/Cube

View File

@ -0,0 +1,8 @@
# Rigged Figure
## Screenshot
![screenshot](screenshot/screenshot.gif)
## License Information
Donated by [Cesium](http://cesiumjs.org/) for glTF testing.

File diff suppressed because it is too large Load Diff

Binary file not shown.

View File

@ -10,9 +10,6 @@
using namespace SimpleMath::GL; using namespace SimpleMath::GL;
typedef tinygltf::Model Model;
typedef tinygltf::TinyGLTF GLTFLoader;
struct Renderer; struct Renderer;
float moving_factor = 1.0f; float moving_factor = 1.0f;
@ -64,8 +61,7 @@ VertexArrayMesh gXZPlaneMesh;
VertexArrayMesh gUnitCubeMesh; VertexArrayMesh gUnitCubeMesh;
VertexArrayMesh gScreenQuad; VertexArrayMesh gScreenQuad;
Model gModel; AssetFile gAssetFile;
GLTFLoader gLoader;
// //
// Module // Module
@ -147,7 +143,7 @@ static void module_unload(struct module_state *state, void* write_serializer) {
static bool module_step(struct module_state *state, float dt) { static bool module_step(struct module_state *state, float dt) {
int width, height; int width, height;
assert (gWindow != nullptr); assert (gWindow != nullptr);
state->renderer->RenderGui(); state->renderer->DrawGui();
state->renderer->RenderGl(); state->renderer->RenderGl();
glBindFramebuffer(GL_FRAMEBUFFER, 0); glBindFramebuffer(GL_FRAMEBUFFER, 0);
@ -396,13 +392,8 @@ void Renderer::Initialize(int width, int height) {
mLight.mShadowMapTarget.mLinearizeDepthProgram.RegisterFileModification(); mLight.mShadowMapTarget.mLinearizeDepthProgram.RegisterFileModification();
// Model // Model
std::string model_file = "data/models/Cube/Cube.gltf"; if (!gAssetFile.Load("data/models/RiggedFigure/glTF/RiggedFigure.gltf")) {
std::string err; assert(false);
bool result = gLoader.LoadASCIIFromFile(&gModel, &err, model_file.c_str());
if (!err.empty()) {
gLog("Error loading model '%s': %s", model_file.c_str(), err.c_str());
} else {
gLog("Successfully loaded model '%s'", model_file.c_str());
} }
} }
@ -569,7 +560,7 @@ void Renderer::RenderScene(RenderProgram &program, const Camera& camera) {
gXZPlaneMesh.Draw(GL_TRIANGLES); gXZPlaneMesh.Draw(GL_TRIANGLES);
} }
void Renderer::RenderGui() { void Renderer::DrawGui() {
if (ImGui::BeginDock("Scene")) { if (ImGui::BeginDock("Scene")) {
ImGui::Checkbox("Draw Depth", &mSettings->DrawDepth); ImGui::Checkbox("Draw Depth", &mSettings->DrawDepth);
@ -615,5 +606,10 @@ void Renderer::RenderGui() {
); );
} }
ImGui::EndDock(); ImGui::EndDock();
if (ImGui::BeginDock("Asset")) {
gAssetFile.DrawGui();
}
ImGui::EndDock();
} }

View File

@ -83,5 +83,5 @@ struct Renderer {
void Shutdown(); void Shutdown();
void RenderGl(); void RenderGl();
void RenderScene(RenderProgram &program, const Camera& camera); void RenderScene(RenderProgram &program, const Camera& camera);
void RenderGui(); void DrawGui();
}; };

View File

@ -15,6 +15,10 @@
using namespace SimpleMath; using namespace SimpleMath;
using namespace SimpleMath::GL; using namespace SimpleMath::GL;
typedef tinygltf::TinyGLTF GLTFLoader;
static GLTFLoader gLoader;
// //
// Camera // Camera
// //
@ -501,6 +505,7 @@ void Texture::MakeGrid(const int& size, const Vector3f &c1, const Vector3f &c2)
bool Texture::Load(const char* filename, int num_components) { bool Texture::Load(const char* filename, int num_components) {
// unsigned char* rgb = stbi_load(filename, &mWidth, &mHeight, num_components); // unsigned char* rgb = stbi_load(filename, &mWidth, &mHeight, num_components);
assert(false); assert(false);
return false;
} }
VertexArray::~VertexArray() { VertexArray::~VertexArray() {
@ -698,3 +703,62 @@ void VertexArrayMesh::Draw(GLenum mode) {
} }
} }
//
// AssetFile
//
bool AssetFile::Load(const char* filename) {
mFilename = filename;
std::string err;
bool result = gLoader.LoadASCIIFromFile(&mGLTFModel, &err, mFilename.c_str());
if (!err.empty()) {
gLog("Error loading model '%s': %s", mFilename.c_str(), err.c_str());
} else {
gLog("Successfully loaded model '%s'", mFilename.c_str());
}
return result;
}
void AssetFile::DrawNodeGui(const tinygltf::Node& node) {
for (int i = 0, n = node.children.size(); i < n; ++i) {
const int child_node_id = node.children[i];
const tinygltf::Node& child_node = mGLTFModel.nodes[child_node_id];
ImGui::PushID("childnode");
ImGui::PushID("i");
if (ImGui::TreeNode((void*)(intptr_t)child_node_id, "[%d] %s", child_node_id, child_node.name.c_str())) {
DrawNodeGui(child_node);
ImGui::TreePop();
}
ImGui::PopID();
ImGui::PopID();
}
}
void AssetFile::DrawGui() {
ImGui::Text("File: %s", mFilename.c_str());
if (ImGui::TreeNode("Meshes")) {
for (int i = 0, n = mGLTFModel.meshes.size(); i < n; ++i) {
const tinygltf::Mesh& mesh = mGLTFModel.meshes[i];
ImGui::PushID("mesh");
if (ImGui::TreeNode((void*)(intptr_t)i, "[%d] %s", i, mesh.name.c_str())) {
ImGui::TreePop();
}
ImGui::PopID();
}
ImGui::TreePop();
}
if (ImGui::TreeNode("Nodes")) {
for (int i = 0, n = mGLTFModel.nodes.size(); i < n; ++i) {
const tinygltf::Node& node = mGLTFModel.nodes[i];
ImGui::PushID("node");
if (ImGui::TreeNode((void*)(intptr_t)i, "[%d] %s", i, node.name.c_str())) {
DrawNodeGui(node);
ImGui::TreePop();
}
ImGui::PopID();
}
ImGui::TreePop();
}
}

View File

@ -415,6 +415,15 @@ struct VertexArrayMesh {
void Draw(GLenum mode); void Draw(GLenum mode);
}; };
struct AssetFile {
std::string mFilename;
tinygltf::Model mGLTFModel;
bool Load(const char* filename);
void DrawNodeGui(const tinygltf::Node& node);
void DrawGui();
};
struct RenderCommand { struct RenderCommand {
typedef enum { typedef enum {
EnableShadowPass = 1 EnableShadowPass = 1