Skip to content

Commit 7ca79db

Browse files
committed
Code formatting and linting
1 parent 902f22a commit 7ca79db

File tree

6 files changed

+95
-24
lines changed

6 files changed

+95
-24
lines changed

.justfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ web *build_type='Release':
3232
@mkdir -p build
3333
@if [ -n "${EM_CACHE-}" ]; then mkdir -p "$EM_CACHE" && mkdir -p "$EM_CACHE/tmp"; fi
3434
@echo "Configuring the build system..."
35-
@cd build && emcmake cmake -S .. -B . -DCMAKE_BUILD_TYPE={{build_type}} -DBUILD_WASM=1
35+
@cd build && emcmake cmake -S .. -B . -DCMAKE_BUILD_TYPE={{build_type}}
3636
@echo "Building the project..."
3737
@cd build && cmake --build . -j$(nproc)
3838
@mkdir -p public/assets/

CMakeLists.txt

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,18 @@ target_include_directories(
2828
include(CheckLanguage)
2929
include(FetchContent)
3030

31-
if(BUILD_WASM)
32-
# Ensure we're using Emscripten compiler
33-
if(NOT DEFINED EMSCRIPTEN)
34-
message(FATAL_ERROR "BUILD_WASM requires using Emscripten CMake toolchain")
31+
if(EMSCRIPTEN)
32+
if(APPLE)
33+
# Tell CMake we are *not* building for macOS and strip any inherited flags.
34+
set(CMAKE_OSX_ARCHITECTURES
35+
""
36+
CACHE STRING "" FORCE)
37+
set(CMAKE_OSX_SYSROOT
38+
""
39+
CACHE PATH "" FORCE)
40+
set(CMAKE_OSX_DEPLOYMENT_TARGET
41+
""
42+
CACHE STRING "" FORCE)
3543
endif()
3644
add_compile_definitions("__EMSCRIPTEN__")
3745

@@ -53,7 +61,7 @@ if(BUILD_WASM)
5361
"-sASSERTIONS=1")
5462

5563
# Set output extension
56-
set(CMAKE_EXECUTABLE_SUFFIX .js)
64+
set(CMAKE_EXECUTABLE_SUFFIX ".js")
5765
else()
5866
# Metal Support (macOS/iOS)
5967
if(APPLE)
@@ -137,7 +145,7 @@ if(BUILD_EXAMPLES)
137145
endif()
138146

139147
# Tests
140-
if(NOT BUILD_WASM AND BUILD_TESTS)
148+
if(NOT EMSCRIPTEN AND BUILD_TESTS)
141149
# Fetch GoogleTest
142150
FetchContent_Declare(
143151
googletest

examples/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
if(BUILD_WASM)
1+
if(EMSCRIPTEN)
22
add_subdirectory(web)
33
else()
44
add_subdirectory(bunny)

examples/web/src/web.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include <stddef.h>
22
#include <stdio.h>
33
#include <iostream>
4+
#include <cmath>
45
#include <kiwigl/kiwigl.hpp>
56

67
#ifdef __EMSCRIPTEN__
@@ -37,10 +38,12 @@ void runMainLoop(void* display) {
3738
//------------------------------------------------------------------------------------
3839
int main(int argc, char** argv) {
3940
// Initialization of display
40-
kiwigl::Display* display = new kiwigl::Display("KiwiGL WebAssembly Demo");
41+
kiwigl::Display* display =
42+
new kiwigl::Display("KiwiGL WebAssembly Demo", false, kiwigl::Vector3D(0, 0, -0.25),
43+
kiwigl::Vector3D(M_PI, M_PI, 0), kiwigl::Vector3D(0.005, 0.005, 0.005));
4144

4245
// Load the Stanford bunny mesh
43-
display->loadMesh("./assets/cube.mesh");
46+
display->loadMesh("./assets/bunny.mesh");
4447

4548
#ifdef __EMSCRIPTEN__
4649
emscripten_set_main_loop_arg(runMainLoop, (void*)display, 0, true);

include/kiwigl/geometry/mesh.hpp

Lines changed: 73 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#include <fstream>
44
#include <iostream>
5+
#include <sstream>
56
#include <string>
67
#include <vector>
78

@@ -32,25 +33,85 @@ class Mesh {
3233
std::ifstream file(filename, std::ios::binary);
3334
if (file.is_open()) {
3435
std::string line;
35-
while (std::getline(file, line) && !file.eof()) {
36-
if (line.find("v ") != std::string::npos) {
37-
// Parse the vertex
38-
Vector3D vertex;
39-
if (sscanf(line.c_str(), "v %lf %lf %lf", &vertex.x, &vertex.y, &vertex.z) == 3) { addVertex(vertex); }
40-
} else if (line.find("vt ") != std::string::npos) {
36+
while (std::getline(file, line)) {
37+
if (line.rfind("vt ", 0) == 0) {
4138
// Parse the texture coordinate
4239
Texture2D texture;
4340
if (sscanf(line.c_str(), "vt %lf %lf", &texture.u, &texture.v) == 2) { addTexture(texture); }
44-
} else if (line.find("f ") != std::string::npos) {
45-
// Parse the face
46-
int v1, v2, v3, t1, t2, t3, n1, n2, n3;
47-
if (sscanf(line.c_str(), "f %d/%d/%d %d/%d/%d %d/%d/%d", &v1, &t1, &n1, &v2, &t2, &n2, &v3, &t3, &n3) ==
48-
9) {
49-
addFace(v1, v2, v3, textures[t1 - 1], textures[t2 - 1], textures[t3 - 1], WHITE);
41+
} else if (line.rfind("v ", 0) == 0) {
42+
// Parse the vertex
43+
Vector3D vertex;
44+
if (sscanf(line.c_str(), "v %lf %lf %lf", &vertex.x, &vertex.y, &vertex.z) == 3) { addVertex(vertex); }
45+
} else if (line.rfind("f ", 0) == 0) {
46+
std::stringstream ss(line.substr(2));
47+
std::string token;
48+
std::vector<int> v_indices, t_indices, n_indices;
49+
50+
while (ss >> token) {
51+
int v = 0, t = 0, n = 0;
52+
if (sscanf(token.c_str(), "%d/%d/%d", &v, &t, &n) == 3) {
53+
v_indices.push_back(v);
54+
t_indices.push_back(t);
55+
n_indices.push_back(n);
56+
} else if (sscanf(token.c_str(), "%d//%d", &v, &n) == 2) {
57+
v_indices.push_back(v);
58+
n_indices.push_back(n);
59+
} else if (sscanf(token.c_str(), "%d/%d", &v, &t) == 2) {
60+
v_indices.push_back(v);
61+
t_indices.push_back(t);
62+
} else if (sscanf(token.c_str(), "%d", &v) == 1) {
63+
v_indices.push_back(v);
64+
}
65+
}
66+
67+
// Triangulate polygon faces
68+
if (v_indices.size() >= 3) {
69+
for (size_t i = 1; i < v_indices.size() - 1; ++i) {
70+
Texture2D tex[3];
71+
if (t_indices.size() == v_indices.size()) {
72+
auto get_abs_idx = [](int idx, size_t s) -> size_t {
73+
if (idx > 0) return idx - 1;
74+
if (idx < 0) return s + idx;
75+
return (size_t)-1; // Invalid index
76+
};
77+
78+
size_t t0 = get_abs_idx(t_indices[0], textures.size());
79+
size_t ti = get_abs_idx(t_indices[i], textures.size());
80+
size_t ti1 = get_abs_idx(t_indices[i + 1], textures.size());
81+
82+
if (t0 < textures.size() && ti < textures.size() && ti1 < textures.size()) {
83+
tex[0] = textures[t0];
84+
tex[1] = textures[ti];
85+
tex[2] = textures[ti1];
86+
}
87+
}
88+
89+
addFace(v_indices[0], v_indices[i], v_indices[i + 1], tex[0], tex[1], tex[2], WHITE);
90+
}
5091
}
5192
}
5293
}
5394
file.close();
95+
96+
// Center the mesh to its origin
97+
if (!vertices.empty()) {
98+
Vector3D center(0, 0, 0);
99+
for (const auto& v : vertices) {
100+
center.x += v.x;
101+
center.y += v.y;
102+
center.z += v.z;
103+
}
104+
center.x /= vertices.size();
105+
center.y /= vertices.size();
106+
center.z /= vertices.size();
107+
108+
for (auto& v : vertices) {
109+
v.x -= center.x;
110+
v.y -= center.y;
111+
v.z -= center.z;
112+
}
113+
}
114+
54115
return true;
55116
} else {
56117
std::cerr << "Failed to open mesh file: " << filename << std::endl;

include/kiwigl/graphics/display.hpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,8 +188,7 @@ class Display {
188188
// Method to update the display
189189
void update() {
190190
#ifndef BENCHMARK_MODE
191-
while (!SDL_TICKS_PASSED(SDL_GetTicks(), prevTime + FRAME_TIME))
192-
;
191+
while (!SDL_TICKS_PASSED(SDL_GetTicks(), prevTime + FRAME_TIME));
193192
prevTime = SDL_GetTicks();
194193
#endif
195194

0 commit comments

Comments
 (0)