-
Notifications
You must be signed in to change notification settings - Fork 0
/
mlx_shaders.c
236 lines (197 loc) · 6.57 KB
/
mlx_shaders.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
// mlx_shaders.c
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <OpenGL/gl3.h>
#include "mlx_int.h"
void display_log(GLuint object, void (*param_func)(), void (*getlog_func)())
{
GLint log_length;
char *log;
param_func(object, GL_INFO_LOG_LENGTH, &log_length);
log = malloc(log_length);
getlog_func(object, log_length, NULL, log);
fprintf(stderr, "%s", log);
free(log);
}
int mlx_shaders_pixel(glsl_info_t *glsl)
{
char *source;
int length;
GLint action_ok;
glsl->pixel_vshader = glCreateShader(GL_VERTEX_SHADER);
source = strdup("#version 110 \n"
"attribute vec2 position;"
"varying vec2 texcoord;"
"void main()"
"{"
" gl_Position = vec4( position, 0.0, 1.0);"
" texcoord = vec2(position[0]+1.0, 1.0 - position[1]) / 2.0;"
"}");
length = strlen(source);
glShaderSource(glsl->pixel_vshader, 1, (const GLchar**)&source, &length);
glCompileShader(glsl->pixel_vshader);
free(source);
glGetShaderiv(glsl->pixel_vshader, GL_COMPILE_STATUS, &action_ok);
if (!action_ok) {
fprintf(stderr, "Failed to compile pixel vshader :\n");
display_log(glsl->pixel_vshader, glGetShaderiv, glGetShaderInfoLog);
return (1);
}
glsl->pixel_fshader = glCreateShader(GL_FRAGMENT_SHADER);
source = strdup("#version 110 \n"
"uniform sampler2D texture;"
"varying vec2 texcoord;"
"void main()"
"{"
" gl_FragColor = texture2D(texture, texcoord);"
"}");
length = strlen(source);
glShaderSource(glsl->pixel_fshader, 1, (const GLchar**)&source, &length);
glCompileShader(glsl->pixel_fshader);
free(source);
glGetShaderiv(glsl->pixel_fshader, GL_COMPILE_STATUS, &action_ok);
if (!action_ok) {
fprintf(stderr, "Failed to compile pixel fshader :\n");
display_log(glsl->pixel_fshader, glGetShaderiv, glGetShaderInfoLog);
return (1);
}
glsl->pixel_program = glCreateProgram();
glAttachShader(glsl->pixel_program, glsl->pixel_vshader);
glAttachShader(glsl->pixel_program, glsl->pixel_fshader);
glLinkProgram(glsl->pixel_program);
glGetProgramiv(glsl->pixel_program, GL_LINK_STATUS, &action_ok);
if (!action_ok) {
fprintf(stderr, "Failed to link pixel shader program:\n");
display_log(glsl->pixel_program, glGetProgramiv, glGetProgramInfoLog);
return (1);
}
glFlush();
return (0);
}
int mlx_shaders_image(glsl_info_t *glsl)
{
char *source;
int length;
GLint action_ok;
glsl->image_vshader = glCreateShader(GL_VERTEX_SHADER);
source = strdup("#version 110 \n"
"attribute vec2 position;"
"uniform vec2 winhalfsize;"
"uniform vec2 imagepos;"
"uniform vec2 imagesize;"
"varying vec2 texcoord;"
"void main()"
"{"
" texcoord = position / imagesize;"
" vec2 pos = position - winhalfsize + imagepos;"
" pos = pos / winhalfsize;"
" gl_Position = vec4( pos, 0.0, 1.0);"
"}");
length = strlen(source);
glShaderSource(glsl->image_vshader, 1, (const GLchar**)&source, &length);
glCompileShader(glsl->image_vshader);
free(source);
glGetShaderiv(glsl->image_vshader, GL_COMPILE_STATUS, &action_ok);
if (!action_ok) {
fprintf(stderr, "Failed to compile image vshader :\n");
display_log(glsl->image_vshader, glGetShaderiv, glGetShaderInfoLog);
return (1);
}
glsl->image_fshader = glCreateShader(GL_FRAGMENT_SHADER);
source = strdup("#version 110 \n"
"uniform sampler2D texture;"
"varying vec2 texcoord;"
"void main()"
"{"
" gl_FragColor = texture2D(texture, texcoord);"
"}");
length = strlen(source);
glShaderSource(glsl->image_fshader, 1, (const GLchar**)&source, &length);
glCompileShader(glsl->image_fshader);
free(source);
glGetShaderiv(glsl->image_fshader, GL_COMPILE_STATUS, &action_ok);
if (!action_ok) {
fprintf(stderr, "Failed to compile image fshader :\n");
display_log(glsl->image_fshader, glGetShaderiv, glGetShaderInfoLog);
return (1);
}
glsl->image_program = glCreateProgram();
glAttachShader(glsl->image_program, glsl->image_vshader);
glAttachShader(glsl->image_program, glsl->image_fshader);
glLinkProgram(glsl->image_program);
glGetProgramiv(glsl->image_program, GL_LINK_STATUS, &action_ok);
if (!action_ok) {
fprintf(stderr, "Failed to link image shader program:\n");
display_log(glsl->image_program, glGetProgramiv, glGetProgramInfoLog);
return (1);
}
glFlush();
return (0);
}
int mlx_shaders_font(glsl_info_t *glsl)
{
char *source;
int length;
GLint action_ok;
glsl->font_vshader = glCreateShader(GL_VERTEX_SHADER);
source = strdup("#version 110 \n"
"attribute vec2 position;"
"uniform vec2 winhalfsize;"
"uniform vec2 fontposinwin;"
"uniform vec2 fontposinatlas;"
"uniform vec2 fontatlassize;"
"varying vec2 texcoord;"
"void main()"
"{"
" texcoord = (position * vec2(1.0, -1.0) + fontposinatlas ) / fontatlassize;"
" vec2 pos = position - winhalfsize + fontposinwin;"
" pos = pos / winhalfsize;"
" gl_Position = vec4( pos, 0.0, 1.0);"
"}");
length = strlen(source);
glShaderSource(glsl->font_vshader, 1, (const GLchar**)&source, &length);
glCompileShader(glsl->font_vshader);
free(source);
glGetShaderiv(glsl->font_vshader, GL_COMPILE_STATUS, &action_ok);
if (!action_ok) {
fprintf(stderr, "Failed to compile font vshader :\n");
display_log(glsl->font_vshader, glGetShaderiv, glGetShaderInfoLog);
return (1);
}
glsl->font_fshader = glCreateShader(GL_FRAGMENT_SHADER);
source = strdup("#version 110 \n"
"uniform sampler2D texture;"
"uniform vec4 color;"
"varying vec2 texcoord;"
"void main()"
"{"
" gl_FragColor = color * texture2D(texture, texcoord);"
"}");
length = strlen(source);
glShaderSource(glsl->font_fshader, 1, (const GLchar**)&source, &length);
glCompileShader(glsl->font_fshader);
free(source);
glGetShaderiv(glsl->font_fshader, GL_COMPILE_STATUS, &action_ok);
if (!action_ok) {
fprintf(stderr, "Failed to compile font fshader :\n");
display_log(glsl->font_fshader, glGetShaderiv, glGetShaderInfoLog);
return (1);
}
glsl->font_program = glCreateProgram();
glAttachShader(glsl->font_program, glsl->font_vshader);
glAttachShader(glsl->font_program, glsl->font_fshader);
glLinkProgram(glsl->font_program);
glGetProgramiv(glsl->font_program, GL_LINK_STATUS, &action_ok);
if (!action_ok) {
fprintf(stderr, "Failed to link font shader program:\n");
display_log(glsl->font_program, glGetProgramiv, glGetProgramInfoLog);
return (1);
}
glFlush();
return (0);
}
int mlx_shaders(glsl_info_t *glsl)
{
return (mlx_shaders_pixel(glsl) + mlx_shaders_image(glsl) + mlx_shaders_font(glsl));
}