-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModel.cpp
More file actions
113 lines (94 loc) · 2.4 KB
/
Copy pathModel.cpp
File metadata and controls
113 lines (94 loc) · 2.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
//
// Created by 郭珂桢 on 2024/5/29.
//
#include "Model.h"
Model::Model(const Mesh &mesh)
{
AddData(mesh);
}
Model::~Model()
{
DeleteData();
}
Model::Model(Model &&other)
: m_renderInfo(other.m_renderInfo),
m_vboCount(other.m_vboCount),
m_buffers(std::move(other.m_buffers))
{
other.m_renderInfo.Reset();
other.m_vboCount = 0;
}
Model &Model::operator=(Model &&other)
{
if (this != &other) {
DeleteData();
m_renderInfo = other.m_renderInfo;
m_vboCount = other.m_vboCount;
m_buffers = std::move(other.m_buffers);
other.m_renderInfo.Reset();
other.m_vboCount = 0;
}
return *this;
}
void Model::AddData(const Mesh &mesh)
{
GenVAO();
AddVBO(3, mesh.vertexPositions);
AddVBO(2, mesh.textureCoords);
AddEBO(mesh.indices);
}
void Model::DeleteData()
{
if (m_renderInfo.VAO)
glDeleteVertexArrays(1, &m_renderInfo.VAO);
if (m_buffers.size() > 0)
glDeleteBuffers(static_cast<GLsizei>(m_buffers.size()), m_buffers.data());
m_buffers.clear();
m_vboCount = 0;
m_renderInfo.Reset();
}
void Model::GenVAO()
{
if (m_renderInfo.VAO != 0)
DeleteData();
glGenVertexArrays(1, &m_renderInfo.VAO);
glBindVertexArray(m_renderInfo.VAO);
}
void Model::AddEBO(const std::vector<GLuint> &indices)
{
m_renderInfo.IndicesCount = static_cast<GLuint>(indices.size());
GLuint EBO;
glGenBuffers(1, &EBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(GLuint),
indices.data(), GL_STATIC_DRAW);
m_buffers.push_back(EBO);
}
void Model::AddVBO(int dimensions, const std::vector<GLfloat> &data)
{
GLuint VBO;
glGenBuffers(1, &VBO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, data.size() * sizeof(GLfloat), data.data(),
GL_STATIC_DRAW);
glVertexAttribPointer(static_cast<GLuint>(m_vboCount), dimensions, GL_FLOAT,
GL_FALSE, 0, (GLvoid *)0);
glEnableVertexAttribArray(static_cast<GLuint>(m_vboCount++));
m_buffers.push_back(VBO);
}
void Model::BindVAO() const
{
glBindVertexArray(m_renderInfo.VAO);
}
void Model::UnbindVAO() const
{
glBindVertexArray(0);
}
unsigned int Model::GetIndicesCount() const
{
return m_renderInfo.IndicesCount;
}
const RenderInfo &Model::GetRenderInfo() const
{
return m_renderInfo;
}