-
Notifications
You must be signed in to change notification settings - Fork 0
/
Canvas.cpp
630 lines (521 loc) · 20.1 KB
/
Canvas.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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
// perspective
#define PSHIFT 50
// shading options
#define ZBUFFER 1
#define DIFUSE 2
#define REFLECT 3
// light coordinates
#define INTENSITY 300
#define LIGHT_X 80
#define LIGHT_Y 0
#define LIGHT_Z 0
#include "Canvas.h"
#include "dgcapp.h"
#include <QMouseEvent>
#include <cmath>
#include <vector>
#include <algorithm>
using namespace std;
/**
* @brief Creates canvas. Set initial coords.
* @param parent Window where canvas is to be painted
*/
Canvas::Canvas( QWidget * parent ) : QWidget( parent )
{
// Coords: Beginning and End
x = y = j = k = 0;
L = NULL; // label
axis = true; // paint axis?
pressed = false; // mouse not pressed
}
Canvas::~Canvas() { }
/**
* @brief assign label to canvas to show coords
* @param L Label where to update coords through (Canvas::UpdateCoords)
*/
void Canvas::SetLabel( QLabel * L ) { this->L = L; LabelText = L->text(); }
/**
* @brief Updates coords when mouse is pressed
* @param x X Coord
* @param y Y Coord
*/
void Canvas::SetInitialCoords( int x, int y ) { this->x = x; this->y = y; }
/**
* @brief Updates coords when mouse is released
* @param x X Coord
* @param y Y Coord
*/
void Canvas::SetFinalCoords( int j, int k ) { this->j = j; this->k = k; }
/**
* @brief Shows X and Y coords in Canvas label when mouse moves
* @param x X Coord
* @param y Y Coord
*/
void Canvas::UpdateCoords( int x, int y )
{
L->setText( LabelText + ": " + QString::number( x ) + ", " + QString::number( y ) );
}
/**
*
* Slots
*
*/
/**
* @brief indicates if to paint or not to paint the axis
*/
void Canvas::ToggleAxis() { axis = ! axis; update(); }
/**
* @brief Updates all canvases
*
* Retrieves all edges from every object in every layer and paints them according
* its view (front, side, etc.)
*/
void Canvas::Update()
{
if ( Edges.size() ) { Edges.clear(); C.clear(); } // clean vector to avoid mistakes
vector < Edge * > * E = NULL;
int size = Lay->size();
for ( int i = 0; i < size; i++ )
if ( E = Lay->at( i )->GetState() ) // retrieve edges info
{
Edges.push_back( E ); // store them to paint them in the paint event
E = NULL;
C.push_back( Lay->at( i )->getColor() ); // store also their color
}
update();
}
/**
*
* MouseEvents
*
*/
/**
* @brief udpates coords and performs object transformatios if mouse is pressed.
* @param e mouseEvent
*
* Calls dgcapp::getCurrentLayer to know if there is an object selected or not.
* If so, dgcapp::getCurrentTransform is invoked to take action.
* According to the corresponding transformation, actions are performed through
* layers.
*/
void Canvas::mouseMoveEvent( QMouseEvent * e )
{
int X = e->x(), Y = e->y();
UpdateCoords( e->x(), e->y() );
if ( pressed )
{
dgcapp * d = ( dgcapp * ) this->parent();
int l = d->getCurrentLayer();
if ( l < 0 ) return;
int t = d->getCurrentTransform();
switch ( t )
{
case 1: // translation
if ( this == d->getCanvas( Front ) )
Lay->at( l )->Translation( X - x, -Y + y, 0 ); // XY
else
{
if ( this == d->getCanvas( Air ) )
Lay->at( l )->Translation( X - x, 0, -Y + y ); // XZ
else
Lay->at( l )->Translation( 0, - Y + y, X - x ); // YZ
}
break;
case 2: // escalation
if ( this == d->getCanvas( Front ) )
Lay->at( l )->RotationZ( 0.2 * ( Y - y ) ); // XY
else
{
if ( this == d->getCanvas( Air ) )
Lay->at( l )->RotationY( 0.2 * ( Y - y ) ); // XZ
else
Lay->at( l )->RotationX( 0.2 * ( Y - y ) ); // YZ
}
break;
case 3: Lay->at( l )->selfRotation( Y - y ); break; // self rotation
case 4:
if ( this == d->getCanvas( Front ) )
Lay->at( l )->Escalation( 0.02 * ( X - x ) + 1,
0.02 * ( Y - y ) + 1, 1 ); // XY
else
{
if ( this == d->getCanvas( Air ) )
Lay->at( l )->Escalation( 0.02 * ( X - x ) + 1, 1,
0.02 * ( Y - y ) + 1 ); // YZ
else
Lay->at( l )->Escalation( 1, 0.02 * ( Y - y ) + 1,
0.02 * ( X - x ) + 1 ); // XZ
}
break;
}
SetInitialCoords( e->x(), e->y() );
if ( t ) Update(); // if t -> transformation performed -> update
}
}
/**
* @brief Updates coords and sets mouse 'pressed' to true.
* @param e MouseEvent
*/
void Canvas::mousePressEvent( QMouseEvent * e )
{
SetInitialCoords( e->x(), e->y() );
SetFinalCoords( e->x(), e->y() );
pressed = true;
}
/**
* @brief Updates coords and sets mouse 'pressed' to true and adds the new point
* of the border defined by the user, if border is selected.
* @param e MouseEvent
*/
void Canvas::mouseReleaseEvent( QMouseEvent * e )
{
SetInitialCoords( e->x(), e->y() );
SetFinalCoords( e->x(), e->y() );
pressed = false;
dgcapp * d = ( dgcapp * ) this->parent();
if ( d->isBorderSelected() )
{
int centerW = this->size().width() / 2,
centerH = this->size().height() / 2;
// border
d->setRevPoint( -e->x() + centerW, -e->y() + centerH, 0, 1 );
}
update();
}
/**
* @brief PaintEvent
* @param e
* Paints axis and objects (zbuffered or not, or difused)
*/
void Canvas::paintEvent( QPaintEvent * e )
{
QPainter p( this );
p.setPen( Qt::black ); // black color
dgcapp * d = ( dgcapp * ) this->parent();
QSize s = this->size();
int centerW = s.width() / 2, centerH = s.height() / 2;
if ( axis ) // paint axis ?
{
p.drawLine( 0, centerH, s.width(), centerH ); // horizontal axis
p.drawLine( centerW, 0, centerW, s.height() ); // vertical axis
p.setPen( Qt::red );
p.drawLine( 0, s.height(), s.width(), 0 ); // 3º axis
}
if ( Edges.size() )
{
int size = Edges.size(); // updated through Canvas::Update
vector < Edge * > * E = NULL;
for ( int i = 0; i < size; i++ )
{
float x1, y1, x2, y2, z1, z2;
float parx1, pary1, parx2, pary2;
E = Edges.at( i );
p.setPen( C[ i ] ); // set axis color
int NEdges = E->size();
for ( int i = 0; i < NEdges; i++ ) // travel through edges
{
x1 = E->at( i )->GetIni()->x; x2 = E->at( i )->GetEnd()->x;
y1 = E->at( i )->GetIni()->y; y2 = E->at( i )->GetEnd()->y;
z1 = E->at( i )->GetIni()->z; z2 = E->at( i )->GetEnd()->z;
// perpective: shift in Z and scale and translate
z1 += PSHIFT; z2 += PSHIFT;
float r = 1 / ( float ) 20; // shift point -> perspective
parx1 = x1; pary1 = y1; // default: no depth
parx2 = x2; pary2 = y2; // default: no depth
if ( ( int ) z1 > 0 ) // front point -> make perspective
{
parx1 /= ( r * z1 + 1 );
pary1 /= ( r * z1 + 1 );
}
if ( ( int ) z2 > 0 ) // front point -> make perspective
{
parx2 = x2 / ( r * z2 + 1 );
pary2 = y2 / ( r * z2 + 1 );
}
//escalation for representation
parx1 *= 3; parx2 *= 3; pary1 *= 3; pary2 *= 3;
dgcapp * d = ( dgcapp * ) this->parent();
Canvas * A = d->getCanvas( Air ), * F = d->getCanvas( Front ),
* L = d->getCanvas( Lat );
if ( this == F ) p.drawLine( x1 + centerW, -y1 + centerH,
x2 + centerW, -y2 + centerH ); // XY: FRONTAL
else
{ // XZ: AÉREA
if ( this == A ) p.drawLine( x1 + centerW, -z1 + centerH + PSHIFT,
x2 + centerW, -z2 + centerH + PSHIFT );
else
{ // ZY: LATERAL
if ( this == L ) p.drawLine( z1 + centerW - PSHIFT, -y1 + centerH,
z2 + centerW - PSHIFT, -y2 + centerH );
else // if both edges points are visible -> paint them: perspective
if ( ( int ) z1 >= 0 and ( int ) z2 >= 0 )
p.drawLine( parx1 + centerW, -pary1 + centerH,
parx2 + centerW, -pary2 + centerH ); // PERSPECTIVE
}
}
}
}
if ( d->getCurrentShading() /*and this == d->getCanvas( Front )*/ ) ZBuffer( & p ); // zbuffer or difuse lighting
update();
}
if ( d->getRevNumP() > 1 ) // a line is made out of 2 points
{
int x0, y0, x, y;
for( int i = 0; i < d->getRevNumP() - 1; i++ )
{
x0 = ( d->getRevIX( i ) * -1 ) + centerW;
y0 = ( d->getRevIY( i ) * -1 ) + centerH;
x = ( d->getRevIX( i + 1 ) * -1 ) + centerW;
y = ( d->getRevIY( i + 1 ) * -1 ) + centerH;
p.drawLine( x0, y0, x, y );
}
update();
}
}
/**
* @brief informs canvases (observers) whom to watch
* @param S Subject (dgcapp::ScreenLayers)
*/
void Canvas::SetSubject( vector < Layer * > * S ) { Lay = S; }
/**
* @brief Z-Buffer algorith, may also illuminate objects if selected.
* @param p pointer to painter in paintEvent to draw points to fill polygons
*/
void Canvas::ZBuffer( QPainter * p )
{
int Faces, w = this->size().width(), h = this->height();
struct punto3d Light;
// light position & intensity
dgcapp * d = ( dgcapp * ) this->parent();
// update light in scene
Light.x = d->getLightX();
Light.y = d->getLightY();
Light.z = d->getLightZ();
Light.h = 1.;
float ** zbuffer = new float * [ w ];
for( int i = 0; i < w; i++ ) zbuffer[ i ] = new float[ h ];
// init screen memory -> zbuffer = ∞
for ( int i = 0; i < w; i++ )
for ( int j = 0; j < h; j++ ) *( zbuffer[ i ] + j ) = 7777;
int size = Lay->size();
for ( int k = 0; k < size; k++ )
{
Faces = Lay->at( k )->getObjNumFaces();
for ( int n = 0; n < Faces; n++ )
{
punto3d A, B, C;
Lay->at( k )->FacePoints( n, & A, & B, & C ); // face coordinates
Canvas * Ae = d->getCanvas( Air ), * F = d->getCanvas( Front ),
* L = d->getCanvas( Lat );
p->setPen( Qt::red );
p->setBrush( Qt::red );
if ( this == F ) // XY: FRONTAL
{
// mimic light bulb
p->drawEllipse( Light.x + w / 2, Light.y + h / 2, 10, 10 );
fill( p, zbuffer, A.x, A.y, B.x, B.y, C.x, C.y,
Lay->at( k )->getNFace( n ), Lay->at( k )->getColor(),
d->getCurrentShading() >= DIFUSE, & Light, d->getIntensity() );
}
else
{
// XZ: AÉREA
if ( this == Ae )
{
// mimic light bulb
p->drawEllipse( Light.x + w / 2, Light.z + h / 2, 10, 10 );
fill( p, zbuffer, A.x, A.z, B.x, B.z, C.x, C.z,
Lay->at( k )->getNFace( n ), Lay->at( k )->getColor(),
d->getCurrentShading() >= DIFUSE, & Light, d->getIntensity() );
}
else
// ZY: LATERAL
if ( this == L )
{
// mimic light bulb
p->drawEllipse( Light.z + w / 2, Light.y + h / 2, 10, 10 );
fill( p, zbuffer, A.z, A.y, B.z, B.y, C.z, C.y,
Lay->at( k )->getNFace( n ), Lay->at( k )->getColor(),
d->getCurrentShading() >= DIFUSE, & Light,
d->getIntensity() );
}
}
}
}
// clean memory
for( int i = 0; i < w; i++ ) delete [] zbuffer[ i ];
delete [] zbuffer;
}
/**
* @brief Fills object triangular faces according to z-buffer or phong illumination
* model.
* @param p pointer to painter in paintEvent to draw points to fill polygons
* @param zbuffer matrix (screen)
* @param x1 first point x coordinate
* @param y1 first point y coordinate
* @param x2 second point x coordinate
* @param y2 second point y coordinate
* @param x3 third point x coordinate
* @param y3 third point y coordinate
* @param F Face pointer
* @param Color Face color
* @param Shade illuminate or not?
* @param Light light coords
* @param intensity Color intensity
*/
void Canvas::fill( QPainter * p, float ** zbuffer, float x1, float y1, float x2,
float y2, float x3, float y3, Face * F, QColor Color,
bool Shade, punto3d * Light, int intensity )
{
// sort y coordinates from high to low (and its corresponding x pairs)
vector < Coords > v;
struct Coords first = { x1, y1 }, second = { x2, y2 }, third = { x3, y3 };
v.push_back( first ); v.push_back( second ); v.push_back( third );
sort( v.begin(), v.end() );
// get min and max x coord
float Xmin = x1, Xmax = x1;
if ( x2 >= x1 and x2 >= x3 ) Xmax = x2;
else if ( x3 >= x1 and x3 >= x2 ) Xmax = x3;
if ( x2 <= x1 and x2 <= x3 ) Xmin = x2;
else if ( x3 <= x1 and x3 <= x2 ) Xmin = x3;
for ( int i = ( int ) v[ 0 ].y; i > ( int ) v[ 1 ].y; i-- )
{
int ini, end;
// compute where to start and to end painting
ini = ( int ) v[ 0 ].x * ( ( v[ 1 ].y - i ) / ( v[ 1 ].y - v[ 0 ].y ) )
+ v[ 1 ].x * ( 1 - ( ( v[ 1 ].y - i ) / ( v[ 1 ].y - v[ 0 ].y ) ) );
end = ( int ) v[ 0 ].x * ( ( v[ 2 ].y - i ) / ( v[ 2 ].y - v[ 0 ].y ) )
+ v[ 2 ].x * ( 1 - ( ( v[ 2 ].y - i ) / ( v[ 2 ].y - v[ 0 ].y ) ) );
if ( ini > end ) { swap( ini, end ); }
for ( int j = ini; j <= end; j++ )
{
if ( j <= end and j >= ini and j <= Xmax and j >= Xmin )
{
// point depth -> plane equation
float depth = -F->getD() - F->getNormal().x * j - F->getNormal().y * i;
if ( F->getNormal().z ) depth /= F->getNormal().z;
else depth = 0;
// calculate zbuffer index
QSize s = this->size();
int centerW = s.width() / 2, centerH = s.height() / 2;
int w = ( int ) i + centerH;
int h = ( int ) j + centerW;
if ( h > 0 and h < s.width() and w > 0 and w < s.height() and
depth < zbuffer[ h ][ w ] )
{
// update zbuffer
zbuffer[ h ][ w ] = depth;
// compute color
if( ! Shade ) p->setPen( Color ); // zbuffer
// difuse (phong)
else p->setPen( lightColor ( i, j, depth, F->getNormal(),
Color, Light, intensity ) );
p->drawPoint( j + centerW, -i + centerH ); // paint
}
}
}
}
// second part of the face
for ( int i = ( int ) v[ 1 ].y; i > ( int ) v[ 2 ].y; i-- )
{
int ini, end;
ini = ( int ) v[ 1 ].x * ( ( v[ 2 ].y - i ) / ( v[ 2 ].y - v[ 1 ].y ) )
+ v[ 2 ].x * ( 1 - ( ( v[ 2 ].y - i ) / ( v[ 2 ].y - v[ 1 ].y ) ) );
end = ( int ) v[ 0 ].x * ( ( v[ 2 ].y - i ) / ( v[ 2 ].y - v[ 0 ].y ) )
+ v[ 2 ].x * ( 1 - ( ( v[ 2 ].y - i ) / ( v[ 2 ].y - v[ 0 ].y ) ) );
if ( ini > end ) { swap( ini, end ); }
for ( int j = ini; j <= end; j++ )
{
if ( j <= end and j >= ini and j <= Xmax and j >= Xmin )
{
// point depth -> plane equation
float depth = -F->getD() - F->getNormal().x * j - F->getNormal().y * i;
if ( F->getNormal().z ) depth /= F->getNormal().z;
else depth = 0;
// calculate zbuffer index
QSize s = this->size();
int centerW = s.width() / 2, centerH = s.height() / 2;
int w = ( int ) i + centerH;
int h = ( int ) j + centerW;
//if ( depth < zbuffer[ h ][ w ] )
if ( h > 0 and h < s.width() and w > 0 and w < s.height() and
depth < zbuffer[ h ][ w ] )
{
// update zbuffer
zbuffer[ h ][ w ] = depth;
if( ! Shade ) p->setPen( Color );
else p->setPen( lightColor ( i, j, depth, F->getNormal(),
Color, Light, intensity ) );
p->drawPoint( h, -i + centerH );
}
}
}
}
}
/**
* @brief Computes de intensity of Color at a given point by x, y and z.
* @param x float
* @param y float
* @param z float
* @param normalFace Normal
* @param Color (QColor)
* @param Light coords
* @param intensity of color
* @return QColor (according to phong and the intensity passed as a parameter)
*/
QColor Canvas::lightColor ( float x, float y, float z, Normal normalFace,
QColor Color, punto3d * Light, int intensity )
{
// Color components
float Red, Green, Blue, Redp, Greenp, Bluep;
Red = Color.red(); Green = Color.green(); Blue = Color.green();
// pixel illumination
float difu, AmbientLight = 55.;
// phong
difu = Difuse( normalFace, x, y, z, Light, intensity ); // Canvas::Difuse
dgcapp * d = ( dgcapp * ) this->parent();
QColor C( d->getRefColor() );
float reflect = 2 * z * ( y + x + z - 1 ) / 100000;
if ( d->getCurrentShading() != REFLECT ) reflect = 0;
// update color components
Redp = AmbientLight + Red * difu + C.red() * reflect;
if ( Redp > 255 ) Redp = 255;
Greenp = AmbientLight + Green * difu + C.green() * reflect;
if ( Greenp > 255 ) Greenp = 255;
Bluep = AmbientLight + Blue * difu + C.blue() * reflect;
if ( Bluep > 255 ) Bluep = 255;
// compose color
QColor c; c.setRgb( Redp, Greenp, Bluep );
return c;
}
/**
* @brief modifies the color of a given pixel
* @param normalPixel pixel Normal (struct Normal { float x, y, z; };)
* @param x x coord of pixel
* @param y y coord of pixel
* @param z z coord of pixel
* @param Light light coords
* @param intensity light power
* @return color modifier
*/
float Canvas::Difuse ( Normal normalPixel, float x, float y, float z,
punto3d * Light, int intensity )
{
// known intensity and constant
float k = 1, kd = 1;
Normal N;
float innerProd, total = 0;
double dist;
// x, y, z -> resultant vector
swap(x,y);
N.x = Light->x - x; N.y = Light->y - y; N.z = Light->z - z;
// tenemos que normalizar el vector
dist = sqrt( N.x * N.x + N.y * N.y + N.z * N.z );
N.x /= dist; N.y /= dist; N.z /= dist;
innerProd = normalPixel.x * N.x + normalPixel.y * N.y + normalPixel.z * N.z;
// si esta entre cero y uno, afecta al color
// Ecuacion de phong sin reflexion, iluminacion difusa
if ( innerProd > 0. and innerProd <= 1. )
total += ( intensity * innerProd * kd ) / ( k + dist );
return total;
}