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

View File

@@ -4,6 +4,7 @@
#include <time.h> #include <time.h>
void __window_callback_input(GLFWwindow *, int, int); void __window_callback_input(GLFWwindow *, int, int);
void __cursor_callback_input(GLFWwindow *, double, double);
void __mouse_callback_input(GLFWwindow *, int, int, int); void __mouse_callback_input(GLFWwindow *, int, int, int);
void __scroll_callback_input(GLFWwindow *, double, double); void __scroll_callback_input(GLFWwindow *, double, double);
void __key_callback_input(GLFWwindow *, int, int, int, int); void __key_callback_input(GLFWwindow *, int, int, int, int);
@@ -69,6 +70,7 @@ window_t init_window(unsigned int width, unsigned int height, const char *title)
glfwMakeContextCurrent((GLFWwindow *)(window)); glfwMakeContextCurrent((GLFWwindow *)(window));
glfwSetWindowSizeCallback((GLFWwindow *)window, __window_callback_input); glfwSetWindowSizeCallback((GLFWwindow *)window, __window_callback_input);
glfwSetCursorPosCallback((GLFWwindow *)window, __cursor_callback_input);
glfwSetMouseButtonCallback((GLFWwindow *)window, __mouse_callback_input); glfwSetMouseButtonCallback((GLFWwindow *)window, __mouse_callback_input);
glfwSetScrollCallback((GLFWwindow *)window, __scroll_callback_input); glfwSetScrollCallback((GLFWwindow *)window, __scroll_callback_input);
glfwSetKeyCallback((GLFWwindow *)window, __key_callback_input); glfwSetKeyCallback((GLFWwindow *)window, __key_callback_input);