10/12/2023

This commit is contained in:
Bailey Harrison 2023-12-10 20:57:47 +00:00
parent 7aca66ff06
commit 0b911d11d6
6 changed files with 2123 additions and 29 deletions

View File

@ -29,6 +29,8 @@ set(SRC_FILES
"src/imgui/imstb_textedit.h" "src/imgui/imstb_textedit.h"
"src/input_manager.cpp" "src/input_manager.cpp"
"src/libs/json.hpp" "src/libs/json.hpp"
"src/libs/mikktspace.c"
"src/libs/mikktspace.h"
"src/libs/stb_impl.cpp" "src/libs/stb_impl.cpp"
"src/libs/tiny_gltf.h" "src/libs/tiny_gltf.h"
"src/libs/tiny_gltf_impl.cpp" "src/libs/tiny_gltf_impl.cpp"

View File

@ -231,7 +231,7 @@ void Application::GameLoop()
ImGui::NewFrame(); ImGui::NewFrame();
if (debug_menu_state.menu_active) { if (debug_menu_state.menu_active) {
if (ImGui::Begin("debugMenu")) { if (ImGui::Begin("debugMenu", 0)) {
ImGui::Text("Test!"); ImGui::Text("Test!");
ImGui::Text("FPS: %.3f", std::roundf(avg_fps)); ImGui::Text("FPS: %.3f", std::roundf(avg_fps));
} }

1899
src/libs/mikktspace.c Normal file

File diff suppressed because it is too large Load Diff

145
src/libs/mikktspace.h Normal file
View File

@ -0,0 +1,145 @@
/** \file mikktspace/mikktspace.h
* \ingroup mikktspace
*/
/**
* Copyright (C) 2011 by Morten S. Mikkelsen
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
*/
#ifndef __MIKKTSPACE_H__
#define __MIKKTSPACE_H__
#ifdef __cplusplus
extern "C" {
#endif
/* Author: Morten S. Mikkelsen
* Version: 1.0
*
* The files mikktspace.h and mikktspace.c are designed to be
* stand-alone files and it is important that they are kept this way.
* Not having dependencies on structures/classes/libraries specific
* to the program, in which they are used, allows them to be copied
* and used as is into any tool, program or plugin.
* The code is designed to consistently generate the same
* tangent spaces, for a given mesh, in any tool in which it is used.
* This is done by performing an internal welding step and subsequently an order-independent evaluation
* of tangent space for meshes consisting of triangles and quads.
* This means faces can be received in any order and the same is true for
* the order of vertices of each face. The generated result will not be affected
* by such reordering. Additionally, whether degenerate (vertices or texture coordinates)
* primitives are present or not will not affect the generated results either.
* Once tangent space calculation is done the vertices of degenerate primitives will simply
* inherit tangent space from neighboring non degenerate primitives.
* The analysis behind this implementation can be found in my master's thesis
* which is available for download --> http://image.diku.dk/projects/media/morten.mikkelsen.08.pdf
* Note that though the tangent spaces at the vertices are generated in an order-independent way,
* by this implementation, the interpolated tangent space is still affected by which diagonal is
* chosen to split each quad. A sensible solution is to have your tools pipeline always
* split quads by the shortest diagonal. This choice is order-independent and works with mirroring.
* If these have the same length then compare the diagonals defined by the texture coordinates.
* XNormal which is a tool for baking normal maps allows you to write your own tangent space plugin
* and also quad triangulator plugin.
*/
typedef int tbool;
typedef struct SMikkTSpaceContext SMikkTSpaceContext;
typedef struct {
// Returns the number of faces (triangles/quads) on the mesh to be processed.
int (*m_getNumFaces)(const SMikkTSpaceContext * pContext);
// Returns the number of vertices on face number iFace
// iFace is a number in the range {0, 1, ..., getNumFaces()-1}
int (*m_getNumVerticesOfFace)(const SMikkTSpaceContext * pContext, const int iFace);
// returns the position/normal/texcoord of the referenced face of vertex number iVert.
// iVert is in the range {0,1,2} for triangles and {0,1,2,3} for quads.
void (*m_getPosition)(const SMikkTSpaceContext * pContext, float fvPosOut[], const int iFace, const int iVert);
void (*m_getNormal)(const SMikkTSpaceContext * pContext, float fvNormOut[], const int iFace, const int iVert);
void (*m_getTexCoord)(const SMikkTSpaceContext * pContext, float fvTexcOut[], const int iFace, const int iVert);
// either (or both) of the two setTSpace callbacks can be set.
// The call-back m_setTSpaceBasic() is sufficient for basic normal mapping.
// This function is used to return the tangent and fSign to the application.
// fvTangent is a unit length vector.
// For normal maps it is sufficient to use the following simplified version of the bitangent which is generated at pixel/vertex level.
// bitangent = fSign * cross(vN, tangent);
// Note that the results are returned unindexed. It is possible to generate a new index list
// But averaging/overwriting tangent spaces by using an already existing index list WILL produce INCRORRECT results.
// DO NOT! use an already existing index list.
void (*m_setTSpaceBasic)(const SMikkTSpaceContext * pContext, const float fvTangent[], const float fSign, const int iFace, const int iVert);
// This function is used to return tangent space results to the application.
// fvTangent and fvBiTangent are unit length vectors and fMagS and fMagT are their
// true magnitudes which can be used for relief mapping effects.
// fvBiTangent is the "real" bitangent and thus may not be perpendicular to fvTangent.
// However, both are perpendicular to the vertex normal.
// For normal maps it is sufficient to use the following simplified version of the bitangent which is generated at pixel/vertex level.
// fSign = bIsOrientationPreserving ? 1.0f : (-1.0f);
// bitangent = fSign * cross(vN, tangent);
// Note that the results are returned unindexed. It is possible to generate a new index list
// But averaging/overwriting tangent spaces by using an already existing index list WILL produce INCRORRECT results.
// DO NOT! use an already existing index list.
void (*m_setTSpace)(const SMikkTSpaceContext * pContext, const float fvTangent[], const float fvBiTangent[], const float fMagS, const float fMagT,
const tbool bIsOrientationPreserving, const int iFace, const int iVert);
} SMikkTSpaceInterface;
struct SMikkTSpaceContext
{
SMikkTSpaceInterface * m_pInterface; // initialized with callback functions
void * m_pUserData; // pointer to client side mesh data etc. (passed as the first parameter with every interface call)
};
// these are both thread safe!
tbool genTangSpaceDefault(const SMikkTSpaceContext * pContext); // Default (recommended) fAngularThreshold is 180 degrees (which means threshold disabled)
tbool genTangSpace(const SMikkTSpaceContext * pContext, const float fAngularThreshold);
// To avoid visual errors (distortions/unwanted hard edges in lighting), when using sampled normal maps, the
// normal map sampler must use the exact inverse of the pixel shader transformation.
// The most efficient transformation we can possibly do in the pixel shader is
// achieved by using, directly, the "unnormalized" interpolated tangent, bitangent and vertex normal: vT, vB and vN.
// pixel shader (fast transform out)
// vNout = normalize( vNt.x * vT + vNt.y * vB + vNt.z * vN );
// where vNt is the tangent space normal. The normal map sampler must likewise use the
// interpolated and "unnormalized" tangent, bitangent and vertex normal to be compliant with the pixel shader.
// sampler does (exact inverse of pixel shader):
// float3 row0 = cross(vB, vN);
// float3 row1 = cross(vN, vT);
// float3 row2 = cross(vT, vB);
// float fSign = dot(vT, row0)<0 ? -1 : 1;
// vNt = normalize( fSign * float3(dot(vNout,row0), dot(vNout,row1), dot(vNout,row2)) );
// where vNout is the sampled normal in some chosen 3D space.
//
// Should you choose to reconstruct the bitangent in the pixel shader instead
// of the vertex shader, as explained earlier, then be sure to do this in the normal map sampler also.
// Finally, beware of quad triangulations. If the normal map sampler doesn't use the same triangulation of
// quads as your renderer then problems will occur since the interpolated tangent spaces will differ
// eventhough the vertex level tangent spaces match. This can be solved either by triangulating before
// sampling/exporting or by using the order-independent choice of diagonal for splitting quads suggested earlier.
// However, this must be used both by the sampler and your tools/rendering pipeline.
#ifdef __cplusplus
}
#endif
#endif

View File

@ -3,14 +3,83 @@
#include "log.h" #include "log.h"
#include "util/files.h" #include "util/files.h"
#include "libs/mikktspace.h"
#include "libs/tiny_gltf.h" #include "libs/tiny_gltf.h"
#include "components/mesh_renderable.h" #include "components/mesh_renderable.h"
#include <components/transform.h>
namespace tg = tinygltf; namespace tg = tinygltf;
namespace engine::util { namespace engine::util {
static void DecomposeTransform(glm::mat4 transform, glm::vec3& pos, glm::quat& rot, glm::vec3& scale)
{
// get position
pos.x = transform[3][0];
pos.y = transform[3][1];
pos.z = transform[3][2];
// remove position from matrix
transform[3][0] = 0.0f;
transform[3][1] = 0.0f;
transform[3][2] = 0.0f;
// get scale
scale.x = sqrtf(transform[0][0] * transform[0][0] + transform[0][1] * transform[0][1] + transform[0][2] * transform[0][2]);
scale.y = sqrtf(transform[1][0] * transform[1][0] + transform[1][1] * transform[1][1] + transform[1][2] * transform[1][2]);
scale.z = sqrtf(transform[2][0] * transform[2][0] + transform[2][1] * transform[2][1] + transform[2][2] * transform[2][2]);
// remove scaling from matrix
for (int row = 0; row < 3; row++) {
transform[0][row] /= scale.x;
transform[1][row] /= scale.y;
transform[2][row] /= scale.z;
}
// get rotation
rot = glm::quat_cast(transform);
}
static glm::mat4 MatFromDoubleArray(const std::vector<double>& arr)
{
glm::mat4 mat{};
for (int i = 0; i < 4; ++i) {
mat[i][0] = static_cast<float>(arr[i * 4 + 0]);
mat[i][1] = static_cast<float>(arr[i * 4 + 1]);
mat[i][2] = static_cast<float>(arr[i * 4 + 2]);
mat[i][3] = static_cast<float>(arr[i * 4 + 3]);
}
return mat;
}
static void CreateNodes(engine::Scene& app_scene, const tg::Scene& gl_scene, const tg::Model& gl_model, engine::Entity parent_entity,
const tg::Node& node)
{
static int node_uuid = 0;
const glm::mat4 matrix = MatFromDoubleArray(node.matrix);
glm::vec3 pos;
glm::quat rot;
glm::vec3 scale;
DecomposeTransform(matrix, pos, rot, scale);
engine::Entity entity = app_scene.CreateEntity(std::string("test_node") + std::to_string(node_uuid), parent_entity, pos, rot, scale);
if (node.mesh >= 0) {
const tg::Mesh& mesh = gl_model.meshes.at(node.mesh);
const tg::Primitive& prim = mesh.primitives.front();
const tg::Accessor& indices_accessor = gl_model.accessors.at(prim.indices);
const tg::BufferView& indices_bufferview = gl_model.bufferViews.at(indices_accessor.bufferView);
}
for (const int node : node.children) {
CreateNodes(app_scene, gl_scene, gl_model, entity, gl_model.nodes.at(node));
}
++node_uuid;
}
engine::Entity LoadGLTF(Scene& scene, const std::string& path, bool isStatic) engine::Entity LoadGLTF(Scene& scene, const std::string& path, bool isStatic)
{ {
@ -45,31 +114,16 @@ engine::Entity LoadGLTF(Scene& scene, const std::string& path, bool isStatic)
int scene_index = 0; int scene_index = 0;
if (model.defaultScene != -1) scene_index = model.defaultScene; if (model.defaultScene != -1) scene_index = model.defaultScene;
tg::Scene& s = model.scenes.at(scene_index); const tg::Scene& s = model.scenes.at(scene_index);
if (s.nodes.size() < 1) { const Entity parent =
throw std::runtime_error("Need at least 1 node in the scene"); scene.CreateEntity("test_node", 0, glm::vec3{}, glm::quat{glm::one_over_root_two<float>(), glm::one_over_root_two<float>(), 0.0f, 0.0f});
for (int node : s.nodes) {
CreateNodes(scene, s, model, parent, model.nodes.at(node));
} }
const tg::Node& node = model.nodes.at(s.nodes[0]); return parent;
const Entity e = scene.CreateEntity("test_node", 0);
//const tg::Mesh& gt_mesh = model.meshes.at(0);
//std::vector<uint32_t> indices;
//model.buffers[0].
//auto mesh = std::make_unique<Mesh>(scene.app()->renderer()->GetDevice(), vertices, indices);
//auto mr = scene.AddComponent<MeshRenderableComponent>(e);
// if (node.mesh)
return static_cast<Entity>(0u);
} }
} // namespace engine::util } // namespace engine::util

View File

@ -184,12 +184,6 @@ void PlayGame(GameSettings settings)
wall_renderable->material->SetAlbedoTexture(albedo_texture); wall_renderable->material->SetAlbedoTexture(albedo_texture);
wall_renderable->material->SetNormalTexture(normal_texture); wall_renderable->material->SetNormalTexture(normal_texture);
auto custom = scene2->AddComponent<engine::CustomComponent>(wall2);
custom->onInit = []() {};
custom->onUpdate = [&](float dt) {
scene2->GetComponent<engine::TransformComponent>(pivot)->rotation *= glm::angleAxis(dt * 0.03f, glm::normalize(glm::vec3{0.0f, 0.0f, 1.0f}));
};
} }
{ /* light */ { /* light */