improve background animation

This commit is contained in:
Zack Buhman 2025-12-05 22:56:54 -06:00
parent 29098a4eb9
commit af783c98c8
13 changed files with 112 additions and 59 deletions

View File

@ -111,10 +111,10 @@ MAIN_OBJS = \
src/collision2.o \ src/collision2.o \
src/update.o \ src/update.o \
src/unparse.o \ src/unparse.o \
src/color.o \
$(patsubst %.glsl,%.glsl.o,$(wildcard src/shader/*.glsl)) \ $(patsubst %.glsl,%.glsl.o,$(wildcard src/shader/*.glsl)) \
$(patsubst %.data,%.data.o,$(wildcard src/level/*.data)) \ $(patsubst %.data,%.data.o,$(wildcard src/level/*.data)) \
$(patsubst %.data,%.data.o,$(wildcard src/font/*.data)) \ $(patsubst %.data,%.data.o,$(wildcard src/font/*.data)) \
$(patsubst %.data,%.data.o,$(wildcard src/texture/*.data)) \
$(patsubst %.data.pal,%.data.pal.o,$(wildcard src/level/*.data.pal)) \ $(patsubst %.data.pal,%.data.pal.o,$(wildcard src/level/*.data.pal)) \
$(GLFW) $(GLFW)

View File

@ -1,5 +0,0 @@
#pragma once
#include "math/float_types.hpp"
vec3 hsv_to_rgb(float h, float s, float v);

View File

@ -49,7 +49,10 @@ extern "C" {
uint attrib_position, uint attrib_position,
uint uniform_resolution, uint uniform_resolution,
uint uniform_trans, uint uniform_trans,
uint uniform_texture1,
uint uniform_time, uint uniform_time,
uint uniform_palette,
uint uniform_aspect,
struct game_state * state); struct game_state * state);
void render_paddle(struct mesh paddle_mesh, void render_paddle(struct mesh paddle_mesh,

View File

@ -36,6 +36,7 @@ extern "C" {
double start_time; double start_time;
double time; double time;
double remaining; double remaining;
double time_bg;
bool intro_shown; bool intro_shown;
}; };

View File

@ -0,0 +1,19 @@
#pragma once
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
extern uint32_t _binary_src_texture_noise_data_start __asm("_binary_src_texture_noise_data_start");
extern uint32_t _binary_src_texture_noise_data_end __asm("_binary_src_texture_noise_data_end");
extern uint32_t _binary_src_texture_noise_data_size __asm("_binary_src_texture_noise_data_size");
#define src_texture_noise_data_start ((const char *)&_binary_src_texture_noise_data_start)
#define src_texture_noise_data_end ((const char *)&_binary_src_texture_noise_data_end)
#define src_texture_noise_data_size (src_texture_noise_data_end - src_texture_noise_data_start)
#ifdef __cplusplus
}
#endif

View File

@ -1,26 +0,0 @@
#include "color.hpp"
vec3 hsv_to_rgb(float h, float s, float v)
{
if (s == 0.0)
return vec3(v, v, v);
int i = h * 6.0f;
float f = (h * 6.0f) - i;
float p = v * (1.0f - s);
float q = v * (1.0f - s * f);
float t = v * (1.0f - s * (1.0f - f));
i = i % 6;
if (i == 0)
return vec3(v, t, p);
if (i == 1)
return vec3(q, v, p);
if (i == 2)
return vec3(p, v, t);
if (i == 3)
return vec3(p, q, v);
if (i == 4)
return vec3(t, p, v);
if (i == 5)
return vec3(v, p, q);
return vec3(0, 0, 0);
}

View File

@ -17,6 +17,7 @@
#include "shader/block.fp.glsl.h" #include "shader/block.fp.glsl.h"
#include "font/ter_u32n.data.h" #include "font/ter_u32n.data.h"
#include "texture/noise.data.h"
#include "opengl.h" #include "opengl.h"
#include "render.hpp" #include "render.hpp"
@ -187,7 +188,10 @@ int main()
uint bg__attrib_position = glGetAttribLocation(bg_program, "position"); uint bg__attrib_position = glGetAttribLocation(bg_program, "position");
uint bg__uniform_resolution = glGetUniformLocation(bg_program, "resolution"); uint bg__uniform_resolution = glGetUniformLocation(bg_program, "resolution");
uint bg__uniform_trans = glGetUniformLocation(bg_program, "trans"); uint bg__uniform_trans = glGetUniformLocation(bg_program, "trans");
uint bg__uniform_texture1 = glGetUniformLocation(bg_program, "texture1");
uint bg__uniform_time = glGetUniformLocation(bg_program, "time"); uint bg__uniform_time = glGetUniformLocation(bg_program, "time");
uint bg__uniform_palette = glGetUniformLocation(bg_program, "palette");
uint bg__uniform_aspect = glGetUniformLocation(bg_program, "aspect");
// paddle // paddle
@ -207,6 +211,7 @@ int main()
// textures // textures
////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////
glActiveTexture(GL_TEXTURE0);
uint terminus_font = make_texture(src_font_ter_u32n_data_start, uint terminus_font = make_texture(src_font_ter_u32n_data_start,
GL_RED, GL_RED,
256, 256,
@ -214,6 +219,14 @@ int main()
GL_RED); GL_RED);
(void)terminus_font; (void)terminus_font;
glActiveTexture(GL_TEXTURE1);
uint noise_texture = make_texture(src_texture_noise_data_start,
GL_RGBA,
256,
256,
GL_RGBA);
(void)noise_texture;
////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////
// main loop // main loop
////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////
@ -318,7 +331,10 @@ int main()
bg__attrib_position, bg__attrib_position,
bg__uniform_resolution, bg__uniform_resolution,
bg__uniform_trans, bg__uniform_trans,
bg__uniform_texture1,
bg__uniform_time, bg__uniform_time,
bg__uniform_palette,
bg__uniform_aspect,
&state); &state);
glEnable(GL_BLEND); glEnable(GL_BLEND);

View File

@ -78,11 +78,10 @@ int make_texture(const void * data,
{ {
unsigned int texture; unsigned int texture;
glGenTextures(1, &texture); glGenTextures(1, &texture);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture); glBindTexture(GL_TEXTURE_2D, texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_MIRRORED_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_MIRRORED_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

View File

@ -6,7 +6,6 @@
#include "unparse.h" #include "unparse.h"
#include "render.hpp" #include "render.hpp"
#include "color.hpp"
#include "math/float_types.hpp" #include "math/float_types.hpp"
#include "math/transform.hpp" #include "math/transform.hpp"
@ -190,6 +189,13 @@ void render_blocks(mesh block_mesh,
} }
} }
vec3 sample_palette(float d, vec3 p) {
vec3 v = vec3(d, d, d) + p;
v = v * vec3(6.28318548202514648438f) ;
v = vec3(cos(v.x), cos(v.y), cos(v.z));
return vec3(0.5, 0.5, 0.5) * v + vec3(0.5, 0.5, 0.5); // 48
}
void render_balls(mesh ball_mesh, void render_balls(mesh ball_mesh,
uint attrib_position, uint attrib_position,
uint attrib_texture, uint attrib_texture,
@ -249,14 +255,7 @@ void render_balls(mesh ball_mesh,
vec4 base_color = vec4(0.5f, 0.5f, 0.5f, 1.0f); vec4 base_color = vec4(0.5f, 0.5f, 0.5f, 1.0f);
if (ball.super_ball) { if (ball.super_ball) {
float hue = state->time - ball.launch_time; float hue = state->time - ball.launch_time;
hue = hue - floorf(hue); base_color = sample_palette(hue, vec3(0, 0.33, 0.67));
vec3 c = hsv_to_rgb(hue, 1.0f, 1.0f);
base_color = vec4(c.x, c.y, c.z, 1.0f);
} else if (0) {
float hue = sin(ball.launch_time) * 0.5 - 0.5;
hue = hue - floorf(hue);
vec3 c = hsv_to_rgb(hue, 1.0f, 1.0f);
base_color = vec4(c.x, c.y, c.z, 1.0f) * 0.5f;
} }
glUniform4fv(uniform_trans, 4, &trans[0][0]); glUniform4fv(uniform_trans, 4, &trans[0][0]);
@ -307,9 +306,10 @@ void render_text(struct mesh plane_mesh,
mat4x4 s, mat4x4 s,
mat4x4 r, mat4x4 r,
float _scale, float _scale,
int h_advance, int h_advance_init,
int v_advance) int v_advance)
{ {
int h_advance = h_advance_init;
for (int i = 0; i < text_length; i++) { for (int i = 0; i < text_length; i++) {
const char * txt = text[i]; const char * txt = text[i];
while (*txt) { while (*txt) {
@ -330,7 +330,7 @@ void render_text(struct mesh plane_mesh,
h_advance += grid_width; h_advance += grid_width;
} }
v_advance += grid_height; v_advance += grid_height;
h_advance = 0; h_advance = h_advance_init;
} }
} }
@ -388,7 +388,7 @@ void render_font(struct mesh plane_mesh,
const int intro_length = (sizeof (intro)) / (sizeof (intro[0])); const int intro_length = (sizeof (intro)) / (sizeof (intro[0]));
const char * loss[] = { const char * loss[] = {
"You were not successful.", "You were not prepared.",
"Press option to retry.", "Press option to retry.",
}; };
const int loss_length = (sizeof (loss)) / (sizeof (loss[0])); const int loss_length = (sizeof (loss)) / (sizeof (loss[0]));
@ -411,7 +411,7 @@ void render_font(struct mesh plane_mesh,
aspect, aspect,
a, s, r, a, s, r,
text_scale, text_scale,
0, 0); -grid_width * 3, 0);
} else if (state->remaining <= 0) { } else if (state->remaining <= 0) {
if (state->level_ix == 0) { if (state->level_ix == 0) {
vec3 base_color = vec3(1, 0.1, 0.1); vec3 base_color = vec3(1, 0.1, 0.1);
@ -533,7 +533,10 @@ void render_background(struct mesh plane_mesh,
uint attrib_position, uint attrib_position,
uint uniform_resolution, uint uniform_resolution,
uint uniform_trans, uint uniform_trans,
uint uniform_texture1,
uint uniform_time, uint uniform_time,
uint uniform_palette,
uint uniform_aspect,
struct game_state * state) struct game_state * state)
{ {
glBindBuffer(GL_ARRAY_BUFFER, plane_mesh.vtx); glBindBuffer(GL_ARRAY_BUFFER, plane_mesh.vtx);
@ -552,14 +555,24 @@ void render_background(struct mesh plane_mesh,
mat4x4 trans = r; mat4x4 trans = r;
glUniform4fv(uniform_trans, 4, &trans[0][0]); glUniform4fv(uniform_trans, 4, &trans[0][0]);
if (state->balls_launched == 0 || state->remaining <= 0.0) { glUniform1i(uniform_texture1, 1);
glUniform1f(uniform_time, 0);
} else { glUniform1f(uniform_time, state->time_bg);
glUniform1f(uniform_time, state->time);
}
vec2 resolution = vec2(vp_width, vp_height); vec2 resolution = vec2(vp_width, vp_height);
glUniform2fv(uniform_resolution, 1, &resolution[0]); glUniform2fv(uniform_resolution, 1, &resolution[0]);
const vec3 palettes[5] = {
vec3(0.440f, 0.021f, 0.512f),
vec3(0.131f, 0.958f, 0.678f),
vec3(0.507f, 0.407f, 0.103f),
vec3(0.709f, 0.240f, 0.475f),
vec3(0.658f, 0.944f, 0.719f),
};
glUniform3fv(uniform_palette, 1, &palettes[state->level_ix % 5][0]);
float aspect = (float)vp_height / (float)vp_width;
glUniform1f(uniform_aspect, aspect);
glDrawElements(GL_TRIANGLES, plane_mesh.length, GL_UNSIGNED_INT, 0); glDrawElements(GL_TRIANGLES, plane_mesh.length, GL_UNSIGNED_INT, 0);
} }

View File

@ -1,11 +1,17 @@
#version 120 #version 120
uniform sampler2D texture1;
varying vec2 fp_uv; varying vec2 fp_uv;
uniform float time; uniform float time;
vec3 palette(float d) { uniform vec3 palette;
vec3 v = d + vec3(0.25, 0.40625, 0.5625); // 40 45 49
uniform float aspect;
vec3 sample_palette(float d, vec3 p) {
vec3 v = d + p;
v = v * 6.28318548202514648438; v = v * 6.28318548202514648438;
v = cos(v); v = cos(v);
return vec3(0.5, 0.5, 0.5) * v + vec3(0.5, 0.5, 0.5); // 48 return vec3(0.5, 0.5, 0.5) * v + vec3(0.5, 0.5, 0.5); // 48
@ -13,9 +19,31 @@ vec3 palette(float d) {
void main() void main()
{ {
vec2 tc = vec2(fp_uv.x * aspect, fp_uv.y) * 0.5 + 0.5;
vec4 c = texture2D(texture1, tc);
vec2 bc = fp_uv * 0.5 + 0.5;
if (bc.x > 0.95 || bc.x < 0.05 ||
bc.y > 1.0 || bc.y < 0.0) {
vec2 uv = fp_uv; vec2 uv = fp_uv;
float d = length(uv); float d = length(uv);
float st = sin(time * 0.05);
float td = time * st * 0.5 + d;
vec3 cc = mix(c.xyz, c.zyx, sin(time) * 0.5 + 0.5);
gl_FragColor = vec4(sample_palette(td, palette) * cc, 1);
} else {
float noise_0 = mix(c.x, c.y, sin(time) * 0.5 + 0.5);
float noise_1 = mix(c.z, c.w, cos(time) * 0.5 + 0.5);
vec2 uv = vec2(sin(fp_uv.x + time + noise_0),
cos(fp_uv.y + time + noise_1));
float d = length(uv);
float td = time * 0.5 + d; float td = time * 0.5 + d;
gl_FragColor = vec4(palette(td) * 0.1, 1); gl_FragColor = vec4(sample_palette(td, palette) * 0.1, 1);
}
//gl_FragColor = vec4(noise * noise);
} }

1
src/texture/noise.data Normal file

File diff suppressed because one or more lines are too long

BIN
src/texture/noise.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 98 KiB

View File

@ -239,6 +239,10 @@ void update(struct game_state * state, double time)
state->start_time = time; state->start_time = time;
} }
double dt = time - state->time;
state->time = time; state->time = time;
if (!(state->balls_launched == 0 || state->remaining <= 0.0)) {
state->time_bg += dt;
}
state->remaining = 20.0 - (time - state->start_time); state->remaining = 20.0 - (time - state->start_time);
} }