-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgameobj.cpp
More file actions
113 lines (97 loc) · 3.84 KB
/
gameobj.cpp
File metadata and controls
113 lines (97 loc) · 3.84 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
#include <GL/glew.h>
#include <glm/glm.hpp>
#include <stdio.h>
#include <objloader.hpp>
#include <texture.hpp>
#include <gameobj.hpp>
#include <string>
GameObject::GameObject(
std::vector<glm::vec3>& vertices,
std::vector<glm::vec2>& uvs,
std::vector<glm::vec3>& normals,
std::vector<glm::vec3>& tangents,
std::vector<glm::vec3>& bitangents,
GLuint textureID,
GLuint normalMapID,
glm::vec3 objCenter
) {
printf("\n=== Creating GameObject from pre-loaded data ===\n");
// Initialize
this->modelMatrix = glm::mat4(1.0f);
this->VAO = 0;
this->vertexBuffer = 0;
this->uvBuffer = 0;
this->normalBuffer = 0;
this->lightID = -1;
this->isWall = false;
this->objectCenter = objCenter;
// Assign the data from the loader
this->textureID = textureID;
this->normalMapID = normalMapID;
this->vertexCount = vertices.size();
if(normalMapID != 0) {
this->isWall = true;
}
// Create VAO
glGenVertexArrays(1, &this->VAO);
glBindVertexArray(this->VAO);
printf("VAO: %d\n", this->VAO);
// Vertex buffer
glGenBuffers(1, &this->vertexBuffer);
glBindBuffer(GL_ARRAY_BUFFER, this->vertexBuffer);
glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(glm::vec3), &vertices[0], GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);
// UV buffer
glGenBuffers(1, &this->uvBuffer);
glBindBuffer(GL_ARRAY_BUFFER, this->uvBuffer);
glBufferData(GL_ARRAY_BUFFER, uvs.size() * sizeof(glm::vec2), &uvs[0], GL_STATIC_DRAW);
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, (void*)0);
// Normal buffer
glGenBuffers(1, &this->normalBuffer);
glBindBuffer(GL_ARRAY_BUFFER, this->normalBuffer);
glBufferData(GL_ARRAY_BUFFER, normals.size() * sizeof(glm::vec3), &normals[0], GL_STATIC_DRAW);
glEnableVertexAttribArray(2);
glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);
// --- 3: TANGENTS (NEW) ---
glGenBuffers(1, &this->tangentBuffer);
glBindBuffer(GL_ARRAY_BUFFER, this->tangentBuffer);
glBufferData(GL_ARRAY_BUFFER, tangents.size() * sizeof(glm::vec3), &tangents[0], GL_STATIC_DRAW);
glEnableVertexAttribArray(3);
glVertexAttribPointer(3, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);
// --- 4: BITANGENTS (NEW) ---
glGenBuffers(1, &this->bitangentBuffer);
glBindBuffer(GL_ARRAY_BUFFER, this->bitangentBuffer);
glBufferData(GL_ARRAY_BUFFER, bitangents.size() * sizeof(glm::vec3), &bitangents[0], GL_STATIC_DRAW);
glEnableVertexAttribArray(4);
glVertexAttribPointer(4, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);
glBindVertexArray(0);
printf("GameObject created successfully from data!\n");
}
GameObject::~GameObject() {
printf("Destroying GameObject (VAO: %d)\n", this->VAO);
if (this->vertexBuffer) glDeleteBuffers(1, &this->vertexBuffer);
if (this->uvBuffer) glDeleteBuffers(1, &this->uvBuffer);
if (this->normalBuffer) glDeleteBuffers(1, &this->normalBuffer);
if (this->textureID) glDeleteTextures(1, &this->textureID);
if (this->VAO) glDeleteVertexArrays(1, &this->VAO);
}
void GameObject::Draw(const glm::mat4& projection, const glm::mat4& view,
GLuint mvpUniform, GLuint modelUniform, GLuint textureUniform) {
if (this->vertexCount == 0) return;
// Calculate MVP
glm::mat4 MVP = projection * view * this->modelMatrix;
// Set uniforms
glUniformMatrix4fv(mvpUniform, 1, GL_FALSE, &MVP[0][0]);
glUniformMatrix4fv(modelUniform, 1, GL_FALSE, &this->modelMatrix[0][0]);
// Bind texture
if (this->textureID != 0) {
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, this->textureID);
glUniform1i(textureUniform, 0);
}
// Bind VAO and draw
glBindVertexArray(this->VAO);
glDrawArrays(GL_TRIANGLES, 0, (GLsizei)this->vertexCount);
}