/
Renderer.cpp
324 lines (277 loc) · 8.03 KB
/
Renderer.cpp
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
#include "Renderer.h"
Renderer::Renderer()
{
//the shader used for rendering models
this->debug=true;
this->modelShader.compileShaderFromFile("model.vsh",GLSLShader::VERTEX);
this->modelShader.compileShaderFromFile("model.fsh",GLSLShader::FRAGMENT);
this->modelShader.bindAttribLocation(0,"vertexPosition");
this->modelShader.bindAttribLocation(1,"vertexNormal");
this->modelShader.bindAttribLocation(2,"vertexUv");
if(debug)
cout<<this->modelShader.log();
this->modelShader.link();
if(debug)
cout<<this->modelShader.log();
this->modelShader.setUniform("tex1",0);
//the shader used for rendering the terrain
this->debug=true;
//bbox
this->bBoxShader.compileShaderFromFile("bbox.vsh",GLSLShader::VERTEX);
this->bBoxShader.compileShaderFromFile("bbox.fsh",GLSLShader::FRAGMENT);
this->bBoxShader.bindAttribLocation(3,"bBoxCordsVertex");
if(debug)
cout<<this->bBoxShader.log();
this->bBoxShader.link();
if(debug)
cout<<this->bBoxShader.log();
glUseProgram(0);
if(debug)
{
//info
const GLubyte *vendor = glGetString(GL_VENDOR);
const GLubyte *renderer = glGetString(GL_RENDERER);
const GLubyte *version = glGetString(GL_VERSION);
const GLubyte *glslver = glGetString(GL_SHADING_LANGUAGE_VERSION);
GLint major,minor;
glGetIntegerv(GL_MAJOR_VERSION,&major);
glGetIntegerv(GL_MINOR_VERSION,&minor);
cout <<"vendor: "<<vendor<<endl;
cout <<"renderer: "<<renderer<<endl;
cout <<"openGL version: "<<version<<endl;
cout <<"GLSL version: "<<glslver<<endl;
cout <<"GL version, maj min: "<<major<<minor<<endl;
}
glEnable(GL_DEPTH_TEST); // enable depth-testing
glDepthFunc(GL_LESS); // set depth-testing function type
//glEnable(GL_CULL_FACE); // enable culling of back-faces
//glCullFace(GL_BACK); // enable culling of back-faces
//glFrontFace(GL_CCW);
this->viewMatrix=mat4(0.0f);
this->projMatrix=mat4(0.0f);
}
Renderer::~Renderer()
{
}
void Renderer::draw()
{
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
//renderin models
this->modelShader.use();
mat4 mvp=mat4(0.0f);
glActiveTexture(GL_TEXTURE0);
for(unsigned int i=0;i<this->models.size();i++)
{
//set upp uniforms for rendering call
mvp=this->projMatrix*this->viewMatrix*this->models[i].getModelMatrix();
this->modelShader.setUniform("modelMatrix",this->models[i].getModelMatrix());
this->modelShader.setUniform("MVP",mvp);
mat3 normalMatrix=transpose(inverse(mat3(this->viewMatrix*this->models[i].getModelMatrix())));
this->modelShader.setUniform("normalMatrix",normalMatrix);
//get a pointer to a vector with meshes info for each mesh
for(unsigned int j=0;j<this->models[i].getMeshInfo()->size();j++)
{
glBindTexture(GL_TEXTURE_2D, this->models[i].getMeshInfo()->at(j).getTexh());
glBindVertexArray(this->models[i].getMeshInfo()->at(j).getVaoh());
glDrawArrays(GL_TRIANGLES,0,this->models[i].getMeshInfo()->at(j).getNrOfVerts());
}
}
this->bBoxShader.use();
this->bBoxShader.setUniform("ro",0.0f);
this->bBoxShader.setUniform("go",1.0f);
this->bBoxShader.setUniform("bo",0.0f);
//renders bounding box
glLineWidth(2);
for(unsigned int i=0;i<this->models.size();i++)
{
//if the object is selected
if(this->models[i].isSelected())
{
//set upp uniforms for rendering call
mvp=this->projMatrix*this->viewMatrix*this->models[i].getModelMatrix();
this->modelShader.setUniform("MVP",mvp);
glBindVertexArray(this->models[i].getBoundingBox()->getVaoh());
glDrawArrays(GL_LINE_STRIP,0,16);
}
}
glUseProgram(0);
glBindVertexArray(0);
glDisable(GL_CULL_FACE);
}
void Renderer::updateViewMatrix(mat4 viewMatrix)
{
this->viewMatrix=viewMatrix;
this->modelShader.use();
this->modelShader.setUniform("viewMatrix",this->viewMatrix);
glUseProgram(0);
}
void Renderer::updateProjMatrix(float width, float height)
{
this->wheight=height;
this->wwidth=width;
float nearClip = 0.5f;
float farClip = 1000.0f;
float fov_deg = 45.0f;
float aspect = (float)width/(float)height;
this->projMatrix=perspective(fov_deg, aspect,nearClip,farClip);
this->modelShader.use();
this->modelShader.setUniform("projectionMatrix",this->projMatrix);
glUseProgram(0);
}
void Renderer::addModel(Model m)
{
this->models.push_back(m);
}
mat4 Renderer::getProjMatrix()
{
return this->projMatrix;
}
int Renderer::selectModel(float normalizedX, float normalizedY,vec3 pos)
{
float min=999999.0f;
int minIndex=-1;
for(unsigned int j=0;j<this->models.size();j++)
{
bool found=true;
float t1=0;
float t2=0;
mat4 unview = inverse(this->projMatrix*this->viewMatrix);
vec4 near_point = unview * vec4(normalizedX, normalizedY, 0, 1);
vec4 mm = inverse(mat4(models[j].getModelMatrix()))*vec4(pos,1);
vec3 t = vec3(near_point.x/near_point.w,near_point.y/near_point.w,near_point.z/near_point.w)-pos;
vec3 rayd=inverse(mat3(models[j].getModelMatrix()))*t;
rayd = normalize(rayd);
float sides[3] =
{
this->models.at(j).getBoundingBox()->getBboxSide().x,
this->models.at(j).getBoundingBox()->getBboxSide().y,
this->models.at(j).getBoundingBox()->getBboxSide().z
};
vec3 normalizedSides[3] =
{
vec3(1,0,0),
vec3(0,1,0),
vec3(0,0,1)
};
float tmin=-0.000001;
float tmax=99999999;
vec3 p = this->models.at(j).getBoundingBox()->getBboxPos()-vec3(mm.x/mm.w,mm.y/mm.w,mm.z/mm.w);
for(int i=0;i<3;i++)
{
float e = dot(normalizedSides[i],p);
float f = dot(normalizedSides[i],rayd);
if(abs(f)>0.000001)
{
t1=(e+sides[i])/f;
t2=(e-sides[i])/f;
if(t1>t2)
{
float tmp=t1;
t1=t2;
t2=tmp;
}
if(t1>tmin)
tmin=t1;
if(t2<tmax)
tmax=t2;
if(tmin>tmax)
found=false;
if(tmax<0)
found=false;
}
else if(-e-sides[i]>0 || -e+sides[i]<0)
found=false;
}
if(found)
{
if(tmin>0)
{
if(tmin<min)
{
min = tmin;
minIndex=j;
}
}
}
}
if(minIndex>=0)
{
if(this->models[minIndex].isSelected())
this->models[minIndex].unSelect();
else
this->models[minIndex].select();
return minIndex;
}
return -1;
}
void Renderer::saveModels(string path, string filename)
{
fstream file;
string p = path+filename+".txt";
file.open(p.c_str(),fstream::out|fstream::app);
file << "MODELS: name, pos x,y,z, rot x,y,z, scale"<<endl;
for(unsigned int i = 0; i< this->models.size();i++)
{
file << models[i].getMeshName() <<" " << models[i].getPos().x<< " " << models[i].getPos().y<< " " << models[i].getPos().z<< " " << models[i].getRot().x<< " " << models[i].getRot().y<< " " << models[i].getRot().z<< " "<<models[i].getScale()<< " " << models[i].getId()<<endl;
}
file << "end"<<endl;
file.close();
}
void Renderer::clear()
{
this->models.clear();
}
void Renderer::drawModel(Model m)
{
glEnable(GL_BLEND);
glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
this->modelShader.use();
this->modelShader.setUniform("outAlpha",0.7f);
mat4 mvp=mat4(0.0f);
glActiveTexture(GL_TEXTURE0);
//set upp uniforms for rendering call
mvp=this->projMatrix*this->viewMatrix*m.getModelMatrix();
this->modelShader.setUniform("modelMatrix",m.getModelMatrix());
this->modelShader.setUniform("MVP",mvp);
mat3 normalMatrix=transpose(inverse(mat3(this->viewMatrix*m.getModelMatrix())));
this->modelShader.setUniform("normalMatrix",normalMatrix);
//get a pointer to a vector with meshes info for each mesh
for(unsigned int j=0;j<m.getMeshInfo()->size();j++)
{
glBindTexture(GL_TEXTURE_2D, m.getMeshInfo()->at(j).getTexh());
glBindVertexArray(m.getMeshInfo()->at(j).getVaoh());
glDrawArrays(GL_TRIANGLES,0,m.getMeshInfo()->at(j).getNrOfVerts());
}
glDisable(GL_BLEND);
}
vector<Model> Renderer::removeSelectedModels()
{
vector<Model> removedModels;
for(int i=0;i<this->models.size();i++)
{
if(this->models[i].isSelected())
{
removedModels.push_back(this->models[i]);
this->models[i]=this->models[this->models.size()-1];
this->models.pop_back();
i--;
}
}
return removedModels;
}
void Renderer::unselectAllModels()
{
for(int i=0;i<this->models.size();i++)
{
this->models[i].unSelect();
}
}
vector<Model> Renderer::getModels()
{
return this->models;
}
int Renderer::getNrOfModels()
{
return this->models.size();
}