-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrendering_engine.cpp
333 lines (258 loc) · 7.21 KB
/
rendering_engine.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
325
326
327
328
329
330
331
332
333
/* rendering engine
*
* all methods of all classes, and all global functions, which deal with graphics:
* rendering, uploading stuff to GPU, etc
*
*/
#include<Windows.h>
// We use OpenGL (with glew) but the rest of the code is fairly independent from this.
// It should be trivial to switch to, e.g., directX
#include<GL/glew.h>
#include<GL/GL.h>
#include<glm/mat4x4.hpp>
#include"transform.h"
#include"phys_object.h"
#include"custom_classes.h"
#include"mesh.h"
#include"texture.h"
GpuMesh aMesh;
GpuMesh CpuMesh::uploadToGPU(){
GpuMesh res;
glGenBuffers(1, & res.geomBufferId );
glBindBuffer( GL_ARRAY_BUFFER, res.geomBufferId );
glBufferData(
GL_ARRAY_BUFFER, // a buffer containing vertices
sizeof(Vertex)*verts.size(), // how many bytes to copy on GPU
&(verts[0]), // location in CPU of the data to copy on GPU
GL_STATIC_DRAW // please know that this buffer is readonly!
);
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3,GL_FLOAT,
sizeof(Vertex),
(const void*)offsetof(Vertex,pos) );
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glTexCoordPointer(2,GL_FLOAT,
sizeof(Vertex),
(const void*)offsetof(Vertex,uv) );
glGenBuffers(1,&res.connBufferId);
glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, res.connBufferId );
glEnableClientState(GL_INDEX_ARRAY);
glBufferData(
GL_ELEMENT_ARRAY_BUFFER, // a buffer containing vertex indices
sizeof(Tri)*tris.size(),
&(tris[0]),
GL_STATIC_DRAW
);
//glIndexPointer( GL_INT, 0, 0);
res.nElements = tris.size() * 3;
return res;
}
void GpuTexture::bind() const{
glBindTexture( GL_TEXTURE_2D, textureId );
// dear FIXED FUNCTIONALITY shaders, please access my texture (per fragment)
glEnable(GL_TEXTURE_2D);
}
GpuTexture CpuTexture::uploadToGPU() const{
GpuTexture res;
glGenTextures(1, &res.textureId );
glBindTexture( GL_TEXTURE_2D, res.textureId );
glTexImage2D(
GL_TEXTURE_2D,
0, // higest res mipmap level
GL_RGB,
sizeX, sizeY,
0,
GL_RGBA, // becasue our class Texel is made like this
GL_UNSIGNED_BYTE, // idem
&(data[0])
);
// let's determine how this texture will be accessed (by the fragment shader)!
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
return res;
}
void GpuMesh::render() const{
// "caro opengl, ti ricodi di quel buffer (che mi hai detto di chiamare "buffer N. geomBufferId?"
glBindBuffer( GL_ARRAY_BUFFER, geomBufferId );
// "ebbene, li ci troverai le (posizioni dei) VERTICI (nel modo che ti ho detto prima)"
glEnableClientState(GL_VERTEX_ARRAY);
// "e li ci troverai anche le (posiizoni delle) COORD TEXGURE (sempre nel modo di cui ti dissi)"
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, connBufferId );
glEnableClientState(GL_INDEX_ARRAY);
glDrawElements(GL_TRIANGLES, nElements, GL_UNSIGNED_INT, 0 );
}
void CpuMesh::renderDeprecated(){
// using OpenGL 1.0
glBegin(GL_TRIANGLES);
for (Tri t: tris) {
{
Vertex &v( verts[ t.i ] );
glVertex3f( v.pos.x, v.pos.y, v.pos.z );
}
{
Vertex &v( verts[ t.j ] );
glVertex3f( v.pos.x, v.pos.y, v.pos.z );
}
{
Vertex &v( verts[ t.k ] );
glVertex3f( v.pos.x, v.pos.y, v.pos.z );
}
}
glEnd();
}
void Transform::setModelMatrix() const{
glm::mat4 rotMat = mat4_cast( ori );
glm::mat4 traMat(1.0);
glm::mat4 scaMat;
scaMat[0][0] = scale;
scaMat[1][1] = scale;
scaMat[2][2] = scale;
traMat[3] = vec4( pos , 1 );
glm::mat4 m = traMat * rotMat * scaMat;
glMultMatrixf( & (m[0][0]) );
}
void PhysObject::render() const{
glColor3f(1,1,1);
// set the local transform
glPushMatrix();
t.setModelMatrix();
meshComponent.t.setModelMatrix();
meshComponent.texture.bind();
meshComponent.mesh.render();
glPopMatrix();
glDisable(GL_TEXTURE_2D);
}
void drawAStupidTriangle();
void Bullet::renderPlaceHolder() const{
// sending the cords in world space! (optimization)
if (alive)
glVertex3f( t.pos.x , t.pos.y , t.pos.z );
}
void Ship::render() const{
PhysObject::render();
glPointSize(4.0);
glColor3f( 1,1,0 );
glBegin(GL_POINTS);
for (const Bullet &b: bullets){
b.renderPlaceHolder();
}
glEnd();
}
void PhysObject::setCameraInside(){
t.inverse().setModelMatrix();
}
void Ship::renderPlaceHolder() const{
glPushMatrix();
t.setModelMatrix();
drawAStupidTriangle();
/* TODO: se fosse un albero della scena, i figli
* di questo nodo vanno disegnai qui
* con una chiamata ricosrica alla renderPlaceHolder */
glPopMatrix();
glPointSize(4.0);
glColor3f( 1,1,0 );
glBegin(GL_POINTS);
for (const Bullet &b: bullets){
b.renderPlaceHolder();
}
glEnd();
}
void Scene::cameraOnTwoObjects( const PhysObject& a, const PhysObject& b ){
vec3 center = ( a.t.pos + b.t.pos )*0.5f;
float radius = length( a.t.pos - b.t.pos )/2.0f + 2.0f;
/*
* if it was a MODEL TRANSFORM of an object which is a camera:
glTranslatef(center.x,center.y,center.z);
glScalef(radius,radius,radius);
*/
glScalef(1/radius,1/radius,1/radius);
glTranslatef(-center.x,-center.y,-center.z);
}
// this functions is an exact copy of gluPerspective (of GLU library)
// added here to avoid a dependency.
static void myGluPerspective(GLdouble fovx, GLdouble aspect, GLdouble zNear, GLdouble zFar)
{
GLdouble xMin, xMax, yMin, yMax;
xMax = zNear * tan(fovx * 3.1415926535 / 360.0);
xMin = -xMax;
yMin = xMin / aspect;
yMax = xMax / aspect;
GLdouble m[16] = {
(2.0 * zNear) / (xMax - xMin), 0, 0, 0,
0, (2.0 * zNear) / (yMax - yMin), 0, 0,
(xMax + xMin) / (xMax - xMin),
(yMax + yMin) / (yMax - yMin),
-(zFar + zNear) / (zFar - zNear),
-1,
0, 0, -(2.0 * zFar * zNear) / (zFar - zNear), 0
};
glMultMatrixd(m);
}
void Scene::render(){
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
myGluPerspective(60,1.0,0.2,100.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0,0,-2); // take a step behind
if (0) {
// 3rd person camera
glTranslatef(0,-2,-4);
glRotatef(-75,1,0,0);
ships[0].setCameraInside();
} else {
// bird-eye camera
cameraOnTwoObjects(ships[0],ships[1]);
}
glPushMatrix();
//aMesh.render();
glPushMatrix();
glRotatef(60,1,1,3);
//aMesh.renderDeprecated();
aMesh.render();
glPopMatrix();
renderFloor();
for ( Ship& s : ships){
s.render(); // including its bullets
}
glPopMatrix();
}
void Scene::renderFloor() const{
glPushMatrix();
glTranslatef(0,0,-2);
glBegin(GL_QUADS);
glColor3f(0.3f,0.2f,0);
for (float x=-arenaRadius; x<arenaRadius; x+=4)
for (float y=-arenaRadius; y<arenaRadius; y+=4) {
glVertex2d(x ,y);
glVertex2d(x+2,y);
glVertex2d(x+2,y+2);
glVertex2d(x ,y+2);
}
glEnd();
glPopMatrix();
}
void drawAStupidTriangle(){
glColor3f(1,1,1);
glBegin(GL_TRIANGLES);
glVertex2f( 0, 0.8f);
glVertex2f(-0.5,-0.5);
glVertex2f(+0.5,-0.5);
glEnd();
}
/* metodo globale che disegna la scena */
void rendering(){
glViewport(0,0,500,500); // TODO: use actual windows size
glClearColor( 0.2f, 0.15f, 0.0f, 1.0f );
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
scene.render();
}
/* metodo globale che inizializza il sistema grafico */
void initRendering(){
glewInit();
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
}