From 64ed0d6551714ae18edc67078dcdae1ec20c9eb4 Mon Sep 17 00:00:00 2001 From: Szymon Szczubkowski Date: Thu, 8 Feb 2024 01:53:22 +0100 Subject: [PATCH] add: gamepad integration --- cw_8/grk-cw8.vcxproj | 6 ++++-- cw_8/src/projekt.hpp | 20 +++++++++++++++----- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/cw_8/grk-cw8.vcxproj b/cw_8/grk-cw8.vcxproj index 7dfffdb..e2577f6 100644 --- a/cw_8/grk-cw8.vcxproj +++ b/cw_8/grk-cw8.vcxproj @@ -43,6 +43,8 @@ + + @@ -58,14 +60,14 @@ Application true Unicode - v142 + v143 Application false true Unicode - v142 + v143 diff --git a/cw_8/src/projekt.hpp b/cw_8/src/projekt.hpp index 9bc62b2..6e46459 100644 --- a/cw_8/src/projekt.hpp +++ b/cw_8/src/projekt.hpp @@ -94,6 +94,8 @@ glm::vec3 spaceshipDir = glm::vec3(1.0f, 0.0f, 0.0f); float lastTime = -1.f; float deltaTime = 0.f; +GLFWgamepadstate currentState; + //declarations of functions void loadModelToContext(std::string path, Core::RenderContext& context); void framebuffer_size_callback(GLFWwindow* window, int width, int height); @@ -111,7 +113,8 @@ void drawObjectPBR(Core::RenderContext& context, glm::mat4 modelMatrix, GLuint a void processInput(GLFWwindow* window) { - if (!glfwJoystickPresent(GLFW_JOYSTICK_1)) { + + if (!glfwJoystickIsGamepad(GLFW_JOYSTICK_1)) { if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) glfwSetWindowShouldClose(window, true); @@ -124,9 +127,18 @@ void processInput(GLFWwindow* window) spaceshipDir = glm::vec3(glm::eulerAngleY(cameraSpeed) * glm::vec4(spaceshipDir, 0)); if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS) spaceshipDir = glm::vec3(glm::eulerAngleY(-cameraSpeed) * glm::vec4(spaceshipDir, 0)); - } - + }else if (glfwGetGamepadState(GLFW_JOYSTICK_1, ¤tState)) { + if(currentState.axes[GLFW_GAMEPAD_AXIS_RIGHT_TRIGGER] > 0.01) + spaceshipPos += deltaTime * 5 * spaceshipDir * currentState.axes[GLFW_GAMEPAD_AXIS_RIGHT_TRIGGER]; + if (currentState.axes[GLFW_GAMEPAD_AXIS_LEFT_TRIGGER] > 0.01) + spaceshipPos -= deltaTime * 5 * spaceshipDir * currentState.axes[GLFW_GAMEPAD_AXIS_LEFT_TRIGGER]; + if (abs(currentState.axes[GLFW_GAMEPAD_AXIS_LEFT_X]) > 0.01) + spaceshipDir = glm::vec3(glm::eulerAngleY(deltaTime * -5 * currentState.axes[GLFW_GAMEPAD_AXIS_LEFT_X]) * glm::vec4(spaceshipDir, 0)); + if (abs(currentState.axes[GLFW_GAMEPAD_AXIS_RIGHT_Y]) > 0.01) + spaceshipDir.y = currentState.axes[GLFW_GAMEPAD_AXIS_RIGHT_Y]/3; + } + std::cout << glm::to_string(spaceshipDir) << std::endl; cameraDir = spaceshipDir; cameraPos = spaceshipPos - 0.5 * spaceshipDir + glm::vec3(0, 1, 0) * 0.05f; } @@ -201,8 +213,6 @@ void drawSkybox(Core::RenderContext& context, glm::mat4 modelMatrix, GLuint text Core::DrawContext(context); glEnable(GL_DEPTH_TEST); - - }