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
|
/*
gl/sphere.cc
This file is part of the Osirion project and is distributed under
the terms of the GNU General Public License version 2
*/
#include "render/sphere.h"
#include "math/mathlib.h"
using math::Vector3f;
using math::Color;
namespace render {
const int segments = 33;
Sphere::Sphere(float r)
{
radius = r;
// TODO make global sine-cosine lists
sintable = new float[segments];
costable = new float[segments];
float d = 2 * M_PI / (segments-1);
for (int i=0; i < segments; i++) {
sintable[i] = sin( d * (float) i );
costable[i] = cos ( d * (float) i );
}
}
Sphere::~Sphere()
{
delete[] sintable;
delete[] costable;
}
Sphere::Sphere(const Sphere &other)
{
(*this) = other;
}
Sphere& Sphere::operator=(const Sphere &other)
{
sphere_color = other.sphere_color;
radius = other.radius;
return (*this);
}
void Sphere::draw()
{
using namespace gl;
float r = radius*sintable[1];
//float h = radius*costable[1];
gl::color(sphere_color);
/*
// draw top
begin(Polygon);
normal(0, 1, 0);
for (int i = segments-1; i >= 0; i--) {
v = Vector3f(r*costable[i], h, r*sintable[i]);
n = v;
n.normalize();
normal(n);
vertex(v);
}
end();
// draw bottom
begin(Polygon);
normal(0, -1, 0);
for (int i = 0; i< segments; i++) {
//for (int i = segments-1; i >= 0; i--)
v = Vector3f(r*costable[i], -h, r*sintable[i]);
n = v;
n.normalize();
normal(n);
vertex(v);
}
end();
*/
Vector3f v;
Vector3f n;
// draw body
for (int j=0; j < segments-1; j++) {
r = radius*sintable[j];
float r1 = radius*sintable[j+1];
begin(QuadStrip);
v = Vector3f(r, radius*costable[j], 0);
n = v;
n.normalize();
normal(n);
vertex(v);
v = Vector3f(r1, radius*costable[j+1], 0);
n = v;
n.normalize();
normal(n);
vertex(v);
for (int i = segments-1; i >= 0; i--) {
v = Vector3f(r*costable[i], radius*costable[j], r*sintable[i]);
n = v;
n.normalize();
normal(n);
vertex(v);
v = Vector3f(r1*costable[i], radius*costable[j+1], r1*sintable[i]);
n = v;
n.normalize();
normal(n);
vertex(v);
//vertex(r*costable[i-1], radius*costable[j], r*sintable[i-1]);
//vertex(r1*costable[i-1], radius*costable[j+1], r1*sintable[i-1]);
}
end();
}
}
}
|