Hash :
77f4bb8d
Author :
Thomas de Grivel
Date :
2024-01-16T21:09:36
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
/* 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_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 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)
{
assert(camera);
glDeleteProgram(camera->gl_shader_program);
}
void gl_camera_delete (s_gl_camera *camera)
{
gl_camera_clean(camera);
free(camera);
}
s_gl_camera * gl_camera_init (s_gl_camera *camera, uw w, uw h)
{
GLuint fragment_shader;
GLint success;
GLuint vertex_shader;
assert(camera);
gl_camera_set_aspect_ratio(camera, w, h);
camera->clip_z_far = 1000.0f;
camera->clip_z_near = 0.1f;
camera->fov_y = 90.0f;
camera->position.x = 0.0f;
camera->position.y = 0.0f;
camera->position.z = -10.0f;
camera->rotation.x = 90.0f;
camera->rotation.y = 0.0f;
camera->rotation.z = 0.0f;
vertex_shader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertex_shader, 1, &g_gl_camera_vertex_shader_src,
NULL);
glCompileShader(vertex_shader);
glGetShaderiv(vertex_shader, GL_COMPILE_STATUS, &success);
if (! success) {
char info_log[512];
glGetShaderInfoLog(vertex_shader, sizeof(info_log), NULL, info_log);
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);
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;
}
s_gl_camera * gl_camera_new (uw w, uw h)
{
s_gl_camera *camera;
camera = calloc(1, sizeof(s_gl_camera));
if (! camera) {
err_puts("gl_camera_new: failed to allocate memory");
return NULL;
}
if (! gl_camera_init(camera, w, h)) {
free(camera);
return NULL;
}
return camera;
}
void gl_camera_render (s_gl_camera *camera)
{
assert(camera);
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_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->view_matrix, camera->rotation.x,
&(s_gl_point_3f) { 1.0f, 0.0f, 0.0f });
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->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_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)
{
assert(camera);
(void) camera;
glUseProgram(0);
}
s_gl_camera * gl_camera_set_aspect_ratio (s_gl_camera *camera, uw w,
uw h)
{
assert(camera);
camera->aspect_ratio = (f64) (w ? w : 1) / (h ? h : 1);
return camera;
}