feat(input): Add mouse input

This commit is contained in:
PedroEdiaz
2025-05-11 15:31:04 -06:00
parent 64a6f60b6d
commit fbd7821c12
2 changed files with 40 additions and 23 deletions

View File

@@ -10,9 +10,9 @@ unsigned char selected_axis = 0;
int window_width;
int window_height;
static versor q = GLM_QUAT_IDENTITY_INIT;
unsigned char animate_index = 0;
versor q = GLM_QUAT_IDENTITY_INIT;
vec3 axis[3] = {
{1, 0, 0},
@@ -22,12 +22,23 @@ vec3 axis[3] = {
extern struct projection projection;
static inline
void __input_update_q(versor p)
{
glm_quat_mul(p, q, q);
glm_quat_rotatev(p, axis[0], axis[0]);
glm_quat_rotatev(p, axis[1], axis[1]);
glm_quat_rotatev(p, axis[2], axis[2]);
glm_quat_normalize(q);
}
void __error_callback_input(int x, const char * msg )
{
mlog("[GLFW] ");
mlog(msg);
mlog("\n");
}
void __key_callback_input(
GLFWwindow *window, int key, int scancode, int action, int mods)
{
@@ -93,26 +104,32 @@ void __window_callback_input(GLFWwindow *window, int w, int h)
glViewport((w - m) / 2, (h - m) / 2, m, m);
}
void __mouse_callback_input(
GLFWwindow *window, int button, int action, int mods)
static double xpos_s, ypos_s;
static char lbutton_down = 0;
void __mouse_callback_input(GLFWwindow* window, int button, int action, int mods)
{
unsigned char green_value;
double xpos, ypos;
if( button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_PRESS)
lbutton_down = 1;
if (button != GLFW_MOUSE_BUTTON_LEFT || action != GLFW_PRESS)
return;
glfwGetCursorPos(window, &xpos, &ypos);
glReadPixels((int)xpos, (int)(window_height - ypos), 1, 1, GL_GREEN,
GL_UNSIGNED_BYTE, &green_value);
switch (green_value)
if( button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_RELEASE)
{
case 0xD3:
case 0xD4:
case 0xD5:
selected_axis = green_value - 0xD3;
glfwGetCursorPos(window, &xpos_s, &ypos_s);
lbutton_down = 0;
}
}
void __cursor_callback_input(
GLFWwindow *window, double xpos, double ypos)
{
if( lbutton_down )
{
versor p = GLM_QUAT_IDENTITY_INIT;
glm_quatv(p, ANGLE, (vec3){-1*(ypos-ypos_s),(xpos-xpos_s),0});
__input_update_q(p);
}
}
void __scroll_callback_input(GLFWwindow *window, double xoffset, double yoffset)
@@ -210,12 +227,7 @@ quat_t poll_input(window_t window)
}
end:
glm_quat_mul(p, q, q);
glm_quat_rotatev(p, axis[0], axis[0]);
glm_quat_rotatev(p, axis[1], axis[1]);
glm_quat_rotatev(p, axis[2], axis[2]);
glm_quat_normalize(q);
/*
// LOG INFO
if (0)
{
@@ -224,5 +236,8 @@ end:
projection.z, projection.w);
printf("\n");
}
*/
__input_update_q(p);
return q;
}