-
Notifications
You must be signed in to change notification settings - Fork 2
feat(cgltf): add LLAR formula #140
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| cmake_minimum_required(VERSION 3.15) | ||
| project(cgltf C) | ||
|
|
||
| set(SOURCES_DIR ${CMAKE_CURRENT_LIST_DIR}/src) | ||
|
|
||
| set(SRC_FILES | ||
| ${SOURCES_DIR}/cgltf.c | ||
| ${SOURCES_DIR}/cgltf_write.c | ||
| ) | ||
| set(HEADER_FILES | ||
| ${SOURCES_DIR}/cgltf.h | ||
| ${SOURCES_DIR}/cgltf_write.h | ||
| ) | ||
|
|
||
| add_library(${PROJECT_NAME} ${SRC_FILES}) | ||
| set_property(TARGET ${PROJECT_NAME} PROPERTY C_STANDARD 99) | ||
| if(MSVC AND BUILD_SHARED_LIBS) | ||
| set_property(TARGET ${PROJECT_NAME} PROPERTY WINDOWS_EXPORT_ALL_SYMBOLS ON) | ||
| endif() | ||
|
|
||
| include(GNUInstallDirs) | ||
| install(TARGETS ${PROJECT_NAME}) | ||
| install(FILES ${HEADER_FILES} DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,156 @@ | ||
| import ( | ||
| "os" | ||
| "path/filepath" | ||
| "slices" | ||
| "strings" | ||
| ) | ||
|
|
||
| const consumerSource = `#define CGLTF_IMPLEMENTATION | ||
| #include <cgltf.h> | ||
|
|
||
| #include <cstdio> | ||
| #include <cstdlib> | ||
|
|
||
| static void transform(const cgltf_float matrix[16], const cgltf_float source[4], cgltf_float target[4]) { | ||
| target[0] = matrix[0] * source[0] + matrix[4] * source[1] + matrix[ 8] * source[2] + matrix[12] * source[3]; | ||
| target[1] = matrix[1] * source[0] + matrix[5] * source[1] + matrix[ 9] * source[2] + matrix[13] * source[3]; | ||
| target[2] = matrix[2] * source[0] + matrix[6] * source[1] + matrix[10] * source[2] + matrix[14] * source[3]; | ||
| target[3] = matrix[3] * source[0] + matrix[7] * source[1] + matrix[11] * source[2] + matrix[15] * source[3]; | ||
| } | ||
|
|
||
| static void set(cgltf_float target[3], float x, float y, float z) { | ||
| target[0] = x; | ||
| target[1] = y; | ||
| target[2] = z; | ||
| } | ||
|
|
||
| static void check(cgltf_float target[3], float x, float y, float z) { | ||
| if (target[0] != x || target[1] != y || target[2] != z) { | ||
| std::fprintf(stderr, "Mismatch detected.\n"); | ||
| std::exit(1); | ||
| } | ||
| } | ||
|
|
||
| int main() { | ||
| cgltf_node node = {}; | ||
|
|
||
| cgltf_float matrix[16]; | ||
| cgltf_float source[4] = {1, 2, 3, 1}; | ||
| cgltf_float target[4]; | ||
|
|
||
| set(node.scale, 1, 1, 1); | ||
| set(node.translation, 1, 0, 0); | ||
| cgltf_node_transform_local(&node, matrix); | ||
| transform(matrix, source, target); | ||
| check(target, 2, 2, 3); | ||
|
|
||
| set(node.scale, 3, 1, 1); | ||
| set(node.translation, 0, 0, 0); | ||
| cgltf_node_transform_local(&node, matrix); | ||
| transform(matrix, source, target); | ||
| check(target, 3, 2, 3); | ||
|
|
||
| set(node.scale, 1, 3, 1); | ||
| set(node.translation, 1, 0, 0); | ||
| cgltf_node_transform_local(&node, matrix); | ||
| transform(matrix, source, target); | ||
| check(target, 2, 6, 3); | ||
|
|
||
| return 0; | ||
| } | ||
| ` | ||
|
|
||
| id "jkuhlmann/cgltf" | ||
|
|
||
| fromVer "v1.5" | ||
|
|
||
| defaults { | ||
| "shared": "OFF", | ||
| "fPIC": "ON", | ||
| } | ||
|
|
||
| filter => { | ||
| for name, values in target.options { | ||
| if name != "shared" && name != "fPIC" { | ||
| return false | ||
| } | ||
| for value in values { | ||
| if value != "ON" && value != "OFF" { | ||
| return false | ||
| } | ||
| } | ||
| } | ||
| return true | ||
| } | ||
|
|
||
| onBuild ctx => { | ||
| installDir := ctx.outputDir | ||
| sourceDir := filepath.join(ctx.SourceDir, "src") | ||
| os.mkdirAll(sourceDir, 0o755)! | ||
|
|
||
| cmakeLists := ctx.Proj.readFile("v1.5/CMakeLists.txt")! | ||
| os.writeFile(filepath.join(ctx.SourceDir, "CMakeLists.txt"), cmakeLists, 0o644)! | ||
| os.writeFile(filepath.join(sourceDir, "cgltf.c"), []byte("#define CGLTF_IMPLEMENTATION\n#include \"cgltf.h\"\n"), 0o644)! | ||
| os.writeFile(filepath.join(sourceDir, "cgltf_write.c"), []byte("#define CGLTF_WRITE_IMPLEMENTATION\n#include \"cgltf_write.h\"\n"), 0o644)! | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
|
||
| shared := slices.contains(target.options["shared"], "ON") | ||
| fPIC := slices.contains(target.options["fPIC"], "ON") | ||
| c := cmake.new(ctx.SourceDir, filepath.join(ctx.SourceDir, "_build"), installDir) | ||
| c.defineBool "BUILD_SHARED_LIBS", shared | ||
| c.defineBool "CMAKE_POSITION_INDEPENDENT_CODE", fPIC | ||
| c.configure | ||
| c.build | ||
| c.install | ||
|
|
||
| licenseDir := filepath.join(installDir, "licenses") | ||
| os.mkdirAll(licenseDir, 0o755)! | ||
| os.writeFile(filepath.join(licenseDir, "LICENSE"), os.readFile(filepath.join(ctx.SourceDir, "LICENSE"))!, 0o644)! | ||
|
|
||
| markerBegin := "/*\n *\n * Stop now, if you are only interested in the API." | ||
| markerEnd := "/* cgltf is distributed under MIT license:" | ||
| replacement := "/**\n * Implementation removed by conan during packaging.\n * Don't forget to link libs provided in this package.\n */\n\n" | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This replacement text is written into the shipped |
||
| for header in []string{"cgltf.h", "cgltf_write.h"} { | ||
| headerPath := filepath.join(installDir, "include", header) | ||
| headerContent := string(os.readFile(headerPath)!) | ||
| begin := strings.index(headerContent, markerBegin) | ||
| end := strings.index(headerContent, markerEnd) | ||
| implementation := headerContent[begin:end] | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Guard the marker lookups before slicing. |
||
| headerContent = strings.replace(headerContent, implementation, replacement, 1) | ||
| os.writeFile(headerPath, []byte(headerContent), 0o644)! | ||
| } | ||
|
|
||
| flags := []string{ | ||
| "-I" + filepath.join(installDir, "include"), | ||
| "-L" + filepath.join(installDir, "lib"), | ||
| "-lcgltf", | ||
| } | ||
| ctx.setMetadata strings.join(flags, " ") | ||
| } | ||
|
|
||
| onTest ctx => { | ||
| installDir := ctx.outputDir | ||
| testDir := filepath.join(ctx.SourceDir, "_llar_consumer") | ||
| os.mkdirAll(testDir, 0o755)! | ||
|
|
||
| sourcePath := filepath.join(testDir, "consumer.cpp") | ||
| os.writeFile(sourcePath, []byte(consumerSource), 0o644)! | ||
|
|
||
| binary := filepath.join(testDir, "consumer") | ||
| args := []string{ | ||
| "-std=c++11", | ||
| sourcePath, | ||
| "-I" + filepath.join(installDir, "include"), | ||
| "-L" + filepath.join(installDir, "lib"), | ||
| "-lcgltf", | ||
| "-o", binary, | ||
| } | ||
| exec "c++", args... | ||
| lastErr! | ||
|
|
||
| if slices.contains(target.options["shared"], "ON") { | ||
| os.setenv("LD_LIBRARY_PATH", filepath.join(installDir, "lib"))! | ||
| os.setenv("DYLD_LIBRARY_PATH", filepath.join(installDir, "lib"))! | ||
| } | ||
| exec binary | ||
| lastErr! | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| { | ||
| "path": "jkuhlmann/cgltf", | ||
| "deps": {} | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The reference
recp/cglm/v0.7.2/Cglm_llar.goxdocuments its build contract, option mapping, metadata fallback, and test rationale; this formula has no explanatory comments. A few genuinely non-obvious steps would benefit from a short note:CMakeLists.txt+.ctranslation units that#define ..._IMPLEMENTATION.libcgltf, so theCGLTF_IMPLEMENTATIONblock is excised from the shipped headers to avoid duplicate-symbol conflicts for consumers.shared→BUILD_SHARED_LIBS,fPIC→CMAKE_POSITION_INDEPENDENT_CODE.