summaryrefslogtreecommitdiff
path: root/src/sphere.cpp
blob: f51160ecf285c269f34aed05f0d132dbd89473b4 (plain)
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
/*

 * Copyright © 2010 Ian D. Romanick
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 */
#include "glu3.h"
#include "revolve.h"
#include "mesh.h"

/**
 * \name Call-back functions for sphere generation routine
 */
/*@{*/
static void sphere_revolve_cb(void *data,
			      const GLUvec4 *position,
			      const GLUvec4 *normal,
			      const GLUvec4 *tangent,
			      const GLUvec4 *uv);

static void sphere_begin_cb(void *data, GLenum mode);

static void sphere_index_cb(void *data, unsigned index);

static void sphere_end_cb(void *data);
/*@}*/

/**
 * GLUsphere decorator to implement call-backs
 *
 * To generate the sphere data, the \c GLUsphere class interfaces with various
 * C functions that use a call-back mechanism.  These call-backs are analogous
 * the \c emit_vertex, \c emit_begin, \c emit_index, and \c emit_end methods
 * that \c GLUsphere subclasses will provide.
 *
 * However, these methods are all \c protected.  As a result the non-class
 * call-back functions cannot call these methods unless they are \c friend
 * functions.  It is undesireable to expose the implementation detail in the
 * application-facing header file.  This can be worked around by creating a
 * dummy subclass of \c GLUsphere that only contains the \c friend function
 * declarations.  Pointers to \c GLUsphere objects can be cast to pointers to
 * \c GLUsphereFriend objects without side-effect.
 *
 * This is arguably a mis-use of the "decorator" pattern, but it is the most
 * efficient way to do this.
 */
class GLUsphereFriend : public GLUsphere {
	friend void sphere_revolve_cb(void *data,
				      const GLUvec4 *position,
				      const GLUvec4 *normal,
				      const GLUvec4 *tangent,
				      const GLUvec4 *uv);

	friend void sphere_begin_cb(void *data, GLenum mode);

	friend void sphere_index_cb(void *data, unsigned index);

	friend void sphere_end_cb(void *data);
};


GLUsphere::GLUsphere(GLdouble radius, GLint slices, GLint stacks)
{
	this->radius = radius;
	this->slices = (slices < 4) ? 4 : (unsigned) slices;
	this->stacks = (stacks < 4) ? 4 : (unsigned) stacks;
}


unsigned
GLUsphere::vertex_count(void) const
{
	/* Each line of vertices from the north pole to the south pole consists
	 * of (stacks+1) vertices.  There are a total of (slices+1) of these
	 * lines of vertices.
	 */
	return (slices + 1) * (stacks + 1);
}


unsigned
GLUsphere::primitive_count(void) const
{
	/* For each slice there is a triangle strip from the north pole to the
	 * south pole.
	 */
	return slices;
}


unsigned
GLUsphere::element_count(void) const
{
	/* Each slice is a triangle strip represented by 2 elements plus
	 * 2 elements for each stack;
	 */
	return slices * (2 * (stacks + 1));
}


static void
sphere_revolve_cb(void *data, const GLUvec4 *position, const GLUvec4 *normal,
		  const GLUvec4 *tangent, const GLUvec4 *uv)
{
	GLUsphereFriend *s = (GLUsphereFriend *) data;

	s->emit_vertex(*position, *normal, *tangent, *uv);
}


static void
sphere_begin_cb(void *data, GLenum mode)
{
	GLUsphereFriend *s = (GLUsphereFriend *) data;

	s->emit_begin(mode);
}


static void
sphere_index_cb(void *data, unsigned index)
{
	GLUsphereFriend *s = (GLUsphereFriend *) data;

	s->emit_index(index);
}


static void
sphere_end_cb(void *data)
{
	GLUsphereFriend *s = (GLUsphereFriend *) data;

	s->emit_end();
}


void
GLUsphere::generate(void)
{
	generate_sphere(radius, slices, stacks,
			normals_point_out,
			sphere_revolve_cb, (void *) this);

	generate_triangle_mesh(slices, stacks + 1, stacks + 1,
			       sphere_begin_cb,
			       sphere_index_cb,
			       sphere_end_cb,
			       (void *) this);
}