diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/center.c | 22 | ||||
| -rw-r--r-- | src/color.c | 20 | ||||
| -rw-r--r-- | src/gl.c | 14 | ||||
| -rw-r--r-- | src/glfw.c | 4 | ||||
| -rw-r--r-- | src/main.c | 92 | ||||
| -rw-r--r-- | src/texture.c | 54 |
6 files changed, 154 insertions, 52 deletions
diff --git a/src/center.c b/src/center.c index 7974896..a3a18b4 100644 --- a/src/center.c +++ b/src/center.c @@ -6,7 +6,7 @@ /* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/05/13 10:48:15 by charles #+# #+# */ -/* Updated: 2020/05/13 12:52:43 by charles ### ########.fr */ +/* Updated: 2020/05/13 16:43:38 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -50,3 +50,23 @@ void center_mat4_init_translate(t_ftmmat4 *dst, float *vertices, size_t vertices -(max.y + min.y) / 2.0f, -(max.z + min.z) / 2.0f); } + +float *texture_coord_create(float *vertices, size_t vertices_len) +{ + t_ftmvec3 min; + t_ftmvec3 max; + float *coords; + size_t i; + + if ((coords = malloc(sizeof(float) * (vertices_len * 2))) == NULL) + return (NULL); + st_find_boundary(vertices, vertices_len, &min, &max); + i = 0; + while (i < vertices_len) + { + coords[i * 2 + 0] = (vertices[i * 4 + 0] - min.x) * (1.0 / (max.x - min.x)); + coords[i * 2 + 1] = (vertices[i * 4 + 1] - min.y) * (1.0 / (max.y - min.y)); + i++; + } + return (coords); +} diff --git a/src/color.c b/src/color.c index 45a00fa..edbafc3 100644 --- a/src/color.c +++ b/src/color.c @@ -6,13 +6,12 @@ /* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/05/13 11:53:53 by charles #+# #+# */ -/* Updated: 2020/05/13 13:00:48 by charles ### ########.fr */ +/* Updated: 2020/05/13 16:43:59 by charles ### ########.fr */ /* */ /* ************************************************************************** */ #include "scop.h" -#include <string.h> float *color_new(size_t n) { size_t i; @@ -22,11 +21,11 @@ float *color_new(size_t n) if ((colors = malloc(sizeof(float) * (n * 4))) == NULL) return (NULL); - step = 1.0 / (float)n; + step = 0.8 / (float)n; i = 0; - c.r = 0.0; - c.g = 0.0; - c.b = 0.0; + c.r = 0.1; + c.g = 0.1; + c.b = 0.1; c.a = 1.0; while (i < n) { @@ -39,7 +38,7 @@ float *color_new(size_t n) return (colors); } -bool color_merge_vertices(t_object *object) +bool color_merge_vertices(t_object *object, float *coords) { size_t i; float *colors; @@ -47,7 +46,7 @@ bool color_merge_vertices(t_object *object) if ((colors = color_new(object->vertices_len)) == NULL) return (false); - if ((new_vertices = malloc(sizeof(float) * (object->vertices_len * 8))) == NULL) + if ((new_vertices = malloc(sizeof(float) * object->vertices_len * (4 + 4 + 2))) == NULL) { free(colors); return (false); @@ -55,8 +54,9 @@ bool color_merge_vertices(t_object *object) i = 0; while (i < object->vertices_len) { - ft_memcpy(&new_vertices[i * 8], &object->vertices[i * 4], 4 * sizeof(float)); - ft_memcpy(&new_vertices[i * 8 + 4], &colors[i * 4], 4 * sizeof(float)); + ft_memcpy(&new_vertices[i * (4 + 4 + 2)], &object->vertices[i * 4], 4 * sizeof(float)); + ft_memcpy(&new_vertices[i * (4 + 4 + 2) + 4], &colors[i * 4], 4 * sizeof(float)); + ft_memcpy(&new_vertices[i * (4 + 4 + 2) + 8], &coords[i * 2], 2 * sizeof(float)); i++; } free(object->vertices); @@ -6,7 +6,7 @@ /* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/05/11 01:31:10 by charles #+# #+# */ -/* Updated: 2020/05/13 12:48:04 by charles ### ########.fr */ +/* Updated: 2020/05/13 16:44:20 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -33,7 +33,7 @@ int gl_state_init(t_gl_state *state, t_object *object) GL_CALL(glBindVertexArray(state->vertex_array)); GL_CALL(glBindBuffer(GL_ARRAY_BUFFER, state->vertex_buf)); - GL_CALL(glBufferData(GL_ARRAY_BUFFER, sizeof(float) * 8 * object->vertices_len, + GL_CALL(glBufferData(GL_ARRAY_BUFFER, sizeof(float) * 10 * object->vertices_len, object->vertices, GL_STATIC_DRAW)); GL_CALL(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, state->index_buf)); @@ -41,9 +41,15 @@ int gl_state_init(t_gl_state *state, t_object *object) object->indices, GL_STATIC_DRAW)); GL_CALL(glEnableVertexAttribArray(0)); - GL_CALL(glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, sizeof(float) * 8, (void*)0)); + GL_CALL(glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, sizeof(float) * 10, (void*)0)); GL_CALL(glEnableVertexAttribArray(1)); - GL_CALL(glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, sizeof(float) * 8, (void*)(4 * sizeof(float)))); + GL_CALL(glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, sizeof(float) * 10, (void*)(4 * sizeof(float)))); + GL_CALL(glEnableVertexAttribArray(2)); + GL_CALL(glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 10, (void*)(8 * sizeof(float)))); + + state->polygon_mode = GL_FILL; + state->polygon_mode_last_time = glfwGetTime(); + state->fov = M_PI_4; return (0); } @@ -6,7 +6,7 @@ /* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/05/11 01:58:00 by charles #+# #+# */ -/* Updated: 2020/05/13 12:54:59 by charles ### ########.fr */ +/* Updated: 2020/05/13 13:16:32 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,7 +14,7 @@ bool g_window_resized = false; -void st_resize_callback(GLFWwindow *window, int width, int height) +static void st_resize_callback(GLFWwindow *window, int width, int height) { (void)window; g_window_resized = true; @@ -6,7 +6,7 @@ /* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/05/09 10:20:09 by charles #+# #+# */ -/* Updated: 2020/05/13 13:04:45 by charles ### ########.fr */ +/* Updated: 2020/05/13 17:11:37 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,11 +17,40 @@ ** - texture ** - parse vt ** - parse coord index of f -** - parse mtl file -** - color -** - transition with texture +** - transition color/texture */ +void handle_event(GLFWwindow *window, t_gl_state *state) +{ + if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) + glfwSetWindowShouldClose(window, true); + else if (glfwGetKey(window, GLFW_KEY_P) == GLFW_PRESS) + { + if (state->polygon_mode_last_time + 0.1 > glfwGetTime()) + return ; + if (state->polygon_mode == GL_FILL) + state->polygon_mode = GL_LINE; + else if (state->polygon_mode == GL_LINE) + state->polygon_mode = GL_POINT; + else if (state->polygon_mode == GL_POINT) + state->polygon_mode = GL_FILL; + GL_CALL(glPolygonMode(GL_FRONT_AND_BACK, state->polygon_mode)); + state->polygon_mode_last_time = glfwGetTime(); + } + else if (glfwGetKey(window, GLFW_KEY_EQUAL) == GLFW_PRESS) + { + state->fov -= M_PI / 100.0f; + if (state->fov <= M_PI_4 / 2) + state->fov = M_PI_4 / 2; + } + else if (glfwGetKey(window, GLFW_KEY_MINUS) == GLFW_PRESS) + { + state->fov += M_PI / 100.0f; + if (state->fov >= M_PI - M_PI_4 / 2) + state->fov = M_PI - M_PI_4 / 2; + } +} + void debugmat(t_ftmmat4 *mat) { for (int i = 0; i < 4; i++) @@ -57,11 +86,14 @@ int main(int argc, char **argv) return (1); } + unsigned int texture = texture_new("res/brick.bmp"); + float *coords = texture_coord_create(object.vertices, object.vertices_len); + t_ftmmat4 center_trans; center_mat4_init_translate(¢er_trans, object.vertices, object.vertices_len); /* debugmat(¢er_trans); */ - if (!color_merge_vertices(&object)) + if (!color_merge_vertices(&object, coords)) { ft_putstr("Error: couldn't create colors"); return (1); @@ -76,18 +108,20 @@ int main(int argc, char **argv) /* } */ /* printf("yo %lu\n", object.indices_len); */ - /* for (size_t i = 0; i < object.vertices_len * 8; i++) */ - /* { */ - /* printf("% f, ", object.vertices[i++]); */ - /* printf("% f, ", object.vertices[i++]); */ - /* printf("% f, ", object.vertices[i++]); */ - /* printf("% f\tcolor: ", object.vertices[i++]); */ - /* printf("% f, ", object.vertices[i++]); */ - /* printf("% f, ", object.vertices[i++]); */ - /* printf("% f, ", object.vertices[i++]); */ - /* printf("% f\n", object.vertices[i]); */ - /* } */ - /* printf("yo %lu\n", object.vertices_len); */ + for (size_t i = 0; i < object.vertices_len * 10; i++) + { + printf("% f, ", object.vertices[i++]); + printf("% f, ", object.vertices[i++]); + printf("% f, ", object.vertices[i++]); + printf("% f\tcolor: ", object.vertices[i++]); + printf("% f, ", object.vertices[i++]); + printf("% f, ", object.vertices[i++]); + printf("% f, ", object.vertices[i++]); + printf("% f\ttex: ", object.vertices[i]); + printf("% f", object.vertices[i++]); + printf("% f\n", object.vertices[i]); + } + printf("yo %lu\n", object.vertices_len); t_ftmmat4 model; t_ftmmat4 view; @@ -116,17 +150,22 @@ int main(int argc, char **argv) GL_CALL(glBindBuffer(GL_ARRAY_BUFFER, 0)); GL_CALL(glBindVertexArray(0)); - GL_CALL(glPolygonMode(GL_FRONT_AND_BACK, GL_LINE)); - GL_CALL(glUseProgram(state.shader)); gl_state_set_mvp(&state, &model, &view, &proj); + GL_CALL(glUniform1i(glGetUniformLocation(state.shader, "u_texture"), 0)); + GL_CALL(glActiveTexture(GL_TEXTURE0)); + /* printf("%u\n", texture); */ + /* GL_CALL(glBindTexture(GL_TEXTURE_2D, texture)); */ + float deg = 0.0; while (!glfwWindowShouldClose(window)) { + handle_event(window, &state); GL_CALL(glClearColor(0.2f, 0.3f, 0.3f, 1.0f)); GL_CALL(glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)); + GL_CALL(glUseProgram(state.shader)); ftm_mat4init_eye(&model, 1.0); @@ -135,19 +174,16 @@ int main(int argc, char **argv) ftm_mat4mul(&model, ¢er_trans); deg += 0.4; - if (g_window_resized) - { - int w, h; - glfwGetFramebufferSize(window, &w, &h); - /* printf("%d %d\n", w, h); */ - ftm_mat4init_perspective(&proj, M_PI_4, (float)w / (float)h, 0.1, 100.0); - g_window_resized = false; - } + int w, h; + glfwGetFramebufferSize(window, &w, &h); + ftm_mat4init_perspective(&proj, state.fov, (float)w / (float)h, 0.1, 100.0); + g_window_resized = false; gl_state_set_mvp(&state, &model, &view, &proj); + GL_CALL(glBindVertexArray(state.vertex_array)); - GL_CALL(glDrawElements(GL_TRIANGLES, object.indices_len * sizeof(unsigned int), GL_UNSIGNED_INT, (void*)0)); + GL_CALL(glDrawElements(GL_TRIANGLES, object.indices_len * sizeof(unsigned int) * 3, GL_UNSIGNED_INT, (void*)0)); glfwSwapBuffers(window); glfwPollEvents(); diff --git a/src/texture.c b/src/texture.c index e499af6..75f4797 100644 --- a/src/texture.c +++ b/src/texture.c @@ -6,21 +6,60 @@ /* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/05/11 14:27:34 by charles #+# #+# */ -/* Updated: 2020/05/11 16:19:56 by charles ### ########.fr */ +/* Updated: 2020/05/13 17:11:06 by charles ### ########.fr */ /* */ /* ************************************************************************** */ #include "scop.h" +typedef struct __attribute__((__packed__)) +{ + uint8_t signature[2]; + uint32_t file_size; + uint32_t reserved; + uint32_t img_data_offset; +} t_bmp_file_header; + +typedef struct __attribute__((__packed__)) +{ + uint32_t header_size; + uint32_t width; + uint32_t height; + uint16_t color_planes; + uint16_t depth; + uint32_t compression; + uint32_t image_size; + uint32_t horizontal_res; + uint32_t vertical_res; + uint32_t color_table_len; + uint32_t color_count; +} t_bmp_info_header; + uint8_t *st_read_bmp(char *filepath, size_t *width, size_t *height) { - t_ftmem file; + uint8_t *data; + t_bmp_file_header file_header; + t_bmp_info_header info_header; + int fd; - if (ft_getfile(open(filepath, O_RDONLY), &file) == -1) + fd = open(filepath, O_RDONLY); + read(fd, &file_header, sizeof(t_bmp_file_header)); + if (file_header.signature[0] != 'B' || file_header.signature[1] != 'M') + { + close(fd); + return (NULL); + } + read(fd, &info_header, sizeof(t_bmp_info_header)); + if ((data = malloc(info_header.image_size)) == NULL) + { + close(fd); return (NULL); - *width = 0; - *height = 0; - return (file.data); + } + *width = info_header.width; + *height = info_header.height; + read(fd, data, info_header.image_size); // padding + close(fd); + return (data); } unsigned int texture_new(char *filepath) @@ -38,7 +77,8 @@ unsigned int texture_new(char *filepath) 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_RGBA8, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data)); + GL_CALL(glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data)); + /* glBindTexture(GL_TEXTURE_2D, 0); */ free(data); return (texture); } |
