-
Notifications
You must be signed in to change notification settings - Fork 58
/
mlx_images.c
148 lines (136 loc) · 5.17 KB
/
mlx_images.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/* ************************************************************************** */
/* */
/* :::::::: */
/* mlx_images.c :+: :+: */
/* +:+ */
/* By: W2Wizard <w2.wizzard@gmail.com> +#+ */
/* +#+ */
/* Created: 2022/01/21 15:34:45 by W2Wizard #+# #+# */
/* Updated: 2022/02/06 22:39:01 by w2wizard ######## odam.nl */
/* */
/* ************************************************************************** */
#include "MLX42/MLX42_Int.h"
// Reference: https://bit.ly/3KuHOu1 (Matrix View Projection)
static void mlx_draw_texture(t_mlx *mlx, t_mlx_image *img, uint8_t *pixels, \
t_vert *vertices)
{
t_mlx_ctx *mlxctx;
const int32_t w = img->width;
const int32_t h = img->height;
const float matrix[16] = {
2.f / mlx->width, 0, 0, 0,
0, 2. / -mlx->height, 0, 0,
0, 0, -2. / (1000. - -1000.), 0,
-1, -(mlx->height / -mlx->height),
-((1000. + -1000.) / (1000. - -1000.)), 1
};
mlxctx = mlx->context;
glUseProgram(mlxctx->shaderprogram);
glUniformMatrix4fv(glGetUniformLocation(mlxctx->shaderprogram, \
"proj_matrix"), 1, GL_FALSE, matrix);
glUniform1i(glGetUniformLocation(mlxctx->shaderprogram, "outTexture"), 0);
glBindVertexArray(mlxctx->vao);
glBindBuffer(GL_ARRAY_BUFFER, mlxctx->vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(t_vert) * 6, vertices, \
GL_STATIC_DRAW);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, \
GL_UNSIGNED_BYTE, pixels);
glDrawArrays(GL_TRIANGLES, 0, 6);
}
/**
* Internal function to draw a single instance of an image
* to the screen.
*
* @param mlx The MLX handle.
* @param img The image.
* @param instance The instance to draw.
*/
void mlx_draw_instance(t_mlx *mlx, t_mlx_image *img, \
t_mlx_instance *instance)
{
t_mlx_image_ctx *imgctx;
const int32_t w = img->width;
const int32_t h = img->height;
const int32_t x = instance->x;
const int32_t y = instance->y;
imgctx = img->context;
imgctx->vertices[0] = (t_vert){x, y, instance->z, 0.f, 0.f};
imgctx->vertices[1] = (t_vert){x + w, y + h, instance->z, 1.f, 1.f};
imgctx->vertices[2] = (t_vert){x + w, y, instance->z, 1.f, 0.f};
imgctx->vertices[3] = (t_vert){x, y, instance->z, 0.f, 0.f};
imgctx->vertices[4] = (t_vert){x, y + h, instance->z, 0.f, 1.f};
imgctx->vertices[5] = (t_vert){x + w, y + h, instance->z, 1.f, 1.f};
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, imgctx->texture);
mlx_draw_texture(mlx, img, img->pixels, imgctx->vertices);
}
//= Exposed =//
// TODO: Change to linked list, then introduce function for ease of access?
void mlx_image_to_window(t_mlx *mlx, t_mlx_image *img, int32_t x, int32_t y)
{
int32_t index;
t_mlx_ctx *mlxctx;
t_mlx_instance *temp;
t_draw_queue *queue;
mlxctx = mlx->context;
if (!mlx || !img)
return ;
temp = realloc(img->instances, (++img->count) * \
sizeof(t_mlx_instance));
queue = malloc(sizeof(t_draw_queue));
if (!temp || !queue)
{
mlx_log(MLX_ERROR, MLX_MEMORY_FAIL);
free(queue);
return ;
}
index = img->count - 1;
img->instances = temp;
img->instances[index].x = x;
img->instances[index].y = y;
img->instances[index].z = 0;
queue->image = img;
queue->instance = &img->instances[index];
mlx_lstadd_back(&mlxctx->render_queue, mlx_lstnew(queue));
}
// TODO: Protect malloc and report actual errors.
t_mlx_image *mlx_new_image(t_mlx *mlx, uint16_t width, uint16_t height)
{
t_mlx_image *newimg;
t_mlx_image_ctx *newctx;
const t_mlx_ctx *mlxctx = mlx->context;
newimg = calloc(1, sizeof(t_mlx_image));
newctx = calloc(1, sizeof(t_mlx_image_ctx));
if (!newimg || !newctx)
return ((void *)mlx_freen(2, newimg, newctx));
(*(uint16_t *)&newimg->width) = width;
(*(uint16_t *)&newimg->height) = height;
newimg->context = newctx;
newimg->pixels = calloc(width * height, sizeof(int32_t));
if (!newimg->pixels)
return ((void *)mlx_freen(2, newimg, newctx));
glGenTextures(1, &newctx->texture);
glBindTexture(GL_TEXTURE_2D, newctx->texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, \
GL_UNSIGNED_BYTE, newimg->pixels);
mlx_lstadd_back((t_mlx_list **)(&mlxctx->images), mlx_lstnew(newimg));
return (newimg);
}
void mlx_delete_image(t_mlx *mlx, t_mlx_image *image)
{
t_mlx_image *img;
t_mlx_ctx *mlxctx;
mlxctx = mlx->context;
img = mlx_lstremove(&mlxctx->images, image, &mlx_equal_image);
if (img)
{
glDeleteTextures(1, &((t_mlx_image_ctx *)img->context)->texture);
mlx_freen(3, img->pixels, img->instances, img->context);
memset(img, 0, sizeof(t_mlx_image));
}
free(mlx_lstremove(&mlxctx->render_queue, image, &mlx_equal_inst));
}