Hash :
752330a5
Author :
Thomas de Grivel
Date :
2024-08-08T17:20:11
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
/* kc3
* Copyright 2022,2023,2024 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 <libkc3/kc3.h>
#include "gl_object.h"
#include "vec3.h"
#include "gl_square.h"
void gl_square_clean (s_gl_square *square)
{
assert(square);
gl_object_clean(&square->object);
}
void gl_square_delete (s_gl_square *square)
{
assert(square);
gl_square_clean(square);
free(square);
}
s_gl_square * gl_square_init (s_gl_square *square, uw seg_u, uw seg_v)
{
uw i;
uw j;
s_gl_square tmp = {0};
s_gl_triangle *triangle;
s_gl_vertex *vertex;
f32 y;
assert(square);
if (seg_u < 2)
seg_u = 2;
if (seg_v < 2)
seg_v = 2;
tmp.segments_u = seg_u;
tmp.segments_v = seg_v;
if (! gl_object_init(&tmp.object) ||
! gl_object_allocate(&tmp.object, seg_u * seg_v,
2 * (seg_u - 1) * (seg_v - 1)))
return NULL;
vertex = tmp.object.vertex.data;
i = 0;
while (i < seg_v) {
y = (f32) i / (seg_v - 1);
j = 0;
while (j < seg_u) {
vertex->tex_coord_x = vertex->pos_x = (f32) j / (seg_u - 1);
vertex->tex_coord_y = vertex->pos_y = y;
vertex->pos_z = 0.0;
vertex->normal_x = 0.0;
vertex->normal_y = 0.0;
vertex->normal_z = 1.0;
vertex++;
j++;
}
i++;
}
triangle = tmp.object.triangle.data;
i = 0;
while (i < seg_v - 1) {
j = 0;
while (j < seg_u - 1) {
triangle->a = i * seg_u + j;
triangle->b = (i + 1) * seg_u + j;
triangle->c = (i + 1) * seg_u + j + 1;
triangle++;
triangle->a = i * seg_u + j;
triangle->b = (i + 1) * seg_u + j + 1;
triangle->c = i * seg_u + j + 1;
triangle++;
j++;
}
i++;
}
gl_object_update(&tmp.object);
*square = tmp;
return square;
}
s_gl_square * gl_square_new (uw segments_u, uw segments_v)
{
s_gl_square *square;
square = calloc(1, sizeof(s_gl_square));
if (! square) {
err_puts("gl_square_new: failed to allocate memory");
return NULL;
}
if (! gl_square_init(square, segments_u, segments_v)) {
free(square);
return NULL;
}
return square;
}
void gl_square_render (const s_gl_square *square)
{
assert(square);
gl_object_render(&square->object);
}
void gl_square_render_wireframe (const s_gl_square *square)
{
assert(square);
gl_object_render_wireframe(&square->object);
}