Skip to content

Commit 8cdb7e2

Browse files
committed
reorganize model class
1 parent fb43f04 commit 8cdb7e2

17 files changed

Lines changed: 191 additions & 378 deletions

include/Triangle.hpp

Lines changed: 0 additions & 20 deletions
This file was deleted.

include/Vertex.hpp

Lines changed: 40 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,53 @@
22

33
#include <glm/glm.hpp>
44

5-
struct Vertex {
6-
glm::vec4 Position; // Position of the vertex in homogenous coordinates
7-
glm::vec3 Normal; // Normal vector for the vertex
8-
glm::vec2 TexCoords; // Texture coordinates
9-
glm::vec3 Color; // Color of the vertex
5+
#include "color.h"
6+
7+
namespace simple_renderer {
8+
9+
class Vertex {
10+
public:
11+
12+
// Default constructor
13+
Vertex() = default;
14+
// Copy constructor
15+
Vertex(const Vertex& vertex) = default;
16+
Vertex& operator=(const Vertex& vertex) = default;
17+
// Move constructor
18+
Vertex(Vertex&& vertex) = default;
19+
Vertex& operator=(Vertex&& vertex) = default;
20+
// Destructor
21+
~Vertex() = default;
1022

1123
// Constructor with parameters
12-
Vertex(const glm::vec4& pos, const glm::vec3& norm, const glm::vec2& tex, const glm::vec3& color = glm::vec3(1.0f, 1.0f, 1.0f))
13-
: Position(pos), Normal(norm), TexCoords(tex), Color(color) {}
24+
explicit Vertex(const glm::vec4& pos, const glm::vec3& norm, const glm::vec2& tex, const Color& color_)
25+
: position_(pos), normal_(norm), texCoords_(tex), color_(color_) {}
1426

1527
// Transform the vertex with a matrix
16-
Vertex transform(const glm::mat4& matrix) const {
17-
return Vertex(matrix * Position, Normal, TexCoords, Color);
28+
void transform(const glm::mat4& matrix) {
29+
position_ = matrix * position_;
1830
}
1931

2032
// Perspective divide to convert from clip space to normalized device coordinates
2133
void perspectiveDivide() {
22-
if (Position.w != 0) {
23-
Position.x /= Position.w;
24-
Position.y /= Position.w;
25-
Position.z /= Position.w;
26-
Position.w = 1.0f; // Homogenize
34+
if (position_.w != 0) {
35+
position_.x /= position_.w;
36+
position_.y /= position_.w;
37+
position_.z /= position_.w;
38+
position_.w = 1.0f; // Homogenize
2739
}
2840
}
29-
};
41+
42+
glm::vec4 position() const { return position_; }
43+
glm::vec3 normal() const { return normal_; }
44+
glm::vec2 texCoords() const { return texCoords_; }
45+
Color color() const { return color_; }
46+
47+
private:
48+
glm::vec4 position_; // Position of the vertex in homogenous coordinates
49+
glm::vec3 normal_; // Normal vector for the vertex
50+
glm::vec2 texCoords_; // Texture coordinates
51+
Color color_; // Color of the vertex
52+
};
53+
54+
}

include/light.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
#include <string>
2222

2323
#include "color.h"
24-
#include "vector.hpp"
24+
#include "log_math.hpp"
2525

2626
namespace simple_renderer {
2727

include/material.hpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#pragma once
2+
3+
#include <glm/glm.hpp>
4+
5+
class Material {
6+
public:
7+
// Default constructor
8+
Material() = default;
9+
// Copy constructor
10+
Material(const Material& material) = default;
11+
Material& operator=(const Material& material) = default;
12+
// Move constructor
13+
Material(Material&& material) = default;
14+
Material& operator=(Material&& material) = default;
15+
// Destructor
16+
~Material() = default;
17+
18+
float shininess = 0.0f;
19+
glm::vec3 ambient;
20+
glm::vec3 diffuse;
21+
glm::vec3 specular;
22+
};

include/matrix.hpp

Lines changed: 0 additions & 44 deletions
This file was deleted.

include/model.hpp

Lines changed: 20 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -25,90 +25,43 @@
2525
#include "color.h"
2626
#include "config.h"
2727
#include "log_system.h"
28-
#include "matrix.hpp"
29-
#include "vector.hpp"
28+
#include "log_math.hpp"
3029

31-
namespace simple_renderer {
32-
33-
class Material {
34-
public:
35-
Material() = default;
36-
Material(const Material& material) = default;
37-
Material(Material&& material) = default;
38-
auto operator=(const Material& material) -> Material& = default;
39-
auto operator=(Material&& material) -> Material& = default;
40-
~Material() = default;
41-
42-
float shininess = 0.0f;
43-
glm::vec3 ambient;
44-
glm::vec3 diffuse;
45-
glm::vec3 specular;
46-
};
47-
48-
class Vertex {
49-
public:
50-
explicit Vertex(glm::vec3 coord, glm::vec3 normal, glm::vec2 texture_coord, const Color& color);
51-
Vertex() = default;
52-
Vertex(const Vertex& vertex) = default;
53-
Vertex(Vertex&& vertex) = default;
54-
auto operator=(const Vertex& vertex) -> Vertex& = default;
55-
auto operator=(Vertex&& vertex) -> Vertex& = default;
56-
~Vertex() = default;
57-
58-
[[nodiscard]] Vertex operator*(const glm::mat4 &tran) const;
59-
60-
glm::vec3 coord_;
61-
glm::vec3 normal_;
62-
glm::vec2 texture_coord_;
63-
Color color_;
64-
};
30+
#include "vertex.hpp"
31+
#include "face.hpp"
6532

66-
class Face {
67-
public:
68-
explicit Face(const Vertex& v0, const Vertex& v1, const Vertex& v2, Material material);
69-
Face() = default;
70-
Face(const Face& face) = default;
71-
Face(Face&& face) = default;
72-
auto operator=(const Face& face) -> Face& = default;
73-
auto operator=(Face&& face) -> Face& = default;
74-
~Face() = default;
75-
76-
[[nodiscard]] Face operator*(const glm::mat4 &tran) const;
77-
78-
Vertex v0_;
79-
Vertex v1_;
80-
Vertex v2_;
81-
glm::vec3 normal_;
82-
Material material_;
83-
};
33+
namespace simple_renderer {
8434

8535
class Model {
8636
public:
87-
Model(const std::string &model_path);
37+
38+
// Default constructor
8839
Model() = default;
40+
// Default copy constructor
8941
Model(const Model& model) = default;
42+
Model& operator=(const Model& model) = default;
43+
// Default move constructor
9044
Model(Model&& model) = default;
91-
auto operator=(const Model& model) -> Model& = default;
92-
auto operator=(Model&& model) -> Model& = default;
45+
Model& operator=(Model&& model) = default;
9346
~Model() = default;
9447

95-
[[nodiscard]] Model operator*(const glm::mat4 &tran) const;
48+
Model(const std::string &model_path);
49+
50+
void transform(const glm::mat4 &tran);
9651

97-
const std::vector<Face>& GetFaces() const;
98-
const std::string ModelPath() const;
52+
const std::vector<Face>& faces() const { return faces_; };
53+
const std::string& modelPath() const { return directory_; };
9954

10055
private:
10156
static constexpr const uint8_t kTriangleFaceVertexCount = 3;
57+
std::string directory_;
10258

10359
std::vector<Face> faces_;
104-
std::string model_path_ = "";
10560

106-
std::pair<glm::vec3, glm::vec3> GetMaxMinXYZ() const;
107-
108-
void NormalizeModel();
109-
void LoadModel(const std::string& path);
110-
void ProcessNode(aiNode* node, const aiScene* scene);
111-
Face ProcessMesh(aiMesh* mesh, const aiScene* scene);
61+
void loadModel(const std::string& path);
62+
void processNode(aiNode* node, const aiScene* scene);
63+
void processMesh(aiMesh* mesh, const aiScene* scene);
64+
Material processMaterial(aiMaterial* material);
11265
};
11366
} // namespace simple_renderer
11467

include/shader_base.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,8 @@
1717
#ifndef SIMPLERENDER_SRC_INCLUDE_SHADER_BASE_H_
1818
#define SIMPLERENDER_SRC_INCLUDE_SHADER_BASE_H_
1919

20-
#include "matrix.hpp"
2120
#include "model.hpp"
22-
#include "vector.hpp"
21+
#include "log_math.hpp"
2322

2423
namespace simple_renderer {
2524

include/simple_renderer.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,9 @@
2323

2424
#include "light.h"
2525
#include "log_system.h"
26-
#include "matrix.hpp"
2726
#include "model.hpp"
2827
#include "shader_base.h"
29-
#include "vector.hpp"
28+
#include "log_math.hpp"
3029

3130
namespace simple_renderer {
3231

include/vector.hpp

Lines changed: 0 additions & 57 deletions
This file was deleted.

src/default_shader.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,10 @@ auto DefaultShader::Vertex(const ShaderVertexIn &shader_vertex_in) const
4747
-> ShaderVertexOut {
4848
auto face(shader_vertex_in.face_);
4949

50-
face = face * shader_data_.model_matrix_ * shader_data_.view_matrix_ *
51-
shader_data_.project_matrix_;
50+
face.transform(
51+
shader_data_.project_matrix_
52+
* shader_data_.view_matrix_
53+
* shader_data_.model_matrix_);
5254

5355
/// @todo 变换贴图
5456
return ShaderVertexOut(face);

0 commit comments

Comments
 (0)