boilerplate in shader reduction
This commit is contained in:
parent
f6706982ce
commit
2383faf456
2 changed files with 42 additions and 22 deletions
|
|
@ -1,9 +1,3 @@
|
||||||
#version 330 core
|
|
||||||
|
|
||||||
uniform vec2 WindowSize;
|
|
||||||
uniform float time;
|
|
||||||
layout(location = 0) out vec4 color;
|
|
||||||
|
|
||||||
float lerp(float a, float b, float x) {
|
float lerp(float a, float b, float x) {
|
||||||
return a + x*(b-a);
|
return a + x*(b-a);
|
||||||
}
|
}
|
||||||
|
|
@ -73,19 +67,20 @@ float limit(float val, float min, float max) {
|
||||||
return out_val;
|
return out_val;
|
||||||
}
|
}
|
||||||
|
|
||||||
void main() {
|
void mainImage(out vec4 fragColor, in vec2 fragCoord) {
|
||||||
vec2 screen_uv = gl_FragCoord.xy / WindowSize;
|
vec2 screen_uv = gl_FragCoord.xy / iResolution;
|
||||||
// We'll make origin in the top-left.
|
// We'll make origin in the top-left.
|
||||||
screen_uv.y = 1.0 - screen_uv.y;
|
screen_uv.y = 1.0 - screen_uv.y;
|
||||||
screen_uv.x *= WindowSize.x / WindowSize.y;
|
screen_uv.x *= iResolution.x / iResolution.y;
|
||||||
vec2 uv = vec2(screen_uv.x, screen_uv.y);
|
vec2 uv = vec2(screen_uv.x, screen_uv.y);
|
||||||
uv.y += sin(uv.x * 2.0 + time * 0.2) * 0.47;
|
uv.y += sin(uv.x * 2.0 + iTime * 0.2) * 0.47;
|
||||||
uv.y += sin(uv.x * 4.3 + time * 1.34) * 0.09;
|
uv.y += sin(uv.x * 4.3 + iTime * 1.34) * 0.09;
|
||||||
|
|
||||||
uv += 100.0;
|
uv += 100.0;
|
||||||
float out_val = perlin_noise(uv, vec2(0.0, 0.0), time / 1.0);
|
float out_val = perlin_noise(uv, vec2(0.0, 0.0), iTime / 1.0);
|
||||||
float min = 0.42 + sin(time)/8.0;
|
float min = 0.42 + sin(iTime)/8.0;
|
||||||
float max = 0.58 + sin(time)/8.0;
|
float max = 0.58 + sin(iTime)/8.0;
|
||||||
out_val = limit(out_val, min, max);
|
out_val = limit(out_val, min, max);
|
||||||
color = vec4(out_val, 0.4, 0.8, 1.0);
|
fragColor = vec4(out_val, 0.4, 0.8, 1.0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
39
renderer.c
39
renderer.c
|
|
@ -28,7 +28,7 @@ GLuint compile_shader(GLuint type, const char *src) {
|
||||||
}
|
}
|
||||||
|
|
||||||
const char *vertex_shader_src =
|
const char *vertex_shader_src =
|
||||||
"#version 330 core\n"
|
"#version 300 es\n"
|
||||||
"\n"
|
"\n"
|
||||||
"layout(location = 0) in vec2 aPos;\n"
|
"layout(location = 0) in vec2 aPos;\n"
|
||||||
"\n"
|
"\n"
|
||||||
|
|
@ -36,6 +36,21 @@ const char *vertex_shader_src =
|
||||||
" gl_Position = vec4(aPos, 0.0, 1.0);\n"
|
" gl_Position = vec4(aPos, 0.0, 1.0);\n"
|
||||||
"}\n";
|
"}\n";
|
||||||
|
|
||||||
|
const char *fragment_header_src =
|
||||||
|
"#version 300 es\n"
|
||||||
|
"precision highp float;\n"
|
||||||
|
"precision highp int;\n"
|
||||||
|
"precision lowp sampler2D;\n"
|
||||||
|
"precision lowp samplerCube;\n"
|
||||||
|
"layout(location = 0) out vec4 color;\n"
|
||||||
|
"uniform vec2 iResolution;\n"
|
||||||
|
"uniform float iTime;\n"
|
||||||
|
"void mainImage(out vec4 fragColor, in vec2 fragCoord);\n"
|
||||||
|
"void main() {\n"
|
||||||
|
" mainImage(color, gl_FragCoord.xy);\n"
|
||||||
|
"}\n"
|
||||||
|
"\n";
|
||||||
|
|
||||||
/// returns 0 if failed.
|
/// returns 0 if failed.
|
||||||
GLuint create_shader(const char *fragment_src) {
|
GLuint create_shader(const char *fragment_src) {
|
||||||
GLuint program = glCreateProgram();
|
GLuint program = glCreateProgram();
|
||||||
|
|
@ -60,14 +75,24 @@ GLuint load_shader(const char *path) {
|
||||||
printf("failed to open shader file: '%s'\n", path);
|
printf("failed to open shader file: '%s'\n", path);
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
fseek(handle, 0L, SEEK_END);
|
fseek(handle, 0L, SEEK_END);
|
||||||
long length = ftell(handle);
|
long file_length = ftell(handle);
|
||||||
fseek(handle, 0L, SEEK_SET);
|
fseek(handle, 0L, SEEK_SET);
|
||||||
|
|
||||||
|
long header_length = strlen(fragment_header_src);
|
||||||
|
long length = header_length + file_length;
|
||||||
|
|
||||||
char *string = malloc((length + 1) * sizeof(char));
|
char *string = malloc((length + 1) * sizeof(char));
|
||||||
if (!fread(string, sizeof(char), length, handle)) {
|
|
||||||
|
// Copy header.
|
||||||
|
strcpy(string, fragment_header_src);
|
||||||
|
|
||||||
|
if (!fread(string + header_length, sizeof(char), file_length, handle)) {
|
||||||
printf("failed to read shader file\n");
|
printf("failed to read shader file\n");
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
// I love C
|
// I love C
|
||||||
string[length] = '\0';
|
string[length] = '\0';
|
||||||
fclose(handle);
|
fclose(handle);
|
||||||
|
|
@ -164,11 +189,11 @@ void render(Renderer *state, int w, int h, double time, char *shader_path, int r
|
||||||
// Shader parameters.
|
// Shader parameters.
|
||||||
glUseProgram(state->shader);
|
glUseProgram(state->shader);
|
||||||
|
|
||||||
int uniform_WindowSize = glGetUniformLocation(state->shader, "WindowSize");
|
int uniform_iResolution = glGetUniformLocation(state->shader, "iResolution");
|
||||||
glUniform2f(uniform_WindowSize, w, h);
|
glUniform2f(uniform_iResolution, w, h);
|
||||||
|
|
||||||
int uniform_time = glGetUniformLocation(state->shader, "time");
|
int uniform_iTime = glGetUniformLocation(state->shader, "iTime");
|
||||||
glUniform1f(uniform_time, (float)time);
|
glUniform1f(uniform_iTime, (float)time);
|
||||||
|
|
||||||
glBindVertexArray(state->vao);
|
glBindVertexArray(state->vao);
|
||||||
glDrawArrays(GL_TRIANGLES, 0, 6);
|
glDrawArrays(GL_TRIANGLES, 0, 6);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue