Skip to content

Commit

Permalink
Code formatting and linting
Browse files Browse the repository at this point in the history
  • Loading branch information
Kaweees committed Nov 27, 2024
1 parent 880366c commit e9a5c16
Show file tree
Hide file tree
Showing 10 changed files with 533 additions and 545 deletions.
18 changes: 9 additions & 9 deletions examples/f22/f22.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@
#include <kiwigl/kiwigl.hpp>

#ifdef USE_METAL
#include <cassert>
# include <cassert>

#define NS_PRIVATE_IMPLEMENTATION
#define MTL_PRIVATE_IMPLEMENTATION
#define MTK_PRIVATE_IMPLEMENTATION
#define CA_PRIVATE_IMPLEMENTATION
#include <simd/simd.h>
# define NS_PRIVATE_IMPLEMENTATION
# define MTL_PRIVATE_IMPLEMENTATION
# define MTK_PRIVATE_IMPLEMENTATION
# define CA_PRIVATE_IMPLEMENTATION
# include <simd/simd.h>

#include <AppKit/AppKit.hpp>
#include <Metal/Metal.hpp>
#include <MetalKit/MetalKit.hpp>
# include <AppKit/AppKit.hpp>
# include <Metal/Metal.hpp>
# include <MetalKit/MetalKit.hpp>
#endif

//------------------------------------------------------------------------------------
Expand Down
12 changes: 6 additions & 6 deletions include/kiwigl/core/constants.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
#include "../graphics/color.hpp"

namespace kiwigl {
// constexpr int WIDTH = 640;
constexpr int HEIGHT = 480;
constexpr int FRAME_RATE = 60;
constexpr int FRAME_TIME = 1000 / FRAME_RATE;
constexpr int FOV = 640;
const Color WHITE = Color(255, 255, 255, 255);
// constexpr int WIDTH = 640;
constexpr int HEIGHT = 480;
constexpr int FRAME_RATE = 60;
constexpr int FRAME_TIME = 1000 / FRAME_RATE;
constexpr int FOV = 640;
const Color WHITE = Color(255, 255, 255, 255);
} // namespace kiwigl
30 changes: 15 additions & 15 deletions include/kiwigl/geometry/face.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,24 @@
#include "../texture/texture.hpp"

namespace kiwigl {
// Represents a two-dimensional texture
class Face {
// Represents a face of a 3D object
class Face {
public:
// Constructor to initialize memory
Face(int v1, int v2, int v3, const Texture2D& t1, const Texture2D& t2,
const Texture2D& t3, const Color& color)
: vertexIndices{v1, v2, v3}, textures{t1, t2, t3}, color(color) {}
// Constructor to initialize memory
Face(int v1, int v2, int v3, const Texture2D& t1, const Texture2D& t2, const Texture2D& t3,
const Color& color)
: vertexIndices{v1, v2, v3}, textures{t1, t2, t3}, color(color) {}

// Destructor to free the memory allocated
~Face() = default;
// Destructor to free the memory allocated
~Face() = default;

// The indices of the vertices
int vertexIndices[3];
// The indices of the vertices
int vertexIndices[3];

// The indices of the textures
Texture2D textures[3];
// The indices of the textures
Texture2D textures[3];

// The color of the face
Color color;
};
// The color of the face
Color color;
};
} // namespace kiwigl
127 changes: 63 additions & 64 deletions include/kiwigl/geometry/mesh.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,84 +13,83 @@

namespace kiwigl {

class Mesh {
class Mesh {
public:
// Constructor to initialize memory
Mesh() = default;
// Constructor to initialize memory
Mesh() = default;

// Destructor to free the memory allocated
~Mesh() = default;
// Destructor to free the memory allocated
~Mesh() = default;

// Method to load a mesh from a file
void loadMesh(const std::string& filename) {
loadOBJ(filename);
// loadTexture(filename);
}
// Method to load a mesh from a file
void loadMesh(const std::string& filename) {
loadOBJ(filename);
// loadTexture(filename);
}

// Method to load textures from a Wavefront .obj file
bool loadOBJ(const std::string& filename) {
std::ifstream file(filename, std::ios::binary);
if (file.is_open()) {
std::string line;
while (std::getline(file, line) && !file.eof()) {
if (line.find("v ") != std::string::npos) {
// Parse the vertex
Vector3D vertex;
if (sscanf(line.c_str(), "v %lf %lf %lf", &vertex.x, &vertex.y,
&vertex.z) == 3) {
addVertex(vertex);
}
} else if (line.find("vt ") != std::string::npos) {
// Parse the texture coordinate
Texture2D texture;
if (sscanf(line.c_str(), "vt %lf %lf", &texture.u, &texture.v) == 2) {
addTexture(texture);
}
} else if (line.find("f ") != std::string::npos) {
// Parse the face
int v1, v2, v3, t1, t2, t3, n1, n2, n3;
if (sscanf(line.c_str(), "f %d/%d/%d %d/%d/%d %d/%d/%d", &v1, &t1,
&n1, &v2, &t2, &n2, &v3, &t3, &n3) == 9) {
addFace(v1, v2, v3, textures[t1 - 1], textures[t2 - 1],
textures[t3 - 1], WHITE);
// Method to load textures from a Wavefront .obj file
bool loadOBJ(const std::string& filename) {
std::ifstream file(filename, std::ios::binary);
if (file.is_open()) {
std::string line;
while (std::getline(file, line) && !file.eof()) {
if (line.find("v ") != std::string::npos) {
// Parse the vertex
Vector3D vertex;
if (sscanf(line.c_str(), "v %lf %lf %lf", &vertex.x, &vertex.y, &vertex.z) == 3) {
addVertex(vertex);
}
} else if (line.find("vt ") != std::string::npos) {
// Parse the texture coordinate
Texture2D texture;
if (sscanf(line.c_str(), "vt %lf %lf", &texture.u, &texture.v) == 2) {
addTexture(texture);
}
} else if (line.find("f ") != std::string::npos) {
// Parse the face
int v1, v2, v3, t1, t2, t3, n1, n2, n3;
if (sscanf(line.c_str(), "f %d/%d/%d %d/%d/%d %d/%d/%d", &v1, &t1, &n1, &v2, &t2, &n2,
&v3, &t3, &n3)
== 9) {
addFace(v1, v2, v3, textures[t1 - 1], textures[t2 - 1], textures[t3 - 1], WHITE);
}
}
}
file.close();
return true;
} else {
std::cerr << "Failed to open mesh file: " << filename << std::endl;
return false;
}
file.close();
return true;
} else {
std::cerr << "Failed to open mesh file: " << filename << std::endl;
return false;
}
}

// Method to load textures from a file
bool loadTextures(const std::string& filename);
// Method to load textures from a file
bool loadTextures(const std::string& filename);

// Method to get vertices
const std::vector<Vector3D>& getVertices() const { return vertices; }
// Method to get vertices
const std::vector<Vector3D>& getVertices() const { return vertices; }

// Method to add a vertex
void addVertex(const Vector3D& vertex) { vertices.push_back(vertex); }
// Method to add a vertex
void addVertex(const Vector3D& vertex) { vertices.push_back(vertex); }

// Method to get textures
const std::vector<Texture2D>& getTextures() const { return textures; }
// Method to get textures
const std::vector<Texture2D>& getTextures() const { return textures; }

// Method to add a texture
void addTexture(const Texture2D& texture) { textures.push_back(texture); }
// Method to add a texture
void addTexture(const Texture2D& texture) { textures.push_back(texture); }

// Method to add a face
void addFace(int v1, int v2, int v3, const Texture2D& t1, const Texture2D& t2,
const Texture2D& t3, const Color& color) {
faces.push_back(Face(v1, v2, v3, t1, t2, t3, color));
}
// Method to add a face
void addFace(int v1, int v2, int v3, const Texture2D& t1, const Texture2D& t2,
const Texture2D& t3, const Color& color) {
faces.push_back(Face(v1, v2, v3, t1, t2, t3, color));
}

std::vector<Vector3D> vertices;
std::vector<Face> faces;
std::vector<Texture2D> textures;
Vector3D scale;
Vector3D rotation;
Vector3D translation;
};
std::vector<Vector3D> vertices;
std::vector<Face> faces;
std::vector<Texture2D> textures;
Vector3D scale;
Vector3D rotation;
Vector3D translation;
};

} // namespace kiwigl
14 changes: 7 additions & 7 deletions include/kiwigl/geometry/triangle.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
#include "vector.hpp"

namespace kiwigl {
class Triangle {
class Triangle {
public:
// Constructor to initialize memory
Triangle() = default;
// Constructor to initialize memory
Triangle() = default;

// Destructor to free the memory allocated
~Triangle() = default;
// Destructor to free the memory allocated
~Triangle() = default;

Vector2D points[3];
};
Vector2D points[3];
};
} // namespace kiwigl
Loading

0 comments on commit e9a5c16

Please sign in to comment.