aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/center.c72
-rw-r--r--src/color.c104
-rw-r--r--src/gl.c80
-rw-r--r--src/glfw.c51
-rw-r--r--src/main.c201
-rw-r--r--src/parse.c68
-rw-r--r--src/scene.c97
-rw-r--r--src/shader.c44
-rw-r--r--src/state.c126
-rw-r--r--src/texture.c161
10 files changed, 492 insertions, 512 deletions
diff --git a/src/center.c b/src/center.c
deleted file mode 100644
index a3a18b4..0000000
--- a/src/center.c
+++ /dev/null
@@ -1,72 +0,0 @@
-/* ************************************************************************** */
-/* */
-/* ::: :::::::: */
-/* center.c :+: :+: :+: */
-/* +:+ +:+ +:+ */
-/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */
-/* +#+#+#+#+#+ +#+ */
-/* Created: 2020/05/13 10:48:15 by charles #+# #+# */
-/* Updated: 2020/05/13 16:43:38 by charles ### ########.fr */
-/* */
-/* ************************************************************************** */
-
-#include "scop.h"
-
-void st_find_boundary(float *vertices, size_t vertices_len, t_ftmvec3 *min, t_ftmvec3 *max)
-{
- size_t i;
-
- ftm_vec3init(min, INFINITY, INFINITY, INFINITY);
- ftm_vec3init(max, -INFINITY, -INFINITY, -INFINITY);
- i = 0;
- while (i < vertices_len * 4)
- {
- if (vertices[i] > max->x)
- max->x = vertices[i];
- if (vertices[i] < min->x)
- min->x = vertices[i];
- i++;
- if (vertices[i] > max->y)
- max->y = vertices[i];
- if (vertices[i] < min->y)
- min->y = vertices[i];
- i++;
- if (vertices[i] > max->z)
- max->z = vertices[i];
- if (vertices[i] < min->z)
- min->z = vertices[i];
- i += 2;
- }
-}
-
-void center_mat4_init_translate(t_ftmmat4 *dst, float *vertices, size_t vertices_len)
-{
- t_ftmvec3 min;
- t_ftmvec3 max;
-
- st_find_boundary(vertices, vertices_len, &min, &max);
- ftm_mat4init_eye(dst, 1.0);
- ftm_mat4translate(dst, -(max.x + min.x) / 2.0f,
- -(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 edbafc3..166eaaf 100644
--- a/src/color.c
+++ b/src/color.c
@@ -6,60 +6,60 @@
/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/05/13 11:53:53 by charles #+# #+# */
-/* Updated: 2020/05/13 16:43:59 by charles ### ########.fr */
+/* Updated: 2020/05/14 13:58:15 by charles ### ########.fr */
/* */
/* ************************************************************************** */
#include "scop.h"
-float *color_new(size_t n)
-{
- size_t i;
- float step;
- float *colors;
- union u_color c;
-
- if ((colors = malloc(sizeof(float) * (n * 4))) == NULL)
- return (NULL);
- step = 0.8 / (float)n;
- i = 0;
- c.r = 0.1;
- c.g = 0.1;
- c.b = 0.1;
- c.a = 1.0;
- while (i < n)
- {
- c.r += step;
- c.g += step;
- c.b += step;
- ft_memcpy(&colors[i * 4], c.data, 4 * sizeof(float));
- i++;
- }
- return (colors);
-}
-
-bool color_merge_vertices(t_object *object, float *coords)
-{
- size_t i;
- float *colors;
- float *new_vertices;
-
- if ((colors = color_new(object->vertices_len)) == NULL)
- return (false);
- if ((new_vertices = malloc(sizeof(float) * object->vertices_len * (4 + 4 + 2))) == NULL)
- {
- free(colors);
- return (false);
- }
- i = 0;
- while (i < object->vertices_len)
- {
- 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);
- object->vertices = new_vertices;
- return (true);
-}
+/* float *color_new(size_t n) */
+/* { */
+/* size_t i; */
+/* float step; */
+/* float *colors; */
+/* union u_color c; */
+/* */
+/* if ((colors = malloc(sizeof(float) * (n * 4))) == NULL) */
+/* return (NULL); */
+/* step = 0.8 / (float)n; */
+/* i = 0; */
+/* c.r = 0.1; */
+/* c.g = 0.1; */
+/* c.b = 0.1; */
+/* c.a = 1.0; */
+/* while (i < n) */
+/* { */
+/* c.r += step; */
+/* c.g += step; */
+/* c.b += step; */
+/* ft_memcpy(&colors[i * 4], c.data, 4 * sizeof(float)); */
+/* i++; */
+/* } */
+/* return (colors); */
+/* } */
+/* */
+/* bool color_merge_vertices(t_model_data *data, float *coords) */
+/* { */
+/* size_t i; */
+/* float *colors; */
+/* float *new_vertices; */
+/* */
+/* if ((colors = color_new(data->vertices_len)) == NULL) */
+/* return (false); */
+/* if ((new_vertices = malloc(sizeof(float) * data->vertices_len * (4 + 4 + 2))) == NULL) */
+/* { */
+/* free(colors); */
+/* return (false); */
+/* } */
+/* i = 0; */
+/* while (i < data->vertices_size) */
+/* { */
+/* ft_memcpy(&new_vertices[i * (4 + 4 + 2)], &data->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(data->vertices); */
+/* data->vertices = new_vertices; */
+/* return (true); */
+/* } */
diff --git a/src/gl.c b/src/gl.c
deleted file mode 100644
index 4da958d..0000000
--- a/src/gl.c
+++ /dev/null
@@ -1,80 +0,0 @@
-/* ************************************************************************** */
-/* */
-/* ::: :::::::: */
-/* gl.c :+: :+: :+: */
-/* +:+ +:+ +:+ */
-/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */
-/* +#+#+#+#+#+ +#+ */
-/* Created: 2020/05/11 01:31:10 by charles #+# #+# */
-/* Updated: 2020/05/13 16:44:20 by charles ### ########.fr */
-/* */
-/* ************************************************************************** */
-
-#include "scop.h"
-
-int gl_state_init(t_gl_state *state, t_object *object)
-{
- if ((state->shader = shader_new()) == 0)
- return (-1);
- GL_CALL(state->view_location = glGetUniformLocation(state->shader, "u_view"));
- if (state->view_location == -1)
- return (-1);
- GL_CALL(state->model_location = glGetUniformLocation(state->shader, "u_model"));
- if (state->model_location == -1)
- return (-1);
- GL_CALL(state->proj_location = glGetUniformLocation(state->shader, "u_proj"));
- if (state->proj_location == -1)
- return (-1);
-
- GL_CALL(glGenVertexArrays(1, &state->vertex_array));
- GL_CALL(glGenBuffers(1, &state->vertex_buf));
- GL_CALL(glGenBuffers(1, &state->index_buf));
-
- GL_CALL(glBindVertexArray(state->vertex_array));
-
- GL_CALL(glBindBuffer(GL_ARRAY_BUFFER, state->vertex_buf));
- 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));
- GL_CALL(glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(unsigned int) * 3 * object->indices_len,
- object->indices, GL_STATIC_DRAW));
-
- GL_CALL(glEnableVertexAttribArray(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) * 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);
-}
-
-void gl_state_quit(t_gl_state *state, t_object *object)
-{
- GL_CALL(glDeleteVertexArrays(1, &state->vertex_array));
- GL_CALL(glDeleteBuffers(1, &state->vertex_buf));
- GL_CALL(glDeleteBuffers(1, &state->index_buf));
- GL_CALL(glDeleteProgram(state->shader));
- free(object->vertices);
- free(object->indices);
-}
-
-void gl_state_set_mvp(t_gl_state *state, t_ftmmat4 *model, t_ftmmat4 *view, t_ftmmat4 *proj)
-{
- if (model != NULL)
- {
- GL_CALL(glUniformMatrix4fv(state->model_location, 1, GL_TRUE, model->m));
- }
- if (view != NULL)
- {
- GL_CALL(glUniformMatrix4fv(state->view_location, 1, GL_TRUE, view->m));
- }
- if (proj != NULL)
- {
- GL_CALL(glUniformMatrix4fv(state->proj_location, 1, GL_TRUE, proj->m));
- }
-}
diff --git a/src/glfw.c b/src/glfw.c
deleted file mode 100644
index 17b1de3..0000000
--- a/src/glfw.c
+++ /dev/null
@@ -1,51 +0,0 @@
-/* ************************************************************************** */
-/* */
-/* ::: :::::::: */
-/* glfw.c :+: :+: :+: */
-/* +:+ +:+ +:+ */
-/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */
-/* +#+#+#+#+#+ +#+ */
-/* Created: 2020/05/11 01:58:00 by charles #+# #+# */
-/* Updated: 2020/05/13 13:16:32 by charles ### ########.fr */
-/* */
-/* ************************************************************************** */
-
-#include "scop.h"
-
-bool g_window_resized = false;
-
-static void st_resize_callback(GLFWwindow *window, int width, int height)
-{
- (void)window;
- g_window_resized = true;
- glViewport(0, 0, width, height);
-}
-
-GLFWwindow *glfw_init(int width, int height)
-{
- GLFWwindow *window;
-
- if (!glfwInit())
- return (NULL);
- glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
- glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
- glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
- window = glfwCreateWindow(width, height, "scop", NULL, NULL);
- if (window == NULL)
- {
- glfwTerminate();
- return (NULL);
- }
- glfwMakeContextCurrent(window);
- glfwSetFramebufferSizeCallback(window, st_resize_callback);
- if (glewInit() != GLEW_OK)
- {
- glfwDestroyWindow(window);
- glfwTerminate();
- return (NULL);
- }
- glfwSwapInterval(1);
- GL_CALL(glViewport(0, 0, width, height));
- GL_CALL(glEnable(GL_DEPTH_TEST));
- return (window);
-}
diff --git a/src/main.c b/src/main.c
index cc83f4e..c02bc2f 100644
--- a/src/main.c
+++ b/src/main.c
@@ -6,7 +6,7 @@
/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/05/09 10:20:09 by charles #+# #+# */
-/* Updated: 2020/05/13 17:11:37 by charles ### ########.fr */
+/* Updated: 2020/05/14 14:49:02 by charles ### ########.fr */
/* */
/* ************************************************************************** */
@@ -18,38 +18,39 @@
** - parse vt
** - parse coord index of f
** - transition color/texture
+** - cleaner code
*/
-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 handle_event(GLFWwindow *window) */
+/* { */
+/* 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)
{
@@ -64,13 +65,10 @@ void debugmat(t_ftmmat4 *mat)
printf("\n");
}
-extern bool g_window_resized;
-
int main(int argc, char **argv)
{
- GLFWwindow *window;
- t_object object;
- t_gl_state state;
+ t_state state;
+ t_model_data data;
if (argc != 2)
{
@@ -79,117 +77,54 @@ int main(int argc, char **argv)
ft_putendl(" [obj file]");
return (1);
}
- if (parse(argv[1], &object) == -1)
+ if (parse(argv[1], &data) == -1)
{
ft_putstr("Error: couldn't parse ");
ft_putendl(argv[1]);
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(&center_trans, object.vertices, object.vertices_len);
- /* debugmat(&center_trans); */
-
- if (!color_merge_vertices(&object, coords))
- {
- ft_putstr("Error: couldn't create colors");
- return (1);
- }
-
-
- /* for (size_t i = 0; i < object.indices_len * 3; i++) */
+ /* unsigned int texture = texture_new("res/brick.bmp"); */
+ /* float *coords = texture_coord_create(object.vertices, object.vertices_len); */
+ /* */
+ /* if (!color_merge_vertices(&object, coords)) */
/* { */
- /* printf("%u, ", object.indices[i++]); */
- /* printf("%u, ", object.indices[i++]); */
- /* printf("%u\n", object.indices[i]); */
+ /* ft_putstr("Error: couldn't create colors"); */
+ /* return (1); */
/* } */
- /* printf("yo %lu\n", object.indices_len); */
- for (size_t i = 0; i < object.vertices_len * 10; i++)
+ printf("size %lu\n", data.indices_size);
+ for (size_t i = 0; i < data.indices_size; 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("%u, ", data.indices[i++]);
+ printf("%u, ", data.indices[i++]);
+ printf("%u\n", data.indices[i]);
}
- printf("yo %lu\n", object.vertices_len);
-
- t_ftmmat4 model;
- t_ftmmat4 view;
- t_ftmmat4 proj;
- t_ftmvec3 vec;
-
- ftm_mat4init_eye(&model, 1.0);
- /* ftm_mat4translate(&model, 0.0, 0.0, -10.0); */
- /* ftm_mat4rotate(&model, ftm_radian(45.0f), ftm_vec3init(&vec, 0.0, 1.0, 0.0)); */
- /* ftm_mat4mul(&model, &center_trans); */
- /* ftm_mat4rotate(&model, M_PI_4, ftm_vec3init(&vec, 0.0, 1.0, 0.0)); */
- /* ftm_mat4scale(&model, 1.1, 1.1, 1.1); */
-
- ftm_mat4init_eye(&view, 1.0);
- ftm_mat4init_perspective(&proj, M_PI_2 / 2.0, 1.0, 0.1, 100.0);
-
- /* debugmat(&model); */
- /* debugmat(&view); */
- /* debugmat(&proj); */
-
- if ((window = glfw_init(400, 400)) == NULL
- || gl_state_init(&state, &object) == -1)
- return (1);
-
- GL_CALL(glBindBuffer(GL_ARRAY_BUFFER, 0));
- GL_CALL(glBindVertexArray(0));
-
- 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))
+ printf("size %lu\n", data.vertices_size);
+ for (size_t i = 0; i < data.vertices_size; i++)
{
- 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);
- ftm_mat4translate(&model, 0.0, 0.0, -10.0);
- ftm_mat4rotate(&model, ftm_radian(deg), ftm_vec3init(&vec, 0.0, 1.0, 0.0));
- ftm_mat4mul(&model, &center_trans);
- deg += 0.4;
-
- 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);
-
+ printf("% f, ", data.vertices[i++]);
+ printf("% f, ", data.vertices[i++]);
+ printf("% f, ", data.vertices[i++]);
+ printf("% f\n", data.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]); */
+ }
- GL_CALL(glBindVertexArray(state.vertex_array));
- GL_CALL(glDrawElements(GL_TRIANGLES, object.indices_len * sizeof(unsigned int) * 3, GL_UNSIGNED_INT, (void*)0));
+ /* GL_CALL(glUniform1i(glGetUniformLocation(state.shader, "u_texture"), 0)); */
+ /* GL_CALL(glActiveTexture(GL_TEXTURE0)); */
+ /* GL_CALL(glBindTexture(GL_TEXTURE_2D, texture)); */
- glfwSwapBuffers(window);
- glfwPollEvents();
- }
- gl_state_quit(&state, &object);
- glfwDestroyWindow(window);
- glfwTerminate();
+ if (!state_init(&state, &data))
+ return (1);
+ state_run(&state);
+ state_quit(&state);
+ free(data.vertices);
+ free(data.indices);
return 0;
}
diff --git a/src/parse.c b/src/parse.c
index c0952c0..eb127e5 100644
--- a/src/parse.c
+++ b/src/parse.c
@@ -6,7 +6,7 @@
/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/05/09 11:02:00 by charles #+# #+# */
-/* Updated: 2020/05/13 12:25:46 by charles ### ########.fr */
+/* Updated: 2020/05/14 14:01:26 by charles ### ########.fr */
/* */
/* ************************************************************************** */
@@ -71,34 +71,34 @@ static int st_parse_vertex(char **positions_strs, t_ftvec *vertices)
return (0);
}
-static int st_parse_texture_coord(char **coord_strs, t_ftvec *coords)
-{
- float tmp;
-
- tmp = ft_atof(coord_strs[0]);
- ft_vecpush(coords, *((void**)&tmp));
- if (coord_strs[1] != NULL)
- {
- tmp = ft_atof(coord_strs[1]);
- ft_vecpush(coords, *((void**)&tmp));
- }
- else
- {
- tmp = 0.0f;
- ft_vecpush(coords, *((void**)&tmp));
- }
- if (ft_strslen(coord_strs) == 3)
- {
- tmp = ft_atof(coord_strs[2]);
- ft_vecpush(coords, *((void**)&tmp));
- }
- else
- {
- tmp = 0.0f;
- ft_vecpush(coords, *((void**)&tmp));
- }
- return (0);
-}
+/* static int st_parse_texture_coord(char **coord_strs, t_ftvec *coords) */
+/* { */
+/* float tmp; */
+/* */
+/* tmp = ft_atof(coord_strs[0]); */
+/* ft_vecpush(coords, *((void**)&tmp)); */
+/* if (coord_strs[1] != NULL) */
+/* { */
+/* tmp = ft_atof(coord_strs[1]); */
+/* ft_vecpush(coords, *((void**)&tmp)); */
+/* } */
+/* else */
+/* { */
+/* tmp = 0.0f; */
+/* ft_vecpush(coords, *((void**)&tmp)); */
+/* } */
+/* if (ft_strslen(coord_strs) == 3) */
+/* { */
+/* tmp = ft_atof(coord_strs[2]); */
+/* ft_vecpush(coords, *((void**)&tmp)); */
+/* } */
+/* else */
+/* { */
+/* tmp = 0.0f; */
+/* ft_vecpush(coords, *((void**)&tmp)); */
+/* } */
+/* return (0); */
+/* } */
static int st_parse_line(char *line, t_ftvec *vertices, t_ftvec *indices)
{
@@ -138,7 +138,7 @@ static int st_parse_file(int fd, t_ftvec *vertices, t_ftvec *indices)
return (0);
}
-int parse(char *filepath, t_object *object)
+int parse(char *filepath, t_model_data *data)
{
int fd;
t_ftvec *vertices;
@@ -153,10 +153,10 @@ int parse(char *filepath, t_object *object)
return (-1);
st_parse_file(fd, vertices, indices);
ft_veciter_ret(indices, st_iter_func_decrement_uint);
- object->vertices = (float*)ft_vectobuf32(vertices);
- object->indices = (unsigned int*)ft_vectobuf32(indices);
- object->vertices_len = vertices->size / 4;
- object->indices_len = indices->size / 3;
+ data->vertices = (float*)ft_vectobuf32(vertices);
+ data->indices = (unsigned int*)ft_vectobuf32(indices);
+ data->vertices_size = vertices->size;
+ data->indices_size = indices->size;
ft_vecdestroy(vertices, NULL);
ft_vecdestroy(indices, NULL);
return (0);
diff --git a/src/scene.c b/src/scene.c
new file mode 100644
index 0000000..bfacb41
--- /dev/null
+++ b/src/scene.c
@@ -0,0 +1,97 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* scene.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2020/05/14 12:28:57 by charles #+# #+# */
+/* Updated: 2020/05/14 14:52:00 by charles ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "scop.h"
+
+#define VERTEX_STRIDE sizeof(float) * 4
+
+void st_transform_center_init(t_ftmmat4 *dst, float *vertices, size_t vertices_size)
+{
+ size_t i;
+ t_ftmvec3 min;
+ t_ftmvec3 max;
+
+ ftm_vec3init(&min, INFINITY, INFINITY, INFINITY);
+ ftm_vec3init(&max, -INFINITY, -INFINITY, -INFINITY);
+ i = 0;
+ while (i < vertices_size)
+ {
+ if (vertices[i + 0] > max.x)
+ max.x = vertices[i + 0];
+ if (vertices[i + 0] < min.x)
+ min.x = vertices[i + 0];
+ if (vertices[i + 1] > max.y)
+ max.y = vertices[i + 1];
+ if (vertices[i + 1] < min.y)
+ min.y = vertices[i + 1];
+ if (vertices[i + 2] > max.z)
+ max.z = vertices[i + 2];
+ if (vertices[i + 2] < min.z)
+ min.z = vertices[i + 2];
+ i += VERTEX_STRIDE / sizeof(float);
+ }
+ ftm_mat4init_eye(dst, 1.0);
+ ftm_mat4translate(dst, -(max.x + min.x) / 2.0f,
+ -(max.y + min.y) / 2.0f, -(max.z + min.z) / 2.0f);
+}
+
+void scene_init(t_scene *scene, t_model_data *data)
+{
+ GL_CALL(glGenVertexArrays(1, &scene->vertex_array));
+ GL_CALL(glGenBuffers(1, &scene->vertex_buf));
+ GL_CALL(glGenBuffers(1, &scene->index_buf));
+
+ GL_CALL(glBindVertexArray(scene->vertex_array));
+
+ GL_CALL(glBindBuffer(GL_ARRAY_BUFFER, scene->vertex_buf));
+ GL_CALL(glBufferData(GL_ARRAY_BUFFER, data->vertices_size * sizeof(float), data->vertices, GL_STATIC_DRAW));
+
+ GL_CALL(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, scene->index_buf));
+ GL_CALL(glBufferData(GL_ELEMENT_ARRAY_BUFFER, data->indices_size * sizeof(unsigned int), data->indices, GL_STATIC_DRAW));
+ scene->index_buf_size = data->indices_size;
+
+ GL_CALL(glEnableVertexAttribArray(0));
+ GL_CALL(glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, VERTEX_STRIDE, (void*)0));
+ /* GL_CALL(glEnableVertexAttribArray(1)); */
+ /* GL_CALL(glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, VERTEX_STRIDE, (void*)(4 * sizeof(float)))); */
+ /* GL_CALL(glEnableVertexAttribArray(2)); */
+ /* GL_CALL(glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, VERTEX_STRIDE, (void*)(8 * sizeof(float)))); */
+
+ ftm_mat4init_eye(&scene->transform.view, 1.0);
+ st_transform_center_init(&scene->transform.center, data->vertices, data->vertices_size);
+ debugmat(&scene->transform.center);
+ scene_update_model(scene, M_PI_4);
+ scene_update_proj(scene, M_PI_4, WINDOW_WIDTH, WINDOW_HEIGHT);
+}
+
+void scene_update_proj(t_scene *scene, float fov, int width, int height)
+{
+ ftm_mat4init_eye(&scene->transform.proj, 1.0);
+ ftm_mat4init_perspective(&scene->transform.proj, fov, (float)width / (float)height, 0.1, 100.0);
+}
+
+void scene_update_model(t_scene *scene, float rotation_radian)
+{
+ t_ftmvec3 axis;
+
+ ftm_mat4init_eye(&scene->transform.model, 1.0);
+ ftm_mat4translate(&scene->transform.model, 0.0, 0.0, -10.0);
+ ftm_mat4rotate(&scene->transform.model, rotation_radian, ftm_vec3init(&axis, 0.0, 1.0, 0.0));
+ ftm_mat4mul(&scene->transform.model, &scene->transform.center);
+}
+
+void scene_quit(t_scene *scene)
+{
+ GL_CALL(glDeleteVertexArrays(1, &scene->vertex_array));
+ GL_CALL(glDeleteBuffers(1, &scene->vertex_buf));
+ GL_CALL(glDeleteBuffers(1, &scene->index_buf));
+}
diff --git a/src/shader.c b/src/shader.c
index 3a31d4e..a325f16 100644
--- a/src/shader.c
+++ b/src/shader.c
@@ -6,7 +6,7 @@
/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/05/11 09:35:54 by charles #+# #+# */
-/* Updated: 2020/05/12 21:23:06 by charles ### ########.fr */
+/* Updated: 2020/05/14 14:02:56 by charles ### ########.fr */
/* */
/* ************************************************************************** */
@@ -44,31 +44,37 @@ static unsigned int st_compile(char *filepath, unsigned int type)
return (id);
}
-unsigned int shader_new(void)
+static bool st_get_uniform_location(unsigned int shader_id, int *location, char *name)
+{
+ GL_CALL(*location = glGetUniformLocation(shader_id, name));
+ return (*location != -1);
+}
+
+bool shader_init(t_shader *shader)
{
unsigned int shader_vertex;
unsigned int shader_fragment;
- unsigned int program;
if ((shader_vertex = st_compile(SHADER_VERTEX_FILEPATH, GL_VERTEX_SHADER)) == 0
|| (shader_fragment = st_compile(SHADER_FRAGMENT_FILEPATH, GL_FRAGMENT_SHADER)) == 0)
- return (0);
- GL_CALL(program = glCreateProgram());
- GL_CALL(glAttachShader(program, shader_vertex));
- GL_CALL(glAttachShader(program, shader_fragment));
- GL_CALL(glLinkProgram(program));
- GL_CALL(glValidateProgram(program));
+ return (false);
+ GL_CALL(shader->id = glCreateProgram());
+ GL_CALL(glAttachShader(shader->id, shader_vertex));
+ GL_CALL(glAttachShader(shader->id, shader_fragment));
+ GL_CALL(glLinkProgram(shader->id));
+ GL_CALL(glValidateProgram(shader->id));
GL_CALL(glDeleteShader(shader_vertex));
GL_CALL(glDeleteShader(shader_fragment));
- return (program);
+ 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"))
+ return (false);
+ return (true);
}
-/* static int shader_uniform_location(unsigned int shader, const char *name) */
-/* { */
-/* int location; */
-/* */
-/* GL_CALL(location = glGetUniformLocation(shader, name)); */
-/* return (location); */
-/* } */
-
-/* void shader_uniform_matrix4( */
+void shader_update_mvp(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));
+}
diff --git a/src/state.c b/src/state.c
new file mode 100644
index 0000000..8cd3cca
--- /dev/null
+++ b/src/state.c
@@ -0,0 +1,126 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* state.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2020/05/11 01:31:10 by charles #+# #+# */
+/* Updated: 2020/05/14 14:59:48 by charles ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "scop.h"
+
+/* static void st_resize_callback(GLFWwindow *window, int width, int height) */
+/* { */
+/* (void)window; */
+/* glViewport(0, 0, width, height); */
+/* ftm_mat4init_perspective(&g_state->proj, state.fov, (float)width / (float)height, 0.1, 100.0); */
+/* } */
+
+static void st_handle_event(t_state *state)
+{
+ SDL_Event e;
+
+ while (SDL_PollEvent(&e))
+ {
+ if (e.type == SDL_QUIT)
+ state->running = false;
+ else if (e.type == SDL_KEYDOWN)
+ {
+ if (e.key.keysym.sym == SDLK_ESCAPE)
+ state->running = false;
+ else if (e.key.keysym.sym == SDLK_p)
+ {
+ 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));
+ }
+ }
+ else if (e.type == SDL_MOUSEWHEEL)
+ {
+ if (e.wheel.y > 0)
+ {
+ state->fov -= M_PI / 70.0f;
+ if (state->fov <= M_PI_4 / 3)
+ state->fov = M_PI_4 / 3;
+ }
+ else if (e.wheel.y < 0)
+ {
+ state->fov += M_PI / 70.0f;
+ if (state->fov >= M_PI - M_PI_4 / 3)
+ state->fov = M_PI - M_PI_4 / 3;
+ }
+ scene_update_proj(&state->scene, state->fov, state->width, state->height);
+ }
+ }
+}
+
+bool state_init(t_state *state, t_model_data *data)
+{
+ if (SDL_Init(SDL_INIT_VIDEO) < 0)
+ return (false);
+ SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 4);
+ SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);
+ SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
+ SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
+ state->window = SDL_CreateWindow(
+ "scop", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
+ WINDOW_WIDTH, WINDOW_HEIGHT, SDL_WINDOW_OPENGL);
+ if (state->window == NULL)
+ return (false);
+ state->context = SDL_GL_CreateContext(state->window);
+ if (glewInit() != GLEW_OK)
+ return (false);
+ SDL_GL_SetSwapInterval(1);
+ GL_CALL(glViewport(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT));
+ GL_CALL(glEnable(GL_DEPTH_TEST));
+ GL_CALL(glPolygonMode(GL_FRONT_AND_BACK, GL_LINE));
+
+ if (!shader_init(&state->shader))
+ return (false);
+ scene_init(&state->scene, data);
+
+ state->fov = M_PI_4;
+ state->running = true;
+ state->polygon_mode = GL_LINE;
+ state->width = WINDOW_WIDTH;
+ state->height = WINDOW_HEIGHT;
+ return (true);
+}
+
+void state_run(t_state *state)
+{
+ float rotation;
+
+ rotation = M_PI_4;
+ while (state->running)
+ {
+ GL_CALL(glClearColor(0.1f, 0.1f, 0.1f, 1.0f));
+ GL_CALL(glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT));
+
+ GL_CALL(glUseProgram(state->shader.id));
+ shader_update_mvp(&state->shader, &state->scene);
+ GL_CALL(glBindVertexArray(state->scene.vertex_array));
+ GL_CALL(glDrawElements(GL_TRIANGLES, state->scene.index_buf_size, GL_UNSIGNED_INT, (void*)0));
+
+ scene_update_model(&state->scene, rotation);
+ SDL_GL_SwapWindow(state->window);
+ st_handle_event(state);
+ rotation += M_PI / 100.0;
+ }
+}
+
+void state_quit(t_state *state)
+{
+ scene_quit(&state->scene);
+ GL_CALL(glDeleteProgram(state->shader.id));
+ SDL_GL_DeleteContext(state->context);
+ SDL_DestroyWindow(state->window);
+ SDL_Quit();
+}
diff --git a/src/texture.c b/src/texture.c
index 75f4797..e74f6be 100644
--- a/src/texture.c
+++ b/src/texture.c
@@ -6,79 +6,98 @@
/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/05/11 14:27:34 by charles #+# #+# */
-/* Updated: 2020/05/13 17:11:06 by charles ### ########.fr */
+/* Updated: 2020/05/14 14:01:38 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)
-{
- uint8_t *data;
- t_bmp_file_header file_header;
- t_bmp_info_header info_header;
- int fd;
-
- 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 = 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)
-{
- uint8_t *data;
- unsigned int texture;
- size_t width;
- size_t height;
-
- if ((data = st_read_bmp(filepath, &width, &height)) == NULL)
- return (0);
- GL_CALL(glGenTextures(1, &texture));
- 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));
- /* glBindTexture(GL_TEXTURE_2D, 0); */
- free(data);
- return (texture);
-}
+/* 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) */
+/* { */
+/* uint8_t *data; */
+/* t_bmp_file_header file_header; */
+/* t_bmp_info_header info_header; */
+/* int fd; */
+/* */
+/* 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 = 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) */
+/* { */
+/* uint8_t *data; */
+/* unsigned int texture; */
+/* size_t width; */
+/* size_t height; */
+/* */
+/* if ((data = st_read_bmp(filepath, &width, &height)) == NULL) */
+/* return (0); */
+/* GL_CALL(glGenTextures(1, &texture)); */
+/* 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)); */
+/* free(data); */
+/* return (texture); */
+/* } */
+/* */
+/* 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); */
+/* } */