From 91d86a4f85732a6f689ec500d083751d3c634b80 Mon Sep 17 00:00:00 2001 From: Zack Buhman Date: Mon, 9 Mar 2026 23:24:34 -0500 Subject: [PATCH] use line points from eye position --- include/test.h | 3 ++- main.lua | 8 +++++-- shader/lighting.frag | 5 ++++- src/test.cpp | 50 +++++++++++++++++++++++++++++++++----------- 4 files changed, 50 insertions(+), 16 deletions(-) diff --git a/include/test.h b/include/test.h index da9779a..380d938 100644 --- a/include/test.h +++ b/include/test.h @@ -9,7 +9,8 @@ extern "C" { void kb_update(int up, int down, int left, int right); void update(float lx, float ly, float rx, float ry, float tl, float tr, int up, int down, int left, int right, - int a, int b, int x, int y); + int a, int b, int x, int y, + int leftshoulder, int rightshoulder); #ifdef __cplusplus } diff --git a/main.lua b/main.lua index b9b1425..f2c1986 100644 --- a/main.lua +++ b/main.lua @@ -11,7 +11,8 @@ void draw(); void kb_update(int kbup, int kbdown, int kbleft, int kbright); void update(float lx, float ly, float rx, float ry, float tl, float tr, int up, int down, int left, int right, - int a, int b, int x, int y); + int a, int b, int x, int y, + int leftshoulder, int rightshoulder); ]] local source_path = love.filesystem.getSource() test = ffi.load(source_path .. "/test.so") @@ -35,10 +36,13 @@ local update = function(dt) local b = joystick:isGamepadDown("b") local x = joystick:isGamepadDown("x") local y = joystick:isGamepadDown("y") + local leftshoulder = joystick:isGamepadDown("leftshoulder") + local rightshoulder = joystick:isGamepadDown("rightshoulder") test.update(lx, ly, rx, ry, tl, tr, up, down, left, right, - a, b, x, y) + a, b, x, y, + leftshoulder, rightshoulder) end local up = love.keyboard.isDown("up") diff --git a/shader/lighting.frag b/shader/lighting.frag index 291f122..ed27865 100644 --- a/shader/lighting.frag +++ b/shader/lighting.frag @@ -32,9 +32,12 @@ void main() //float attenuation = 1.0 / (1.0 + Linear * light_distance + Quadratic * light_distance * light_distance); float attenuation = 1.0 / (1.0 + Quadratic * light_distance * light_distance); - out_color += color.xyz * attenuation * diffuse; + //out_color += color.xyz * attenuation * diffuse; //out_color = vec3(diffuse); } + vec3 light_direction = normalize(Eye.xyz - position.xyz); + float diffuse = max(dot(normal.xyz, light_direction), 0.0); + out_color = color.xyz * diffuse; Color = vec4(out_color, 1.0); } diff --git a/src/test.cpp b/src/test.cpp index b7bdddd..77b2a90 100644 --- a/src/test.cpp +++ b/src/test.cpp @@ -470,7 +470,7 @@ static_assert((sizeof (short_point)) == 6); short_point line_points[128]; static int line_point_ix = 0; -void line_point(int x, int y, int z) +void append_line_point(int x, int y, int z) { if (line_point_ix >= 128) return; @@ -543,22 +543,38 @@ void load_line_instance_buffer() glBindBuffer(GL_ARRAY_BUFFER, 0); } +struct line_state { + struct { + int x; + int y; + int z; + } point[2]; +}; +static line_state line_state = {}; + void load_line() { - int x1 = -55; - int z1 = 48; - int y1 = 50; - - int x2 = -60; - int z2 = 55; - int y2 = 53; - line_point_ix = 0; - bresenham(x1, y1, z1, x2, y2, z2, line_point); + bresenham(line_state.point[0].x, line_state.point[0].y, line_state.point[0].z, + line_state.point[1].x, line_state.point[1].y, line_state.point[1].z, + append_line_point); load_line_instance_buffer(); } +void load_line_point_from_eye(int point_ix) +{ + int x = XMVectorGetX(view_state.eye); + int y = XMVectorGetY(view_state.eye); + int z = XMVectorGetZ(view_state.eye); + + line_state.point[point_ix].x = x; + line_state.point[point_ix].y = z; + line_state.point[point_ix].z = y; + + load_line(); +} + void load(const char * source_path) { g_source_path_length = strlen(source_path); @@ -612,7 +628,6 @@ void load(const char * source_path) load_line_vertex_attributes(); glGenBuffers(1, &line_instance_buffer); - load_line(); } float _ry = 0.0; @@ -635,7 +650,8 @@ void kb_update(int up, int down, int left, int right) void update(float lx, float ly, float rx, float ry, float tl, float tr, int up, int down, int left, int right, - int a, int b, int x, int y) + int a, int b, int x, int y, + int leftshoulder, int rightshoulder) { //view_state.yaw += rx; XMMATRIX mrz = XMMatrixRotationZ(rx * -0.035); @@ -662,6 +678,13 @@ void update(float lx, float ly, float rx, float ry, float tl, float tr, lighting.linear += 0.01 * x + -0.01 * y; if (lighting.linear < 0.0f) lighting.linear = 0.0f; + + if (leftshoulder) { + load_line_point_from_eye(0); + } + if (rightshoulder) { + load_line_point_from_eye(1); + } } static inline int popcount(int x) @@ -860,6 +883,9 @@ void draw_lighting() void draw_line() { + if (line_point_ix == 0) + return; + glUseProgram(line_program); glBlendFunc(GL_ONE, GL_ZERO);