From 2ebbf895eadd37445deaeb9325216a563e50d694 Mon Sep 17 00:00:00 2001 From: cinnaboot Date: Tue, 12 Oct 2021 14:51:25 -0400 Subject: [PATCH] replace assimp dependancy with TinyGLTF --- .gitmodules | 3 +++ Makefile | 34 +++------------------------ README | 14 +++-------- examples/Makefile | 11 ++------- examples/render_groups/main.cpp | 6 +++-- ext/tinygltf | 1 + include/asset.h | 41 +++++++++++++++++++++++++++++++++ include/mesh.h | 3 --- include/renderer.h | 35 +--------------------------- src/asset.cpp | 28 ++++++++++++++++++++++ src/mesh.cpp | 28 ++++++++++++++++++++++ src/renderer.cpp | 3 +-- 12 files changed, 115 insertions(+), 92 deletions(-) create mode 160000 ext/tinygltf create mode 100644 include/asset.h create mode 100644 src/asset.cpp diff --git a/.gitmodules b/.gitmodules index 4f320ec..9fdde73 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,3 +1,6 @@ [submodule "ext/stb_libs"] path = ext/stb_libs url = https://github.com/nothings/stb.git +[submodule "ext/tinygltf"] + path = ext/tinygltf + url = https://github.com/syoyo/tinygltf diff --git a/Makefile b/Makefile index 69bf5af..db68f31 100644 --- a/Makefile +++ b/Makefile @@ -4,7 +4,8 @@ CXX = g++ CXXFLAGS = -std=c++11 -g -ggdb3 -Wall \ -Iinclude \ -I/usr/include/SDL2 \ - -Iext/stb_libs + -Iext/stb_libs \ + -Iext/tinygltf EXAMPLEDIR = examples OBJDIR = build SRCDIR = src @@ -13,41 +14,13 @@ LIBNAME = libTangerine.a RENDER_SOURCES = $(wildcard $(SRCDIR)/*.cpp) RENDER_OBJECTS = $(patsubst $(SRCDIR)/%.cpp, $(OBJDIR)/%.o, $(RENDER_SOURCES)) -# NOTE: target to automatically build a current release of assimp. Fixes -# issue on Fedora where the default assimp version doesn't support glTF2 -ASSIMP_DIR=ext/assimp - -ifeq ($(USE_INTERNAL_ASSIMP), y) -CXXFLAGS += -I$(ASSIMP_DIR)/include -I$(ASSIMP_DIR)/build/include -ASSIMP_LIB := $(ASSIMP_DIR)/build/code/libassimp.a -CLEAN_ASSIMP_TARGET := $(MAKE) -C $(ASSIMP_DIR)/build clean -endif - all: mkdirs $(LIBNAME) -ifeq ($(USE_INTERNAL_ASSIMP), y) - $(MAKE) $(ASSIMP_LIB) -endif $(MAKE) examples .PHONY: all -include $(OBJDIR)/*.d -$(ASSIMP_LIB): - @if [ ! -d $(ASSIMP_DIR) ]; then \ - git clone -n "https://github.com/assimp/assimp" $(ASSIMP_DIR); \ - cd $(ASSIMP_DIR); \ - git checkout -q v5.0.1; \ - fi - - cmake -S $(ASSIMP_DIR) -B $(ASSIMP_DIR)/build \ - -DBUILD_SHARED_LIBS=OFF \ - -DASSIMP_BUILD_TOOLS=OFF -DASSIMP_BUILD_TESTS=OFF \ - -DASSIMP_BUILD_ALL_EXPORTERS_BY_DEFAULT=OFF \ - -DASSIMP_BUILD_ALL_IMPORTERS_BY_DEFAULT=OFF \ - -DASSIMP_BUILD_GLTF_IMPORTER=ON ./ - $(MAKE) -C $(ASSIMP_DIR)/build - mkdirs: @mkdir -p $(OBJDIR) .PHONY: mkdirs @@ -59,11 +32,10 @@ $(RENDER_OBJECTS): $(OBJDIR)/%.o : $(SRCDIR)/%.cpp $(CXX) $(CXXFLAGS) -c -MMD $< -o $@ examples: - ASSIMP_DIR=$(ASSIMP_DIR) $(MAKE) -C $(EXAMPLEDIR) + $(MAKE) -C $(EXAMPLEDIR) .PHONY: examples clean: rm -rf $(OBJDIR)/* $(MAKE) clean -C $(EXAMPLEDIR) - $(CLEAN_ASSIMP_TARGET) .PHONY: clean diff --git a/README b/README index 5a37d11..4232edf 100644 --- a/README +++ b/README @@ -18,20 +18,18 @@ TODO: update camera to use quaternions for rotation Dependencies: - assimp >= 5.0.0 glew glm SDL2 stb_image + TinyGLTF Build Instructions: install dependencies: Arch) - $ sudo pacman -S assimp glew glm sd2 catch2 gcc make + $ sudo pacman -S glew glm sdl2 gcc make Fedora) - NOTE: Fedora (33) see note below about building assimp - $ sudo dnf install glew-devel glm-devel catch-devel SDL2-devel \ - gcc-c++ make cmake + $ sudo dnf install glew-devel glm-devel SDL2-devel gcc-c++ make checkout, and initialize submodules: (stb_image) $ git submodule init $ git submodule update @@ -43,9 +41,3 @@ Build Instructions: examples will be in examples/bin - NOTE: if build fails with errors from assimp or mesh.cpp, you can build - a recent version of assimp that supports glTF2 - https://en.wikipedia.org/wiki/GlTF. Requires cmake - - $ make build_assimp && USE_INTERNAL_ASSIMP=y make - diff --git a/examples/Makefile b/examples/Makefile index 5fbaee2..5ca0470 100644 --- a/examples/Makefile +++ b/examples/Makefile @@ -2,17 +2,11 @@ SHELL = /bin/sh CXX = g++ CXXFLAGS = -std=c++11 -g -ggdb3 -Wall -I../include -I/usr/include/SDL2 -LDFLAGS = -lSDL2 -lGLEW -lGL -lassimp +LDFLAGS = -lSDL2 -lGLEW -lGL OBJDIR = build LIB = ../build/libTangerine.a BINDIR = bin -ifeq ($(USE_INTERNAL_ASSIMP), y) -CXXFLAGS += -I../$(ASSIMP_DIR)/include -I../$(ASSIMP_DIR)/build/include -LDFLAGS = -lSDL2 -lGLEW -lGL -ASSIMP_LIB := ../$(ASSIMP_DIR)/build/code/libassimp.a -endif - EXAMPLE_SOURCES = \ render_groups/main.cpp \ #assimp_loading/main.cpp @@ -26,8 +20,7 @@ all: mkdirs $(EXAMPLE_OBJECTS) $(EXAMPLE_OBJECTS): $(LIB) $(CXX) $(CXXFLAGS) -c -MMD $(basename $(notdir $@))/main.cpp -o $@ - $(CXX) -o $(BINDIR)/$(notdir $(basename $@)) $(LDFLAGS) $@ $(LIB) \ - $(ASSIMP_LIB) + $(CXX) -o $(BINDIR)/$(notdir $(basename $@)) $(LDFLAGS) $@ $(LIB) mkdirs: @mkdir -p $(BINDIR) $(OBJDIR) diff --git a/examples/render_groups/main.cpp b/examples/render_groups/main.cpp index cc971fd..62b99e8 100644 --- a/examples/render_groups/main.cpp +++ b/examples/render_groups/main.cpp @@ -4,6 +4,7 @@ #include +#include "asset.h" #include "dumbLog.h" #include "input.h" #include "mesh.h" @@ -180,8 +181,9 @@ main() rs->asset_list = (render_asset*) arenaAllocateBlock(rs->arena, sizeof(render_asset)); - mesh_group mg; - meLoadFromFile(mg, "../data/blender/icosphere.gltf"); + //mesh_group mg; + //meLoadFromFile(mg, "../data/blender/icosphere.gltf"); + render_asset* rass = assetLoadFromFile("../data/blender/icosphere.gltf"); renShutdown(rs); return 0; diff --git a/ext/tinygltf b/ext/tinygltf new file mode 160000 index 0000000..19a41d2 --- /dev/null +++ b/ext/tinygltf @@ -0,0 +1 @@ +Subproject commit 19a41d20ec03d35cee68ff85511bc5875f6037b8 diff --git a/include/asset.h b/include/asset.h new file mode 100644 index 0000000..8d70af1 --- /dev/null +++ b/include/asset.h @@ -0,0 +1,41 @@ + +#pragma once + +#include + +#include "animation.h" +#include "util.h" +#include "util_image.h" + + +struct mesh_asset +{ + uint asset_size; + uint num_vertices; + uint num_indices; + mesh_asset* next; + glm::mat4 model_transform; + glm::vec3* vertices; + uint* indices; + glm::vec3* texture_coords; +}; + +#define MAX_PATH_SIZE 256 +struct render_asset +{ + uint total_size; + uint num_meshes; + uint name_len; + render_asset* next; + char* filepath; + mesh_asset* meshes; // NOTE: fixed sized array + // NOTE: pointer to a texture in render_state->textures + util_image* diffuse_texture; + // FIXME: node_animation has a pointer to a render_object. need to decouple + // that somehow + //node_animation* node_anim; +}; + +render_asset* +assetLoadFromFile(const char* filename); + diff --git a/include/mesh.h b/include/mesh.h index 44e697f..1fe81b6 100644 --- a/include/mesh.h +++ b/include/mesh.h @@ -45,9 +45,6 @@ struct simple_mesh }; -bool -meInitAssimp(); - // NOTE: meshes loaded from assimp require texture coordinates, and a diffuse // texture, which can be a seperate file, or embedded in the input file bool diff --git a/include/renderer.h b/include/renderer.h index 107d921..96e1150 100644 --- a/include/renderer.h +++ b/include/renderer.h @@ -9,6 +9,7 @@ #include #include +#include "asset.h" #include "camera.h" #include "entity.h" #include "lights.h" @@ -41,40 +42,6 @@ struct SDL_Handles SDL_DisplayMode currentDisplayMode; }; -// -------------------------- -// Memory allocation WIP - -struct mesh_asset -{ - uint asset_size; - uint num_vertices; - uint num_indices; - mesh_asset* next; - glm::mat4 model_transform; - glm::vec3* vertices; - uint* indices; - glm::vec3* texture_coords; -}; - -#define MAX_PATH_SIZE 256 -struct render_asset -{ - uint total_size; - uint num_meshes; - uint name_len; - render_asset* next; - char* filepath; - mesh_asset* meshes; // NOTE: fixed sized array - // NOTE: pointer to a texture in render_state->textures - util_image* diffuse_texture; - // FIXME: node_animation has a pointer to a render_object. need to decouple - // that somehow - //node_animation* node_anim; -}; - -// Memory allocation WIP -// -------------------------- - struct render_state { glm::vec2 viewport_dims; diff --git a/src/asset.cpp b/src/asset.cpp new file mode 100644 index 0000000..04e1399 --- /dev/null +++ b/src/asset.cpp @@ -0,0 +1,28 @@ + +#include + +#define TINYGLTF_IMPLEMENTATION +//#define STB_IMAGE_IMPLEMENTATION +#define STB_IMAGE_WRITE_IMPLEMENTATION +#include "tiny_gltf.h" + +#include "asset.h" + + +render_asset* +assetLoadFromFile(const char* filename) +{ + tinygltf::Model model; + // TODO: see if there's any impact in creating a new context for each file + tinygltf::TinyGLTF gltf_ctx; + std::string err; + std::string warn; + bool ret = false; + + ret = gltf_ctx.LoadASCIIFromFile(&model, &err, &warn, filename); + + + + + return nullptr; +} diff --git a/src/mesh.cpp b/src/mesh.cpp index c94051e..6020251 100644 --- a/src/mesh.cpp +++ b/src/mesh.cpp @@ -1,8 +1,10 @@ #include +#if 0 #include #include #include +#endif #include #include @@ -13,6 +15,30 @@ #include "mesh.h" +// WIP +bool meInitAssimp() { return false; } + +bool meLoadFromFile(mesh_group& mesh_group, const char* filepath) +{ + return false; +} + +simple_mesh* +meInitMesh(uint num_vertices) { return nullptr; } + +void +meFreeMeshGroup(mesh_group& mesh_group) {} + +void +meFreeSimpleMesh(simple_mesh* mesh) {} + +void +meShutdownAssimp() {} + +// WIP + + +#if 0 // forward declarations mesh_info* allocateMeshInfo(uint num_vertices, uint num_indices); @@ -232,3 +258,5 @@ validateScene(const aiScene* scene, const char* filepath) return true; } +#endif + diff --git a/src/renderer.cpp b/src/renderer.cpp index 480315b..3fb4a45 100644 --- a/src/renderer.cpp +++ b/src/renderer.cpp @@ -48,8 +48,7 @@ renInit(const char* title, glm::vec2 viewport_dims, Uint32 SDL_init_flags) createWindow(title, rs->handles, rs->viewport_dims) && initContext(rs->handles) && initGlOptions() && - initShaders(rs) && - meInitAssimp()) + initShaders(rs)) { return rs; }