demo: draw ballistics

This commit is contained in:
Zack Buhman 2025-07-16 17:55:43 -05:00
parent 719c97c9e4
commit eaba4457c0
6 changed files with 145 additions and 30 deletions

View File

@ -12,7 +12,9 @@ DEMO_OBJ = \
$(LIB)/printf/parse.o \ $(LIB)/printf/parse.o \
src/demo/demo.o \ src/demo/demo.o \
src/demo/graphics.o \ src/demo/graphics.o \
src/demo/input.o src/demo/input.o \
src/demo/ballistics.o \
src/physics/particle.o \
demo.elf: LDSCRIPT = $(LIB)/main.lds demo.elf: LDSCRIPT = $(LIB)/main.lds
demo.elf: $(START_OBJ) $(DEMO_OBJ) $(LIBGCC) demo.elf: $(START_OBJ) $(DEMO_OBJ) $(LIBGCC)

View File

@ -1,12 +1,20 @@
#include "physics/particle.hpp" #include "physics/particle.hpp"
#include "demo/ballistics.hpp" #include "demo/ballistics.hpp"
#include "math/transform.hpp"
#include "demo/graphics_primitive.hpp"
namespace demo { namespace demo {
void ballistics::projectile::draw(ta_parameter_writer& writer, const mat4x4& trans)
{
const vec3 color(1.0, 1.0, 1.0);
draw_cube(writer, trans * translate(particle.position) * scale(1.f), color);
}
void ballistics::init() void ballistics::init()
{ {
current_projectile_type = projectile_type::PISTOL; current_projectile_type = projectile_type::ARTILLERY;
for (int i = 0; i < max_projectiles; i++) { for (int i = 0; i < max_projectiles; i++) {
projectiles[i].type = projectile_type::NONE; projectiles[i].type = projectile_type::NONE;
@ -60,6 +68,7 @@ namespace demo {
//projectile->start_time = FIXME; //projectile->start_time = FIXME;
projectile->type = current_projectile_type; projectile->type = current_projectile_type;
//projectile->particle.clear_accumulator(); FIXME //projectile->particle.clear_accumulator(); FIXME
projectile->particle.force_accum = vec3(0, 0, 0);
} }
void ballistics::update() void ballistics::update()
@ -72,10 +81,22 @@ namespace demo {
continue; continue;
projectile->particle.integrate(duration); projectile->particle.integrate(duration);
if (projectile->particle.position.z > 200.0f ||
projectile->particle.position.y < 0.0f) {
projectile->type = projectile_type::NONE;
}
} }
} }
void ballistics::draw() void ballistics::draw(ta_parameter_writer& writer, const mat4x4& trans)
{ {
for (int i = 0; i < max_projectiles; i++) {
projectile * projectile = &projectiles[i];
if (projectile->type == projectile_type::NONE)
continue;
projectile->draw(writer, trans);
}
} }
} }

View File

@ -21,9 +21,7 @@ namespace demo {
projectile_type type; projectile_type type;
uint32_t start_time; uint32_t start_time;
void draw() void draw(ta_parameter_writer& writer, const mat4x4& trans);
{
}
}; };
constexpr static int max_projectiles = 32; constexpr static int max_projectiles = 32;
@ -33,6 +31,6 @@ namespace demo {
void init() override; void init() override;
void fire() override; void fire() override;
void update() override; void update() override;
void draw() override; void draw(ta_parameter_writer& writer, const mat4x4& trans) override;
}; };
}; };

View File

@ -89,18 +89,16 @@ namespace graphics {
} }
} }
demo::ballistics b;
void draw() void draw()
{ {
global_polygon_intensity(writer, {1.0, 0.0, 0.0});
quad_type_2(writer,
{10, 10, 1},
{90, 10, 1},
{90, 90, 1},
{10, 90, 1},
1.0);
draw_grid(writer, view_trans); draw_grid(writer, view_trans);
draw_axis(writer, view_trans);
//draw_cube(writer, view_trans * scale(0.1f), {1, 1, 0});
b.draw(writer, view_trans * scale(0.05f));
writer.append<ta_global_parameter::end_of_list>() = writer.append<ta_global_parameter::end_of_list>() =
ta_global_parameter::end_of_list(para_control::para_type::end_of_list); ta_global_parameter::end_of_list(para_control::para_type::end_of_list);
@ -111,6 +109,8 @@ namespace graphics {
core_init(); core_init();
framebuffer::scaler_init(); framebuffer::scaler_init();
b.init();
// read // read
while (spg_status::vsync(holly.SPG_STATUS)); while (spg_status::vsync(holly.SPG_STATUS));
while (!spg_status::vsync(holly.SPG_STATUS)); while (!spg_status::vsync(holly.SPG_STATUS));
@ -148,18 +148,19 @@ namespace graphics {
texture_memory_alloc.region_array.start, texture_memory_alloc.region_array.start,
texture_memory_alloc.object_list.start); texture_memory_alloc.object_list.start);
vec3 t = {framebuffer::framebuffer.px_width / 2.f, framebuffer::framebuffer.px_height / 2.f, 0}; //vec3 t = {framebuffer::framebuffer.px_width / 2.f, framebuffer::framebuffer.px_height / 2.f, 0};
float s = framebuffer::framebuffer.px_height / 3.f; float s = framebuffer::framebuffer.px_height / 3.f;
view_trans view_trans
= translate(t) = scale(vec3(s, s, 1))
* scale(vec3(s, s, 1))
* translate(vec3(0, 0, 10)) * translate(vec3(0, 0, 10))
* scale(vec3(-1, -1, 1)); * scale(vec3(-1, -1, 1));
} }
void view_transform() void view_transform()
{ {
static int last_a[4] = {};
for (int port_ix = 0; port_ix < 4; port_ix++) { for (int port_ix = 0; port_ix < 4; port_ix++) {
auto& port = input::state.port[port_ix]; auto& port = input::state.port[port_ix];
if ((port.function_type & function_type::controller) != 0 && port.host_response_data_transfer_ft0 != nullptr) { if ((port.function_type & function_type::controller) != 0 && port.host_response_data_transfer_ft0 != nullptr) {
@ -173,14 +174,23 @@ namespace graphics {
float dx = static_cast<float>(data.analog_coordinate_axis[2] - 0x80) * 0.001; float dx = static_cast<float>(data.analog_coordinate_axis[2] - 0x80) * 0.001;
float dy = static_cast<float>(data.analog_coordinate_axis[3] - 0x80) * 0.001; float dy = static_cast<float>(data.analog_coordinate_axis[3] - 0x80) * 0.001;
bool a = ft0::data_transfer::digital_button::a(data.digital_button) == 0;
if (last_a[port_ix] == 0 && a) {
b.fire();
}
last_a[port_ix] = a;
view_trans = view_trans * rotate_x(dx) * rotate_y(dy); view_trans = view_trans * rotate_x(dx) * rotate_y(dy);
} }
} }
} }
static int tick = 0;
void step() void step()
{ {
view_transform(); view_transform();
b.update();
writer.offset = 0; writer.offset = 0;
draw(); draw();

View File

@ -1,12 +1,12 @@
#include "ta_parameter_presets.hpp" #include "ta_parameter_presets.hpp"
#include "math/math.hpp" #include "math/math.hpp"
#include "holly/framebuffer.hpp"
#define _fsrra(n) (1.0f / (sqrt<float>(n))) #define _fsrra(n) (1.0f / (sqrt<float>(n)))
static inline void draw_line(ta_parameter_writer& writer, static inline void draw_line(ta_parameter_writer& writer,
const vec3& p1, const vec3& p1,
const vec3& p2, const vec3& p2)
const vec3& color)
{ {
float dy = p2.y - p1.y; float dy = p2.y - p1.y;
float dx = p2.x - p1.x; float dx = p2.x - p1.x;
@ -30,27 +30,108 @@ static inline void draw_line(ta_parameter_writer& writer,
static inline vec3 screen_transform(const vec3& v) static inline vec3 screen_transform(const vec3& v)
{ {
return {v.x, v.y, _fsrra(v.z)}; float tx = framebuffer::framebuffer.px_width / 2.f;
float ty = framebuffer::framebuffer.px_height / 2.f;
float z = _fsrra(v.z - 9);
float z2 = z * z;
return {v.x * z2 + tx , v.y * z2 + ty, z2};
} }
static inline void draw_grid(ta_parameter_writer& writer, static inline void draw_grid(ta_parameter_writer& writer,
const mat4x4& trans) const mat4x4& trans)
{ {
const vec3 color = {0, 1, 0}; const vec3 color = {0, 1, 1};
global_polygon_intensity(writer, color); global_polygon_intensity(writer, color);
for (int i = 1; i < 10; i++) { for (int i = 1; i < 10; i++) {
float x = (float)i * 0.1f - 0.5f; float x = (float)i * 0.1f - 0.5f;
{ {
vec3 p1 = screen_transform(trans * vec3(x, 0.5, 0)); vec3 p1 = screen_transform(trans * vec3(x, 0, 0.5));
vec3 p2 = screen_transform(trans * vec3(x, -0.5, 0)); vec3 p2 = screen_transform(trans * vec3(x, 0, -0.5));
draw_line(writer, p1, p2, color); draw_line(writer, p1, p2);
} }
{ {
vec3 p1 = screen_transform(trans * vec3(0.5, x, 0)); vec3 p1 = screen_transform(trans * vec3(0.5, 0, x));
vec3 p2 = screen_transform(trans * vec3(-0.5, x, 0)); vec3 p2 = screen_transform(trans * vec3(-0.5, 0, x));
draw_line(writer, p1, p2, color); draw_line(writer, p1, p2);
} }
} }
} }
static inline void draw_axis(ta_parameter_writer& writer,
const mat4x4& trans)
{
vec3 origin = screen_transform(trans * vec3(0, 0, 0));
vec3 x_axis = screen_transform(trans * vec3(1, 0, 0));
vec3 y_axis = screen_transform(trans * vec3(0, 1, 0));
vec3 z_axis = screen_transform(trans * vec3(0, 0, 1));
global_polygon_intensity(writer, {1, 0, 0});
draw_line(writer,
origin,
x_axis);
global_polygon_intensity(writer, {0, 1, 0});
draw_line(writer,
origin,
y_axis);
global_polygon_intensity(writer, {0, 0, 1});
draw_line(writer,
origin,
z_axis);
}
static inline void draw_cube(ta_parameter_writer& writer,
const mat4x4& trans,
const vec3& color)
{
static const vec3 position[] = {
{-1.0, -1.0, 1.0},
{-1.0, 1.0, 1.0},
{-1.0, -1.0, -1.0},
{-1.0, 1.0, -1.0},
{1.0, -1.0, 1.0},
{1.0, 1.0, 1.0},
{1.0, -1.0, -1.0},
{1.0, 1.0, -1.0},
};
const int position_length = (sizeof (position)) / (sizeof (position[0]));
struct quad {
int a, b, c, d;
};
static const quad quads[] = {
{0, 1, 3, 2},
{2, 3, 7, 6},
{6, 7, 5, 4},
{4, 5, 1, 0},
{2, 6, 4, 0},
{7, 3, 1, 5}
};
vec3 cache[position_length];
for (int i = 0; i < position_length; i++) {
cache[i] = screen_transform(trans * position[i]);
}
global_polygon_intensity(writer, color);
for (int i = 0; i < 6; i++) {
const vec3& ap = cache[quads[i].a];
const vec3& bp = cache[quads[i].b];
const vec3& cp = cache[quads[i].c];
const vec3& dp = cache[quads[i].d];
quad_type_2(writer,
ap,
bp,
cp,
dp,
1.0f);
}
}

View File

@ -1,11 +1,14 @@
#pragma once #pragma once
#include "holly/ta_parameter.hpp"
#include "math/float_types.hpp"
namespace demo { namespace demo {
struct scene { struct scene {
virtual void init() = 0; virtual void init() = 0;
virtual void fire() = 0; virtual void fire() = 0;
virtual void update() = 0; virtual void update() = 0;
virtual void draw() = 0; virtual void draw(ta_parameter_writer& writer, const mat4x4& trans) = 0;
}; };
}; };