-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBarrelEntity.cpp
86 lines (76 loc) · 1.63 KB
/
BarrelEntity.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
/*
* CUDARadiosity 0.87a
* Copyright 2012 by David Sargeant, Patrick Gradie, Michael Rogers
*
* Based on code from rrv (Radiosity Renderer and Visualizer) by TODO
* Distributed under GPL (see <http://www.gnu.org/licenses/>)
*
*/
#include "Entity.h"
#include "BarrelEntity.h"
#include <math.h>
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
using namespace XML;
void BarrelEntity::impl_deserialize( XMLNode *from ) {
setName(from, "barrel");
this->polygonize();
}
void BarrelEntity::polygonize ( ) {
const int PLANE_COUNT = 20;
const float RADIUS = 0.5;
Vertex v[4];
for(int i=0; i<PLANE_COUNT+1; i++)
{
v[3] = v[2];
v[0] = v[1];
// compute coords (http://en.wikipedia.org/wiki/Sphere)
float angle = 2 * M_PI / PLANE_COUNT * i;
float x = RADIUS * cos(angle);
float z = RADIUS * sin(angle);
v[2].x = v[1].x = x;
v[2].z = v[1].z = z;
v[2].y = -0.5;
v[1].y = +0.5;
if(0 != i)
{
// face
this->addQuad(v);
// upper and lower base
this->addBaseTriangles(v);
}
}
}
void BarrelEntity::addBaseTriangles(Vertex vertex[4])
{
// Triangle initialization
Triangle t;
setTriangleProperties( t );
Vertex v;
for(int j=0; j<4; j+=2)
{
for (int i=j+0; i<j+2; i++)
{
t.vertex[i-j] = vertex[2*j+1-i];
v.x=v.z=0;
v.y=vertex[i].y;
t.vertex[i+1-j]=v;
}
this->addTriangle(&t);
}
}
void BarrelEntity::addQuad(Vertex vertex[4])
{
// Triangle initialization
Triangle t;
setTriangleProperties( t );
// Add triangle #1
for (int i=0; i<3; i++)
t.vertex[i] = vertex[i];
this->addTriangle(&t);
// Add triangle #2
t.vertex[1] = vertex[2];
t.vertex[2] = vertex[3];
this->addTriangle(&t);
}