From f6de2b1453930267015ab9323d5f83daa25667ba Mon Sep 17 00:00:00 2001 From: Charles Date: Thu, 14 May 2020 19:09:39 +0200 Subject: Added ugly Cylindric UV mapping, texture/color transition --- Makefile | 5 ++++- inc/scop.h | 18 ++++++++++++++---- res/mylittlepony.bmp | Bin 0 -> 611118 bytes shader/fragment.glsl | 4 +++- shader/vertex.glsl | 2 +- src/graphic/event.c | 6 +++++- src/graphic/state.c | 16 +++++++++++++++- src/main.c | 6 +----- src/scene.c | 3 ++- src/shader.c | 6 ++++-- src/texture.c | 28 ++++++++++++++++------------ vendor/libftm/inc/libftm_mat4.h | 13 +++++++++---- vendor/libftm/inc/libftm_vec3.h | 21 +++++++++++---------- vendor/libftm/src/vec3/ftm_vec3norm.c | 18 ++++++++++++++++++ vendor/libftm/src/vec3/ftm_vec3normalize.c | 24 ++++++++++++++++++++++++ 15 files changed, 127 insertions(+), 43 deletions(-) create mode 100644 res/mylittlepony.bmp create mode 100644 vendor/libftm/src/vec3/ftm_vec3norm.c create mode 100644 vendor/libftm/src/vec3/ftm_vec3normalize.c diff --git a/Makefile b/Makefile index fb41877..4a77289 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ # By: charles +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2020/05/09 10:24:52 by charles #+# #+# # -# Updated: 2020/05/14 15:30:27 by charles ### ########.fr # +# Updated: 2020/05/14 19:05:41 by charles ### ########.fr # # # # **************************************************************************** # @@ -39,6 +39,9 @@ INC = $(shell find $(INC_DIR) -type f -name '*.h') all: prebuild $(NAME) +release: CFLAGS += -DSCOP_RELEASE -O3 +release: re + prebuild: @mkdir -p $(OBJ_DIR) @mkdir -p $(OBJ_DIR)/graphic diff --git a/inc/scop.h b/inc/scop.h index 50d433c..4cf7731 100644 --- a/inc/scop.h +++ b/inc/scop.h @@ -6,7 +6,7 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/05/09 10:41:44 by charles #+# #+# */ -/* Updated: 2020/05/14 16:29:38 by charles ### ########.fr */ +/* Updated: 2020/05/14 19:08:24 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -59,6 +59,7 @@ typedef struct t_ftmmat4 view; t_ftmmat4 proj; } transform; + float color_ratio; } t_scene; typedef struct @@ -70,6 +71,7 @@ typedef struct int view; int proj; int texture; + int color_ratio; } location; } t_shader; @@ -84,6 +86,8 @@ typedef struct t_shader shader; GLenum polygon_mode; float fov; + bool transition; + bool is_texture; } t_state; union u_color @@ -131,9 +135,15 @@ int parse(char *filepath, t_model_data *object); ** error.c */ -# define GL_CALL(x) error_clear(); \ - x; \ - error_check(#x, __FILE__, __LINE__) +# ifndef SCOP_RELEASE +# define GL_CALL(x) do { \ + error_clear(); \ + x; \ + error_check(#x, __FILE__, __LINE__); \ +} while (0) +# else +# define GL_CALL(x) x +# endif void error_clear(void); void error_check(char *code, char *filename, int line_num); diff --git a/res/mylittlepony.bmp b/res/mylittlepony.bmp new file mode 100644 index 0000000..a750e8e Binary files /dev/null and b/res/mylittlepony.bmp differ diff --git a/shader/fragment.glsl b/shader/fragment.glsl index 4ada539..710cfbe 100644 --- a/shader/fragment.glsl +++ b/shader/fragment.glsl @@ -6,8 +6,10 @@ in vec2 v_texture_coord; out vec4 out_color; uniform sampler2D u_texture; +uniform float u_color_ratio; void main() { - out_color = texture(u_texture, v_texture_coord); + out_color = v_color * u_color_ratio + + texture(u_texture, v_texture_coord) * (1.0 - u_color_ratio); } diff --git a/shader/vertex.glsl b/shader/vertex.glsl index b26899e..e61ec34 100644 --- a/shader/vertex.glsl +++ b/shader/vertex.glsl @@ -13,7 +13,7 @@ out vec2 v_texture_coord; void main() { + gl_Position = u_proj * u_view * u_model * in_position; v_color = in_color; v_texture_coord = in_texture_coord; - gl_Position = u_proj * u_view * u_model * in_position; } diff --git a/src/graphic/event.c b/src/graphic/event.c index c5986d4..ee20eab 100644 --- a/src/graphic/event.c +++ b/src/graphic/event.c @@ -6,7 +6,7 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/05/14 15:25:52 by charles #+# #+# */ -/* Updated: 2020/05/14 16:39:13 by charles ### ########.fr */ +/* Updated: 2020/05/14 18:49:43 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -26,6 +26,10 @@ static void st_handle_keydown(t_state *state, SDL_Keycode sym) state->polygon_mode = GL_FILL; GL_CALL(glPolygonMode(GL_FRONT_AND_BACK, state->polygon_mode)); } + else if (sym == SDLK_t) + { + state->transition = true; + } } static void st_handle_mousewheel(t_state *state, int y) diff --git a/src/graphic/state.c b/src/graphic/state.c index c3aaa8e..06569f8 100644 --- a/src/graphic/state.c +++ b/src/graphic/state.c @@ -6,12 +6,14 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/05/11 01:31:10 by charles #+# #+# */ -/* Updated: 2020/05/14 16:39:59 by charles ### ########.fr */ +/* Updated: 2020/05/14 19:02:56 by charles ### ########.fr */ /* */ /* ************************************************************************** */ #include "scop.h" +#define COLOR_RATIO_INC -0.02 + bool state_init(t_state *state, t_model_data *data) { if (SDL_Init(SDL_INIT_VIDEO) < 0) @@ -39,6 +41,8 @@ bool state_init(t_state *state, t_model_data *data) state->fov = M_PI_4; state->running = true; state->polygon_mode = GL_FILL; + state->transition = false; + state->is_texture = false; SDL_GL_GetDrawableSize(state->window, &state->width, &state->height); GL_CALL(glViewport(0, 0, state->width, state->height)); return (true); @@ -53,6 +57,16 @@ void state_run(t_state *state) rotation = M_PI_4; while (state->running) { + if (state->transition) + { + state->scene.color_ratio += (state->is_texture ? -1.0 : 1.0) * COLOR_RATIO_INC; + if ((!state->is_texture && state->scene.color_ratio <= 0.0) + || (state->is_texture && state->scene.color_ratio >= 1.0)) + { + state->transition = false; + state->is_texture = !state->is_texture; + } + } GL_CALL(glClearColor(0.1f, 0.1f, 0.1f, 1.0f)); GL_CALL(glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)); diff --git a/src/main.c b/src/main.c index b712a5a..3820cd5 100644 --- a/src/main.c +++ b/src/main.c @@ -6,7 +6,7 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/05/09 10:20:09 by charles #+# #+# */ -/* Updated: 2020/05/14 16:16:24 by charles ### ########.fr */ +/* Updated: 2020/05/14 16:48:35 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -82,10 +82,6 @@ int main(int argc, char **argv) printf("% f\n", data.vertices[i]); } - /* GL_CALL(glUniform1i(glGetUniformLocation(state.shader, "u_texture"), 0)); */ - /* GL_CALL(glActiveTexture(GL_TEXTURE0)); */ - /* GL_CALL(glBindTexture(GL_TEXTURE_2D, texture)); */ - if (!state_init(&state, &data)) return (1); state_run(&state); diff --git a/src/scene.c b/src/scene.c index 5c56bac..e8861e6 100644 --- a/src/scene.c +++ b/src/scene.c @@ -6,7 +6,7 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/05/14 12:28:57 by charles #+# #+# */ -/* Updated: 2020/05/14 16:35:25 by charles ### ########.fr */ +/* Updated: 2020/05/14 18:48:00 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -49,6 +49,7 @@ void scene_init(t_scene *scene, t_model_data *data) ftm_mat4init_eye(&scene->transform.view, 1.0); st_transform_center_init(&scene->transform.center, data->vertices, data->vertices_num); + scene->color_ratio = 1.0; } void scene_update_proj(t_scene *scene, float fov, int width, int height) diff --git a/src/shader.c b/src/shader.c index fb659ad..788cd47 100644 --- a/src/shader.c +++ b/src/shader.c @@ -6,7 +6,7 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/05/11 09:35:54 by charles #+# #+# */ -/* Updated: 2020/05/14 16:41:29 by charles ### ########.fr */ +/* Updated: 2020/05/14 18:48:17 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -70,7 +70,8 @@ bool shader_init(t_shader *shader) GL_CALL(glDeleteShader(shader_fragment)); if (!st_get_uniform_location(shader->id, &shader->location.model, "u_model") || !st_get_uniform_location(shader->id, &shader->location.view, "u_view") - || !st_get_uniform_location(shader->id, &shader->location.proj, "u_proj")) + || !st_get_uniform_location(shader->id, &shader->location.proj, "u_proj") + || !st_get_uniform_location(shader->id, &shader->location.color_ratio, "u_color_ratio")) /* || !st_get_uniform_location(shader->id, &shader->location.texture, "u_texture")) */ return (false); return (true); @@ -81,5 +82,6 @@ void shader_set_uniforms(t_shader *shader, t_scene *scene) GL_CALL(glUniformMatrix4fv(shader->location.model, 1, GL_TRUE, scene->transform.model.m)); GL_CALL(glUniformMatrix4fv(shader->location.view, 1, GL_TRUE, scene->transform.view.m)); GL_CALL(glUniformMatrix4fv(shader->location.proj, 1, GL_TRUE, scene->transform.proj.m)); + GL_CALL(glUniform1f(shader->location.color_ratio, scene->color_ratio)); /* GL_CALL(glUniformMatrix4fv(shader->location.texture, 1, GL_TRUE, scene->transform.proj.m)); */ } diff --git a/src/texture.c b/src/texture.c index a91b355..057db5f 100644 --- a/src/texture.c +++ b/src/texture.c @@ -6,7 +6,7 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/05/11 14:27:34 by charles #+# #+# */ -/* Updated: 2020/05/14 16:37:42 by charles ### ########.fr */ +/* Updated: 2020/05/14 18:41:44 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -75,28 +75,32 @@ unsigned int texture_create(char *filepath) GL_CALL(glBindTexture(GL_TEXTURE_2D, texture)); GL_CALL(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)); GL_CALL(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)); - GL_CALL(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE)); - GL_CALL(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE)); - GL_CALL(glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data)); + GL_CALL(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT)); + GL_CALL(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT)); + GL_CALL(glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_BGR, GL_UNSIGNED_BYTE, data)); + GL_CALL(glGenerateMipmap(GL_TEXTURE_2D)); free(data); return (texture); } +/* +** https://www.wikiwand.com/en/UV_mapping#/Finding_UV_on_a_sphere +*/ + void texture_coord_init(float *vertices, size_t vertices_num) { - t_ftmvec3 min; - t_ftmvec3 max; + t_ftmvec3 v; size_t i; - size_t index; - helper_find_boundary(vertices, vertices_num, &min, &max); i = 0; while (i < vertices_num) { - index = i + VERTEX_TEX_COORD_OFFSET; - vertices[index] = 0.0; //(vertices[index] - min.x) * (1.0 / (max.x - min.x)); - index++; - vertices[index] = 1.0; //(vertices[index] - min.y) * (1.0 / (max.y - min.y)); + v.x = vertices[i + 0]; + v.y = vertices[i + 1]; + v.z = vertices[i + 2]; + ftm_vec3normalize(&v); + vertices[i + VERTEX_TEX_COORD_OFFSET + 0] = 15.0 * (0.5 + atan2(v.z, v.x) / M_PI * 0.5); + vertices[i + VERTEX_TEX_COORD_OFFSET + 1] = 15.0 * (v.y / 10.0); i += VERTEX_COUNT; } } diff --git a/vendor/libftm/inc/libftm_mat4.h b/vendor/libftm/inc/libftm_mat4.h index 5fc183b..16e1c61 100644 --- a/vendor/libftm/inc/libftm_mat4.h +++ b/vendor/libftm/inc/libftm_mat4.h @@ -6,7 +6,7 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/05/12 11:04:59 by charles #+# #+# */ -/* Updated: 2020/05/12 17:21:34 by charles ### ########.fr */ +/* Updated: 2020/05/14 16:46:03 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -47,8 +47,14 @@ typedef struct t_ftmmat4 *ftm_mat4init_eye(t_ftmmat4 *mat4, float x); t_ftmmat4 *ftm_mat4init_fill(t_ftmmat4 *mat4, float x); t_ftmmat4 *ftm_mat4init_frustum(t_ftmmat4 *mat4, t_ftmfrustum *frustum); -t_ftmmat4 *ftm_mat4init_frustum_sym(t_ftmmat4 *mat4, t_ftmfrustum_sym *frustum); -t_ftmmat4 *ftm_mat4init_perspective(t_ftmmat4 *mat4, float fov, float aspect_ratio, float near, float far); + +t_ftmmat4 *ftm_mat4init_frustum_sym( + t_ftmmat4 *mat4, t_ftmfrustum_sym *frustum); + +t_ftmmat4 *ftm_mat4init_perspective( + t_ftmmat4 *mat4, float fov, float aspect_ratio, + float near, float far); + t_ftmmat4 *ftm_mat4translate(t_ftmmat4 *mat4, float x, float y, float z); t_ftmmat4 *ftm_mat4rotate(t_ftmmat4 *mat4, float radian, t_ftmvec3 *axis); t_ftmmat4 *ftm_mat4scale(t_ftmmat4 *mat4, float x, float y, float z); @@ -56,5 +62,4 @@ t_ftmmat4 *ftm_mat4mul(t_ftmmat4 *dst, t_ftmmat4 *other); t_ftmmat4 *ftm_mat4set(t_ftmmat4 *mat4, size_t y, size_t x, float value); float ftm_mat4get(t_ftmmat4 *mat4, size_t y, size_t x); - #endif diff --git a/vendor/libftm/inc/libftm_vec3.h b/vendor/libftm/inc/libftm_vec3.h index c40d6c8..62a6dac 100644 --- a/vendor/libftm/inc/libftm_vec3.h +++ b/vendor/libftm/inc/libftm_vec3.h @@ -6,27 +6,28 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/05/12 11:02:29 by charles #+# #+# */ -/* Updated: 2020/05/13 11:13:12 by charles ### ########.fr */ +/* Updated: 2020/05/14 18:06:33 by charles ### ########.fr */ /* */ /* ************************************************************************** */ #ifndef LIBFTM_VEC3_H # define LIBFTM_VEC3_H -typedef struct +# include + +typedef union { - union + float v[3]; + struct { - float v[3]; - struct - { - float x; - float y; - float z; - }; + float x; + float y; + float z; }; } t_ftmvec3; t_ftmvec3 *ftm_vec3init(t_ftmvec3 *vec3, float x, float y, float z); +float ftm_vec3norm(t_ftmvec3 *vec3); +t_ftmvec3 *ftm_vec3normalize(t_ftmvec3 *vec3); #endif diff --git a/vendor/libftm/src/vec3/ftm_vec3norm.c b/vendor/libftm/src/vec3/ftm_vec3norm.c new file mode 100644 index 0000000..d226e08 --- /dev/null +++ b/vendor/libftm/src/vec3/ftm_vec3norm.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ftm_vec3norm.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/05/14 18:04:25 by charles #+# #+# */ +/* Updated: 2020/05/14 18:05:14 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libftm_vec3.h" + +float ftm_vec3norm(t_ftmvec3 *vec3) +{ + return (sqrt(vec3->x * vec3->x + vec3->y * vec3->y + vec3->z * vec3->z)); +} diff --git a/vendor/libftm/src/vec3/ftm_vec3normalize.c b/vendor/libftm/src/vec3/ftm_vec3normalize.c new file mode 100644 index 0000000..dbf24d3 --- /dev/null +++ b/vendor/libftm/src/vec3/ftm_vec3normalize.c @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ftm_vec3normalize.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/05/14 18:05:22 by charles #+# #+# */ +/* Updated: 2020/05/14 18:06:04 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libftm_vec3.h" + +t_ftmvec3 *ftm_vec3normalize(t_ftmvec3 *vec3) +{ + float norm; + + norm = ftm_vec3norm(vec3); + vec3->x /= norm; + vec3->y /= norm; + vec3->z /= norm; + return (vec3); +} -- cgit