mouse controll

This commit is contained in:
Eikthyrnir 2023-11-24 04:05:56 +01:00
parent 6e48ee3fc2
commit 0267879884

View File

@ -28,12 +28,20 @@ glm::vec3 cameraDir = glm::vec3(1.f, 0.f, 0.f);
glm::vec3 spaceshipPos = glm::vec3(-4.f, 0, 0); glm::vec3 spaceshipPos = glm::vec3(-4.f, 0, 0);
glm::vec3 spaceshipDir = glm::vec3(1.f, 0.f, 0.f); glm::vec3 spaceshipDir = glm::vec3(1.f, 0.f, 0.f);
glm::vec3 spaceshipUp = glm::vec3(0.f, 1.f, 0.f);
GLuint VAO,VBO; GLuint VAO,VBO;
float aspectRatio = 1.f; float aspectRatio = 1.f;
float lastFrameTime = -1.f;
float deltaTime = 0.f;
glm::vec3 lightColor = glm::vec3(0.9, 0.7, 0.8); glm::vec3 lightColor = glm::vec3(0.9, 0.7, 0.8);
bool moveCamWithMouse = false;
glm::vec2 posMousePressed;
glm::vec2 posMouseReleased;
glm::mat4 createCameraMatrix() glm::mat4 createCameraMatrix()
{ {
glm::vec3 cameraSide = glm::normalize(glm::cross(cameraDir,glm::vec3(0.f,1.f,0.f))); glm::vec3 cameraSide = glm::normalize(glm::cross(cameraDir,glm::vec3(0.f,1.f,0.f)));
@ -100,6 +108,9 @@ void renderScene(GLFWwindow* window)
glm::mat4 transformation; glm::mat4 transformation;
float time = glfwGetTime(); float time = glfwGetTime();
deltaTime = time - lastFrameTime;
deltaTime = glm::min(deltaTime, 0.1f);
lastFrameTime = time;
glUseProgram(program); glUseProgram(program);
//planet //planet
@ -155,9 +166,50 @@ void loadModelToContext(std::string path, Core::RenderContext& context)
context.initFromAssimpMesh(scene->mMeshes[0]); context.initFromAssimpMesh(scene->mMeshes[0]);
} }
void cursor_position_callback(GLFWwindow* window, double xpos, double ypos)
{
if (moveCamWithMouse) {
glm::vec3 spaceshipSide = glm::normalize(glm::cross(spaceshipDir, spaceshipUp));
//int winWidth, winHeight;
//glfwGetWindowSize(window, &winWidth, &winHeight);
//glm::vec2 currMouseWorldPos = glm::vec2(2 * xpos / winWidth - 1, -(2 * ypos / winHeight - 1));
//glm::vec2 lastMouseWorldPos = glm::vec2(2 * posMousePressed.x / winWidth - 1, -(2 * posMousePressed.y / winHeight - 1));
//glm::vec3 lastPosOnClickPlane = spaceshipPos + 1.5f * spaceshipDir
// + lastMouseWorldPos.x * spaceshipSide
// + lastMouseWorldPos.y * spaceshipUp;
//glm::vec3 currPosOnClickPlane = spaceshipPos + 1.5f * spaceshipDir
// + currMouseWorldPos.x * spaceshipSide
// + currMouseWorldPos.y * spaceshipUp;
//spaceshipDir = glm::normalize(spaceshipDir - (currPosOnClickPlane - lastPosOnClickPlane) / 10.f);
glm::vec3 diff = spaceshipSide * (xpos - posMousePressed.x) - spaceshipUp * (ypos - posMousePressed.y);
spaceshipDir = glm::normalize(spaceshipDir - diff / 1000.f);
posMousePressed = glm::vec2(xpos, ypos);
}
}
void mouse_button_callback(GLFWwindow* window, int button, int action, int mods)
{
double xpos, ypos;
glfwGetCursorPos(window, &xpos, &ypos);
posMousePressed = glm::vec2(xpos, ypos);
if (button == GLFW_MOUSE_BUTTON_1 && action == GLFW_PRESS) {
moveCamWithMouse = true;
}
if (button == GLFW_MOUSE_BUTTON_1 && action == GLFW_RELEASE) {
moveCamWithMouse = false;
}
}
void init(GLFWwindow* window) void init(GLFWwindow* window)
{ {
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback); glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
glfwSetCursorPosCallback(window, cursor_position_callback);
glfwSetMouseButtonCallback(window, mouse_button_callback);
glEnable(GL_DEPTH_TEST); glEnable(GL_DEPTH_TEST);
program = shaderLoader.CreateProgram("shaders/shader_4_1.vert", "shaders/shader_4_1.frag"); program = shaderLoader.CreateProgram("shaders/shader_4_1.vert", "shaders/shader_4_1.frag");
@ -175,8 +227,8 @@ void shutdown(GLFWwindow* window)
void processInput(GLFWwindow* window) void processInput(GLFWwindow* window)
{ {
glm::vec3 spaceshipSide = glm::normalize(glm::cross(spaceshipDir, glm::vec3(0.f, 1.f, 0.f))); glm::vec3 spaceshipSide = glm::normalize(glm::cross(spaceshipDir, glm::vec3(0.f, 1.f, 0.f)));
float angleSpeed = 0.005f; float angleSpeed = 3.f * deltaTime;
float moveSpeed = 0.005f; float moveSpeed = 3.f * deltaTime;
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) { if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) {
glfwSetWindowShouldClose(window, true); glfwSetWindowShouldClose(window, true);
} }