-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrenderer.h
588 lines (500 loc) · 14.6 KB
/
renderer.h
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
#ifndef RENDERER_H
#define RENDERER_H
#include<QGLWidget>
#include<QVector>
#include<QVector2D>
#include<vector>
#include "ast.h"
#include <cmath>
#include "spline.h"
#include <GL/glut.h>
#include <GL/glu.h>
#define PI 3.141592
#define NUM_STEPS 50
class ITransform{
public:
virtual void Transform(){}
};
class IRotate: public ITransform{
public:
IRotate(float angle,float x,float y,float z){
this->angle = angle;
this->x = x;
this->y = y;
this->z = z;
}
void Transform(){
glRotatef(angle,x,y,z);
}
float angle;
float x;
float y;
float z;
};
class IScale: public ITransform{
public:
IScale(float x,float y,float z){
this->x = x;
this->y = y;
this->z = z;
}
void Transform(){
glScalef(x,y,z);
}
float x;
float y;
float z;
};
class ITranslate: public ITransform{
public:
ITranslate(float x,float y,float z){
this->x = x;
this->y = y;
this->z = z;
}
void Transform(){
glTranslatef(x,y,z);
}
float x;
float y;
float z;
};
class Renderer{
public:
Renderer(){
draw = false;
fill = false;
}
QList<ITransform*> trans;
bool draw;
bool fill;
Color* drawColor;
Color* fillColor;
void Draw(){
//Push model matrix
glPushMatrix();
//apply transformations for each matrix
QList<ITransform*>::iterator i;
for (i = trans.begin(); i != trans.end(); ++i){
(*i)->Transform();
}
//draw shape
if(draw){
//set the draw color
glColor4f(drawColor->red,drawColor->green,drawColor->blue,drawColor->alpha);
DrawShape();
}
//fill shape
if(fill){
//set the fill color
glColor4f(fillColor->red,fillColor->green,fillColor->blue,fillColor->alpha);
FillShape();
}
//pop the model matrix
glPopMatrix();
}
void AddTransform(ITransform* t){
trans.append(t);
}
void AddDraw(Color* color){
draw = true;
drawColor = color;
}
void AddFill(Color* color){
fill = true;
fillColor = color;
}
virtual void DrawShape(){}
virtual void FillShape(){}
};
class PointRenderer: public Renderer{
public:
PointRenderer(Point *p){this->point = p;}
Point* point;
void DrawShape(){
glBegin(GL_POINTS);
glVertex2f(point->point->x,point->point->y);
glEnd();
}
void FillShape(){
DrawShape();
}
};
class RectRenderer: public Renderer{
public:
RectRenderer(Rect *p){this->rect = p;}
Rect* rect;
void DrawShape(){
glBegin(GL_LINES);
glVertex2f(rect->a->x,rect->a->y);
glVertex2f(rect->b->x,rect->b->y);
glEnd();
}
void FillShape(){
DrawShape();
}
};
class QuadRenderer: public Renderer{
public:
QuadRenderer(Quad *p){this->quad = p;}
Quad* quad;
void DrawShape(){
glBegin(GL_LINES);
glVertex2f(quad->a->x,quad->a->y);
glVertex2f(quad->b->x,quad->b->y);
glVertex2f(quad->b->x,quad->b->y);
glVertex2f(quad->c->x,quad->c->y);
glVertex2f(quad->c->x,quad->c->y);
glVertex2f(quad->d->x,quad->d->y);
glVertex2f(quad->a->x,quad->a->y);
glVertex2f(quad->d->x,quad->d->y);
glEnd();
}
void FillShape(){
glBegin(GL_QUADS);
glVertex2f(quad->a->x,quad->a->y);
glVertex2f(quad->b->x,quad->b->y);
glVertex2f(quad->c->x,quad->c->y);
glVertex2f(quad->d->x,quad->d->y);
glEnd();
}
};
class TriangleRenderer: public Renderer{
public:
TriangleRenderer(Triangle *p){this->triangle = p;}
Triangle* triangle;
void DrawShape(){
glBegin(GL_LINES);
glVertex2f(triangle->a->x,triangle->a->y);
glVertex2f(triangle->b->x,triangle->b->y);
glVertex2f(triangle->b->x,triangle->b->y);
glVertex2f(triangle->c->x,triangle->c->y);
glVertex2f(triangle->a->x,triangle->a->y);
glVertex2f(triangle->c->x,triangle->c->y);
glEnd();
}
void FillShape(){
glBegin(GL_TRIANGLES);
glVertex2f(triangle->a->x,triangle->a->y);
glVertex2f(triangle->b->x,triangle->b->y);
glVertex2f(triangle->c->x,triangle->c->y);
glEnd();
}
};
class ElipseRenderer: public Renderer{
public:
ElipseRenderer(Elipse *p){this->elipse = p;initialized=false;}
Elipse* elipse;
bool initialized;
QVector<QVector2D*> verts;
void Init(){
initialized = true;
float x;
float y;
float stepSize;
stepSize = 2*PI/NUM_STEPS;
for(int i=0;i<NUM_STEPS;i++){
float t = stepSize*i;
x = elipse->width*cos(t)+elipse->center->x;
y = elipse->height*sin(t)+elipse->center->y;
verts.append(new QVector2D(x,y));
}
}
void DrawShape(){
if(!initialized){
Init();
}
glBegin(GL_LINE_LOOP);
for(int i=0;i<verts.size();i++){
glVertex2f(verts[i]->x(),verts[i]->y());
}
glEnd();
}
void FillShape(){
if(!initialized){
Init();
}
glBegin(GL_TRIANGLE_FAN);
//glVertex2f(elipse->center->x,elipse->center->y);
for(int i=0;i<verts.size();i++){
glVertex2f(verts[i]->x(),verts[i]->y());
}
glEnd();
}
};
class CircRenderer: public Renderer{
public:
CircRenderer(Circ* c){this->circle=c; initialized=false;}
Circ* circle;
bool initialized;
QVector<QVector2D*> verts;
void Init(){
initialized = true;
float x;
float y;
float stepSize;
stepSize = 2*PI/NUM_STEPS;
for(int i=0;i<NUM_STEPS;i++){
float t = stepSize*i;
x = circle->radius*cos(t)+circle->center->x;
y = circle->radius*sin(t)+circle->center->y;
verts.append(new QVector2D(x,y));
}
}
void DrawShape(){
if(!initialized){
Init();
}
glBegin(GL_LINE_LOOP);
for(int i=0;i<verts.size();i++){
glVertex2f(verts[i]->x(),verts[i]->y());
}
glEnd();
}
void FillShape(){
if(!initialized){
Init();
}
glBegin(GL_TRIANGLE_FAN);
for(int i=0;i<verts.size();i++){
glVertex2f(verts[i]->x(),verts[i]->y());
}
glEnd();
}
};
class ParaboleRenderer: public Renderer{
public:
ParaboleRenderer(Parabole* c){this->parabole=c; initialized=false;}
Parabole* parabole;
bool initialized;
QVector<QVector2D*> verts;
void Init(){
initialized = true;
float x;
float y;
float stepSize;
GLint m_viewport[4];
//this call should be avoided and replaced by variables
glGetIntegerv( GL_VIEWPORT, m_viewport );
float startPoint = (float)m_viewport[2]/(float)m_viewport[3];
stepSize = startPoint*2/NUM_STEPS; //integration range between num steps
for(int i=0;i<=NUM_STEPS;i++){
float t = (-startPoint)+stepSize*i; //start integration from -1
x = t+parabole->minPoint->x;
y = t*t*parabole->factor+parabole->minPoint->y;
verts.append(new QVector2D(x,y));
}
}
void DrawShape(){
if(!initialized){
Init();
}
glBegin(GL_LINE_STRIP);
for(int i=0;i<verts.size();i++){
glVertex2f(verts[i]->x(),verts[i]->y());
}
glEnd();
}
void FillShape(){
if(!initialized){
Init();
}
glBegin(GL_TRIANGLE_FAN);
for(int i=0;i<verts.size();i++){
glVertex2f(verts[i]->x(),verts[i]->y());
}
glEnd();
}
};
class HyperboleRenderer: public Renderer{
public:
HyperboleRenderer(Hyperbole* c){this->hyperbole=c; initialized=false;}
Hyperbole* hyperbole;
bool initialized;
QVector<QVector2D*> vertsUpper;
QVector<QVector2D*> vertsLower;
void Init(){
//this function works with a little math magic and a lot of unstable stuff, threat with extreme caution
initialized = true;
float x;
float y;
float stepSize;
//t ranges between pi and -pi
stepSize = 2*PI/NUM_STEPS;
for(int i=0;i<=NUM_STEPS;i++){
float t = -PI + stepSize*i;
x = hyperbole->a*cosh(t);
y = hyperbole->b*sinh(t);
vertsUpper.append(new QVector2D(x,y));
x = -x;
vertsLower.append(new QVector2D(x,y));
}
}
void DrawShape(){
if(!initialized){
Init();
}
glBegin(GL_LINE_STRIP);
for(int i=0;i<vertsUpper.size();i++){
glVertex2f(vertsUpper[i]->x(),vertsUpper[i]->y());
}
glEnd();
glBegin(GL_LINE_STRIP);
for(int i=0;i<vertsLower.size();i++){
glVertex2f(vertsLower[i]->x(),vertsLower[i]->y());
}
glEnd();
}
void FillShape(){
if(!initialized){
Init();
}
glBegin(GL_TRIANGLE_FAN);
for(int i=0;i<vertsUpper.size();i++){
glVertex2f(vertsUpper[i]->x(),vertsUpper[i]->y());
}
glEnd();
glBegin(GL_TRIANGLE_FAN);
for(int i=0;i<vertsLower.size();i++){
glVertex2f(vertsLower[i]->x(),vertsLower[i]->y());
}
glEnd();
}
};
class CurveRenderer: public Renderer{
public:
CurveRenderer(Curve* c){this->curve=c; initialized=false;}
Curve* curve;
bool initialized;
QVector<QVector2D*> verts;
void Init(){
//this function might be easily changed for more than three point splines!
initialized = true;
//spline calculation
std::vector<double> xs(3);
std::vector<double> ys(3);
xs[0]=curve->a->x;
xs[1]=curve->b->x;
xs[2]=curve->c->x;
ys[0]=curve->a->y;
ys[1]=curve->b->y;
ys[2]=curve->c->y;
tk::spline mySpline;
mySpline.set_points(xs,ys);
//calculate integration range
float startPoint = curve->a->x;
float stepSize = (curve->c->x-curve->a->x)/(NUM_STEPS);
for(int i=0;i<NUM_STEPS+1;i++){
float t = startPoint + stepSize*i;
verts.append(new QVector2D(t,mySpline(t)));
}
}
void DrawShape(){
if(!initialized){
Init();
}
glBegin(GL_LINE_STRIP);
for(int i=0;i<verts.size();i++){
glVertex2f(verts[i]->x(),verts[i]->y());
}
glEnd();
}
void FillShape(){
DrawShape();
}
};
class ConeRenderer: public Renderer{
public:
ConeRenderer(Cone* c){this->cone=c;}
Cone* cone;
void DrawShape(){
glColorMaterial(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE);
glPushMatrix();
glTranslatef(cone->center->x,cone->center->y,cone->center->z);
glutWireCone(cone->radius,cone->height,NUM_STEPS,1);
glPopMatrix();
}
void FillShape(){
glColorMaterial(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE);
glPushMatrix();
glTranslatef(cone->center->x,cone->center->y,cone->center->z);
glutSolidCone(cone->radius,cone->height,NUM_STEPS,1);
glPopMatrix();
}
};
class SphereRenderer: public Renderer{
public:
SphereRenderer(Sphere* c){this->sphere=c;}
Sphere* sphere;
void DrawShape(){
glColorMaterial(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE);
glPushMatrix();
glTranslatef(sphere->center->x,sphere->center->y,sphere->center->z);
glutWireSphere(sphere->radius,NUM_STEPS,NUM_STEPS);
glPopMatrix();
}
void FillShape(){
glColorMaterial(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE);
glPushMatrix();
glTranslatef(sphere->center->x,sphere->center->y,sphere->center->z);
glutSolidSphere(sphere->radius,NUM_STEPS/2,NUM_STEPS/2);
glPopMatrix();
}
};
class CylinderRenderer: public Renderer{
public:
CylinderRenderer(Cylindre* c){this->cylindre=c;}
Cylindre* cylindre;
void DrawShape(){
glColorMaterial(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE);
glPushMatrix();
glTranslatef(cylindre->center->x,cylindre->center->y,cylindre->center->z);
GLUquadric* q = gluNewQuadric();
gluQuadricDrawStyle(q,GLU_LINE);
gluCylinder(q,cylindre->radius,cylindre->radius,cylindre->height,NUM_STEPS/2,1);
glPopMatrix();
}
void FillShape(){
glColorMaterial(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE);
glPushMatrix();
glTranslatef(cylindre->center->x,cylindre->center->y,cylindre->center->z);
GLUquadric* q = gluNewQuadric();
gluQuadricDrawStyle(q,GLU_FILL);
gluCylinder(q,cylindre->radius,cylindre->radius,cylindre->height,NUM_STEPS/2,1);
glPopMatrix();
}
};
class PlaneRenderer: public Renderer{
public:
PlaneRenderer(Plane* c){this->plane=c;this->plane->side=this->plane->side/2;}
Plane* plane;
void DrawShape(){
glColorMaterial(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE);
glBegin(GL_LINE_LOOP);
glVertex3f(plane->center->x-plane->side,plane->center->y,plane->center->z+plane->side);
glVertex3f(plane->center->x+plane->side,plane->center->y,plane->center->z+plane->side);
glVertex3f(plane->center->x+plane->side,plane->center->y,plane->center->z-plane->side);
glVertex3f(plane->center->x-plane->side,plane->center->y,plane->center->z-plane->side);
glEnd();
}
void FillShape(){
glColorMaterial(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE);
glBegin(GL_TRIANGLE_FAN);
glVertex3f(plane->center->x-plane->side,plane->center->y,plane->center->z+plane->side);
glVertex3f(plane->center->x+plane->side,plane->center->y,plane->center->z+plane->side);
glVertex3f(plane->center->x+plane->side,plane->center->y,plane->center->z-plane->side);
glVertex3f(plane->center->x-plane->side,plane->center->y,plane->center->z-plane->side);
glEnd();
}
};
class CubeRenderer: public Renderer{
public:
CubeRenderer(){}
void DrawShape(){
glutWireCube(1);
}
void FillShape(){
glutSolidCube(1);
}
};
#endif // RENDERER_H