Hash :
44f16529
Author :
Thomas de Grivel
Date :
2023-12-14T01:13:47
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 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
/* c3
* Copyright 2022,2023 kmx.io <contact@kmx.io>
*
* Permission is hereby granted to use this software granted the above
* copyright notice and this permission paragraph are included in all
* copies and substantial portions of this software.
*
* THIS SOFTWARE IS PROVIDED "AS-IS" WITHOUT ANY GUARANTEE OF
* PURPOSE AND PERFORMANCE. IN NO EVENT WHATSOEVER SHALL THE
* AUTHOR BE CONSIDERED LIABLE FOR THE USE AND PERFORMANCE OF
* THIS SOFTWARE.
*/
#include <math.h>
#include <libc3/c3.h>
#include "gl.h"
#include "gl_sphere.h"
void gl_sphere_clean (s_gl_sphere *sphere)
{
assert(sphere);
free(sphere->vertex);
}
void gl_sphere_delete (s_gl_sphere *sphere)
{
assert(sphere);
gl_sphere_clean(sphere);
free(sphere);
}
s_gl_sphere * gl_sphere_init (s_gl_sphere *sphere, uw segments_u,
uw segments_v)
{
f64 angle_i;
f64 angle_j;
uw i;
uw j;
s_gl_3d *p;
f64 r;
s_gl_sphere tmp = {0};
f64 z;
assert(sphere);
if (segments_u < 3)
segments_u = 3;
if (segments_v < 2)
segments_v = 2;
tmp.segments_u = segments_u;
tmp.segments_v = segments_v;
p = calloc(segments_u * segments_v + 2, sizeof(s_gl_3d));
if (! p) {
err_write_1("gl_sphere_init(");
err_inspect_uw(&segments_u);
err_write_1(", ");
err_inspect_uw(&segments_v);
err_write_1("): failed to allocate memory");
return NULL;
}
tmp.vertex = p;
i = 0;
while (i < segments_v) {
angle_i = (i + 1) * M_PI / (segments_v + 1);
r = sin(angle_i);
z = cos(angle_i);
j = 0;
while (j < segments_u) {
angle_j = j * M_PI * 2.0 / segments_u;
p->x = cos(angle_j) * r;
p->y = sin(angle_j) * r;
p->z = z;
p++;
j++;
}
i++;
}
p->x = 0.0;
p->y = 0.0;
p->z = 1.0;
p++;
p->x = 0.0;
p->y = 0.0;
p->z = -1.0;
*sphere = tmp;
return sphere;
}
s_gl_sphere * gl_sphere_new (uw segments_u, uw segments_w)
{
s_gl_sphere *sphere;
sphere = calloc(1, sizeof(s_gl_sphere));
if (! sphere) {
err_puts("gl_sphere_new: failed to allocate memory");
return NULL;
}
if (! gl_sphere_init(sphere, segments_u, segments_w)) {
free(sphere);
return NULL;
}
return sphere;
}
void gl_sphere_render (const s_gl_sphere *sphere)
{
uw i;
uw j;
s_gl_3d *p[3];
s_gl_2d tex_coord[3];
uw seg_u;
uw seg_v;
assert(sphere);
seg_u = sphere->segments_u;
seg_v = sphere->segments_v;
glDisable(GL_CULL_FACE);
// first row
glBegin(GL_QUAD_STRIP);
tex_coord[0].y = 0.0;
tex_coord[1].y = 1.0 / (seg_v + 1);
p[0] = sphere->vertex + seg_u * seg_v;
j = 0;
while (j < seg_u + 1) {
p[1] = sphere->vertex + j % seg_u;
tex_coord[0].x = tex_coord[1].x = (f64) (seg_u - j) / seg_u;
gl_tex_coord_2d(tex_coord);
gl_normal_3d(p[0]);
gl_vertex_3d(p[0]);
gl_tex_coord_2d(tex_coord + 1);
gl_normal_3d(p[1]);
gl_vertex_3d(p[1]);
j++;
}
glEnd();
// whole
i = 1;
while (i < seg_v) {
glBegin(GL_QUAD_STRIP);
tex_coord[0].y = (f64) (i + 1) / (seg_v + 1);
tex_coord[1].y = (f64) i / (seg_v + 1);
j = 0;
while (j < seg_u + 1) {
p[0] = sphere->vertex + i * seg_u + j % seg_u;
p[1] = sphere->vertex + (i - 1) * seg_u + j % seg_u;
tex_coord[0].x = tex_coord[1].x = (f64) (seg_u - j) / seg_u;
gl_tex_coord_2d(tex_coord);
gl_normal_3d(p[0]);
gl_vertex_3d(p[0]);
gl_tex_coord_2d(tex_coord + 1);
gl_normal_3d(p[1]);
gl_vertex_3d(p[1]);
j++;
}
glEnd();
i++;
}
// last row
glBegin(GL_QUAD_STRIP);
tex_coord[0].y = 1.0;
tex_coord[1].y = (f64) seg_v / (seg_v + 1);
p[0] = sphere->vertex + seg_u * seg_v + 1;
j = 0;
while (j < seg_u + 1) {
p[1] = sphere->vertex + (seg_v - 1) * seg_u + j % seg_u;
tex_coord[0].x = tex_coord[1].x = (f64) (seg_u - j) / seg_u;
gl_tex_coord_2d(tex_coord);
gl_normal_3d(p[0]);
gl_vertex_3d(p[0]);
gl_tex_coord_2d(tex_coord + 1);
gl_normal_3d(p[1]);
gl_vertex_3d(p[1]);
j++;
}
glEnd();
}
void gl_sphere_render_wireframe (const s_gl_sphere *sphere)
{
uw i;
uw j;
s_gl_3d *p[3];
uw seg_u;
uw seg_v;
assert(sphere);
seg_u = sphere->segments_u;
seg_v = sphere->segments_v;
// first row
j = 0;
while (j < seg_u + 1) {
glBegin(GL_LINE_STRIP);
p[0] = sphere->vertex + seg_u * seg_v;
p[1] = sphere->vertex + j % seg_u;
p[2] = sphere->vertex + (j + 1) % seg_u;
gl_vertex_3d(p[0]);
gl_vertex_3d(p[1]);
gl_vertex_3d(p[2]);
glEnd();
j++;
}
// whole
i = 1;
while (i < seg_v) {
j = 0;
while (j < seg_u + 1) {
glBegin(GL_LINE_STRIP);
p[0] = sphere->vertex + (i - 1) * seg_u + j % seg_u;
p[1] = sphere->vertex + i * seg_u + j % seg_u;
p[2] = sphere->vertex + i * seg_u + (j + 1) % seg_u;
gl_vertex_3d(p[0]);
gl_vertex_3d(p[1]);
gl_vertex_3d(p[2]);
glEnd();
j++;
}
i++;
}
glBegin(GL_LINES);
j = 0;
while (j < seg_u + 1) {
p[0] = sphere->vertex + seg_u * seg_v + 1;
p[1] = sphere->vertex + (seg_v - 1) * seg_u + j % seg_u;
gl_vertex_3d(p[0]);
gl_vertex_3d(p[1]);
j++;
}
glEnd();
}