Add x, y, z keys to change rotation axis

This commit is contained in:
Camden Dixie O'Brien committed 2025-09-27 15:50:05 +01:00
1 parent f265d3777d
commit d7d2462835
1 file changed
+37 -4
+37 -4
View File
@@ -21,6 +21,14 @@ static const vertex_t vertices[] = {
static const uint16_t indices[] = { 0, 1, 2, 2, 3, 0 };
typedef enum {
AXIS_X,
AXIS_Y,
AXIS_Z,
} axis_t;
static axis_t rot_axis = AXIS_Y;
// clang-format off
#define ID \
{ \
@@ -78,10 +86,22 @@ key_callback(GLFWwindow *window, int key, int scancode, int action, int mods)
(void)scancode;
(void)mods;
if (action != GLFW_PRESS)
return;
switch (key) {
case GLFW_KEY_Q:
if (action == GLFW_PRESS)
glfwSetWindowShouldClose(window, GL_TRUE);
glfwSetWindowShouldClose(window, GL_TRUE);
break;
case GLFW_KEY_X:
rot_axis = AXIS_X;
break;
case GLFW_KEY_Y:
rot_axis = AXIS_Y;
break;
case GLFW_KEY_Z:
rot_axis = AXIS_Z;
break;
}
}
@@ -144,13 +164,26 @@ int main()
};
persp(mvp.proj, (float)W / H, M_PI / 6, 0.1, 10.0);
const mat4_t rot = ROT_Y(M_PI / 128);
const mat4_t rotx = ROT_X(M_PI / 128);
const mat4_t roty = ROT_Y(M_PI / 128);
const mat4_t rotz = ROT_Z(M_PI / 128);
while (!glfwWindowShouldClose(window)) {
glfwPollEvents();
mat4_t tmp;
memcpy(tmp, mvp.model, sizeof(mat4_t));
mul(mvp.model, rot, tmp);
switch (rot_axis) {
case AXIS_X:
mul(mvp.model, rotx, tmp);
break;
case AXIS_Y:
mul(mvp.model, roty, tmp);
break;
case AXIS_Z:
mul(mvp.model, rotz, tmp);
break;
}
gpu_render(&gpu, &mvp);
}