-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathsphere.cpp
More file actions
176 lines (136 loc) · 3.19 KB
/
Copy pathsphere.cpp
File metadata and controls
176 lines (136 loc) · 3.19 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
// Copyright (c) 2000, 2001, 2002, 2003 by David Scherer and others.
// Copyright (c) 2003, 2004 by Jonathan Brandmeyer and others.
// See the file license.txt for complete license terms.
// See the file authors.txt for a complete list of contributors.
#include "sphere.hpp"
#include "util/quadric.hpp"
#include "util/errors.hpp"
#include "util/icososphere.hpp"
#include "util/gl_enable.hpp"
#include <vector>
namespace cvisual {
displaylist sphere::lod_cache[6];
sphere::sphere()
{
}
sphere::sphere( const sphere& other)
: axial(other)
{
}
sphere::~sphere()
{
}
void
sphere::gl_pick_render( const view& geometry)
{
if (degenerate())
return;
init_model();
clear_gl_error();
gl_matrix_stackguard guard;
model_world_transform( geometry.gcf, get_scale() ).gl_mult();
lod_cache[0].gl_render();
check_gl_error();
}
double
sphere::get_max_dimension()
{
return radius;
}
void
sphere::gl_render( const view& geometry)
{
if (degenerate())
return;
init_model();
clear_gl_error();
// coverage is the radius of this sphere in pixels:
double coverage = geometry.pixel_coverage( pos, get_max_dimension());
int lod = 0;
if (coverage < 0) // Behind the camera, but still visible.
lod = 4;
else if (coverage < 30)
lod = 0;
else if (coverage < 100)
lod = 1;
else if (coverage < 500)
lod = 2;
else if (coverage < 5000)
lod = 3;
else
lod = 4;
lod += geometry.lod_adjust; // allow user to reduce level of detail
if (lod > 5)
lod = 5;
else if (lod < 0)
lod = 0;
gl_matrix_stackguard guard;
model_world_transform( geometry.gcf, get_scale() ).gl_mult();
color.gl_set(opacity);
if (translucent()) {
// Spheres are convex, so we don't need to sort
gl_enable cull_face( GL_CULL_FACE);
// Render the back half (inside)
glCullFace( GL_FRONT );
lod_cache[lod].gl_render();
// Render the front half (outside)
glCullFace( GL_BACK );
lod_cache[lod].gl_render();
}
else {
// Render a simple sphere.
lod_cache[lod].gl_render();
}
check_gl_error();
}
void
sphere::grow_extent( extent& e)
{
e.add_sphere( pos, radius);
e.add_body();
}
void
sphere::init_model()
{
if (lod_cache[0]) return;
clear_gl_error();
quadric sph;
lod_cache[0].gl_compile_begin();
sph.render_sphere( 1.0, 13, 7);
lod_cache[0].gl_compile_end();
lod_cache[1].gl_compile_begin();
sph.render_sphere( 1.0, 19, 11);
lod_cache[1].gl_compile_end();
lod_cache[2].gl_compile_begin();
sph.render_sphere( 1.0, 35, 19);
lod_cache[2].gl_compile_end();
lod_cache[3].gl_compile_begin();
sph.render_sphere( 1.0, 55, 29);
lod_cache[3].gl_compile_end();
lod_cache[4].gl_compile_begin();
sph.render_sphere( 1.0, 70, 34);
lod_cache[4].gl_compile_end();
// Only for the very largest bodies.
lod_cache[5].gl_compile_begin();
sph.render_sphere( 1.0, 140, 69);
lod_cache[5].gl_compile_end();
check_gl_error();
}
vector
sphere::get_scale()
{
return vector( radius, radius, radius);
}
bool
sphere::degenerate()
{
return !visible || radius == 0.0;
}
void
sphere::get_material_matrix(const view&, tmatrix& out) {
out.translate( vector(.5,.5,.5) );
vector scale = get_scale();
out.scale( scale * (.5 / std::max(scale.x, std::max(scale.y, scale.z))) );
}
PRIMITIVE_TYPEINFO_IMPL(sphere)
} // !namespace cvisual