From 2cbe37fb57c2e223a78caebe37e2f5abc9216487 Mon Sep 17 00:00:00 2001 From: Leo Tenenbaum Date: Fri, 26 Feb 2021 15:27:53 -0500 Subject: perlin noise --- 01f.glsl | 36 ++++++++++++++++++++++++++++++++++++ 01v.glsl | 9 +++++++++ gl.c | 4 ++-- main.c | 16 ++++++++++++---- 4 files changed, 59 insertions(+), 6 deletions(-) create mode 100644 01f.glsl create mode 100644 01v.glsl diff --git a/01f.glsl b/01f.glsl new file mode 100644 index 0000000..9baa911 --- /dev/null +++ b/01f.glsl @@ -0,0 +1,36 @@ +varying vec2 pos; +uniform float u_aspect_ratio; + +vec2 rand(vec2 co) { + float random = 2920.0 * sin(co.x * 21942.0 + co.y * 171324.0 + 8912.0) * cos(co.x * 23157.0 * co.y * 217832.0 + 9758.0); + return vec2(cos(random), sin(random)); +} + +float dot_grid_gradient(vec2 floor_p, vec2 p) { + vec2 gradient = rand(floor_p); + vec2 dist = p - floor_p; + return dot(dist, gradient); +} + +void main() { + vec2 p = pos * 10.0; + p.x *= u_aspect_ratio; + vec2 p0 = floor(p); + vec2 p1 = p0 + vec2(1.0, 1.0); + vec2 weight = p - p0; + weight = smoothstep(0.0, 1.0, weight); + + float n0 = dot_grid_gradient(vec2(p0.x, p0.y), p); + float n1 = dot_grid_gradient(vec2(p1.x, p0.y), p); + float ix0 = mix(n0, n1, weight.x); + + n0 = dot_grid_gradient(vec2(p0.x, p1.y), p); + n1 = dot_grid_gradient(vec2(p1.x, p1.y), p); + float ix1 = mix(n0, n1, weight.x); + + float v = mix(ix0, ix1, weight.y); + v += 1.0; + v *= 0.5; + vec3 color = mix(vec3(0.4, 0.2, 1.0), vec3(1.0, 0.6, 0.6), v); + gl_FragColor = vec4(color, 1.0); +} diff --git a/01v.glsl b/01v.glsl new file mode 100644 index 0000000..d5286df --- /dev/null +++ b/01v.glsl @@ -0,0 +1,9 @@ +attribute vec2 v_render_pos; +attribute vec2 v_pos; + +varying vec2 pos; + +void main() { + gl_Position = vec4(v_render_pos, 0.0, 1.0); + pos = v_pos; +} diff --git a/gl.c b/gl.c index 0fb89ef..00be97a 100644 --- a/gl.c +++ b/gl.c @@ -131,7 +131,7 @@ static GLuint gl_compile_and_link_shaders(char const *vshader_code, char const * static GLuint gl_attrib_loc(GLuint program, char const *attrib) { GLint loc = glGetAttribLocation(program, attrib); if (loc == -1) { - debug_print("Couldn't find vertex attribute %s.\n", attrib); + //debug_print("Couldn't find vertex attribute %s.\n", attrib); return (GLuint)-1; } return (GLuint)loc; @@ -140,7 +140,7 @@ static GLuint gl_attrib_loc(GLuint program, char const *attrib) { static GLint gl_uniform_loc(GLuint program, char const *uniform) { GLint loc = glGetUniformLocation(program, uniform); if (loc == -1) { - debug_print("Couldn't find uniform: %s.\n", uniform); + //debug_print("Couldn't find uniform: %s.\n", uniform); return -1; } return loc; diff --git a/main.c b/main.c index 705c699..8ba68c8 100644 --- a/main.c +++ b/main.c @@ -88,9 +88,11 @@ static void shader_check_for_changes(Shader *shader) { } static double start_time; +static float window_width, window_height; static void shader_draw(Shader *shader, Rect where) { shader_check_for_changes(shader); + if (!shader->program) return; float x1, y1, x2, y2; rect_coords(where, &x1, &y1, &x2, &y2); @@ -132,6 +134,9 @@ static void shader_draw(Shader *shader, Rect where) { GLint u_time = gl_uniform_loc(shader->program, "u_time"); if (u_time >= 0) glUniform1f(u_time, (float)fmod(time_get_seconds() - start_time, 10000)); + GLint u_aspect_ratio = gl_uniform_loc(shader->program, "u_aspect_ratio"); + if (u_aspect_ratio >= 0) + glUniform1f(u_aspect_ratio, window_width / window_height); glDrawArrays(GL_TRIANGLES, 0, 6); } @@ -241,9 +246,12 @@ int main(void) { bool quit = false, fullscreen = false; start_time = time_get_seconds(); while (!quit) { + int iwindow_width, iwindow_height; + SDL_GetWindowSize(window, &iwindow_width, &iwindow_height); + window_width = (float)iwindow_width; + window_height = (float)iwindow_height; + SDL_Event event = {0}; - int window_width, window_height; - SDL_GetWindowSize(window, &window_width, &window_height); v2 *mouse_clicks = NULL; while (SDL_PollEvent(&event)) { switch (event.type) { @@ -251,8 +259,8 @@ int main(void) { quit = true; break; case SDL_MOUSEBUTTONDOWN: { - float x = -1 + 2 * (float)event.button.x / (float)window_width; - float y = +1 - 2 * (float)event.button.y / (float)window_height; + float x = -1 + 2 * (float)event.button.x / window_width; + float y = +1 - 2 * (float)event.button.y / window_height; arr_add(mouse_clicks, V2(x, y)); } break; case SDL_KEYDOWN: -- cgit v1.2.3