"""
#define STB_IMAGE_IMPLEMENTATION
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include
#include <math.h>
#include "Shader.h"
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include
#include "stb_image.h"
// Camera parameters
glm::vec3 cameraPos = glm::vec3(0.0f, 0.0f, 3.0f);
glm::vec3 cameraFront = glm::vec3(0.0f, 0.0f, -1.0f);
glm::vec3 cameraUp = glm::vec3(0.0f, 1.0f, 0.0f);
// Delta time
float deltaTime = 0.0f;
float lastFrame = 0.0f;
// Mouse parameters
float lastX = 400;
float lastY = 300;
const float sensitivity = 0.001f;
float yaw = -90.0f;
float pitch = 0.0f;
bool firstMouse = true;
float zoom = 45.0f;
// Light
glm::vec3 lightPos(1.2f, 5.0f, 8.0f);
void framebuffer_size_callback(GLFWwindow* window, int width, int height)
{
glViewport(0, 0, width, height);
}
void mouse_callback(GLFWwindow* window, double xPos, double yPos)
{
// if (glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_LEFT) == GLFW_PRESS)
// {
if (firstMouse)
{
lastX = xPos;
lastY = yPos;
firstMouse = false;
}
float xOffset = xPos - lastX;
float yOffset = lastY - yPos; // y-axis is reversed
lastX = xPos;
lastY = yPos;
yaw += xOffset * sensitivity;
pitch += yOffset * sensitivity;
if (pitch > 89.0f) pitch = 89.0f;
if (pitch < -89.0f) pitch = -89.0f;
glm::vec3 direction;
direction.x = cos(glm::radians(yaw)) * cos(glm::radians(pitch));
direction.y = sin(glm::radians(pitch));
direction.z = sin(glm::radians(yaw)) * cos(glm::radians(pitch));
cameraFront = glm::normalize(direction);
// }
}
void scroll_callback(GLFWwindow* window, double xoffset, double yoffset)
{
zoom -= 2.0f * (float) yoffset;
if (zoom < 1.0f) zoom = 1.0f;
if (zoom > 45.0f) zoom = 45.0f;
}
void processInput(GLFWwindow* window)
{
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
glfwSetWindowShouldClose(window, true);
const float cameraSpeed = 5.0f * deltaTime;
if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS) cameraPos += cameraSpeed * cameraFront;
if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS) cameraPos -= cameraSpeed * cameraFront;
if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS) cameraPos += cameraSpeed * glm::normalize(glm::cross(cameraFront, cameraUp));
if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS) cameraPos -= cameraSpeed * glm::normalize(glm::cross(cameraFront, cameraUp));
if (glfwGetKey(window, GLFW_KEY_E) == GLFW_PRESS) cameraPos += cameraSpeed * cameraUp;
if (glfwGetKey(window, GLFW_KEY_Q) == GLFW_PRESS) cameraPos -= cameraSpeed * cameraUp;
}
int main()
{
// Initialize GLFW
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
// Create a window
GLFWwindow* window = glfwCreateWindow(800, 600, "GLFW Window", NULL, NULL);
if (window == NULL)
{
std::cout << "Failed to create GLFW window\n";
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
// Initialize GLAD
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
{
std::cout << "Failed to initialize GLAD\n";
return -1;
}
// Define viewport
glViewport(0, 0, 800, 600);
// Handle resizing
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
// Capture the cursor
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
glfwSetCursorPosCallback(window, mouse_callback);
glfwSetScrollCallback(window, scroll_callback);
.
.
.
// Main loop
while (!glfwWindowShouldClose(window))
{
// Input
processInput(window);
// Clear background
glClearColor(0.80f, 0.80f, 0.80f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Objects:
// Deploy shaders
objectShaders.use();
// objectShaders.setVec3("material.ambient", 0.05375f, 0.05f, 0.06625);
// objectShaders.setVec3("material.diffuse", 0.18275, 0.17, 0.22525);
objectShaders.setInt("material.diffuse", 0);
objectShaders.setVec3("material.specular", 0.332741f/2.0f, 0.328634f/2.0f, 0.346435f/2.0f);
objectShaders.setFloat("material.shininess", 128.0f*0.3f);
objectShaders.setVec3("lightColor", 1.0f, 1.0f, 1.0f);
objectShaders.setVec3("lightPos", lightPos);
objectShaders.setVec3("light.ambient", 0.3f, 0.3f, 0.3f);
objectShaders.setVec3("light.diffuse", 0.5f, 0.5f, 0.5f);
objectShaders.setVec3("light.specular", 1.0f, 1.0f, 1.0f);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, diffuseMap);
// Define delta time
float currentFrame = glfwGetTime();
deltaTime = currentFrame - lastFrame;
lastFrame = currentFrame;
// Define camera and matrices
glm::mat4 view = glm::lookAt(cameraPos, cameraPos + cameraFront, cameraUp);
glm::mat4 projection = glm::mat4(1.0f);
projection = glm::perspective(glm::radians(zoom), 800.0f / 600.0f, 0.1f, 100.0f);
int projectionLoc = glGetUniformLocation(objectShaders.ID, "projection");
glUniformMatrix4fv(projectionLoc, 1, GL_FALSE, glm::value_ptr(projection));
int viewLoc = glGetUniformLocation(objectShaders.ID, "view");
glUniformMatrix4fv(viewLoc, 1, GL_FALSE, glm::value_ptr(view));
glm::mat4 model = glm::mat4(1.0f);
objectShaders.setMat4("model", model);
objectShaders.setVec3("viewPos", cameraPos);
// Render objects
glBindVertexArray(VAO);
glDrawArrays(GL_TRIANGLES, 0, 36);
// Lighting:
lightShaders.use();
projectionLoc = glGetUniformLocation(lightShaders.ID, "projection");
glUniformMatrix4fv(projectionLoc, 1, GL_FALSE, glm::value_ptr(projection));
viewLoc = glGetUniformLocation(lightShaders.ID, "view");
glUniformMatrix4fv(viewLoc, 1, GL_FALSE, glm::value_ptr(view));
model = glm::mat4(1.0f);
model = glm::translate(model, lightPos);
model = glm::scale(model, glm::vec3(0.1f));
lightShaders.setMat4("model", model);
glBindVertexArray(lightVAO);
glDrawArrays(GL_TRIANGLES, 0, 36);
// Check and call events and swap the buffers
glfwPollEvents();
glfwSwapBuffers(window);
}
// Terminate GLFW
glfwTerminate();
return 0;
}
"""
There is a problem with my mouse camera controls. Looking around kind of works, but there is something wrong. It currently works like this: any time movement is detected right of the center of the screen, the camera turns right, even if the mouse is moving left. It's similar for all directions - camera movement does not depend on which way the mouse is moving, but on which side of the screen it's on. It's supposed to work like any FPS camera, where moving in a direction turns the camera in that direction irrespective of the mouse position. What's wrong?