Hash :
4b1e3c0c
Author :
Thomas de Grivel
Date :
2025-10-09T10:49:27
fix gl_camera and egl demo earth
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
/* kc3
* Copyright from 2022 to 2025 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/gl.h"
#include "../window_egl.h"
#include "earth.h"
#define EARTH_CAMERA_ROTATION_Z_SPEED 0.1
#define EARTH_SEGMENTS_U 200
#define EARTH_SEGMENTS_V 100
s_gl_sprite g_sprite_earth = {0};
u8 earth_load (s_sequence_egl *seq)
{
s_gl_camera *camera;
s_map *map;
s_mat4 matrix;
s_gl_sphere *sphere;
const f32 sphere_radius = 5.0;
s_window_egl *window;
assert(seq);
window = seq->window;
assert(window);
err_write_1("earth_load: starting, window size: ");
err_inspect_uw_decimal(window->w);
err_write_1(" x ");
err_inspect_uw_decimal(window->h);
err_write_1("\n");
camera = gl_camera_new(window->w, window->h);
if (! camera) {
err_puts("earth_load: gl_camera_new failed");
return false;
}
err_puts("earth_load: camera created");
if (! camera)
return false;
sphere = gl_sphere_new(EARTH_SEGMENTS_U, EARTH_SEGMENTS_V);
if (! sphere)
return false;
mat4_init_scale(&matrix, sphere_radius, sphere_radius,
sphere_radius);
gl_object_transform(&sphere->object, &matrix);
gl_object_update(&sphere->object);
if (! tag_map(&seq->tag, 3))
return false;
map = &seq->tag.data.map;
tag_init_psym( map->key + 0, sym_1("camera"));
tag_init_ptr( map->value + 0, camera);
tag_init_psym( map->key + 1,
sym_1("camera_rot_x_speed"));
tag_init_f64( map->value + 1, 0.01);
tag_init_psym( map->key + 2, sym_1("sphere"));
tag_init_pstruct_with_data(map->value + 2, sym_1("GL.Sphere"),
sphere, false);
return true;
}
u8 earth_render (s_sequence_egl *seq)
{
s_gl_camera *camera;
f64 *camera_rot_x_speed;
s_map *map;
s_gl_sphere *sphere;
s_window_egl *window;
assert(seq);
window = seq->window;
assert(window);
if (! seq || seq->tag.type != TAG_MAP ||
seq->tag.data.map.count != 3) {
err_puts("earth_render: invalid seq->tag");
return false;
}
map = &seq->tag.data.map;
if (map->value[0].type != TAG_PTR ||
map->value[1].type != TAG_F64 ||
map->value[2].type != TAG_PSTRUCT) {
err_puts("earth_render: invalid map");
return false;
}
camera = map->value[0].data.ptr.p;
camera_rot_x_speed = &map->value[1].data.f64;
sphere = map->value[2].data.pstruct->data;
gl_camera_set_aspect_ratio(camera, window->w, window->h);
camera->rotation.x += seq->dt * (*camera_rot_x_speed) *
M_PI * 2.0f;
if (camera->rotation.x > M_PI || camera->rotation.x < 0)
*camera_rot_x_speed *= -1.0;
camera->rotation.z += seq->dt * EARTH_CAMERA_ROTATION_Z_SPEED *
M_PI * 2.0f;
camera->light_count = 1;
camera->light_pos[0] = (s_vec4) { 1.0f, 0.0f, 0.0f, 0.0f };
camera->light_color[0] = (s_rgb) { 1.0f, 0.98f, 0.95f };
assert(glGetError() == GL_NO_ERROR);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
assert(glGetError() == GL_NO_ERROR);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
assert(glGetError() == GL_NO_ERROR);
gl_camera_render(camera);
assert(glGetError() == GL_NO_ERROR);
//glEnable(GL_DEPTH_TEST);
assert(glGetError() == GL_NO_ERROR);
//err_puts("earth_render: binding texture");
GLuint tex = gl_sprite_texture(&g_sprite_earth, 0);
/*err_write_1("earth_render: texture id = ");
err_inspect_u32(tex);
err_write_1("\n");*/
gl_camera_bind_texture(camera, tex);
assert(glGetError() == GL_NO_ERROR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
GL_LINEAR_MIPMAP_LINEAR);
assert(glGetError() == GL_NO_ERROR);
//err_puts("earth_render: rendering sphere");
gl_sphere_render(sphere);
//err_puts("earth_render: sphere rendered");
glDisable(GL_DEPTH_TEST);
assert(glGetError() == GL_NO_ERROR);
gl_camera_render_end(camera);
assert(glGetError() == GL_NO_ERROR);
return true;
}
u8 earth_unload (s_sequence_egl *seq)
{
s_gl_camera *camera;
s_map *map;
if (seq && seq->tag.type == TAG_MAP) {
map = &seq->tag.data.map;
if (map->count >= 1 && map->value[0].type == TAG_PTR) {
camera = map->value[0].data.ptr.p;
if (camera)
gl_camera_delete(camera);
}
}
return true;
}