summaryrefslogtreecommitdiff
path: root/src/revolve.c
blob: 71285ac4deb0c713ff816bcbd2e40fadcd02971f (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
/*
 * 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.
 */
#define _USE_MATH_DEFINES
#include <stdlib.h>
#include <math.h>
#include "revolve.h"

#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif

void
revolve(const GLUvec4 *points, const GLUvec4 *normals, const float *u,
	unsigned num_points, 
	const GLUvec4 *axis,
	unsigned steps, float start_angle, float end_angle,
	revolve_cb *cb, void *data, struct cb_buffer *buf)
{
	const float angle_step = (end_angle - start_angle) / (float) (steps - 1);
	const GLUvec4 tangent = {{ 0.0, 0.0, 1.0, 0.0 }};
	unsigned i;
	unsigned j;


	for (i = 0; i < steps; i++) {
		const float a = start_angle + (angle_step * i);
		const float v = (float) i / (float) (steps - 1);
		GLUmat4 r;

		/* Generate a rotation matrix to rotate the position and the
		 * normal around the axis by the current angular step.  This
		 * generates the new position and normal.
		 */
		gluRotate4v(& r, axis, a);

		for (j = 0; j < num_points; j++) {
			gluMult4m_4v(& buf->pos[buf->used], & r, & points[j]);
			gluMult4m_4v(& buf->nrm[buf->used], & r, & normals[j]);
			gluMult4m_4v(& buf->tng[buf->used], & r, & tangent);

			buf->uv[buf->used].values[0] = u[j];
			buf->uv[buf->used].values[1] = v;
			buf->uv[buf->used].values[2] = 0.0;
			buf->uv[buf->used].values[3] = 0.0;

			buf->used++;

			if (CB_BUFFER_IS_FULL(*buf)) {
				(*cb)(data, buf->pos, buf->nrm, buf->tng,
				      buf->uv, buf->used);
				CB_BUFFER_MAKE_EMPTY(*buf);
			}
		}
	}

	if (!CB_BUFFER_IS_EMPTY(*buf)) {
		(*cb)(data, buf->pos, buf->nrm, buf->tng, buf->uv, buf->used);
		CB_BUFFER_MAKE_EMPTY(*buf);
	}
}


void
generate_sphere(double radius, unsigned slices, unsigned stacks,
		_Bool normals_point_out,
		revolve_cb *cb, void *data, struct cb_buffer *buf)
{
	const GLUvec4 y_axis = {{ 0.0, 1.0, 0.0, 0.0 }};
	GLUvec4 *positions;
	GLUvec4 *normals;
	float *u;
	double latitude_step;
	unsigned i;

	positions = (GLUvec4 *) malloc((2 * sizeof(GLUvec4) + sizeof(float))
				       * (stacks + 1));
	normals = positions + (stacks + 1);
	u = (float *)(normals + (stacks + 1));

	latitude_step = M_PI / (double)(stacks);
	for (i = 0; i < (stacks + 1); i++) {
		const double latitude = latitude_step * i;

		normals[i].values[0] = sin(latitude);
		normals[i].values[1] = cos(latitude);
		normals[i].values[2] = 0.0;
		normals[i].values[3] = 0.0;

		gluMult4v_f(& positions[i],
			    & normals[i],
			    radius);
		positions[i].values[3] = 1.0;

		u[i] = (float) i / (float) stacks;

		if (!normals_point_out) {
			normals[i].values[0] = -normals[i].values[0];
			normals[i].values[1] = -normals[i].values[1];
		}
	}

	/* Note the rotation is *from* 2pi *to* 0.  The counter-clockwise
	 * rotation ensures that the vertices are generated in the correct
	 * order to be drawn using triangle strips with back-face culling.
	 */
	revolve(positions, normals, u, stacks + 1, & y_axis,
		slices + 1, 2.0 * M_PI, 0.0, cb, data, buf);
}