diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5503a9e --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +build/ + +# CLion +cmake-build-*/ +.idea/ \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..158235c --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,19 @@ +cmake_minimum_required(VERSION 3.4 FATAL_ERROR) +project(tgen) + +set(CMAKE_CXX_STANDARD_REQUIRED 11) +set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON) + +if(EMSCRIPTEN) + option(TGEN_WEBASSEMBLY "Generate WebAssembly instead of asm.js" OFF) + if(TGEN_WEBASSEMBLY) + set(CMAKE_CXX_FLAGS "-s WASM=1 ${CMAKE_CXX_FLAGS}") + endif() + + # For some reason CMAKE_CXX_STANDARD_REQUIRED does not work for emscripten + set(CMAKE_CXX_FLAGS "--bind -std=c++11 ${CMAKE_CXX_FLAGS}") +else() + option(TGEN_SHARED_LIBRARY "Build tgen as a shared library" OFF) +endif() + +add_subdirectory(src bin) \ No newline at end of file diff --git a/README.md b/README.md index 68480ec..b575adc 100644 --- a/README.md +++ b/README.md @@ -7,17 +7,35 @@ Current Features: * Generation of per-corner tangents for triangle data with UVs * Computation of per-wedge / per-UV-vertex tangent spaces * Tangent frame orthogonalization -* Encoding of 4-component tangents (with "flip factor") for avoiding explicit binormals +* Encoding of 4-component tangents (with "flip factor") for avoiding explicit bitangents * Simple C++ implementation, no dependencies +* Compilable to asm.js and WebAssembly (right now, only the functions are exported, there is no web demo yet) The code consists basically of one header + .cpp file. For debugging and visualization, there is also a simple X3D exporter in a separate file, which was used to generate the 3D visualizations shown below. The baked tangent-space normal maps are just provided for demonstration purposes, the actual baking code is not part of this repository. -So far, the C++ code from this project has just been compiled and tested with VS 2015. +So far, the C++ code from this project has been compiled and tested with VS 2015, gcc 6.3 / 7.0 trunk, clang 5.0 trunk and emscripten 1.37 (clang 3.9). Feedback and contributions are always welcome. +## Building and installing + +TGen has no external dependencies and can be built using CMake 3.4 or newer: +``` +cd +mkdir build +cd build + +# Native targets +cmake .. [-DCMAKE_BUILD_TYPE=Release] [-DTGEN_STATIC_LIBRARY=0] # Default is building as a shared library +cmake --build . [--target install] + +# Emscripten +emconfigure cmake .. [-DCMAKE_BUILD_TYPE=Release] [-DTGEN_WEBASSEMBLY=1] # Default is asm.js instead of WebAssembly +cmake --build . +emrun --port 8080 bin +``` ## Results diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt new file mode 100644 index 0000000..37296ca --- /dev/null +++ b/src/CMakeLists.txt @@ -0,0 +1,37 @@ +set(SOURCE_FILES + tgen.h tgen.cpp + tgen_debug.h tgen_debug.cpp + ) + +if(TGEN_SHARED_LIBRARY) + add_library(tgen SHARED ${SOURCE_FILES}) +else() + add_library(tgen STATIC ${SOURCE_FILES}) +endif() + +if(EMSCRIPTEN) + add_executable(tgen_driver EmscriptenMain.cpp) +else() + add_executable(tgen_driver main.cpp) +endif() + +target_link_libraries(tgen_driver PRIVATE tgen) +set_target_properties(tgen_driver PROPERTIES OUTPUT_NAME tgen) + +install(TARGETS tgen_driver + RUNTIME DESTINATION bin +) + +install(TARGETS tgen + RUNTIME DESTINATION bin + ARCHIVE DESTINATION lib + LIBRARY DESTINATION lib + ) + +install(DIRECTORY . DESTINATION include + FILES_MATCHING PATTERN "*.h" +) + +if(EMSCRIPTEN) + configure_file(webdemo.html index.html COPYONLY) +endif() \ No newline at end of file diff --git a/src/EmscriptenMain.cpp b/src/EmscriptenMain.cpp new file mode 100644 index 0000000..43aa4ac --- /dev/null +++ b/src/EmscriptenMain.cpp @@ -0,0 +1,27 @@ +/** + * TGen - Simple Tangent Generator + * + * 2016 by Max Limper, Fraunhofer IGD + * + * This code is public domain. + * + */ + +#include +#include "tgen.h" + +using namespace emscripten; + +int add(int a, int b) +{ + return a + b; +} + +EMSCRIPTEN_BINDINGS(tgen) +{ + function("computeCornerTSpace", &tgen::computeCornerTSpace); + function("computeVertexTSpace", &tgen::computeVertexTSpace); + function("orthogonalizeTSpace", &tgen::orthogonalizeTSpace); + function("computeTangent4D", &tgen::computeTangent4D); + function("add", &add); +} \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..82438d9 --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,12 @@ +#include "tgen.h" +#include "tgen_debug.h" +#include + +int main(int argc, char ** argv) +{ + if(argc != 3) + { + std::cout << "Usage: " << argv[0] << " input.gltf output.gltf\n"; + std::exit(EXIT_FAILURE); + } +} \ No newline at end of file diff --git a/src/tgen_debug.cpp b/src/tgen_debug.cpp index d01d0ee..1f91365 100644 --- a/src/tgen_debug.cpp +++ b/src/tgen_debug.cpp @@ -103,20 +103,20 @@ namespace double vScale, std::stringstream & ss ) { - ss << " " << std::endl; - ss << " " << std::endl; + ss << " " << '\n'; + ss << " " << '\n'; ss << " " - << std::endl; - ss << " " << std::endl; + << '\n'; + ss << " " << '\n'; ss << " " - << std::endl; + << '\n'; ss << " " << std::endl; - ss << " " << std::endl; - ss << " " << std::endl; + ss << "\"/>" << '\n'; + ss << " " << '\n'; + ss << " " << '\n'; } //------------------------------------------------------------------------- @@ -178,39 +178,39 @@ namespace tgen std::stringstream sstr; // prologue - sstr << "" << std::endl; + sstr << "" << '\n'; sstr << "" - << std::endl; - sstr << "" << std::endl; - sstr << " " << std::endl; + << '\n'; + sstr << "" << '\n'; + sstr << " " << '\n'; // textured object - sstr << " " << std::endl; - sstr << " " << std::endl; - sstr << " " << std::endl; - sstr << " " << std::endl; - sstr << " " << std::endl; - sstr << " " << std::endl; - sstr << " " << std::endl; - sstr << " " << '\n'; + sstr << " " << '\n'; + sstr << " " << '\n'; + sstr << " " << '\n'; + sstr << " " << '\n'; + sstr << " " << '\n'; + sstr << " " << '\n'; + sstr << " " << std::endl; + sstr << "\">" << '\n'; sstr << " " << std::endl; + sstr << "\"/>" << '\n'; sstr << " " << std::endl; + sstr << "\"/>" << '\n'; sstr << " " << std::endl; - sstr << " " << std::endl; - sstr << " " << std::endl; + sstr << "\"/>" << '\n'; + sstr << " " << '\n'; + sstr << " " << '\n'; // vector field visualizations writeX3DVecFieldVis(positions3D, normals3D, "0 0 1", vScale, sstr); @@ -218,12 +218,12 @@ namespace tgen writeX3DVecFieldVis(positions3D, bitangents3D, "0 1 0", vScale, sstr); // epilogue - sstr << " " << std::endl; - sstr << "" << std::endl; + sstr << " " << '\n'; + sstr << "" << '\n'; - std::ofstream fstr("tangents.x3d"); - fstr << sstr.str() << std::endl; + std::ofstream fstr(filename); + fstr << sstr.str() << '\n'; } //------------------------------------------------------------------------- diff --git a/src/tgen_debug.h b/src/tgen_debug.h index 81dc3eb..aab9b0b 100644 --- a/src/tgen_debug.h +++ b/src/tgen_debug.h @@ -38,7 +38,7 @@ namespace tgen const std::vector & uvs2D, const std::vector & tangents3D, const std::vector & bitangents3D, - const char * filename ); + const char * filename = "tangents.x3d"); //------------------------------------------------------------------------- diff --git a/src/webdemo.html b/src/webdemo.html new file mode 100644 index 0000000..b2dac07 --- /dev/null +++ b/src/webdemo.html @@ -0,0 +1,31 @@ + + + + + +