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 --- 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 ++++++++++++++++------------ 6 files changed, 43 insertions(+), 22 deletions(-) (limited to 'src') 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; } } -- cgit