aboutsummaryrefslogtreecommitdiff
path: root/src/texture.c
diff options
context:
space:
mode:
authorCharles <sircharlesaze@gmail.com>2020-05-14 16:44:28 +0200
committerCharles <sircharlesaze@gmail.com>2020-05-14 16:44:28 +0200
commit23ba151d95e4284ddb2d5f1c9810741061c293fd (patch)
tree98d9aa1322dc108e1adf2a695fc642266e05c2b3 /src/texture.c
parent29ea8338efb0b5450611b73463c9d7d469db2d75 (diff)
downloadscop-23ba151d95e4284ddb2d5f1c9810741061c293fd.tar.gz
scop-23ba151d95e4284ddb2d5f1c9810741061c293fd.tar.bz2
scop-23ba151d95e4284ddb2d5f1c9810741061c293fd.zip
Refactoring to the point before started refactoring, without crash on texture binding
Diffstat (limited to 'src/texture.c')
-rw-r--r--src/texture.c179
1 files changed, 89 insertions, 90 deletions
diff --git a/src/texture.c b/src/texture.c
index e74f6be..a91b355 100644
--- a/src/texture.c
+++ b/src/texture.c
@@ -6,98 +6,97 @@
/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/05/11 14:27:34 by charles #+# #+# */
-/* Updated: 2020/05/14 14:01:38 by charles ### ########.fr */
+/* Updated: 2020/05/14 16:37:42 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)); */
-/* 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); */
-/* } */
+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_create(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);
+}
+
+void texture_coord_init(float *vertices, size_t vertices_num)
+{
+ t_ftmvec3 min;
+ t_ftmvec3 max;
+ 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));
+ i += VERTEX_COUNT;
+ }
+}