feat(input): Add mouse input
This commit is contained in:
61
src/input.c
61
src/input.c
@@ -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;
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
#include <time.h>
|
||||
|
||||
void __window_callback_input(GLFWwindow *, int, int);
|
||||
void __cursor_callback_input(GLFWwindow *, double, double);
|
||||
void __mouse_callback_input(GLFWwindow *, int, int, int);
|
||||
void __scroll_callback_input(GLFWwindow *, double, double);
|
||||
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));
|
||||
|
||||
glfwSetWindowSizeCallback((GLFWwindow *)window, __window_callback_input);
|
||||
glfwSetCursorPosCallback((GLFWwindow *)window, __cursor_callback_input);
|
||||
glfwSetMouseButtonCallback((GLFWwindow *)window, __mouse_callback_input);
|
||||
glfwSetScrollCallback((GLFWwindow *)window, __scroll_callback_input);
|
||||
glfwSetKeyCallback((GLFWwindow *)window, __key_callback_input);
|
||||
|
||||
Reference in New Issue
Block a user