diff --git a/libc3/window/sdl2/demo/earth.c b/libc3/window/sdl2/demo/earth.c
index d665427..6c93c53 100644
--- a/libc3/window/sdl2/demo/earth.c
+++ b/libc3/window/sdl2/demo/earth.c
@@ -117,7 +117,8 @@ bool earth_render (s_sequence *seq)
assert(glGetError() == GL_NO_ERROR);
//glEnable(GL_TEXTURE_2D);
assert(glGetError() == GL_NO_ERROR);
- gl_sprite_bind(&g_sprite_earth, 0);
+ gl_camera_bind_texture(camera,
+ gl_sprite_texture(&g_sprite_earth, 0));
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
GL_LINEAR_MIPMAP_LINEAR);
assert(glGetError() == GL_NO_ERROR);
diff --git a/libc3/window/sdl2/demo/flies.c b/libc3/window/sdl2/demo/flies.c
index d79b9bf..99ae8d5 100644
--- a/libc3/window/sdl2/demo/flies.c
+++ b/libc3/window/sdl2/demo/flies.c
@@ -164,6 +164,7 @@ bool flies_render (s_sequence *seq)
s_map *map;
s_gl_matrix_4f matrix;
s_gl_matrix_4f matrix_1;
+ s_gl_matrix_4f matrix_2;
uw r;
uw random_bits = 0;
f64 x;
@@ -193,8 +194,8 @@ bool flies_render (s_sequence *seq)
board_w = board_item_w * BOARD_SIZE;
board_h = board_item_h * BOARD_SIZE;
board_x = (window->w - board_w) / 2.0;
- fly_scale = 2.0 * board_item_w / g_sprite_fly.w;
- dead_fly_scale = 2.0 * board_item_w / g_sprite_dead_fly.w;
+ fly_scale = 2.0 * board_item_w / g_sprite_fly.pt_w;
+ dead_fly_scale = 2.0 * board_item_w / g_sprite_dead_fly.pt_w;
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
GL_LINEAR_MIPMAP_LINEAR);
gl_matrix_4f_init_identity(&g_ortho.model_matrix);
@@ -205,136 +206,144 @@ bool flies_render (s_sequence *seq)
buf_write_1(&buf, "In ");
buf_inspect_uw(&buf, fly_in);
buf_write_u8(&buf, 0);
- gl_font_set_size(&g_font_flies, board_item_h,
- (f64) window->gl_h / window->h);
+ gl_font_set_size(&g_font_flies, board_item_h);
gl_text_update_1(&g_text_flies_in, a);
gl_ortho_text_render(&g_ortho, &g_text_flies_in);
buf_init(&buf, false, sizeof(a), a);
buf_write_1(&buf, "Out ");
buf_inspect_uw(&buf, fly_out);
buf_write_u8(&buf, 0);
- x = board_item_w * (BOARD_SIZE / 2 + 1);
matrix = g_ortho.model_matrix; {
+ x = board_item_w * (BOARD_SIZE / 2 + 1);
gl_matrix_4f_translate(&g_ortho.model_matrix, x, 0.0, 0.0);
gl_ortho_update_model_matrix(&g_ortho);
gl_text_update_1(&g_text_flies_out, a);
gl_ortho_text_render(&g_ortho, &g_text_flies_out);
- } g_ortho.model_matrix = matrix;
- gl_matrix_4f_translate(&g_ortho.model_matrix, 0.0, board_item_h, 0.0);
- glBlendColor(0.6f, 0.7f, 0.9f, 1.0f);
- gl_ortho_rect(&g_ortho, 0, 0, board_w, board_h);
- address[1] = 0;
- while (address[1] < BOARD_SIZE) {
- y = board_item_h * address[1];
- address[0] = 0;
- while (address[0] < BOARD_SIZE) {
- x = board_item_w * address[0];
- matrix = g_ortho.model_matrix; {
- gl_matrix_4f_translate(&g_ortho.model_matrix, x,
- board_h - board_item_h - y, 0.0);
- board_item = (u8 *) array_data(board, address);
- assert(board_item);
- switch (*board_item) {
- case BOARD_ITEM_SPACE:
- break;
- case BOARD_ITEM_BLOCK:
- glBindTexture(GL_TEXTURE_2D, 0);
- glBlendColor(0.0f, 0.0f, 1.0f, 1.0f);
- gl_ortho_rect(&g_ortho, 0, 0, board_item_w + 1.0, board_item_h + 1.0);
- break;
- case BOARD_ITEM_FLY:
- matrix_1 = g_ortho.model_matrix; {
- gl_matrix_4f_translate(&g_ortho.model_matrix,
- -board_item_w / 2.0,
- -board_item_h / 2.0, 0.0);
- gl_matrix_4f_scale(&g_ortho.model_matrix, fly_scale,
- fly_scale, 1.0);
- gl_ortho_update_model_matrix(&g_ortho);
- gl_sprite_render(&g_sprite_fly, 0);
- } g_ortho.model_matrix = matrix_1;
- if (address[0] == BOARD_SIZE / 2 &&
- address[1] == BOARD_SIZE - 1) {
- array_data_set(board, address, &g_board_item_space);
- (*fly_out)++;
- fly_init(map);
+ g_ortho.model_matrix = matrix;
+ gl_matrix_4f_translate(&g_ortho.model_matrix, 0.0, board_item_h, 0.0);
+ glBlendColor(0.6f, 0.7f, 0.9f, 1.0f);
+ gl_ortho_rect(&g_ortho, 0, 0, board_w, board_h);
+ address[1] = 0;
+ while (address[1] < BOARD_SIZE) {
+ y = board_item_h * address[1];
+ address[0] = 0;
+ while (address[0] < BOARD_SIZE) {
+ x = board_item_w * address[0];
+ matrix_1 = g_ortho.model_matrix; {
+ gl_matrix_4f_translate(&g_ortho.model_matrix, x,
+ board_h - board_item_h - y, 0.0);
+ board_item = (u8 *) array_data(board, address);
+ assert(board_item);
+ switch (*board_item) {
+ case BOARD_ITEM_SPACE:
break;
- }
- fly_address[0] = address[0];
- fly_address[1] = address[1];
- i = 0;
- while (i < 3) {
- j = 0;
- while (j < 9) {
- directions[j] = true;
- j++;
+ case BOARD_ITEM_BLOCK:
+ gl_ortho_bind_texture(&g_ortho, 0);
+ glBlendColor(0.0f, 0.0f, 1.0f, 1.0f);
+ gl_ortho_rect(&g_ortho, 0, 0, board_item_w + 1.0, board_item_h + 1.0);
+ break;
+ case BOARD_ITEM_FLY:
+ matrix_2 = g_ortho.model_matrix; {
+ gl_matrix_4f_translate(&g_ortho.model_matrix,
+ -board_item_w / 2.0,
+ -board_item_h / 2.0, 0.0);
+ gl_matrix_4f_scale(&g_ortho.model_matrix, fly_scale,
+ fly_scale, 1.0);
+ gl_ortho_update_model_matrix(&g_ortho);
+ gl_ortho_bind_texture(&g_ortho,
+ gl_sprite_texture(&g_sprite_fly, 0));
+ gl_ortho_rect(&g_ortho, 0, 0, g_sprite_fly.pt_w,
+ g_sprite_fly.pt_h);
+ gl_ortho_bind_texture(&g_ortho, 0);
+ } g_ortho.model_matrix = matrix_2;
+ if (address[0] == BOARD_SIZE / 2 &&
+ address[1] == BOARD_SIZE - 1) {
+ array_data_set(board, address, &g_board_item_space);
+ (*fly_out)++;
+ fly_init(map);
+ break;
}
- while (directions[0] | directions[1] | directions[2] |
- directions[3] | directions[4] | directions[5] |
- directions[6] | directions[7] | directions[8]) {
- if (random_bits < 4) {
- r = arc4random();
- random_bits = 32;
- }
- direction = r % 16;
- r >>= 4;
- random_bits -= 4;
- if (direction >= 12)
- direction = direction_prev;
- if (direction >= 9)
- direction = (direction - 6) % 3 + 6;
- fly_prev_address[0] = fly_address[0];
- fly_prev_address[1] = fly_address[1];
- switch (direction) {
- case 0: fly_address[0]--; fly_address[1]--; break;
- case 1: fly_address[1]--; break;
- case 2: fly_address[0]++; fly_address[1]--; break;
- case 3: fly_address[0]--; ; break;
- case 4: ; break;
- case 5: fly_address[0]++; ; break;
- case 6: fly_address[0]--; fly_address[1]++; break;
- case 7: fly_address[1]++; break;
- case 8: fly_address[0]++; fly_address[1]++; break;
+ fly_address[0] = address[0];
+ fly_address[1] = address[1];
+ i = 0;
+ while (i < 3) {
+ j = 0;
+ while (j < 9) {
+ directions[j] = true;
+ j++;
}
- if (fly_address[0] < BOARD_SIZE &&
- fly_address[1] < BOARD_SIZE &&
- (board_item = (u8 *) array_data(board,
- fly_address)) &&
- *board_item == g_board_item_space) {
- array_data_set(board, fly_prev_address,
- &g_board_item_space);
- array_data_set(board, fly_address, &g_board_item_fly);
- direction_prev = direction;
- break;
+ while (directions[0] | directions[1] | directions[2] |
+ directions[3] | directions[4] | directions[5] |
+ directions[6] | directions[7] | directions[8]) {
+ if (random_bits < 4) {
+ r = arc4random();
+ random_bits = 32;
+ }
+ direction = r % 16;
+ r >>= 4;
+ random_bits -= 4;
+ if (direction >= 12)
+ direction = direction_prev;
+ if (direction >= 9)
+ direction = (direction - 6) % 3 + 6;
+ fly_prev_address[0] = fly_address[0];
+ fly_prev_address[1] = fly_address[1];
+ switch (direction) {
+ case 0: fly_address[0]--; fly_address[1]--; break;
+ case 1: fly_address[1]--; break;
+ case 2: fly_address[0]++; fly_address[1]--; break;
+ case 3: fly_address[0]--; ; break;
+ case 4: ; break;
+ case 5: fly_address[0]++; ; break;
+ case 6: fly_address[0]--; fly_address[1]++; break;
+ case 7: fly_address[1]++; break;
+ case 8: fly_address[0]++; fly_address[1]++; break;
+ }
+ if (fly_address[0] < BOARD_SIZE &&
+ fly_address[1] < BOARD_SIZE &&
+ (board_item = (u8 *) array_data(board,
+ fly_address)) &&
+ *board_item == g_board_item_space) {
+ array_data_set(board, fly_prev_address,
+ &g_board_item_space);
+ array_data_set(board, fly_address, &g_board_item_fly);
+ direction_prev = direction;
+ break;
+ }
+ directions[direction] = false;
+ fly_address[0] = fly_prev_address[0];
+ fly_address[1] = fly_prev_address[1];
}
- directions[direction] = false;
- fly_address[0] = fly_prev_address[0];
- fly_address[1] = fly_prev_address[1];
+ i++;
}
- i++;
- }
- *fly_time += 1;
- if (*fly_time > FLY_TIME_MAX) {
- array_data_set(board, fly_address, &g_board_item_dead_fly);
- fly_init(map);
+ *fly_time += 1;
+ if (*fly_time > FLY_TIME_MAX) {
+ array_data_set(board, fly_address, &g_board_item_dead_fly);
+ fly_init(map);
+ }
+ break;
+ case BOARD_ITEM_DEAD_FLY:
+ matrix_2 = g_ortho.model_matrix; {
+ gl_matrix_4f_translate(&g_ortho.model_matrix,
+ -board_item_w / 2.0,
+ -board_item_h / 2.0, 0.0);
+ gl_matrix_4f_scale(&g_ortho.model_matrix, dead_fly_scale,
+ dead_fly_scale, 1.0);
+ gl_ortho_update_model_matrix(&g_ortho);
+ gl_ortho_bind_texture(&g_ortho,
+ gl_sprite_texture(&g_sprite_dead_fly, 0));
+ gl_ortho_rect(&g_ortho, 0, 0, g_sprite_dead_fly.pt_w,
+ g_sprite_dead_fly.pt_h);
+ gl_ortho_bind_texture(&g_ortho, 0);
+ } g_ortho.model_matrix = matrix_2;
+ break;
}
- break;
- case BOARD_ITEM_DEAD_FLY:
- matrix_1 = g_ortho.model_matrix; {
- gl_matrix_4f_translate(&g_ortho.model_matrix,
- -board_item_w / 2.0,
- -board_item_h / 2.0, 0.0);
- gl_matrix_4f_scale(&g_ortho.model_matrix, dead_fly_scale,
- dead_fly_scale, 1.0);
- gl_ortho_update_model_matrix(&g_ortho);
- gl_sprite_render(&g_sprite_dead_fly, 0);
- } g_ortho.model_matrix = matrix_1;
- break;
- }
- } g_ortho.model_matrix = matrix;
- address[0]++;
+ } g_ortho.model_matrix = matrix_1;
+ address[0]++;
+ }
+ address[1]++;
}
- address[1]++;
- }
+ } g_ortho.model_matrix = matrix;
return true;
}
diff --git a/libc3/window/sdl2/demo/toasters.c b/libc3/window/sdl2/demo/toasters.c
index 1773268..fd77abc 100644
--- a/libc3/window/sdl2/demo/toasters.c
+++ b/libc3/window/sdl2/demo/toasters.c
@@ -21,7 +21,7 @@
#define TOASTERS_SCALE_TOAST 0.52
#define TOASTERS_SCALE_TOASTER 0.4
#define TOASTERS_SPACING \
- (g_sprite_toaster.h * TOASTERS_SCALE_TOASTER * 2.0)
+ (g_sprite_toaster.pt_h * TOASTERS_SCALE_TOASTER * 2.0)
static const f64 g_speed_x = 80.0;
static const f64 g_speed_y = -40.0;
@@ -50,6 +50,7 @@ static void toast_render (s_tag *toast, s_window_sdl2 *window,
s_sequence *seq)
{
s_gl_matrix_4f matrix;
+ GLuint texture;
f64 *x;
f64 *y;
if (toast->type == TAG_MAP) {
@@ -62,15 +63,18 @@ static void toast_render (s_tag *toast, s_window_sdl2 *window,
toast->type = TAG_VOID;
return;
}
- matrix = g_ortho.model_matrix;
- gl_matrix_4f_translate(&g_ortho.model_matrix, *x,
- *y + g_sprite_toast.h, 0.0);
- gl_matrix_4f_scale(&g_ortho.model_matrix,
- TOASTERS_SCALE_TOAST,
- -TOASTERS_SCALE_TOAST, 1);
- gl_ortho_update_model_matrix(&g_ortho);
- gl_sprite_render(&g_sprite_toast, 0);
- g_ortho.model_matrix = matrix;
+ matrix = g_ortho.model_matrix; {
+ gl_matrix_4f_translate(&g_ortho.model_matrix, *x,
+ *y + g_sprite_toast.pt_h, 0.0);
+ gl_matrix_4f_scale(&g_ortho.model_matrix,
+ TOASTERS_SCALE_TOAST,
+ -TOASTERS_SCALE_TOAST, 1);
+ gl_ortho_update_model_matrix(&g_ortho);
+ texture = gl_sprite_texture(&g_sprite_toast, 0);
+ gl_ortho_bind_texture(&g_ortho, texture);
+ gl_ortho_rect(&g_ortho, 0, 0, g_sprite_toast.pt_w,
+ g_sprite_toast.pt_h);
+ } g_ortho.model_matrix = matrix;
}
}
@@ -88,6 +92,7 @@ static void toaster_render (s_tag *toaster, s_window_sdl2 *window,
s_sequence *seq)
{
s_gl_matrix_4f matrix;
+ GLuint texture;
f64 *x;
f64 *y;
if (toaster->type == TAG_MAP) {
@@ -102,14 +107,18 @@ static void toaster_render (s_tag *toaster, s_window_sdl2 *window,
}
matrix = g_ortho.model_matrix;
gl_matrix_4f_translate(&g_ortho.model_matrix, *x,
- *y + g_sprite_toaster.h, 0.0);
+ *y + g_sprite_toaster.pt_h, 0.0);
gl_matrix_4f_scale(&g_ortho.model_matrix,
TOASTERS_SCALE_TOASTER,
-TOASTERS_SCALE_TOASTER, 1);
gl_ortho_update_model_matrix(&g_ortho);
- gl_sprite_render(&g_sprite_toaster,
- fmod(seq->t * g_sprite_toaster.frame_count,
- g_sprite_toaster.frame_count));
+ texture = gl_sprite_texture(&g_sprite_toaster,
+ fmod(seq->t *
+ g_sprite_toaster.frame_count,
+ g_sprite_toaster.frame_count));
+ gl_ortho_bind_texture(&g_ortho, texture);
+ gl_ortho_rect(&g_ortho, 0, 0, g_sprite_toaster.pt_w,
+ g_sprite_toaster.pt_h);
g_ortho.model_matrix = matrix;
}
}
diff --git a/libc3/window/sdl2/demo/window_sdl2_demo.c b/libc3/window/sdl2/demo/window_sdl2_demo.c
index 2e92cce..84107f7 100644
--- a/libc3/window/sdl2/demo/window_sdl2_demo.c
+++ b/libc3/window/sdl2/demo/window_sdl2_demo.c
@@ -141,8 +141,10 @@ bool window_sdl2_demo_key (s_window_sdl2 *window, SDL_Keysym *keysym)
bool window_sdl2_demo_load (s_window_sdl2 *window)
{
+ f32 point_per_pixel;
assert(window);
assert(glGetError() == GL_NO_ERROR);
+ point_per_pixel = (f32) window->w / window->gl_w;
if (window->sequence_count != WINDOW_SDL2_DEMO_SEQUENCE_COUNT) {
fprintf(stderr, "window_sdl2_demo_load: "
"window->sequence_count = %lu\n", window->sequence_count);
@@ -153,7 +155,8 @@ bool window_sdl2_demo_load (s_window_sdl2 *window)
return false;
gl_ortho_resize(&g_ortho, 0, window->w, 0, window->h, 0, 1);
if (! gl_font_init(&g_font_courier_new,
- "fonts/Courier New/Courier New.ttf"))
+ "fonts/Courier New/Courier New.ttf",
+ point_per_pixel))
return false;
if (! gl_text_init_1(&g_text_seq_title, &g_font_courier_new, ""))
return false;
@@ -168,27 +171,28 @@ bool window_sdl2_demo_load (s_window_sdl2 *window)
lightspeed_load, lightspeed_render, lightspeed_unload,
window);
if (! gl_sprite_init(&g_sprite_toaster, "img/flaps.256.png",
- 4, 1, 4))
+ 4, 1, 4, point_per_pixel))
return false;
if (! gl_sprite_init(&g_sprite_toast, "img/toast.128.png",
- 1, 1, 1))
+ 1, 1, 1, point_per_pixel))
return false;
sequence_init(window->sequence + 2, 60.0, "03. Toasters",
toasters_load, toasters_render, toasters_unload,
window);
if (! gl_font_init(&g_font_flies,
- "fonts/Courier New/Courier New.ttf"))
+ "fonts/Courier New/Courier New.ttf",
+ point_per_pixel))
return false;
if (! gl_sprite_init(&g_sprite_fly, "img/fly-noto.png",
- 1, 1, 1))
+ 1, 1, 1, point_per_pixel))
return false;
if (! gl_sprite_init(&g_sprite_dead_fly, "img/fly-dead.png",
- 1, 1, 1))
+ 1, 1, 1, point_per_pixel))
return false;
sequence_init(window->sequence + 3, 60.0, "04. Flies",
flies_load, flies_render, flies_unload, window);
if (! gl_sprite_init(&g_sprite_earth, "img/earth.png",
- 1, 1, 1))
+ 1, 1, 1, point_per_pixel))
return false;
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
assert(glGetError() == GL_NO_ERROR);
@@ -279,8 +283,7 @@ bool window_sdl2_demo_render (s_window_sdl2 *window)
glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA,
GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
assert(glGetError() == GL_NO_ERROR);
- gl_font_set_size(&g_font_courier_new, 20,
- (f64) window->gl_h / window->h);
+ gl_font_set_size(&g_font_courier_new, 20);
gl_text_update_1(&g_text_seq_title, seq->title);
gl_matrix_4f_init_identity(&g_ortho.model_matrix);
render_text(&g_text_seq_title, 20.0f, 30.0f);
diff --git a/libc3/window/sdl2/gl_camera.c b/libc3/window/sdl2/gl_camera.c
index fb58028..5bb5e71 100644
--- a/libc3/window/sdl2/gl_camera.c
+++ b/libc3/window/sdl2/gl_camera.c
@@ -15,15 +15,54 @@
#include "gl_camera.h"
#include "gl_matrix_4f.h"
-static const char * g_gl_camera_vertex_shader_src = "#version 330 core\n"
-"layout (location = 0) in vec3 aPos;\n"
-"layout (location = 1) in vec3 aNormal;\n"
-"layout (location = 2) in vec2 aTexCoord;\n"
-"uniform mat4 matrix;\n"
-"\n"
-"void main() {\n"
-" gl_Position = vec4(matrix * vec4(aPos, 1.0));\n"
-"}\n";
+static const char * g_gl_camera_vertex_shader_src =
+ "#version 330 core\n"
+ "layout (location = 0) in vec3 iPos;\n"
+ "layout (location = 1) in vec3 iNormal;\n"
+ "layout (location = 2) in vec2 iTexCoord;\n"
+ "out vec3 oFragNormal;\n"
+ "out vec2 oTexCoord;\n"
+ "uniform mat4 uProjectionMatrix;\n"
+ "uniform mat4 uViewMatrix;\n"
+ "uniform mat4 uModelMatrix;\n"
+ "void main() {\n"
+ " gl_Position = vec4(uProjectionMatrix * uViewMatrix *\n"
+ " uModelMatrix * vec4(iPos, 1.0));\n"
+ " oFragNormal = vec3(mat3(transpose(inverse(uModelMatrix))) *\n"
+ " iNormal);\n"
+ " oTexCoord = iTexCoord;\n"
+ "}\n";
+
+static const char * g_gl_camera_fragment_shader_src =
+ "#version 330 core\n"
+ "in vec3 iFragNormal;\n"
+ "in vec2 iTexCoord;\n"
+ "out vec4 oFragColor;\n"
+ "uniform bool uEnableTex2D;\n"
+ "uniform sampler2D uTex2D;\n"
+ "void main() {\n"
+ " if (uEnableTex2D) {\n"
+ " vec4 texColor = texture(uTex2D, iTexCoord);\n"
+ " oFragColor = texColor;\n"
+ " }\n"
+ " else\n"
+ " oFragColor = vec4(1, 1, 1, 1);\n"
+ "}\n";
+
+void gl_camera_bind_texture (s_gl_camera *camera, GLuint texture)
+{
+ assert(camera);
+ assert(glGetError() == GL_NO_ERROR);
+ if (! texture) {
+ glUniform1i(camera->gl_enable_tex2d_loc, 0);
+ assert(glGetError() == GL_NO_ERROR);
+ return;
+ }
+ glUniform1i(camera->gl_enable_tex2d_loc, 1);
+ assert(glGetError() == GL_NO_ERROR);
+ glUniform1i(camera->gl_tex2d_loc, texture);
+ assert(glGetError() == GL_NO_ERROR);
+}
void gl_camera_clean (s_gl_camera *camera)
{
@@ -39,8 +78,9 @@ void gl_camera_delete (s_gl_camera *camera)
s_gl_camera * gl_camera_init (s_gl_camera *camera, uw w, uw h)
{
+ GLuint fragment_shader;
GLint success;
- u32 vertex_shader;
+ GLuint vertex_shader;
assert(camera);
gl_camera_set_aspect_ratio(camera, w, h);
camera->clip_z_far = 1000.0f;
@@ -63,12 +103,32 @@ s_gl_camera * gl_camera_init (s_gl_camera *camera, uw w, uw h)
err_write_1("gl_camera_init: shader compilation failed: ");
err_puts(info_log);
}
+ fragment_shader = glCreateShader(GL_FRAGMENT_SHADER);
+ glShaderSource(fragment_shader, 1, &g_gl_camera_fragment_shader_src,
+ NULL);
+ glCompileShader(fragment_shader);
+ glGetShaderiv(fragment_shader, GL_COMPILE_STATUS, &success);
+ if (! success) {
+ char info_log[512];
+ glGetShaderInfoLog(fragment_shader, sizeof(info_log), NULL, info_log);
+ err_write_1("gl_camera_init: shader compilation failed: ");
+ err_puts(info_log);
+ }
camera->gl_shader_program = glCreateProgram();
glAttachShader(camera->gl_shader_program, vertex_shader);
+ glAttachShader(camera->gl_shader_program, fragment_shader);
glLinkProgram(camera->gl_shader_program);
glDeleteShader(vertex_shader);
- camera->gl_matrix_loc =
- glGetUniformLocation(camera->gl_shader_program, "matrix");
+ glDeleteShader(fragment_shader);
+ camera->gl_projection_matrix_loc =
+ glGetUniformLocation(camera->gl_shader_program,
+ "uProjectionMatrix");
+ camera->gl_view_matrix_loc =
+ glGetUniformLocation(camera->gl_shader_program,
+ "uViewMatrix");
+ camera->gl_model_matrix_loc =
+ glGetUniformLocation(camera->gl_shader_program,
+ "uModelMatrix");
return camera;
}
@@ -90,21 +150,27 @@ s_gl_camera * gl_camera_new (uw w, uw h)
void gl_camera_render (s_gl_camera *camera)
{
assert(camera);
- gl_matrix_4f_init_identity(&camera->matrix);
- gl_matrix_4f_perspective(&camera->matrix, camera->fov_y,
+ gl_matrix_4f_init_identity(&camera->projection_matrix);
+ gl_matrix_4f_perspective(&camera->projection_matrix, camera->fov_y,
camera->aspect_ratio, camera->clip_z_near,
camera->clip_z_far);
- gl_matrix_4f_translate(&camera->matrix, camera->position.x,
+ gl_matrix_4f_init_identity(&camera->view_matrix);
+ gl_matrix_4f_translate(&camera->view_matrix, camera->position.x,
camera->position.y, camera->position.z);
- gl_matrix_4f_rotate_axis(&camera->matrix, camera->rotation.x,
+ gl_matrix_4f_rotate_axis(&camera->view_matrix, camera->rotation.x,
&(s_gl_point_3f) { 1.0f, 0.0f, 0.0f });
- gl_matrix_4f_rotate_axis(&camera->matrix, camera->rotation.y,
+ gl_matrix_4f_rotate_axis(&camera->view_matrix, camera->rotation.y,
&(s_gl_point_3f) { 0.0f, 1.0f, 0.0f });
- gl_matrix_4f_rotate_axis(&camera->matrix, camera->rotation.z,
+ gl_matrix_4f_rotate_axis(&camera->view_matrix, camera->rotation.z,
&(s_gl_point_3f) { 0.0f, 0.0f, 1.0f });
+ gl_matrix_4f_init_identity(&camera->model_matrix);
glUseProgram(camera->gl_shader_program);
- glUniformMatrix4fv(camera->gl_matrix_loc, 1, GL_FALSE,
- &camera->matrix.xx);
+ glUniformMatrix4fv(camera->gl_projection_matrix_loc, 1, GL_FALSE,
+ &camera->projection_matrix.xx);
+ glUniformMatrix4fv(camera->gl_view_matrix_loc, 1, GL_FALSE,
+ &camera->view_matrix.xx);
+ glUniformMatrix4fv(camera->gl_model_matrix_loc, 1, GL_FALSE,
+ &camera->model_matrix.xx);
}
void gl_camera_render_end (s_gl_camera *camera)
diff --git a/libc3/window/sdl2/gl_camera.h b/libc3/window/sdl2/gl_camera.h
index e4e9307..7ccdc65 100644
--- a/libc3/window/sdl2/gl_camera.h
+++ b/libc3/window/sdl2/gl_camera.h
@@ -25,6 +25,8 @@ void gl_camera_delete (s_gl_camera *camera);
s_gl_camera * gl_camera_new (uw w, uw h);
/* Operators. */
+void gl_camera_bind_texture (s_gl_camera *camera,
+ GLuint texture);
void gl_camera_render (s_gl_camera *camera);
s_gl_camera * gl_camera_set_aspect_ratio (s_gl_camera *camera, uw w,
uw h);
diff --git a/libc3/window/sdl2/gl_font.c b/libc3/window/sdl2/gl_font.c
index 41ac14d..73e0309 100644
--- a/libc3/window/sdl2/gl_font.c
+++ b/libc3/window/sdl2/gl_font.c
@@ -23,32 +23,35 @@ void gl_font_clean (s_gl_font *font)
str_clean(&font->real_path);
}
-s_gl_font * gl_font_init (s_gl_font *font, const char *path)
+s_gl_font * gl_font_init (s_gl_font *font, const char *path,
+ f32 point_per_pixel)
{
+ s_gl_font tmp = {0};
assert(font);
assert(path);
- str_init_copy_1(&font->path, path);
- if (! file_search(&font->path, sym_1("r"), &font->real_path)) {
+ str_init_copy_1(&tmp.path, path);
+ if (! file_search(&tmp.path, sym_1("r"), &tmp.real_path)) {
err_write_1("gl_font_init: file not found: ");
err_puts(path);
- str_clean(&font->path);
+ str_clean(&tmp.path);
return NULL;
}
assert(glGetError() == GL_NO_ERROR);
- if (FT_New_Face(g_ft, font->real_path.ptr.pchar, 0, &font->ft_face)) {
+ if (FT_New_Face(g_ft, tmp.real_path.ptr.pchar, 0, &tmp.ft_face)) {
err_write_1("gl_font_init: error loading font: ");
- err_puts(font->real_path.ptr.pchar);
- str_clean(&font->path);
- str_clean(&font->real_path);
+ err_puts(tmp.real_path.ptr.pchar);
+ str_clean(&tmp.path);
+ str_clean(&tmp.real_path);
return NULL;
}
+ tmp.point_per_pixel = point_per_pixel;
+ *font = tmp;
return font;
}
-void gl_font_set_size (s_gl_font *font, f64 point_size,
- f64 pixel_per_point)
+void gl_font_set_size (s_gl_font *font, f32 point_size)
{
- f64 size;
- size = point_size * pixel_per_point;
+ f32 size;
+ size = point_size / font->point_per_pixel;
FT_Set_Pixel_Sizes(font->ft_face, 0, size);
}
diff --git a/libc3/window/sdl2/gl_font.h b/libc3/window/sdl2/gl_font.h
index 6841544..2b2faa8 100644
--- a/libc3/window/sdl2/gl_font.h
+++ b/libc3/window/sdl2/gl_font.h
@@ -20,9 +20,10 @@ extern FT_Library g_ft;
/* Stack-allocation compatible functions, call gl_font_clean
after use. */
void gl_font_clean (s_gl_font *font);
-s_gl_font * gl_font_init (s_gl_font *font, const char *path);
+s_gl_font * gl_font_init (s_gl_font *font, const char *path,
+ f32 point_per_pixel);
/* Operators. */
-void gl_font_set_size (s_gl_font *font, f64 size, f64 pixels_per_point);
+void gl_font_set_size (s_gl_font *font, f32 point_size);
#endif /* GL_FONT_H */
diff --git a/libc3/window/sdl2/gl_ortho.c b/libc3/window/sdl2/gl_ortho.c
index e89d0be..7214c5e 100644
--- a/libc3/window/sdl2/gl_ortho.c
+++ b/libc3/window/sdl2/gl_ortho.c
@@ -19,37 +19,36 @@
static const char * g_gl_ortho_vertex_shader_src =
"#version 330 core\n"
- "layout (location = 0) in vec3 aPos;\n"
- "layout (location = 1) in vec3 aNorm;\n"
- "layout (location = 2) in vec2 aTexCoord;\n"
- "out vec3 FragNormal;\n"
- "out vec2 TexCoord;\n"
- "uniform mat4 projection_matrix;\n"
- "uniform mat4 view_matrix;\n"
- "uniform mat4 model_matrix;\n"
- "\n"
+ "layout (location = 0) in vec3 iPos;\n"
+ "layout (location = 1) in vec3 iNormal;\n"
+ "layout (location = 2) in vec2 iTexCoord;\n"
+ "out vec3 oFragNormal;\n"
+ "out vec2 oTexCoord;\n"
+ "uniform mat4 uProjectionMatrix;\n"
+ "uniform mat4 uViewMatrix;\n"
+ "uniform mat4 uModelMatrix;\n"
"void main() {\n"
- " gl_Position = vec4(projection_matrix * view_matrix * \n"
- " model_matrix * vec4(aPos, 1.0));\n"
- " FragNormal = vec3(mat3(transpose(inverse(model_matrix))) *\n"
- " aNorm);\n"
- " TexCoord = aTexCoord;\n"
+ " gl_Position = vec4(uProjectionMatrix * uViewMatrix * \n"
+ " uModelMatrix * vec4(iPos, 1.0));\n"
+ " oFragNormal = vec3(mat3(transpose(inverse(uModelMatrix))) *\n"
+ " iNormal);\n"
+ " oTexCoord = iTexCoord;\n"
"}\n";
static const char * g_gl_ortho_fragment_shader_src =
"#version 330 core\n"
- "in vec3 FragNormal;\n"
- "in vec2 TexCoord;\n"
- "out vec4 FragColor;\n"
- "uniform bool enable_texture;\n"
- "uniform sampler2D tex;\n"
+ "in vec3 iFragNormal;\n"
+ "in vec2 iTexCoord;\n"
+ "out vec4 oFragColor;\n"
+ "uniform bool uEnableTex2D;\n"
+ "uniform sampler2D uTex2D;\n"
"void main() {\n"
- " if (enable_texture) {\n"
- " vec4 textureColor = texture(tex, TexCoord);\n"
- " FragColor = textureColor;\n"
+ " if (uEnableTex2D) {\n"
+ " vec4 texColor = texture(uTex2D, iTexCoord);\n"
+ " oFragColor = texColor;\n"
" }\n"
" else\n"
- " FragColor = vec4(1, 1, 1, 1);\n"
+ " oFragColor = vec4(1, 1, 1, 1);\n"
"}\n";
void gl_ortho_bind_texture (s_gl_ortho *ortho, GLuint texture)
@@ -57,13 +56,13 @@ void gl_ortho_bind_texture (s_gl_ortho *ortho, GLuint texture)
assert(ortho);
assert(glGetError() == GL_NO_ERROR);
if (! texture) {
- glUniform1i(ortho->gl_enable_texture_loc, 0);
+ glUniform1i(ortho->gl_enable_tex2d_loc, 0);
assert(glGetError() == GL_NO_ERROR);
return;
}
- glUniform1i(ortho->gl_enable_texture_loc, 1);
+ glUniform1i(ortho->gl_enable_tex2d_loc, 1);
assert(glGetError() == GL_NO_ERROR);
- glUniform1i(ortho->gl_texture_loc, texture);
+ glUniform1i(ortho->gl_tex2d_loc, texture);
assert(glGetError() == GL_NO_ERROR);
}
@@ -136,19 +135,19 @@ s_gl_ortho * gl_ortho_init (s_gl_ortho *ortho)
glDeleteShader(fragment_shader);
assert(glGetError() == GL_NO_ERROR);
ortho->gl_projection_matrix_loc =
- glGetUniformLocation(ortho->gl_shader_program, "projection_matrix");
+ glGetUniformLocation(ortho->gl_shader_program, "uProjectionMatrix");
assert(glGetError() == GL_NO_ERROR);
ortho->gl_view_matrix_loc =
- glGetUniformLocation(ortho->gl_shader_program, "view_matrix");
+ glGetUniformLocation(ortho->gl_shader_program, "uViewMatrix");
assert(glGetError() == GL_NO_ERROR);
ortho->gl_model_matrix_loc =
- glGetUniformLocation(ortho->gl_shader_program, "model_matrix");
+ glGetUniformLocation(ortho->gl_shader_program, "uModelMatrix");
assert(glGetError() == GL_NO_ERROR);
- ortho->gl_enable_texture_loc =
- glGetUniformLocation(ortho->gl_shader_program, "enable_texture");
+ ortho->gl_enable_tex2d_loc =
+ glGetUniformLocation(ortho->gl_shader_program, "uEnableTex2D");
assert(glGetError() == GL_NO_ERROR);
- ortho->gl_texture_loc =
- glGetUniformLocation(ortho->gl_shader_program, "texture2D");
+ ortho->gl_tex2d_loc =
+ glGetUniformLocation(ortho->gl_shader_program, "uTex2D");
assert(glGetError() == GL_NO_ERROR);
return ortho;
}
@@ -204,12 +203,13 @@ void gl_ortho_render (s_gl_ortho *ortho)
glUniformMatrix4fv(ortho->gl_model_matrix_loc, 1, GL_FALSE,
&ortho->model_matrix.xx);
assert(glGetError() == GL_NO_ERROR);
- glUniform1i(ortho->gl_enable_texture_loc, 0);
+ glUniform1i(ortho->gl_enable_tex2d_loc, 0);
assert(glGetError() == GL_NO_ERROR);
- glUniform1i(ortho->gl_texture_loc, 0);
+ glUniform1i(ortho->gl_tex2d_loc, 0);
assert(glGetError() == GL_NO_ERROR);
glDepthRange(ortho->clip_z_near, ortho->clip_z_far);
assert(glGetError() == GL_NO_ERROR);
+ /*
err_puts("gl_ortho_render projection matrix");
gl_matrix_4f_buf_inspect(&g_c3_env.err, &ortho->projection_matrix);
err_puts("view matrix");
@@ -217,6 +217,7 @@ void gl_ortho_render (s_gl_ortho *ortho)
err_puts("model matrix");
gl_matrix_4f_buf_inspect(&g_c3_env.err, &ortho->model_matrix);
buf_flush(&g_c3_env.err);
+ */
}
void gl_ortho_render_end (s_gl_ortho *ortho)
@@ -242,7 +243,7 @@ void gl_ortho_text_render (s_gl_ortho *ortho, const s_gl_text *text)
assert(ortho);
assert(text);
gl_ortho_bind_texture(ortho, text->texture);
- gl_ortho_rect(ortho, 0, 0, text->w, text->h);
+ gl_ortho_rect(ortho, 0, 0, text->pt_w, text->pt_h);
gl_ortho_bind_texture(ortho, 0);
}
@@ -253,6 +254,7 @@ void gl_ortho_update_model_matrix (s_gl_ortho *ortho)
glUniformMatrix4fv(ortho->gl_model_matrix_loc, 1, GL_FALSE,
&ortho->model_matrix.xx);
assert(glGetError() == GL_NO_ERROR);
+ /*
err_puts("gl_ortho_update_model_matrix projection matrix");
gl_matrix_4f_buf_inspect(&g_c3_env.err, &ortho->projection_matrix);
err_puts("view matrix");
@@ -260,6 +262,7 @@ void gl_ortho_update_model_matrix (s_gl_ortho *ortho)
err_puts("model matrix");
gl_matrix_4f_buf_inspect(&g_c3_env.err, &ortho->model_matrix);
buf_flush(&g_c3_env.err);
+ */
}
void gl_ortho_update_view_matrix (s_gl_ortho *ortho)
diff --git a/libc3/window/sdl2/gl_sprite.c b/libc3/window/sdl2/gl_sprite.c
index d812be1..d28dd60 100644
--- a/libc3/window/sdl2/gl_sprite.c
+++ b/libc3/window/sdl2/gl_sprite.c
@@ -18,20 +18,9 @@
#include "gl_sprite.h"
#include "gl_triangle.h"
-void gl_sprite_bind (const s_gl_sprite *sprite, uw frame)
-{
- assert(sprite);
- assert(frame < sprite->frame_count);
- frame %= sprite->frame_count;
- assert(glGetError() == GL_NO_ERROR);
- glBindTexture(GL_TEXTURE_2D, sprite->texture[frame]);
- assert(glGetError() == GL_NO_ERROR);
-}
-
void gl_sprite_clean (s_gl_sprite *sprite)
{
assert(sprite);
- gl_object_clean(&sprite->object);
str_clean(&sprite->path);
str_clean(&sprite->real_path);
glDeleteTextures(sprite->frame_count, sprite->texture);
@@ -97,7 +86,8 @@ static bool png_info_to_gl_info (s32 png_color_type,
}
s_gl_sprite * gl_sprite_init (s_gl_sprite *sprite, const char *path,
- uw dim_x, uw dim_y, uw frame_count)
+ uw dim_x, uw dim_y, uw frame_count,
+ f32 point_per_pixel)
{
u8 *data;
FILE *fp;
@@ -122,8 +112,6 @@ s_gl_sprite * gl_sprite_init (s_gl_sprite *sprite, const char *path,
uw y;
uw v;
s_gl_sprite tmp = {0};
- s_gl_triangle *triangle;
- s_gl_vertex *vertex;
assert(sprite);
assert(path);
assert(dim_x);
@@ -246,8 +234,8 @@ s_gl_sprite * gl_sprite_init (s_gl_sprite *sprite, const char *path,
tmp.total_h = png_h;
tmp.dim_x = dim_x;
tmp.dim_y = dim_y;
- tmp.w = tmp.total_w / dim_x;
- tmp.h = tmp.total_h / dim_y;
+ tmp.pix_w = tmp.total_w / dim_x;
+ tmp.pix_h = tmp.total_h / dim_y;
tmp.texture = calloc(tmp.frame_count, sizeof(GLuint));
if (! tmp.texture) {
err_puts("gl_sprite_init: tmp.texture:"
@@ -269,8 +257,8 @@ s_gl_sprite * gl_sprite_init (s_gl_sprite *sprite, const char *path,
str_clean(&tmp.real_path);
return NULL;
}
- sprite_stride = tmp.w * png_pixel_size;
- data = malloc(tmp.h * sprite_stride);
+ sprite_stride = tmp.pix_w * png_pixel_size;
+ data = malloc(tmp.pix_h * sprite_stride);
if (! data) {
err_write_1("gl_sprite_init: failed to allocate memory: ");
err_puts(tmp.real_path.ptr.pchar);
@@ -284,12 +272,12 @@ s_gl_sprite * gl_sprite_init (s_gl_sprite *sprite, const char *path,
while (i < tmp.frame_count && y < dim_y) {
x = 0;
while (i < tmp.frame_count && x < dim_x) {
- sprite_data = data + sprite_stride * tmp.h;
+ sprite_data = data + sprite_stride * tmp.pix_h;
v = 0;
- while (v < tmp.h) {
+ while (v < tmp.pix_h) {
sprite_data -= sprite_stride;
memcpy(sprite_data,
- png_row[y * tmp.h + v] + x * sprite_stride,
+ png_row[y * tmp.pix_h + v] + x * sprite_stride,
sprite_stride);
v++;
}
@@ -317,10 +305,8 @@ s_gl_sprite * gl_sprite_init (s_gl_sprite *sprite, const char *path,
err_puts(gl_error_string(gl_error));
return NULL;
}
- glTexImage2D(GL_TEXTURE_2D, 0, gl_format, tmp.w, tmp.h,
+ glTexImage2D(GL_TEXTURE_2D, 0, gl_format, tmp.pix_w, tmp.pix_h,
0, gl_format, gl_type, data);
- //glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tmp.w, tmp.h,
- // 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
gl_error = glGetError();
if (gl_error != GL_NO_ERROR) {
err_write_1("gl_sprite_init: ");
@@ -337,42 +323,20 @@ s_gl_sprite * gl_sprite_init (s_gl_sprite *sprite, const char *path,
y++;
}
glBindTexture(GL_TEXTURE_2D, 0);
+ assert(glGetError() == GL_NO_ERROR);
+ tmp.pt_w = tmp.pix_w * point_per_pixel;
+ tmp.pt_h = tmp.pix_h * point_per_pixel;
free(data);
free(png_data);
free(png_row);
- assert(glGetError() == GL_NO_ERROR);
- gl_object_init(&tmp.object);
- gl_object_allocate(&tmp.object, 4, 2);
- vertex = tmp.object.vertex.data;
- gl_point_3f_init(&vertex[0].position, 0.0, tmp.h, 0.0);
- gl_point_3f_init(&vertex[0].normal, 0.0, 0.0, -1.0);
- gl_point_2f_init(&vertex[0].tex_coord, 0.0, 1.0);
- gl_point_3f_init(&vertex[1].position, 0.0, 0.0, 0.0);
- gl_point_3f_init(&vertex[1].normal, 0.0, 0.0, -1.0);
- gl_point_2f_init(&vertex[1].tex_coord, 0.0, 0.0);
- gl_point_3f_init(&vertex[2].position, tmp.w, tmp.h, 0.0);
- gl_point_3f_init(&vertex[2].normal, 0.0, 0.0, -1.0);
- gl_point_2f_init(&vertex[2].tex_coord, 1.0, 1.0);
- gl_point_3f_init(&vertex[3].position, tmp.w, 0.0, 0.0);
- gl_point_3f_init(&vertex[3].normal, 0.0, 0.0, -1.0);
- gl_point_2f_init(&vertex[3].tex_coord, 1.0, 0.0);
- triangle = tmp.object.triangle.data;
- gl_triangle_init(triangle + 0, 0, 1, 2);
- gl_triangle_init(triangle + 1, 1, 3, 2);
- gl_object_update(&tmp.object);
*sprite = tmp;
return sprite;
}
-void gl_sprite_render (const s_gl_sprite *sprite, uw frame)
+GLuint gl_sprite_texture (const s_gl_sprite *sprite, uw frame)
{
assert(sprite);
- assert(glGetError() == GL_NO_ERROR);
- gl_sprite_bind(sprite, frame);
- assert(glGetError() == GL_NO_ERROR);
- gl_object_render(&sprite->object);
- assert(glGetError() == GL_NO_ERROR);
- glBindTexture(GL_TEXTURE_2D, 0);
- assert(glGetError() == GL_NO_ERROR);
- gl_object_render_wireframe(&sprite->object);
+ assert(frame < sprite->frame_count);
+ frame %= sprite->frame_count;
+ return sprite->texture[frame];
}
diff --git a/libc3/window/sdl2/gl_sprite.h b/libc3/window/sdl2/gl_sprite.h
index 07ff58e..9756dbf 100644
--- a/libc3/window/sdl2/gl_sprite.h
+++ b/libc3/window/sdl2/gl_sprite.h
@@ -19,10 +19,10 @@
after use. */
void gl_sprite_clean (s_gl_sprite *sprite);
s_gl_sprite * gl_sprite_init (s_gl_sprite *sprite, const char *path,
- uw dim_x, uw dim_y, uw frame_count);
+ uw dim_x, uw dim_y, uw frame_count,
+ f32 point_per_pixel);
/* Observers. */
-void gl_sprite_bind (const s_gl_sprite *sprite, uw frame);
-void gl_sprite_render (const s_gl_sprite *sprite, uw frame);
+GLuint gl_sprite_texture (const s_gl_sprite *sprite, uw frame);
#endif /* GL_SPRITE_H */
diff --git a/libc3/window/sdl2/gl_square.c b/libc3/window/sdl2/gl_square.c
index 8be5864..c709038 100644
--- a/libc3/window/sdl2/gl_square.c
+++ b/libc3/window/sdl2/gl_square.c
@@ -53,10 +53,10 @@ s_gl_square * gl_square_init (s_gl_square *square, uw seg_u, uw seg_v)
vertex = tmp.object.vertex.data;
i = 0;
while (i < seg_v) {
- y = (f32) i / seg_v;
+ y = (f32) i / (seg_v - 1);
j = 0;
while (j < seg_u) {
- vertex->tex_coord.x = vertex->position.x = (f32) j / seg_u;
+ vertex->tex_coord.x = vertex->position.x = (f32) j / (seg_u - 1);
vertex->tex_coord.y = vertex->position.y = y;
vertex->position.z = 0.0;
vertex->normal = normal;
diff --git a/libc3/window/sdl2/gl_text.c b/libc3/window/sdl2/gl_text.c
index 4b2b2b3..a04617b 100644
--- a/libc3/window/sdl2/gl_text.c
+++ b/libc3/window/sdl2/gl_text.c
@@ -19,7 +19,6 @@
void gl_text_clean (s_gl_text *text)
{
- gl_object_clean(&text->object);
str_clean(&text->str);
glDeleteTextures(1, &text->texture);
}
@@ -31,9 +30,6 @@ s_gl_text * gl_text_init (s_gl_text *text, const s_gl_font *font)
tmp.font = font;
glGenTextures(1, &tmp.texture);
assert(glGetError() == GL_NO_ERROR);
- gl_object_init(&tmp.object);
- assert(glGetError() == GL_NO_ERROR);
- gl_object_allocate(&tmp.object, 4, 2);
*text = tmp;
return text;
}
@@ -58,14 +54,6 @@ s_gl_text * gl_text_init_str (s_gl_text *text, const s_gl_font *font,
return text;
}
-void gl_text_render (const s_gl_text *text)
-{
- assert(text);
- assert(glGetError() == GL_NO_ERROR);
- gl_object_render(&text->object);
- assert(glGetError() == GL_NO_ERROR);
-}
-
bool gl_text_render_to_texture (s_gl_text *text)
{
character c;
@@ -159,8 +147,10 @@ bool gl_text_render_to_texture (s_gl_text *text)
assert(glGetError() == GL_NO_ERROR);
free(data);
glBindTexture(GL_TEXTURE_2D, 0);
- text->w = data_w;
- text->h = data_h;
+ text->pix_w = data_w;
+ text->pix_h = data_h;
+ text->pt_w = text->pix_w * text->font->point_per_pixel;
+ text->pt_h = text->pix_h * text->font->point_per_pixel;
return true;
}
@@ -210,28 +200,7 @@ bool gl_text_set_text_buf (s_gl_text *text, s_buf *buf)
bool gl_text_update (s_gl_text *text)
{
- s_gl_vertex *vertex;
- s_gl_triangle *triangle;
- if (! gl_text_render_to_texture(text))
- return false;
- vertex = text->object.vertex.data;
- gl_point_3f_init(&vertex[0].position, 0.0, text->h, 0.0);
- gl_point_3f_init(&vertex[0].normal, 0.0, 0.0, -1.0);
- gl_point_2f_init(&vertex[0].tex_coord, 0.0, 1.0);
- gl_point_3f_init(&vertex[1].position, 0.0, 0.0, 0.0);
- gl_point_3f_init(&vertex[1].normal, 0.0, 0.0, -1.0);
- gl_point_2f_init(&vertex[1].tex_coord, 0.0, 0.0);
- gl_point_3f_init(&vertex[2].position, text->w, text->h, 0.0);
- gl_point_3f_init(&vertex[2].normal, 0.0, 0.0, -1.0);
- gl_point_2f_init(&vertex[2].tex_coord, 1.0, 1.0);
- gl_point_3f_init(&vertex[3].position, text->w, 0.0, 0.0);
- gl_point_3f_init(&vertex[3].normal, 0.0, 0.0, -1.0);
- gl_point_2f_init(&vertex[3].tex_coord, 1.0, 0.0);
- triangle = text->object.triangle.data;
- gl_triangle_init(triangle + 0, 0, 1, 2);
- gl_triangle_init(triangle + 1, 1, 3, 2);
- gl_object_update(&text->object);
- return true;
+ return gl_text_render_to_texture(text);
}
bool gl_text_update_1 (s_gl_text *text, const char *p)
diff --git a/libc3/window/sdl2/types.h b/libc3/window/sdl2/types.h
index 1c824ed..fef23ea 100644
--- a/libc3/window/sdl2/types.h
+++ b/libc3/window/sdl2/types.h
@@ -86,9 +86,9 @@ typedef void (*f_window_sdl2_unload) (s_window_sdl2 *window);
struct gl_font {
FT_Face ft_face;
- f32 point_size;
- f32 pixel_per_point;
s_str path;
+ f32 point_size;
+ f32 point_per_pixel;
s_str real_path;
};
@@ -192,27 +192,27 @@ struct gl_point_3f {
};
struct gl_sprite {
- s_str path;
- s_str real_path;
- uw total_w;
- uw total_h;
uw dim_x;
uw dim_y;
uw frame_count;
+ s_str path;
+ uw pix_w;
+ uw pix_h;
f32 pt_w;
f32 pt_h;
- uw tex_w;
- uw tex_h;
+ s_str real_path;
GLuint *texture;
+ uw total_w;
+ uw total_h;
};
struct gl_text {
const s_gl_font *font;
- s_str str;
- uw tex_w;
- uw tex_h;
+ uw pix_w;
+ uw pix_h;
f32 pt_w;
f32 pt_h;
+ s_str str;
GLuint texture;
};
@@ -272,8 +272,15 @@ struct gl_camera {
f32 fov_y;
s_gl_point_3f position;
s_gl_point_3f rotation;
- s_gl_matrix_4f matrix;
- u32 gl_matrix_loc;
+ s_gl_point_3f scale;
+ s_gl_matrix_4f projection_matrix;
+ u32 gl_projection_matrix_loc;
+ s_gl_matrix_4f view_matrix;
+ u32 gl_view_matrix_loc;
+ s_gl_matrix_4f model_matrix;
+ u32 gl_model_matrix_loc;
+ u32 gl_enable_tex2d_loc;
+ u32 gl_tex2d_loc;
u32 gl_shader_program;
};
@@ -337,9 +344,8 @@ struct gl_ortho {
u32 gl_view_matrix_loc;
s_gl_matrix_4f model_matrix;
u32 gl_model_matrix_loc;
- u32 gl_color_loc;
- u32 gl_enable_texture_loc;
- u32 gl_texture_loc;
+ u32 gl_enable_tex2d_loc;
+ u32 gl_tex2d_loc;
u32 gl_shader_program;
s_gl_square square;
};