aboutsummaryrefslogtreecommitdiff
path: root/contrib/cgltf-tangents/test
diff options
context:
space:
mode:
Diffstat (limited to 'contrib/cgltf-tangents/test')
-rw-r--r--contrib/cgltf-tangents/test/CMakeLists.txt11
-rw-r--r--contrib/cgltf-tangents/test/main.c86
2 files changed, 97 insertions, 0 deletions
diff --git a/contrib/cgltf-tangents/test/CMakeLists.txt b/contrib/cgltf-tangents/test/CMakeLists.txt
new file mode 100644
index 0000000..422c950
--- /dev/null
+++ b/contrib/cgltf-tangents/test/CMakeLists.txt
@@ -0,0 +1,11 @@
1cmake_minimum_required(VERSION 3.0)
2
3project (cgltf-test)
4
5add_executable(cgltf-test
6 main.c)
7
8target_link_libraries(cgltf-test
9 cgltf
10 cgltf-tangents
11 -lm)
diff --git a/contrib/cgltf-tangents/test/main.c b/contrib/cgltf-tangents/test/main.c
new file mode 100644
index 0000000..0d70008
--- /dev/null
+++ b/contrib/cgltf-tangents/test/main.c
@@ -0,0 +1,86 @@
1/*
2Copyright 2022 Marc Sunet
3
4Redistribution and use in source and binary forms, with or without modification,
5are permitted provided that the following conditions are met:
6
71. Redistributions of source code must retain the above copyright notice, this
8list of conditions and the following disclaimer.
9
102. Redistributions in binary form must reproduce the above copyright notice,
11this list of conditions and the following disclaimer in the documentation and/or
12other materials provided with the distribution.
13
14THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
15ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
18ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
21ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
23SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24*/
25#include <cgltf_tangents.h>
26#define CGLTF_IMPLEMENTATION
27#include <cgltf.h>
28
29#include <stdio.h>
30
31void print_tangent_buffer(const cgltfTangentBuffer* buffer, int max_vectors) {
32 printf("Tangent buffer for primitive (%p) (%lu bytes):\n", buffer->primitive,
33 buffer->size_bytes);
34
35 const float* xyzw = (const float*)buffer->data;
36 const float* end = (const float*)(buffer->data + buffer->size_bytes);
37
38 for (int i = 0; i < max_vectors && xyzw < end; ++i, xyzw += 4) {
39 printf("(%3.2f, %3.2f, %3.2f, sign: %3.2f)\n", *xyzw, *(xyzw + 1),
40 *(xyzw + 2), *(xyzw + 3));
41 }
42 printf("--------------------");
43}
44
45void usage(const char* argv0) {
46 fprintf(stderr, "Usage: %s <glTF file path>\n", argv0);
47}
48
49int main(int argc, const char** argv) {
50 cgltf_options options = {0};
51 cgltf_data* data = NULL;
52
53 if (argc != 2) {
54 usage(argv[0]);
55 return 0;
56 }
57
58 const char* filepath = argv[1];
59
60 cgltf_result result = cgltf_parse_file(&options, filepath, &data);
61 if (result != cgltf_result_success) {
62 cgltf_free(data);
63 return 1;
64 }
65
66 // Must call cgltf_load_buffers() to load buffer data.
67 result = cgltf_load_buffers(&options, data, filepath);
68 if (result != cgltf_result_success) {
69 cgltf_free(data);
70 return 2;
71 }
72
73 cgltfTangentBuffer* tangent_buffers = 0;
74 cgltf_size num_tangent_buffers = 0;
75 cgltf_compute_tangents(&options, data, &tangent_buffers,
76 &num_tangent_buffers);
77
78 // cgltf scene not needed beyond this point.
79 cgltf_free(data);
80
81 for (cgltf_size i = 0; i < num_tangent_buffers; ++i) {
82 print_tangent_buffer(tangent_buffers, 10);
83 }
84
85 return 0;
86}