animate background, paddle, balls

This commit is contained in:
Zack Buhman 2025-12-05 16:56:39 -06:00
parent 72de358c18
commit 5164754a26
11 changed files with 6003 additions and 5445 deletions

Binary file not shown.

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -29,12 +29,18 @@ extern "C" {
void render_font(struct mesh plane_mesh, void render_font(struct mesh plane_mesh,
uint attrib_position, uint attrib_position,
uint attrib_texture, uint attrib_texture,
uint attrib_normal,
uint uniform_trans, uint uniform_trans,
uint uniform_texture_trans, uint uniform_texture_trans,
uint uniform_texture0, uint uniform_texture0,
struct game_state * state); struct game_state * state);
void render_background(struct mesh plane_mesh,
uint attrib_position,
uint uniform_resolution,
uint uniform_trans,
uint uniform_time,
struct game_state * state);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View File

@ -0,0 +1,19 @@
#pragma once
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
extern uint32_t _binary_src_shader_background_fp_glsl_start __asm("_binary_src_shader_background_fp_glsl_start");
extern uint32_t _binary_src_shader_background_fp_glsl_end __asm("_binary_src_shader_background_fp_glsl_end");
extern uint32_t _binary_src_shader_background_fp_glsl_size __asm("_binary_src_shader_background_fp_glsl_size");
#define src_shader_background_fp_glsl_start ((const char *)&_binary_src_shader_background_fp_glsl_start)
#define src_shader_background_fp_glsl_end ((const char *)&_binary_src_shader_background_fp_glsl_end)
#define src_shader_background_fp_glsl_size (src_shader_background_fp_glsl_end - src_shader_background_fp_glsl_start)
#ifdef __cplusplus
}
#endif

View File

@ -0,0 +1,19 @@
#pragma once
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
extern uint32_t _binary_src_shader_background_vp_glsl_start __asm("_binary_src_shader_background_vp_glsl_start");
extern uint32_t _binary_src_shader_background_vp_glsl_end __asm("_binary_src_shader_background_vp_glsl_end");
extern uint32_t _binary_src_shader_background_vp_glsl_size __asm("_binary_src_shader_background_vp_glsl_size");
#define src_shader_background_vp_glsl_start ((const char *)&_binary_src_shader_background_vp_glsl_start)
#define src_shader_background_vp_glsl_end ((const char *)&_binary_src_shader_background_vp_glsl_end)
#define src_shader_background_vp_glsl_size (src_shader_background_vp_glsl_end - src_shader_background_vp_glsl_start)
#ifdef __cplusplus
}
#endif

View File

@ -8,9 +8,10 @@
#include "shader/vertex_color.fp.glsl.h" #include "shader/vertex_color.fp.glsl.h"
#include "shader/vertex_color.vp.glsl.h" #include "shader/vertex_color.vp.glsl.h"
#include "shader/font.fp.glsl.h" #include "shader/font.fp.glsl.h"
#include "shader/font.vp.glsl.h" #include "shader/font.vp.glsl.h"
#include "shader/background.fp.glsl.h"
#include "shader/background.vp.glsl.h"
#include "font/ter_u32n.data.h" #include "font/ter_u32n.data.h"
@ -24,8 +25,8 @@
//#include "model/cube.h" //#include "model/cube.h"
#include "model/plane.h" #include "model/plane.h"
int vp_width = 1600; int vp_width = 800;
int vp_height = 1200; int vp_height = 600;
void framebuffer_size_callback(GLFWwindow* window, int width, int height) void framebuffer_size_callback(GLFWwindow* window, int width, int height)
{ {
@ -100,24 +101,20 @@ int main()
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback); glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////
// buffer initialization // mesh initialization
////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////
/*
uint triangle_vertex_buffer = make_buffer(GL_ARRAY_BUFFER,
triangle_vertex_buffer_data,
(sizeof (triangle_vertex_buffer_data)));
*/
struct mesh paddle_mesh; struct mesh paddle_mesh;
struct mesh block_mesh; struct mesh block_mesh;
struct mesh ball_mesh; struct mesh ball_mesh;
struct mesh plane_mesh;
block_mesh.vtx = make_buffer(GL_ARRAY_BUFFER, block_vertices, (sizeof (block_vertices))); block_mesh.vtx = make_buffer(GL_ARRAY_BUFFER, block_vertices, (sizeof (block_vertices)));
block_mesh.idx = make_buffer(GL_ELEMENT_ARRAY_BUFFER, block_Cube_triangles, (sizeof (block_Cube_triangles))); block_mesh.idx = make_buffer(GL_ELEMENT_ARRAY_BUFFER, block_Cube_triangles, (sizeof (block_Cube_triangles)));
block_mesh.length = block_Cube_triangles_length; block_mesh.length = block_Cube_triangles_length;
//block_mesh.vtx = make_buffer(GL_ARRAY_BUFFER, cube_vertices, (sizeof (cube_vertices)));
//block_mesh.idx = make_buffer(GL_ELEMENT_ARRAY_BUFFER, cube_Cube_triangles, (sizeof (cube_Cube_triangles))); //cube_mesh.vtx = make_buffer(GL_ARRAY_BUFFER, cube_vertices, (sizeof (cube_vertices)));
//block_mesh.length = cube_Cube_triangles_length; //cube_mesh.idx = make_buffer(GL_ELEMENT_ARRAY_BUFFER, cube_Cube_triangles, (sizeof (cube_Cube_triangles)));
//cube_mesh.length = cube_Cube_triangles_length;
paddle_mesh.vtx = make_buffer(GL_ARRAY_BUFFER, paddle_vertices, (sizeof (paddle_vertices))); paddle_mesh.vtx = make_buffer(GL_ARRAY_BUFFER, paddle_vertices, (sizeof (paddle_vertices)));
paddle_mesh.idx = make_buffer(GL_ELEMENT_ARRAY_BUFFER, paddle_Cylinder_triangles, (sizeof (paddle_Cylinder_triangles))); paddle_mesh.idx = make_buffer(GL_ELEMENT_ARRAY_BUFFER, paddle_Cylinder_triangles, (sizeof (paddle_Cylinder_triangles)));
@ -127,11 +124,20 @@ int main()
ball_mesh.idx = make_buffer(GL_ELEMENT_ARRAY_BUFFER, ball_Icosphere_triangles, (sizeof (ball_Icosphere_triangles))); ball_mesh.idx = make_buffer(GL_ELEMENT_ARRAY_BUFFER, ball_Icosphere_triangles, (sizeof (ball_Icosphere_triangles)));
ball_mesh.length = ball_Icosphere_triangles_length; ball_mesh.length = ball_Icosphere_triangles_length;
plane_mesh.vtx = make_buffer(GL_ARRAY_BUFFER, plane_vertices, (sizeof (plane_vertices)));
plane_mesh.idx = make_buffer(GL_ELEMENT_ARRAY_BUFFER, plane_Plane_triangles, (sizeof (plane_Plane_triangles)));
plane_mesh.length = plane_Plane_triangles_length;
//////////////////////////////////////////////////////////////////////
// shaders
//////////////////////////////////////////////////////////////////////
//
uint program = compile_shader(src_shader_vertex_color_vp_glsl_start, uint program = compile_shader(src_shader_vertex_color_vp_glsl_start,
src_shader_vertex_color_vp_glsl_size, src_shader_vertex_color_vp_glsl_size,
src_shader_vertex_color_fp_glsl_start, src_shader_vertex_color_fp_glsl_start,
src_shader_vertex_color_fp_glsl_size); src_shader_vertex_color_fp_glsl_size);
glUseProgram(program);
uint attrib_position = glGetAttribLocation(program, "position"); uint attrib_position = glGetAttribLocation(program, "position");
uint attrib_texture = glGetAttribLocation(program, "_texture"); uint attrib_texture = glGetAttribLocation(program, "_texture");
uint attrib_normal = glGetAttribLocation(program, "normal"); uint attrib_normal = glGetAttribLocation(program, "normal");
@ -140,25 +146,29 @@ int main()
uint uniform_base_color = glGetUniformLocation(program, "base_color"); uint uniform_base_color = glGetUniformLocation(program, "base_color");
uint uniform_light_pos = glGetUniformLocation(program, "light_pos"); uint uniform_light_pos = glGetUniformLocation(program, "light_pos");
// // font
struct mesh plane_mesh;
plane_mesh.vtx = make_buffer(GL_ARRAY_BUFFER, plane_vertices, (sizeof (plane_vertices)));
plane_mesh.idx = make_buffer(GL_ELEMENT_ARRAY_BUFFER, plane_Plane_triangles, (sizeof (plane_Plane_triangles)));
plane_mesh.length = plane_Plane_triangles_length;
uint font_program = compile_shader(src_shader_font_vp_glsl_start, uint font_program = compile_shader(src_shader_font_vp_glsl_start,
src_shader_font_vp_glsl_size, src_shader_font_vp_glsl_size,
src_shader_font_fp_glsl_start, src_shader_font_fp_glsl_start,
src_shader_font_fp_glsl_size); src_shader_font_fp_glsl_size);
glUseProgram(font_program);
uint font__attrib_position = glGetAttribLocation(font_program, "position"); uint font__attrib_position = glGetAttribLocation(font_program, "position");
uint font__attrib_texture = glGetAttribLocation(font_program, "_texture"); uint font__attrib_texture = glGetAttribLocation(font_program, "_texture");
uint font__attrib_normal = glGetAttribLocation(font_program, "normal");
uint font__uniform_trans = glGetUniformLocation(font_program, "trans"); uint font__uniform_trans = glGetUniformLocation(font_program, "trans");
uint font__uniform_texture_trans = glGetUniformLocation(font_program, "texture_trans"); uint font__uniform_texture_trans = glGetUniformLocation(font_program, "texture_trans");
uint font__uniform_texture0 = glGetUniformLocation(font_program, "texture0"); uint font__uniform_texture0 = glGetUniformLocation(font_program, "texture0");
// background
uint bg_program = compile_shader(src_shader_background_vp_glsl_start,
src_shader_background_vp_glsl_size,
src_shader_background_fp_glsl_start,
src_shader_background_fp_glsl_size);
uint bg__attrib_position = glGetAttribLocation(bg_program, "position");
uint bg__uniform_resolution = glGetUniformLocation(bg_program, "resolution");
uint bg__uniform_trans = glGetUniformLocation(bg_program, "trans");
uint bg__uniform_time = glGetUniformLocation(bg_program, "time");
////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////
// textures // textures
////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////
@ -228,7 +238,7 @@ int main()
const char * name = glfwGetJoystickName(GLFW_JOYSTICK_1 + i); const char * name = glfwGetJoystickName(GLFW_JOYSTICK_1 + i);
if (name != last_gamepad_name) { if (name != last_gamepad_name) {
printf("active gamepad: `%s`; axes: %d\n", name, count); printf("active gamepad: `%s`; axes: 6 ; buttons: %d\n", name, count);
last_gamepad_name = name; last_gamepad_name = name;
} }
@ -259,6 +269,16 @@ int main()
update(&state, time); update(&state, time);
glDisable(GL_BLEND);
glDepthFunc(GL_ALWAYS);
glUseProgram(bg_program);
render_background(plane_mesh,
bg__attrib_position,
bg__uniform_resolution,
bg__uniform_trans,
bg__uniform_time,
&state);
glEnable(GL_BLEND); glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glDepthFunc(GL_GREATER); glDepthFunc(GL_GREATER);
@ -283,7 +303,6 @@ int main()
render_font(plane_mesh, render_font(plane_mesh,
font__attrib_position, font__attrib_position,
font__attrib_texture, font__attrib_texture,
font__attrib_normal,
font__uniform_trans, font__uniform_trans,
font__uniform_texture_trans, font__uniform_texture_trans,
font__uniform_texture0, font__uniform_texture0,

View File

@ -176,11 +176,12 @@ void render(mesh paddle_mesh,
{ {
mat4x4 rx = rotate_y(PI / 2.0f); mat4x4 rx = rotate_y(PI / 2.0f);
mat4x4 ry = rotate_x((float)state->time);
mat4x4 t = translate(vec3(state->paddle_x * 4.0f, -state->paddle_y * 2.0f, 0.0)); mat4x4 t = translate(vec3(state->paddle_x * 4.0f, -state->paddle_y * 2.0f, 0.0));
mat4x4 trans = a * t * rx; mat4x4 trans = a * t * ry * rx;
mat3x3 normal_trans = submatrix(rx, 3, 3); mat3x3 normal_trans = submatrix(ry * rx, 3, 3);
vec4 base_color = vec4(0.5f, 0.5f, 0.5f, 1.0f); vec4 base_color = vec4(0.5f, 0.5f, 0.5f, 1.0f);
//vec3 light_pos = vec3(-1, -1, 1); //vec3 light_pos = vec3(-1, -1, 1);
@ -227,12 +228,13 @@ void render(mesh paddle_mesh,
for (int i = 0; i < state->balls_launched; i++) { for (int i = 0; i < state->balls_launched; i++) {
struct ball_state& ball = state->balls[i]; struct ball_state& ball = state->balls[i];
mat4x4 rx = rotate_y(PI / 2.0f); mat4x4 rx = rotate_x((float)state->time);
mat4x4 ry = rotate_y((float)state->time * 0.5f);
vec3 ball_position = vec3(ball.ball_x * 4.0f, -ball.ball_y * 2.0f, 0.0); vec3 ball_position = vec3(ball.ball_x * 4.0f, -ball.ball_y * 2.0f, 0.0);
mat4x4 t = translate(ball_position); mat4x4 t = translate(ball_position);
mat4x4 trans = a * t * rx; mat4x4 trans = a * t * ry * rx;
mat3x3 normal_trans = submatrix(rx, 3, 3); mat3x3 normal_trans = submatrix(ry * rx, 3, 3);
//vec4 base_color = vec4(0.5f, 0.5f, 0.5f, 1.0f); //vec4 base_color = vec4(0.5f, 0.5f, 0.5f, 1.0f);
float hue = state->time - ball.launch_time; float hue = state->time - ball.launch_time;
hue = hue - floorf(hue); hue = hue - floorf(hue);
@ -309,7 +311,6 @@ static inline int max(int a, int b)
void render_font(struct mesh plane_mesh, void render_font(struct mesh plane_mesh,
uint attrib_position, uint attrib_position,
uint attrib_texture, uint attrib_texture,
uint attrib_normal,
uint uniform_trans, uint uniform_trans,
uint uniform_texture_trans, uint uniform_texture_trans,
uint uniform_texture0, uint uniform_texture0,
@ -332,16 +333,8 @@ void render_font(struct mesh plane_mesh,
(sizeof (float)) * 8, (sizeof (float)) * 8,
(void*)(3 * 4) (void*)(3 * 4)
); );
glVertexAttribPointer(attrib_normal,
3,
GL_FLOAT,
GL_FALSE,
(sizeof (float)) * 8,
(void*)(5 * 4)
);
glEnableVertexAttribArray(attrib_position); glEnableVertexAttribArray(attrib_position);
glEnableVertexAttribArray(attrib_texture); glEnableVertexAttribArray(attrib_texture);
glEnableVertexAttribArray(attrib_normal);
float aspect = (float)vp_height / (float)vp_width; float aspect = (float)vp_height / (float)vp_width;
@ -369,3 +362,32 @@ void render_font(struct mesh plane_mesh,
advance += grid_width; advance += grid_width;
} }
} }
void render_background(struct mesh plane_mesh,
uint attrib_position,
uint uniform_resolution,
uint uniform_trans,
uint uniform_time,
struct game_state * state)
{
glBindBuffer(GL_ARRAY_BUFFER, plane_mesh.vtx);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, plane_mesh.idx);
glVertexAttribPointer(attrib_position,
3,
GL_FLOAT,
GL_FALSE,
(sizeof (float)) * 8,
(void*)(0 * 4)
);
glEnableVertexAttribArray(attrib_position);
mat4x4 r = rotate_y(PI / 1.0f) * rotate_z(PI / 1.0f) * rotate_x(PI / 2.0f);
mat4x4 trans = r;
glUniform4fv(uniform_trans, 4, &trans[0][0]);
glUniform1f(uniform_time, state->time);
vec2 resolution = vec2(vp_width, vp_height);
glUniform2fv(uniform_resolution, 1, &resolution[0]);
glDrawElements(GL_TRIANGLES, plane_mesh.length, GL_UNSIGNED_INT, 0);
}

View File

@ -0,0 +1,21 @@
#version 120
varying vec2 fp_uv;
uniform float time;
vec3 palette(float d) {
vec3 v = d + vec3(0.25, 0.40625, 0.5625); // 40 45 49
v = v * 6.28318548202514648438;
v = cos(v);
return vec3(0.5, 0.5, 0.5) * v + vec3(0.5, 0.5, 0.5); // 48
}
void main()
{
vec2 uv = fp_uv;
float d = length(uv);
float td = time * 0.5 + d;
gl_FragColor = vec4(palette(td) * 0.2, 1);
}

View File

@ -0,0 +1,25 @@
#version 120
attribute vec3 position;
uniform vec4 trans[4];
uniform vec2 resolution;
varying vec2 fp_uv;
vec4 transform4(vec4 v)
{
return vec4(dot(trans[0], v),
dot(trans[1], v),
dot(trans[2], v),
dot(trans[3], v));
}
void main()
{
vec4 pos = transform4(vec4(position, 1));
fp_uv = vec2(pos.x * resolution.x / resolution.y, pos.y);
gl_Position = pos;
}

View File

@ -2,7 +2,6 @@
attribute vec3 position; attribute vec3 position;
attribute vec2 _texture; attribute vec2 _texture;
attribute vec3 normal;
varying vec2 fp_texture; varying vec2 fp_texture;
varying vec2 fp_texture_trans; varying vec2 fp_texture_trans;