-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModel3d.cpp
More file actions
99 lines (83 loc) · 2.67 KB
/
Model3d.cpp
File metadata and controls
99 lines (83 loc) · 2.67 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
#include "Model3D.h"
#include <glm/gtc/type_ptr.hpp>
// Initialize static members
GLuint Model3D::s_VAO = 0;
GLuint Model3D::s_VBO = 0;
GLuint Model3D::s_EBO = 0;
GLuint Model3D::s_indexCount = 0;
Model3D::Model3D()
: position(0.0f, 0.0f, 0.0f),
rotation(0.0f, 0.0f, 0.0f),
scale(1.0f, 1.0f, 1.0f)
{
}
Model3D::~Model3D()
{
// No cleanup needed - shared resources handled by static method
}
glm::mat4 Model3D::getTransformMatrix() const
{
glm::mat4 transform = glm::mat4(1.0f);
transform = glm::translate(transform, position);
transform = glm::rotate(transform, glm::radians(rotation.x), glm::vec3(1.0f, 0.0f, 0.0f));
transform = glm::rotate(transform, glm::radians(rotation.y), glm::vec3(0.0f, 1.0f, 0.0f));
transform = glm::rotate(transform, glm::radians(rotation.z), glm::vec3(0.0f, 0.0f, 1.0f));
transform = glm::scale(transform, scale);
return transform;
}
void Model3D::initializeSharedMesh(const float* vertices, unsigned int vertexCount,
const unsigned int* indices, unsigned int indexCount)
{
if (vertices == nullptr || indices == nullptr || vertexCount == 0 || indexCount == 0)
return;
s_indexCount = indexCount;
// Generate VAO, VBO, EBO
glGenVertexArrays(1, &s_VAO);
glGenBuffers(1, &s_VBO);
glGenBuffers(1, &s_EBO);
// Bind VAO
glBindVertexArray(s_VAO);
// Bind and fill VBO
glBindBuffer(GL_ARRAY_BUFFER, s_VBO);
glBufferData(GL_ARRAY_BUFFER, vertexCount * sizeof(float), vertices, GL_STATIC_DRAW);
// Bind and fill EBO
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, s_EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indexCount * sizeof(unsigned int), indices, GL_STATIC_DRAW);
// Vertex attribute pointer for position (location 0)
// Each vertex is 3 floats (x, y, z)
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
// Unbind VAO
glBindVertexArray(0);
}
void Model3D::draw(GLuint shaderProgram, GLint transformLoc) const
{
if (s_VAO == 0 || s_indexCount == 0)
return;
// Get and set transformation matrix
glm::mat4 transform = getTransformMatrix();
glUniformMatrix4fv(transformLoc, 1, GL_FALSE, glm::value_ptr(transform));
// Bind and draw
glBindVertexArray(s_VAO);
glDrawElements(GL_TRIANGLES, s_indexCount, GL_UNSIGNED_INT, 0);
glBindVertexArray(0);
}
void Model3D::cleanupSharedMesh()
{
if (s_VAO != 0)
{
glDeleteVertexArrays(1, &s_VAO);
s_VAO = 0;
}
if (s_VBO != 0)
{
glDeleteBuffers(1, &s_VBO);
s_VBO = 0;
}
if (s_EBO != 0)
{
glDeleteBuffers(1, &s_EBO);
s_EBO = 0;
}
s_indexCount = 0;
}