-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrenderer.cpp
More file actions
261 lines (224 loc) · 6.7 KB
/
renderer.cpp
File metadata and controls
261 lines (224 loc) · 6.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
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
#include "renderer.h"
#include "objectmodel.h"
#include "lightscontext.h"
#include "material.h"
#include <QtAlgorithms>
#include <QImage>
#include <QPainter>
#include <QLine>
#include <cmath>
#include <cassert>
#include "phongmodel.h"
#include "light.h"
#define SHIFT_MOD 2.0
// z value average
double avZ(Triangle* t)
{
return (t->a()->z() + t->b()->z() + t->c()->z()) / 3;
}
// painter's algorithm
bool zLess(Triangle* a, Triangle* b)
{
return (avZ(a) < avZ(b));
}
Renderer::Renderer() :
rotX(0),
rotY(0),
rotZ(0),
_zoom(0),
shiftX(0.0),
shiftY(0.0),
wireframeVisibility(false),
backgroundColor(QColor(0, 25, 40)),
wireframeColor(QColor(255, 255, 0))
{
}
QImage *Renderer::render(ObjectModel *model, QSize size, Material *objectMaterial, LightsContext *lightsContext, QList<Transformation*> *transformations)
{
ObjectModel *modelAux = new ObjectModel(model);
LightsContext *lightsAux = new LightsContext(lightsContext);
// Object transformations
for (int i=0; i<transformations->size(); i++) {
Transformation *t = transformations->at(i);
if (t->getTransformCoordinates() == Transformation::TRANSFORM_OBJECT)
t->transform(modelAux->getVertexes());
}
// Camera transformations
Transformation rot = buildViewportTransformation();
rot.transform(modelAux->getVertexes());
for (unsigned i=0; i<lightsAux->getLightsCount(); i++) {
Light *l = lightsAux->getLightPtrAt(i);
if (l->isRotateWithCamera())
rot.transform(l->getPosPtr());
}
for (int i=0; i<transformations->size(); i++) {
Transformation *t = transformations->at(i);
if (t->getTransformCoordinates() == Transformation::TRANSFORM_CAMERA)
t->transform(modelAux->getVertexes());
}
sortTrianglesZ(modelAux->getTriangles());
QImage* result = paint(modelAux->getTriangles(), size, objectMaterial, lightsAux);
delete modelAux;
delete lightsAux;
return result;
}
double modulus(double a, double b)
{
int result = static_cast<int>( a / b );
return a - static_cast<double>( result ) * b;
}
void Renderer::rotateX(double r)
{
setRotationX(rotX + 2*PI*r);
}
void Renderer::rotateY(double r)
{
setRotationY(rotY + 2*PI*r);
}
void Renderer::rotateZ(double r)
{
setRotationY(rotY + 2*PI*r);
}
void Renderer::setRotationX(double r)
{
rotX = r;
}
void Renderer::setRotationY(double r)
{
rotY = r;
}
void Renderer::setRotationZ(double r)
{
rotZ = r;
}
void Renderer::sortTrianglesZ(QList<Triangle*> &triangles)
{
qSort(triangles.begin(), triangles.end(), zLess);
}
Transformation Renderer::buildViewportTransformation()
{
// Scale object based on _zoom
double zoomScale = 1.0;
if (_zoom < 0)
zoomScale += _zoom / 10.0;
else
zoomScale += _zoom;
QMatrix4x4 scale(zoomScale, 0, 0, 0,
0, zoomScale, 0, 0,
0, 0, zoomScale, 0,
0, 0, 0, zoomScale);
// Rotate the object from its center
QMatrix4x4 xRM(1, 0, 0, 0,
0, cos(rotX), -sin(rotX), 0,
0, sin(rotX), cos(rotX), 0,
0, 0, 0, 1);
QMatrix4x4 yRM(cos(rotY), 0, sin(rotY), 0,
0, 1, 0, 0,
-sin(rotY), 0, cos(rotY), 0,
0, 0, 0, 1);
QMatrix4x4 zRM(cos(rotZ), -sin(rotZ), 0, 0,
sin(rotZ), cos(rotZ), 0, 0,
0, 0, 1, 0,
0, 0, 0, 1);
// Shift the object +(shiftX, shiftY, 0)
QMatrix4x4 shift(1, 0, 0, shiftX,
0, 1, 0, -1*shiftY,
0, 0, 1, 0,
0, 0, 0, 1);
Transformation t(Transformation::TRANSFORMATION_LINEAR);
t.setMatrix(new QMatrix4x4(scale * shift * xRM * yRM * zRM));
return t;
}
QImage *Renderer::paint(QList<Triangle*> &triangles, QSize size, Material *objectMaterial, LightsContext *lightsContext)
{
// qDebug() << "[Renderer::paint] Rendering frame";
// Prepare output bitmap
QImage *output = new QImage(size, QImage::Format_RGB32);
// Paint background
output->fill(backgroundColor.rgb());
QRect clippedRect;
if (size.width() == size.height()) {
clippedRect.setRect(0, 0, size.width(), size.height());
} else if (size.width() > size.height()) {
clippedRect.setRect((size.width()-size.height())/2, 0, size.height(), size.height());
} else {
clippedRect.setRect(0, (size.height()-size.width())/2, size.width(), size.width());
}
unsigned halfWidth = clippedRect.width()/2, halfHeight = clippedRect.height()/2;
QPainter painter(output);
for (int i=0; i<triangles.size(); i++)
{
Triangle *current = triangles.at(i);
QColor color = PhongModel::computeColor(current, lightsContext, objectMaterial);
painter.setPen(color);
painter.setBrush(color);
QPoint points[3];
points[0] = QPoint(clippedRect.x()+halfWidth+halfWidth*current->a()->x(),clippedRect.y()+halfHeight-halfHeight*current->a()->y());
points[1] = QPoint(clippedRect.x()+halfWidth+halfWidth*current->b()->x(),clippedRect.y()+halfHeight-halfHeight*current->b()->y());
points[2] = QPoint(clippedRect.x()+halfWidth+halfWidth*current->c()->x(),clippedRect.y()+halfHeight-halfHeight*current->c()->y());
painter.drawPolygon(points, 3);
if (wireframeVisibility == true) {
painter.setPen(wireframeColor);
QLine lines[3];
lines[0] = QLine(points[0], points[1]);
lines[1] = QLine(points[1], points[2]);
lines[2] = QLine(points[2], points[0]);
painter.drawLines(lines, 3);
}
}
return output;
}
void Renderer::changeRotation(double x, double y)
{
rotateY(x);
rotateX(y);
}
void Renderer::setWireframeVisibility(bool v)
{
wireframeVisibility = v;
}
void Renderer::setBackgroundColor(QColor c)
{
backgroundColor = c;
}
void Renderer::setWireframeColor(QColor c)
{
wireframeColor = c;
}
QColor Renderer::getBackgroundColor()
{
return backgroundColor;
}
QColor Renderer::getWireframeColor()
{
return wireframeColor;
}
void Renderer::setZoom(int zoom)
{
if (zoom > 9)
zoom = 9;
else if (zoom < -9)
zoom = -9;
Renderer::_zoom = zoom;
}
void Renderer::zoom(int delta)
{
setZoom(_zoom + delta);
}
void Renderer::setShiftX(double s)
{
if (s > SHIFT_MOD) s = SHIFT_MOD;
else if (s < -SHIFT_MOD) s = -SHIFT_MOD;
shiftX = s;
}
void Renderer::setShiftY(double s)
{
if (s > SHIFT_MOD) s = SHIFT_MOD;
else if (s < -SHIFT_MOD) s = -SHIFT_MOD;
shiftY = s;
}
void Renderer::shift(double x, double y)
{
setShiftX(shiftX + x * SHIFT_MOD);
setShiftY(shiftY + y * SHIFT_MOD);
}