4 Copyright 2008-2012 Michel Pollet <buserror@gmail.com>
6 This file is part of libc3.
8 libc3 is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
13 libc3 is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with libc3. If not, see <http://www.gnu.org/licenses/>.
24 #include "c3geometry.h"
29 struct c3object_t * parent,
35 float const R = 1. / (float) (rings - 1);
36 float const S = 1. / (float) (sectors - 1);
38 c3geometry_p g = c3geometry_new(c3geometry_type(C3_SPHERE_TYPE, 0), parent);
40 c3vertex_array_realloc(&g->vertice, rings * sectors);
41 c3vertex_array_realloc(&g->normals, rings * sectors);
42 c3tex_array_realloc(&g->textures, rings * sectors);
43 c3indices_array_realloc(&g->indices, rings * sectors * 6);
45 for (int r = 0; r < rings; r++)
46 for (int s = 0; s < sectors; s++) {
47 float const y = sin(-M_PI_2 + M_PI * r * R);
48 float const x = cos(2 * M_PI * s * S) * sin(M_PI * r * R);
49 float const z = sin(2 * M_PI * s * S) * sin(M_PI * r * R);
51 c3tex_array_add(&g->textures, c3vec2f(s * S, r * R));
52 c3vertex_array_add(&g->vertice,
53 c3vec3_add(center, c3vec3f(x * radius, y * radius, z * radius)));
54 c3vertex_array_add(&g->normals, c3vec3_normalize(c3vec3f(x, y, z)));
57 for (int r = 0; r < rings - 1; r++)
58 for (int s = 0; s < sectors - 1; s++) {
60 r * sectors + (s + 1), r * sectors + s, (r + 1) * sectors + (s + 1),
61 (r + 1) * sectors + (s + 1), r * sectors + s, (r + 1) * sectors + s,
63 c3indices_array_insert(&g->indices, g->indices.count, i, 6);