diff --git a/Makefile b/Makefile index 98801a4..f147d3f 100644 --- a/Makefile +++ b/Makefile @@ -116,6 +116,8 @@ MAIN_OBJS = \ src/make.o \ src/shader.o \ src/render.o \ + src/state.o \ + src/input.o \ model/test_scene_color.data.o \ $(SHADER_OBJS) \ $(GLFW) diff --git a/include/input.h b/include/input.h new file mode 100644 index 0000000..9132ef2 --- /dev/null +++ b/include/input.h @@ -0,0 +1,11 @@ +#pragma once + +#ifdef __cplusplus +extern "C" { +#endif + + void input(); + +#ifdef __cplusplus +} +#endif diff --git a/include/render.h b/include/render.h index 7f49aa6..ce78f01 100644 --- a/include/render.h +++ b/include/render.h @@ -1,13 +1,10 @@ #pragma once -#include "state.h" - #ifdef __cplusplus extern "C" { #endif - void render(const struct state * state, - unsigned int program, + void render(unsigned int program, unsigned int program__trans, unsigned int program__texture0, unsigned int color, diff --git a/include/state.h b/include/state.h deleted file mode 100644 index 742f1d7..0000000 --- a/include/state.h +++ /dev/null @@ -1,18 +0,0 @@ -#pragma once - -#ifdef __cplusplus -extern "C" { -#endif - -struct state { - float rx; - float ry; - float rz; - float tx; - float ty; - float tz; -}; - -#ifdef __cplusplus -} -#endif diff --git a/include/state.hpp b/include/state.hpp new file mode 100644 index 0000000..356a5c3 --- /dev/null +++ b/include/state.hpp @@ -0,0 +1,10 @@ +#pragma once + +#include "math/float_types.hpp" + +struct state { + mat4x4 world_to_view; + int button[16]; +}; + +extern state g_state; diff --git a/src/input.cpp b/src/input.cpp new file mode 100644 index 0000000..d3531e8 --- /dev/null +++ b/src/input.cpp @@ -0,0 +1,62 @@ +#include +#include + +#include + +#include "math/transform.hpp" + +#include "input.h" +#include "state.hpp" + +static inline float deadzone(float n) +{ + if (fabsf(n) < 0.05f) { + return 0; + } else { + return n; + } +} + +static inline bool button_down(const GLFWgamepadstate& state, int i) +{ + bool down = state.buttons[i] && (g_state.button[i] != state.buttons[i]); + g_state.button[i] = state.buttons[i]; + return down; +} + +void input() +{ + for (int i = 0; i < 16; i++) { + GLFWgamepadstate state; + int jid = GLFW_JOYSTICK_1 + i; + int ret = glfwGetGamepadState(jid, &state); + if (ret == false) + continue; + + if (button_down(state, GLFW_GAMEPAD_BUTTON_START)) { + for (int i = 0; i < 4; i++) { + printf("%3.06f, %3.06f, %3.06f, %3.06f\n", + g_state.world_to_view[i][0], + g_state.world_to_view[i][1], + g_state.world_to_view[i][2], + g_state.world_to_view[i][3]); + } + } + + float lx = deadzone(state.axes[GLFW_GAMEPAD_AXIS_LEFT_X]); + float ly = deadzone(state.axes[GLFW_GAMEPAD_AXIS_LEFT_Y]); + float rx = deadzone(state.axes[GLFW_GAMEPAD_AXIS_RIGHT_X]); + float ry = deadzone(state.axes[GLFW_GAMEPAD_AXIS_RIGHT_Y]); + float tl = state.axes[GLFW_GAMEPAD_AXIS_LEFT_TRIGGER]; + float tr = state.axes[GLFW_GAMEPAD_AXIS_RIGHT_TRIGGER]; + float y = 0.01 * (tl - tr); + + g_state.world_to_view + = rotate_x(ly * 0.01f) + * rotate_y(lx * 0.01f) + * translate(vec3(rx * -0.01f, y, ry * -0.01f)) + * g_state.world_to_view; + + break; + } +} diff --git a/src/main.c b/src/main.c index 0fd29eb..9df0239 100644 --- a/src/main.c +++ b/src/main.c @@ -11,7 +11,7 @@ #include "make.h" #include "shader.h" #include "render.h" -#include "state.h" +#include "input.h" #include "model/test_scene.h" #include "model/test_scene_color.data.h" #include "shader/scene.vs.glsl.h" @@ -152,24 +152,15 @@ int main() const double first_frame = glfwGetTime(); double last_frame = first_frame; - struct state state = {}; - while (!glfwWindowShouldClose(window)) { if(glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) glfwSetWindowShouldClose(window, true); - if (glfwGetKey(window, GLFW_KEY_1) == GLFW_PRESS) - state.rx += 0.01f; - if (glfwGetKey(window, GLFW_KEY_2) == GLFW_PRESS) - state.rx -= 0.01f; - if (glfwGetKey(window, GLFW_KEY_3) == GLFW_PRESS) - state.ry += 0.01f; - if (glfwGetKey(window, GLFW_KEY_4) == GLFW_PRESS) - state.ry -= 0.01f; + + input(); glBindFramebuffer(GL_FRAMEBUFFER, framebuffer); - render(&state, - program, + render(program, program__trans, program__texture0, scene_color, @@ -178,8 +169,7 @@ int main() glBindFramebuffer(GL_FRAMEBUFFER, 0); - render(&state, - program, + render(program, program__trans, program__texture0, scene_color, diff --git a/src/render.cpp b/src/render.cpp index d043d59..a61e2c9 100644 --- a/src/render.cpp +++ b/src/render.cpp @@ -3,10 +3,12 @@ #include "glad/gl.h" -#include "render.h" #include "math/float_types.hpp" #include "math/transform.hpp" +#include "render.h" +#include "state.hpp" + mat4x4 perspective() { mat4x4 m1 = mat4x4(1, 0, 0, 0, @@ -17,15 +19,14 @@ mat4x4 perspective() return m1; } -void render(const struct state * state, - unsigned int program, +void render(unsigned int program, unsigned int program__trans, unsigned int program__texture0, unsigned int color, unsigned int vertex_array, int triangles_length) { - mat4x4 trans = perspective() * translate(vec3(0, 0, -2)) * scale(1.0f) * rotate_y(state->ry) * rotate_x(state->rx); + mat4x4 trans = perspective() * g_state.world_to_view * translate(vec3(0, 0, -2)) * scale(1.0f); glEnable(GL_DEPTH_TEST); glClearDepth(-1000.0f); diff --git a/src/state.cpp b/src/state.cpp new file mode 100644 index 0000000..243e243 --- /dev/null +++ b/src/state.cpp @@ -0,0 +1,10 @@ +#include "state.hpp" + +state g_state = { + .world_to_view = mat4x4( + 0.827267, -0.007020, -0.561762, -1.162349, + -0.375750, 0.736448, -0.562542, -1.042329, + 0.417658, 0.676455, 0.606601, -1.447194, + 0.000000, 0.000000, 0.000000, 1.000000 + ) +};