add font rendering

This commit is contained in:
Zack Buhman 2025-12-05 10:09:21 -06:00
parent 916041667b
commit 5c535bc176
22 changed files with 445 additions and 52 deletions

View File

@ -73,19 +73,19 @@ endef
%.glsl.o: %.glsl %.glsl.o: %.glsl
$(BUILD_BINARY_O) $(BUILD_BINARY_O)
include/shader/%.glsl.h: src/shader/%.glsl include/%.glsl.h: src/%.glsl
$(BUILD_BINARY_H) $(BUILD_BINARY_H)
%.data.o: %.data %.data.o: %.data
$(BUILD_BINARY_O) $(BUILD_BINARY_O)
include/level/%.data.h: src/level/%.data include/%.data.h: src/%.data
$(BUILD_BINARY_H) $(BUILD_BINARY_H)
%.data.pal.o: %.data.pal %.data.pal.o: %.data.pal
$(BUILD_BINARY_O) $(BUILD_BINARY_O)
include/level/%.data.pal.h: src/level/%.data.pal include/%.data.pal.h: src/%.data.pal
$(BUILD_BINARY_H) $(BUILD_BINARY_H)
clean: clean:
@ -104,8 +104,10 @@ MAIN_OBJS = \
src/opengl.o \ src/opengl.o \
src/render.o \ src/render.o \
src/collision.o \ src/collision.o \
src/update.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.pal,%.data.pal.o,$(wildcard src/level/*.data.pal)) \ $(patsubst %.data.pal,%.data.pal.o,$(wildcard src/level/*.data.pal)) \
$(GLFW) $(GLFW)

Binary file not shown.

BIN
blend/plane.blend Normal file

Binary file not shown.

View File

@ -0,0 +1,19 @@
#pragma once
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
extern uint32_t _binary_src_font_ter_u32n_data_start __asm("_binary_src_font_ter_u32n_data_start");
extern uint32_t _binary_src_font_ter_u32n_data_end __asm("_binary_src_font_ter_u32n_data_end");
extern uint32_t _binary_src_font_ter_u32n_data_size __asm("_binary_src_font_ter_u32n_data_size");
#define src_font_ter_u32n_data_start ((const char *)&_binary_src_font_ter_u32n_data_start)
#define src_font_ter_u32n_data_end ((const char *)&_binary_src_font_ter_u32n_data_end)
#define src_font_ter_u32n_data_size (src_font_ter_u32n_data_end - src_font_ter_u32n_data_start)
#ifdef __cplusplus
}
#endif

View File

@ -149,7 +149,7 @@ template <typename T>
inline constexpr T inverse_length(vec<3, T> v) inline constexpr T inverse_length(vec<3, T> v)
{ {
float f = dot(v, v); float f = dot(v, v);
return 1.0f / sqrt(f); return 1.0f / sqrt<T>(f);
} }
template <typename T> template <typename T>

View File

@ -23,7 +23,7 @@ inline constexpr vec<L, T> normalize(vec<L, T> const& v)
} }
template <int L, typename T> template <int L, typename T>
inline constexpr vec<3, T> reflect(vec<L, T> const& i, vec<L, T> const& n) inline constexpr vec<L, T> reflect(vec<L, T> const& i, vec<L, T> const& n)
{ {
return i - dot(n, i) * n * static_cast<T>(2.0); return i - dot(n, i) * n * static_cast<T>(2.0);
} }

18
include/model/plane.h Normal file
View File

@ -0,0 +1,18 @@
#pragma once
const int plane_Plane_triangles[] = {
0, 1, 2,
0, 3, 1,
};
const int plane_Plane_triangles_length = (sizeof (plane_Plane_triangles)) / (sizeof (plane_Plane_triangles[0]));
const float plane_vertices[] = {
1.000000f, 0.000000f, 1.000000f, 1.000000f, 0.000000f, -0.0000f, 1.0000f, -0.0000f,
-1.000000f, 0.000000f, -1.000000f, 0.000000f, 1.000000f, -0.0000f, 1.0000f, -0.0000f,
-1.000000f, 0.000000f, 1.000000f, 0.000000f, 0.000000f, -0.0000f, 1.0000f, -0.0000f,
1.000000f, 0.000000f, -1.000000f, 1.000000f, 1.000000f, -0.0000f, 1.0000f, -0.0000f,
};
const int plane_vertices_length = (sizeof (plane_vertices)) / (sizeof (plane_vertices[0]));

16
include/model/plane.obj Normal file
View File

@ -0,0 +1,16 @@
# Blender 4.4.3
# www.blender.org
mtllib plane.mtl
o Plane
v -1.000000 0.000000 1.000000
v 1.000000 0.000000 1.000000
v -1.000000 0.000000 -1.000000
v 1.000000 0.000000 -1.000000
vn -0.0000 1.0000 -0.0000
vt 1.000000 0.000000
vt 0.000000 1.000000
vt 0.000000 0.000000
vt 1.000000 1.000000
s 0
f 2/1/1 3/2/1 1/3/1
f 2/1/1 4/4/1 3/2/1

View File

@ -26,6 +26,13 @@ extern "C" {
uint uniform_light_pos, uint uniform_light_pos,
struct game_state * state); struct game_state * state);
void render_font(struct mesh plane_mesh,
uint attrib_position,
uint attrib_texture,
uint attrib_normal,
uint uniform_trans,
uint uniform_texture0);
#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_font_fp_glsl_start __asm("_binary_src_shader_font_fp_glsl_start");
extern uint32_t _binary_src_shader_font_fp_glsl_end __asm("_binary_src_shader_font_fp_glsl_end");
extern uint32_t _binary_src_shader_font_fp_glsl_size __asm("_binary_src_shader_font_fp_glsl_size");
#define src_shader_font_fp_glsl_start ((const char *)&_binary_src_shader_font_fp_glsl_start)
#define src_shader_font_fp_glsl_end ((const char *)&_binary_src_shader_font_fp_glsl_end)
#define src_shader_font_fp_glsl_size (src_shader_font_fp_glsl_end - src_shader_font_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_font_vp_glsl_start __asm("_binary_src_shader_font_vp_glsl_start");
extern uint32_t _binary_src_shader_font_vp_glsl_end __asm("_binary_src_shader_font_vp_glsl_end");
extern uint32_t _binary_src_shader_font_vp_glsl_size __asm("_binary_src_shader_font_vp_glsl_size");
#define src_shader_font_vp_glsl_start ((const char *)&_binary_src_shader_font_vp_glsl_start)
#define src_shader_font_vp_glsl_end ((const char *)&_binary_src_shader_font_vp_glsl_end)
#define src_shader_font_vp_glsl_size (src_shader_font_vp_glsl_end - src_shader_font_vp_glsl_start)
#ifdef __cplusplus
}
#endif

13
include/update.hpp Normal file
View File

@ -0,0 +1,13 @@
#pragma once
#include "state.h"
#ifdef __cplusplus
extern "C" {
#endif
void update(struct game_state * state);
#ifdef __cplusplus
}
#endif

View File

@ -6,5 +6,6 @@ python ~/model_generator2/generate_indexed_cpp2.py paddle.obj paddle > paddle.h
python ~/model_generator2/generate_indexed_cpp2.py block.obj block > block.h python ~/model_generator2/generate_indexed_cpp2.py block.obj block > block.h
python ~/model_generator2/generate_indexed_cpp2.py ball.obj ball > ball.h python ~/model_generator2/generate_indexed_cpp2.py ball.obj ball > ball.h
python ~/model_generator2/generate_indexed_cpp2.py cube.obj cube > cube.h python ~/model_generator2/generate_indexed_cpp2.py cube.obj cube > cube.h
python ~/model_generator2/generate_indexed_cpp2.py plane.obj plane > plane.h
rm -f *.mtl rm -f *.mtl

View File

@ -1,4 +1,5 @@
#include <math.h> #include <math.h>
#include <stdio.h>
#include "collision.hpp" #include "collision.hpp"
@ -51,8 +52,8 @@ vec2 _t0_t1(float signed_distance,
t0 = t1; t0 = t1;
t1 = tmp; t1 = tmp;
} }
t0 = clamp(t0, 0.0, 1.0); t0 = clamp(t0, -1.0, 1.0);
t1 = clamp(t0, 0.0, 1.0); t1 = clamp(t0, -1.0, 1.0);
return vec2(t0, t1); return vec2(t0, t1);
} }
} }
@ -76,14 +77,96 @@ bool point_in_triangle(vec3 point,
float y = (e * a) - (d * b); float y = (e * a) - (d * b);
float z = x + y - ((a * c) - (b * b)); float z = x + y - ((a * c) - (b * b));
return (z < 0) and not ((x < 0) or (y < 0)); return (z < 0) && !((x < 0) || (y < 0));
} }
vec2 collision_inner(vec3 plane_normal, struct ct {
vec3 a, vec3 b, vec3 c, bool collision;
vec3 point, float t;
vec3 velocity) };
#define NONE -99999999.0f
ct lowest_root(float a,
float b,
float c,
float max_r)
{ {
float determinant = b * b - 4.0f * a * c;
if (determinant < 0.0f)
return ct(false, max_r);
float sqrt_det = sqrtf(determinant);
float r_2a = 1.0f / (2.0f * a);
float r1 = (-b - sqrt_det) * r_2a;
float r2 = (-b + sqrt_det) * r_2a;
if (r1 > r2) {
float tmp = r1;
r1 = r2;
r2 = tmp;
}
if (r1 > 0 && (max_r == NONE || r1 < max_r))
return ct(true, r1);
if (r2 > 0 && (max_r == NONE || r2 < max_r))
return ct(true, r2);
return ct(false, max_r);
}
ct triangle_point_collide(vec3 tri_point,
vec3 point,
vec3 velocity,
float t)
{
float a = dot(velocity, velocity);
float b = 2.0f * dot(velocity, point - tri_point);
vec3 c0 = (tri_point - point);
float c = dot(c0, c0) - 1.0f;
return lowest_root(a, b, c, t);
}
struct ctf0 {
bool collision;
float t;
float f0;
};
ctf0 triangle_edge_collide(vec3 tri_point1,
vec3 tri_point2,
vec3 point,
vec3 velocity,
float t)
{
vec3 edge = tri_point2 - tri_point1;
vec3 base = tri_point1 - point;
float edge_dot_edge = dot(edge, edge);
float edge_dot_velocity = dot(edge, velocity);
float edge_dot_base = dot(edge, base);
float velocity_dot_velocity = dot(velocity, velocity);
float a = edge_dot_edge * -velocity_dot_velocity + (edge_dot_velocity * edge_dot_velocity);
float b = edge_dot_edge * 2.0f * (dot(velocity, base)) - 2.0f * edge_dot_velocity * edge_dot_base;
float c = edge_dot_edge * (1.0f - dot(base, base)) + (edge_dot_base * edge_dot_base);
ct res = lowest_root(a, b, c, t);
float f0 = NONE;
if (res.collision) {
f0 = (edge_dot_velocity * res.t - edge_dot_base) * (1.0f / edge_dot_edge);
}
return ctf0(res.collision, t, f0);
}
struct t0t1pip {
float t0;
float t1;
vec3 pip;
};
t0t1pip collision_inner(vec3 a, vec3 b, vec3 c,
vec3 point,
vec3 velocity)
{
vec3 plane_normal = _plane_normal(a, b, c);
vec3 plane_origin = a; vec3 plane_origin = a;
float plane_d = _plane_d(plane_normal, plane_origin); float plane_d = _plane_d(plane_normal, plane_origin);
float signed_distance = _signed_distance(plane_normal, plane_d, point); float signed_distance = _signed_distance(plane_normal, plane_d, point);
@ -91,29 +174,60 @@ vec2 collision_inner(vec3 plane_normal,
float n_dot_v = dot(plane_normal, velocity); float n_dot_v = dot(plane_normal, velocity);
float r_n_dot_v = 1.0f / n_dot_v; float r_n_dot_v = 1.0f / n_dot_v;
//bool embedded = n_dot_v == 0.0f; bool embedded = n_dot_v == 0.0f;
if (n_dot_v == 0.0f) { if (n_dot_v == 0.0f) {
if (fabsf(signed_distance) >= 1.0f) if (fabsf(signed_distance) >= 1.0)
return false; return t0t1pip(NONE, NONE, vec3(0, 0, 0));
} }
vec2 t0t1 = _t0_t1(signed_distance, r_n_dot_v);
vec2 t0_t1 = _t0_t1(signed_distance, r_n_dot_v); float t0 = t0t1.x;
float t0 = t0_t1.x; float t1 = t0t1.y;
//float t1 = t0_t1.y;
vec3 plane_intersection_point = point - plane_normal + t0 * velocity; vec3 plane_intersection_point = point - plane_normal + t0 * velocity;
bool inside = point_in_triangle(plane_intersection_point, a, b, c);
float inside_f = inside ? 1.0f : 0.0f;
return vec2(inside_f, t0); bool inside = point_in_triangle(plane_intersection_point, a, b, c);
bool collision = false;
vec3 collision_point;
float t = NONE;
vec3 abc[] = {a, b, c};
for (int i = 0; i < 3; i++) {
vec3 tri_point = abc[i];
ct res = triangle_point_collide(tri_point, point, velocity, t);
t = res.t;
if (res.collision) {
collision_point = tri_point;
collision = true;
}
}
for (int i = 0; i < 3; i++) {
vec3 p1 = abc[i];
vec3 p2 = abc[(i + 1) % 3];
ctf0 res = triangle_edge_collide(p1, p2, point, velocity, t);
if (res.collision && res.f0 >= 0.0 && res.f0 <= 1.0) {
t = res.t;
collision_point = p1 + res.f0 * (p2 - p1);
collision = true;
}
}
if (inside && collision) {
printf("r_n_dot_v %f %f %f %f\n", n_dot_v, r_n_dot_v, t0, t1);
return t0t1pip(t0, t1, plane_intersection_point);
}
else
return t0t1pip(NONE, NONE, vec3(0, 0, 0));
} }
vec4 collision(mat4x4 trans, vec4 collision(mat4x4 trans,
vec3 point, vec3 velocity) vec3 point, vec3 velocity)
{ {
bool collision = false; bool collision = false;
float smallest = 0.0f; float smallest = -9999.0f;
vec3 smallest_normal = vec3(0, 0, 0); vec3 smallest_normal = vec3(0, 0, 0);
//printf("velocity %f %f %f\n", velocity.x, velocity.y, velocity.z);
for (int i = 0; i < cube_Cube_triangles_length / 3; i++) { for (int i = 0; i < cube_Cube_triangles_length / 3; i++) {
const float * a = &cube_vertices[cube_Cube_triangles[i * 3 + 0] * 8]; const float * a = &cube_vertices[cube_Cube_triangles[i * 3 + 0] * 8];
@ -124,27 +238,27 @@ vec4 collision(mat4x4 trans,
vec3 bp = trans * vec3(b[0], b[1], b[2]); vec3 bp = trans * vec3(b[0], b[1], b[2]);
vec3 cp = trans * vec3(c[0], c[1], c[2]); vec3 cp = trans * vec3(c[0], c[1], c[2]);
vec3 plane_normal = _plane_normal(ap, bp, cp); t0t1pip res = collision_inner(ap, bp, cp, point, velocity);
bool inside = res.t0 != NONE;
vec2 res = collision_inner(plane_normal, ap, bp, cp, point, velocity);
bool inside = res.x == 1.0f;
float t0 = res.y;
if (!inside) if (!inside)
continue; continue;
if (collision == false || t0 < smallest) { if (collision == false || res.t0 < smallest) {
smallest = t0; printf("new smallest %f %f\n", smallest, res.t0);
smallest = res.t0;
vec3 plane_normal = _plane_normal(ap, bp, cp);
smallest_normal = plane_normal; smallest_normal = plane_normal;
} }
collision = true; collision = true;
} }
float collision_f = collision ? 1.0 : 0.0; //float collision_f = collision ? 1.0 : 0.0;
return vec4(smallest_normal.x, return vec4(smallest_normal.x,
smallest_normal.y, smallest_normal.y,
smallest_normal.z, smallest_normal.z,
collision_f); collision ? smallest : -9999.0f);
} }
/* /*

BIN
src/font/ter_u32n.data Normal file

Binary file not shown.

View File

@ -9,13 +9,20 @@
#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.vp.glsl.h"
#include "font/ter_u32n.data.h"
#include "opengl.h" #include "opengl.h"
#include "render.hpp" #include "render.hpp"
#include "update.hpp"
#include "model/block.h" #include "model/block.h"
#include "model/paddle.h" #include "model/paddle.h"
#include "model/ball.h" #include "model/ball.h"
//#include "model/cube.h" //#include "model/cube.h"
#include "model/plane.h"
int vp_width = 1600; int vp_width = 1600;
int vp_height = 1200; int vp_height = 1200;
@ -116,6 +123,34 @@ 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");
//
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,
src_shader_font_vp_glsl_size,
src_shader_font_fp_glsl_start,
src_shader_font_fp_glsl_size);
glUseProgram(font_program);
uint font__attrib_position = glGetAttribLocation(font_program, "position");
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_texture0 = glGetUniformLocation(font_program, "texture0");
//////////////////////////////////////////////////////////////////////
// textures
//////////////////////////////////////////////////////////////////////
uint terminus_font = make_texture(src_font_ter_u32n_data_start,
GL_RED,
256,
256,
GL_RED);
////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////
// main loop // main loop
////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////
@ -142,7 +177,6 @@ int main()
glEnable(GL_DEPTH_TEST); glEnable(GL_DEPTH_TEST);
glClearDepth(-1000.0f); glClearDepth(-1000.0f);
glDepthFunc(GL_GREATER);
glClearColor(0.1, 0.2, 0.3, 1.0); glClearColor(0.1, 0.2, 0.3, 1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
@ -164,7 +198,8 @@ int main()
float left = axes[2] * 0.5 + 0.5; float left = axes[2] * 0.5 + 0.5;
float right = axes[5] * 0.5 + 0.5; float right = axes[5] * 0.5 + 0.5;
paddle_dx = right - left; float sensitivity = 0.4f;
paddle_dx = (right - left) * sensitivity;
//state.ball_dx = deadzone(axes[0]); //state.ball_dx = deadzone(axes[0]);
//state.ball_dy = deadzone(axes[1]); //state.ball_dy = deadzone(axes[1]);
@ -173,12 +208,13 @@ int main()
} }
float extent = 0.25; float extent = 0.25;
state.paddle_x += paddle_dx * 0.4; state.paddle_x += paddle_dx;
if (state.paddle_x < extent) if (state.paddle_x < extent)
state.paddle_x = extent; state.paddle_x = extent;
if (state.paddle_x > 12 - extent) if (state.paddle_x > 12 - extent)
state.paddle_x = 12 - extent; state.paddle_x = 12 - extent;
//state.paddle_y += paddle_dy * 0.4;
update(&state);
if ((state.ball_x + state.ball_dx * 0.4) > 12.25f) { if ((state.ball_x + state.ball_dx * 0.4) > 12.25f) {
state.ball_x = 12.25f; state.ball_x = 12.25f;
@ -186,8 +222,6 @@ int main()
} else if ((state.ball_x + state.ball_dx * 0.4) < -0.25f) { } else if ((state.ball_x + state.ball_dx * 0.4) < -0.25f) {
state.ball_x = -0.25f; state.ball_x = -0.25f;
state.ball_dx = -state.ball_dx; state.ball_dx = -state.ball_dx;
} else {
state.ball_x += state.ball_dx * 0.4;
} }
if ((state.ball_y + state.ball_dy * 0.4) > 27.0f) { if ((state.ball_y + state.ball_dy * 0.4) > 27.0f) {
@ -196,10 +230,11 @@ int main()
} else if ((state.ball_y + state.ball_dy * 0.4) < 0.0f) { } else if ((state.ball_y + state.ball_dy * 0.4) < 0.0f) {
state.ball_y = 0.0f; state.ball_y = 0.0f;
state.ball_dy = -state.ball_dy; state.ball_dy = -state.ball_dy;
} else {
state.ball_y += state.ball_dy * 0.4;
} }
glDepthFunc(GL_GREATER);
glUseProgram(program);
render(paddle_mesh, render(paddle_mesh,
block_mesh, block_mesh,
ball_mesh, ball_mesh,
@ -212,6 +247,17 @@ int main()
uniform_light_pos, uniform_light_pos,
&state); &state);
glDepthFunc(GL_ALWAYS);
glUseProgram(font_program);
render_font(plane_mesh,
font__attrib_position,
font__attrib_texture,
font__attrib_normal,
font__uniform_trans,
font__uniform_texture0
);
glfwSwapBuffers(window); glfwSwapBuffers(window);
glfwPollEvents(); glfwPollEvents();

View File

@ -69,3 +69,24 @@ int make_buffer(unsigned int target,
glBufferData(target, size, data, GL_STATIC_DRAW); glBufferData(target, size, data, GL_STATIC_DRAW);
return buffer; return buffer;
} }
int make_texture(const void * data,
int internalformat,
int width,
int height,
int format)
{
unsigned int texture;
glGenTextures(1, &texture);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, internalformat, width, height, 0, format, GL_UNSIGNED_BYTE, data);
return texture;
}

View File

@ -6,3 +6,9 @@ unsigned int compile_shader(const void * vp,
int make_buffer(unsigned int target, int make_buffer(unsigned int target,
const void * data, const void * data,
size_t size); size_t size);
int make_texture(const void * data,
int internalformat,
int width,
int height,
int format);

View File

@ -139,8 +139,8 @@ void render(mesh paddle_mesh,
for (int y = 0; y < 28; y++) { for (int y = 0; y < 28; y++) {
for (int x = 0; x < 13; x++) { for (int x = 0; x < 13; x++) {
char tile = level[y * 13 + x]; char tile = level[y * 13 + x];
//if (tile == 0) if (tile == 0)
//continue; continue;
const float cs = 1.0f / 255.0f; const float cs = 1.0f / 255.0f;
vec3 base_color = vec3(((float)pal[tile * 3 + 0]) * cs, vec3 base_color = vec3(((float)pal[tile * 3 + 0]) * cs,
@ -152,15 +152,6 @@ void render(mesh paddle_mesh,
mat4x4 trans = a * t * rx; mat4x4 trans = a * t * rx;
vec4 res = collision(t * rx,
vec3(state->ball_x * 4.0f, -state->ball_y * 2.0f, 0),
vec3(0, 0, 0));
bool collision = res.w == 1.0f;
if (collision) {
//vec3 normal = vec3(res.x, res.y, res.z);
base_color = vec3(1, 0, 0);
}
//mat3x3 normal_trans = transpose(inverse(submatrix(trans, 0, 0))); //mat3x3 normal_trans = transpose(inverse(submatrix(trans, 0, 0)));
mat3x3 normal_trans = submatrix(rx, 3, 3); mat3x3 normal_trans = submatrix(rx, 3, 3);
@ -173,7 +164,6 @@ void render(mesh paddle_mesh,
} }
} }
////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////
// render paddle // render paddle
////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////
@ -275,3 +265,48 @@ void render(mesh paddle_mesh,
glDrawElements(GL_TRIANGLES, paddle_mesh.length, GL_UNSIGNED_INT, 0); glDrawElements(GL_TRIANGLES, paddle_mesh.length, GL_UNSIGNED_INT, 0);
} }
} }
void render_font(struct mesh plane_mesh,
uint attrib_position,
uint attrib_texture,
uint attrib_normal,
uint uniform_trans,
uint uniform_texture0)
{
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)
);
glVertexAttribPointer(attrib_texture,
2,
GL_FLOAT,
GL_FALSE,
(sizeof (float)) * 8,
(void*)(3 * 4)
);
glVertexAttribPointer(attrib_normal,
3,
GL_FLOAT,
GL_FALSE,
(sizeof (float)) * 8,
(void*)(5 * 4)
);
glEnableVertexAttribArray(attrib_position);
glEnableVertexAttribArray(attrib_texture);
glEnableVertexAttribArray(attrib_normal);
mat4x4 r = rotate_y(PI / 1.0f) * rotate_z(PI / 1.0f) * rotate_x(PI / 2.0f);
mat4x4 trans = scale(0.5f) * r;
glUniform4fv(uniform_trans, 4, &trans[0][0]);
glUniform1i(uniform_texture0, 0);
glDrawElements(GL_TRIANGLES, plane_mesh.length, GL_UNSIGNED_INT, 0);
}

15
src/shader/font.fp.glsl Normal file
View File

@ -0,0 +1,15 @@
#version 120
uniform sampler2D texture0;
varying vec2 fp_texture;
void main()
{
vec4 c = texture2D(texture0, fp_texture);
float i = c.x == 0 ? 0.0 : 1.0;
gl_FragColor = vec4(i, i, i, 1.0);
//gl_FragColor = vec4(fp_texture.xy, 1, 1.0);
}

26
src/shader/font.vp.glsl Normal file
View File

@ -0,0 +1,26 @@
#version 120
attribute vec3 position;
attribute vec2 _texture;
attribute vec3 normal;
varying vec2 fp_texture;
uniform vec4 trans[4];
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_texture = _texture;
gl_Position = pos;
}

16
src/update.cpp Normal file
View File

@ -0,0 +1,16 @@
#include <math.h>
#include <stdio.h>
#include "update.hpp"
#include "collision.hpp"
#include "state.h"
#include "level/level1.data.h"
#include "level/level1.data.pal.h"
#include "math/float_types.hpp"
#include "math/transform.hpp"
void update(struct game_state * state)
{
}