diff --git a/CMakeLists.txt b/CMakeLists.txt index f0640b8..1cc55ac 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -37,9 +37,9 @@ target_link_libraries(couch couchlua) add_subdirectory(thirdparty) add_subdirectory(shaders) +add_dependencies(couch shader_headers) target_include_directories(couch - PUBLIC "${CMAKE_CURRENT_BINARY_DIR}/shaders") - + PUBLIC "${CMAKE_CURRENT_BINARY_DIR}/shaders/") if(WIN32) target_link_libraries(couch glfw3dll) diff --git a/core/Mesh.cpp b/core/Mesh.cpp index 3b2e3ad..26d8706 100644 --- a/core/Mesh.cpp +++ b/core/Mesh.cpp @@ -5,7 +5,9 @@ Mesh::Mesh() { } Mesh::~Mesh() { - delete material; + if (material) { + delete material; + } } Mesh::Mesh(VertexList vertices, IndexList indices) { @@ -38,8 +40,26 @@ void Mesh::SetupMesh() { glBindVertexArray(0); } -Mesh Mesh::FromFile(const char *filename) { - return Mesh(); +Mesh* Mesh::FromFile(const char *filename) { + // HOCUS: https://assimp-docs.readthedocs.io/en/latest/usage/use_the_lib.html + Assimp::Importer importer; + + const aiScene* scene = importer.ReadFile( + filename, // Read the file + aiProcess_Triangulate | // We only do triangles + aiProcess_GenNormals | // We want normals precalculated + aiProcess_GenUVCoords // We want UV mappings precalculated + ); + + if (!scene) { + std::cerr << importer.GetErrorString() << std::endl; + exit(1); + } + + aiNode *root = scene->mRootNode; + aiMesh *mesh_to_import = scene->mMeshes[root->mMeshes[0]]; + + return Util::aiMesh2Mesh(mesh_to_import); } void Mesh::Draw() { diff --git a/core/Mesh.h b/core/Mesh.h index c91c581..ae57ad7 100644 --- a/core/Mesh.h +++ b/core/Mesh.h @@ -2,8 +2,16 @@ #define MESH_H #include +#include +#include + +// Thirdpart includes +#include +#include +#include #include "types.h" +#include "Util.h" #include "Spatial.h" #include "Vertex.h" #include "Index.h" @@ -17,7 +25,7 @@ public: Mesh(); ~Mesh(); Mesh(VertexList vertices, IndexList indices); - static Mesh FromFile(const char *filename); + static Mesh *FromFile(const char *filename); virtual bool IsDrawable() const {return true;} virtual void Draw(); virtual void SetupMesh(); diff --git a/core/Util.cpp b/core/Util.cpp new file mode 100644 index 0000000..3f3f741 --- /dev/null +++ b/core/Util.cpp @@ -0,0 +1,18 @@ +#include "Util.h" + +Mesh *Util::aiMesh2Mesh(aiMesh *aimesh){ + Mesh *mymesh = new Mesh(); + for (int i = 0; i < aimesh->mNumVertices; i++) { + aiVector3D aiPosition = aimesh->mVertices[i]; + aiVector3D aiUV = aimesh->mTextureCoords[0][i]; // TODO get ALL texture coords + Vertex vertex(aiPosition.x, aiPosition.y, aiPosition.z, aiUV.x, aiUV.y); + mymesh->vertices.push_back(vertex); + } + for (int i = 0; i < aimesh->mNumFaces; i++) { + // We're importing triangulated meshes, so each face is three indices + unsigned int *face = aimesh->mFaces[i].mIndices; + Index index(face[0], face[1], face[2]); + mymesh->indices.push_back(index); + } + return mymesh; +} diff --git a/core/Util.h b/core/Util.h new file mode 100644 index 0000000..20e6fe5 --- /dev/null +++ b/core/Util.h @@ -0,0 +1,15 @@ +#ifndef UTIL_H +#define UTIL_H + +// Thirdparty includes +#include + +// Gahhhhhh mutual inclusion! +#include "Mesh.h" +class Mesh; + +namespace Util { + Mesh *aiMesh2Mesh(aiMesh *mesh); +} + +#endif /* UTIL_H */ diff --git a/demo/cube.glb b/demo/cube.glb new file mode 100644 index 0000000..0f1a5cd Binary files /dev/null and b/demo/cube.glb differ diff --git a/demo/main.lua b/demo/main.lua index 602b89b..1964701 100644 --- a/demo/main.lua +++ b/demo/main.lua @@ -40,6 +40,12 @@ function init() couch.Node.GetRoot().children:Append(ball1) ball1.transform:Translate(0.0, 3.0, 0.0) + + cube = couch.Mesh.FromFile("cube.glb") + cube:SetupMesh(); + cube.material.tex = couch.Texture.FromFile("container.png") + cube.material.usesTex = true + couch.Node.GetRoot().children:Append(cube) end function update(delta) diff --git a/shaders/CMakeLists.txt b/shaders/CMakeLists.txt index fedf513..9946efe 100644 --- a/shaders/CMakeLists.txt +++ b/shaders/CMakeLists.txt @@ -1,14 +1,16 @@ project(Couch) -file(GLOB shaders *.vert *.frag) -list( - TRANSFORM shaders - APPEND .h - OUTPUT_VARIABLE shader_header_files) +macro(add_shader shaderfile) + add_custom_command( + OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${shaderfile}.h + COMMAND perl "${CMAKE_CURRENT_SOURCE_DIR}/makeheaders.pl" ${CMAKE_CURRENT_SOURCE_DIR}/${shaderfile} ${CMAKE_CURRENT_BINARY_DIR}/${shaderfile}.h + DEPENDS ${shaderfile} makeheaders.pl + ) +endmacro() + +add_shader(flat.vert) +add_shader(flat.frag) add_custom_target(shader_headers - BYPRODUCTS ${shader_header_files} - COMMAND perl "${CMAKE_CURRENT_SOURCE_DIR}/makeheaders.pl" - DEPENDS ${shaders} ${CMAKE_CURRENT_SOURCE_DIR}/makeheaders.pl) - -add_dependencies(couch shader_headers) + DEPENDS flat.vert.h flat.frag.h + COMMENT "Generated shaders headers.") diff --git a/shaders/makeheaders.pl b/shaders/makeheaders.pl index c6d5a6b..b206951 100644 --- a/shaders/makeheaders.pl +++ b/shaders/makeheaders.pl @@ -1,33 +1,27 @@ use strict; use warnings; +use File::Basename; -sub convert { - my $filename = $1; - - my $headerguard = "$filename.h"; - $headerguard =~ tr/a-z./A-Z_/; - - my $constname = "$filename"; - $constname =~ tr/./_/; +my ($filename, $outfilename) = @ARGV; - open my $fin, '<', $filename; - open my $fout, '>', "$filename.h"; - - print $fout "#ifndef $headerguard \n#define $headerguard\nconst char * $constname = \n"; +my $headerguard = basename $outfilename; +$headerguard =~ tr/a-z./A-Z_/; - while(my $line = <$fin>) { - $line =~ s/\n/\\n/; - print $fout "\"$line\"\n"; - } +my $constname = basename "$filename"; +$constname =~ tr/./_/; - print $fout ";\n#endif // $headerguard\n"; - close $fin; - close $fout; +open(my $fin, '<', $filename) or die("Couldn't find $filename"); +open my $fout, '>', $outfilename; + +print $fout "#ifndef $headerguard \n#define $headerguard\nconst char * $constname = \n"; + +while(my $line = <$fin>) { + $line =~ s/\n/\\n/; + print $fout "\"$line\"\n"; } -opendir DIR, "."; -while (readdir DIR) { - if (/(.*\.(frag|vert))/) { - convert; - } -} +print $fout ";\n#endif // $headerguard\n"; +close $fin; +close $fout; +print "Generated $outfilename\n"; + diff --git a/thirdparty/CMakeLists.txt b/thirdparty/CMakeLists.txt index 8315bf5..e4d83fb 100644 --- a/thirdparty/CMakeLists.txt +++ b/thirdparty/CMakeLists.txt @@ -1,7 +1,15 @@ project(Couch) + +## STB (image) target_include_directories(couch PUBLIC stb) - target_include_directories(couchlua PUBLIC stb) + +## ASSIMP +set(ASSIMP_BUILD_ALL_IMPORTERS_BY_DEFAULT FALSE CACHE BOOL "Turn off all assimp importers") +set(ASSIMP_BUILD_GLTF_IMPORTER ON CACHE BOOL "Turn on gltf importer") +add_subdirectory(assimp) +target_link_libraries(couch assimp::assimp) +target_link_libraries(couchlua assimp::assimp)