-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModel.hpp
More file actions
223 lines (176 loc) · 7 KB
/
Copy pathModel.hpp
File metadata and controls
223 lines (176 loc) · 7 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#pragma once
#include <iostream>
#include <vector>
#include <glm/common.hpp>
#define GLM_ENABLE_EXPERIMENTAL
#include <glm/gtx/transform.hpp>
#include <fstream>
#include <string>
#include "Shader.hpp"
using namespace std;
class Vertex {
public:
glm::vec3 pos;
glm::vec2 texCoord;
};
class Model {
public:
vector<Vertex> vertices;
vector<uint32_t> indices;
vector<glm::vec3> instancePoses;
glm::vec3 centorPos, maxPos, minPos;
glm::mat4 modelMatrix;
GLuint VAO, VBO, EBO, instanceVBO;
Model() {
modelMatrix = glm::mat4{1};
}
~Model() {
}
void setVertices(vector<Vertex> vertices_) {
vertices = vertices_;
}
void setIndices(vector<uint32_t> indices_) {
indices = indices_;
}
void transliate(glm::vec3 movement_) {
modelMatrix = modelMatrix * glm::translate(movement_);
}
void rotate(glm::vec3 axis_, float angle_) {
modelMatrix = modelMatrix * glm::rotate(angle_, axis_);
}
void scale(float factor_) {
modelMatrix = modelMatrix * glm::scale(glm::vec3{factor_});
}
void loadObj(string fileName_) {
bool isVertexStart = false;
int commentNum = 0;
ifstream objStream(fileName_, ios::in);
float max_x, max_y, max_z;
max_x = max_y = max_z = -99999999999999.0f;
float min_x, min_y, min_z;
min_x, min_y, min_z = 99999999999999.0f;
if(objStream.is_open()) {
string line;
float x, y, z;
while(getline(objStream,line)) {
vector<string> splitVal = split(line, " " );
if (splitVal[0] == "v") {
isVertexStart = true;
x = stof(splitVal[1]);
y = stof(splitVal[2]);
z = stof(splitVal[3]);
x <= min_x ? (min_x = x) : true;
y <= min_y ? (min_y = y) : true;
z <= min_z ? (min_z = z) : true;
x >= max_x ? (max_x = x) : true;
y >= max_y ? (max_y = y) : true;
z >= max_z ? (max_z = z) : true;
vertices.push_back (Vertex {
glm::vec3(x,y,z)
});
} else if (splitVal[0] == "f") {
indices.push_back(stoi(splitVal[1]) - commentNum+1);
indices.push_back(stoi(splitVal[2]) - commentNum+1);
indices.push_back(stoi(splitVal[3]) - commentNum+1);
} else {
if (isVertexStart==false) {
commentNum ++;
}
}
}
}
for (auto &itr : vertices) {
itr.pos -= glm::vec3((max_x + min_x)*0.5, (max_y + min_y)*0.5, (max_z + min_z)*0.5);
}
objStream.close();
}
void addInstance(vector<glm::vec3> instancePoses_) {
for (auto &itr : instancePoses_) {
instancePoses.push_back(itr);
}
// instance VBO
glBindBuffer(GL_ARRAY_BUFFER, instanceVBO);
glBufferData(GL_ARRAY_BUFFER, instancePoses.size()*sizeof(glm::vec3), instancePoses.data(), GL_STATIC_DRAW);
glBindVertexArray(VAO);
glEnableVertexAttribArray(3);
glVertexAttribPointer(3, 3, GL_FLOAT, GL_FALSE, sizeof(glm::vec3), (void*)0);
glVertexAttribDivisor(3,1);
glBindVertexArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
void updateInstance(vector<glm::vec3> instancePoses_) {
this->instancePoses = instancePoses_;
// instance VBO
glBindBuffer(GL_ARRAY_BUFFER, instanceVBO);
glBufferData(GL_ARRAY_BUFFER, instancePoses.size()*sizeof(glm::vec3), instancePoses.data(), GL_STATIC_DRAW);
glBindVertexArray(VAO);
glEnableVertexAttribArray(3);
glVertexAttribPointer(3, 3, GL_FLOAT, GL_FALSE, sizeof(glm::vec3), (void*)0);
glVertexAttribDivisor(3,1);
glBindVertexArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
void setupModel() {
//this->instancePoses.push_back(glm::vec3(0,0,0));
glGenVertexArrays(1, &VAO);
glBindVertexArray(VAO); //순서.. 반드시 여기와야함?
glGenBuffers(1, &VBO);
glGenBuffers(1, &EBO);
glGenBuffers(1, &instanceVBO);
// setting VBO
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(Vertex), &vertices[0], GL_STATIC_DRAW);
// setting EBO
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(uint32_t), &indices[0], GL_STATIC_DRAW);
// setting VAO
// vertex positions
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, pos));
// vertex normals
// glEnableVertexAttribArray(1);
// glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, normal));
// vertex texture coords
glEnableVertexAttribArray(2);
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, texCoord));
// instance VBO
glBindBuffer(GL_ARRAY_BUFFER, instanceVBO);
glBufferData(GL_ARRAY_BUFFER, instancePoses.size()*sizeof(glm::vec3), &instancePoses[0], GL_STATIC_DRAW);
// setting VAO for instance VBO
glEnableVertexAttribArray(3);
glVertexAttribPointer(3, 3, GL_FLOAT, GL_FALSE, sizeof(glm::vec3), (void*)0);
glVertexAttribDivisor(3,1);
glBindVertexArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
}
void updateModel() {
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferSubData(GL_ARRAY_BUFFER, 0, vertices.size() * sizeof(Vertex), &vertices[0]); //replace data in VBO with new data
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
void draw(Shader* pShader) {
glUseProgram(pShader->shaderId);
pShader->set_uniform("modelMatrix",modelMatrix);
// glBindBuffer(GL_ARRAY_BUFFER, VBO); 주석해도 돌아감
// glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBindVertexArray(VAO);
//glPolygonMode(GL_FRONT, GL_LINES);
glDrawElementsInstanced(GL_TRIANGLES, indices.size(), GL_UNSIGNED_INT, 0, instancePoses.size());
//glDrawElements(GL_TRIANGLES, indices.size(), GL_UNSIGNED_INT, 0);
glBindVertexArray(0);
}
private:
vector<string> split(string target_, string delimiter_) {
vector<string> retVal;
size_t startPos = 0;
size_t endPos = target_.find(delimiter_);
while(endPos != string::npos) {
retVal.push_back(target_.substr(startPos,endPos-startPos));
startPos = endPos + delimiter_.length();
endPos = target_.find(delimiter_, startPos);
}
retVal.push_back(target_.substr(startPos,target_.size()-1));
return retVal;
}
};