ToyEngine/Exp2/ModelWorld.cpp

77 lines
2.5 KiB
C++
Raw Normal View History

2023-06-03 01:04:32 +08:00
#include "ModelWorld.h"
#include <RenderingSystem.h>
ModelWorld::ModelWorld()
{
light.lightDirection = glm::normalize(glm::vec3((cos(lightPitch) * cos(lightYaw)), (sin(lightPitch)), (cos(lightPitch) * sin(lightYaw))));
light.radiance = 3.f * lightColor;
RenderingSystem::instance().exposure = 0.45f;
2023-06-04 00:59:02 +08:00
camera.Position = { 0,5,10 };
2023-06-03 01:04:32 +08:00
model = std::make_shared<Actor>("Models/David_Brown_25D_tractor_SF/David_Brown_25D_tractor_SF.obj");
model->setPosition({ 0,0,0 });
model->setScale(glm::vec3(0.5));
addActor(model);
}
void ModelWorld::cursorPosCallback(GLFWwindow* window, double xpos, double ypos)
{
static bool firstMouse = true;
static float lastX;
static float lastY;
float xoffset = xpos - lastX;
float yoffset = lastY - ypos;
lastX = xpos;
lastY = ypos;
if (firstMouse)
{
firstMouse = false;
return;
}
if (glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_RIGHT) == GLFW_PRESS)
camera.processMouseMovement(xoffset, yoffset);
if (glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_LEFT) == GLFW_PRESS)
{
float speed = 0.2f;
model->setRotaion(glm::rotate(glm::quat(1, 0, 0, 0),
glm::radians(glm::length(glm::vec2(xoffset, yoffset)) * speed), glm::vec3(glm::vec2(-yoffset, xoffset), 0.0)) * model->getRotation());
}
}
void ModelWorld::scrollCallback(GLFWwindow* window, double xoffset, double yoffset)
{
camera.processMouseScroll(yoffset);
}
void ModelWorld::processInput(GLFWwindow* window, float deltaTime)
{
if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS)
camera.processKeyboard(FORWARD, deltaTime);
if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS)
camera.processKeyboard(BACKWARD, deltaTime);
if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS)
camera.processKeyboard(LEFT, deltaTime);
if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS)
camera.processKeyboard(RIGHT, deltaTime);
if (glfwGetKey(window, GLFW_KEY_SPACE) == GLFW_PRESS)
camera.processKeyboard(UP, deltaTime);
if (glfwGetKey(window, GLFW_KEY_LEFT_SHIFT) == GLFW_PRESS)
camera.processKeyboard(DOWN, deltaTime);
2023-06-04 00:59:02 +08:00
float speed = 10;
float d = speed * deltaTime;
2023-06-03 01:04:32 +08:00
if (glfwGetKey(window, GLFW_KEY_UP) == GLFW_PRESS)
2023-06-04 00:59:02 +08:00
model->setPosition(model->getPosition() + glm::vec3(0, 0, -d));
2023-06-03 01:04:32 +08:00
if (glfwGetKey(window, GLFW_KEY_DOWN) == GLFW_PRESS)
2023-06-04 00:59:02 +08:00
model->setPosition(model->getPosition() + glm::vec3(0, 0, d));
2023-06-03 01:04:32 +08:00
if (glfwGetKey(window, GLFW_KEY_LEFT) == GLFW_PRESS)
2023-06-04 00:59:02 +08:00
model->setPosition(model->getPosition() + glm::vec3(-d, 0, 0));
2023-06-03 01:04:32 +08:00
if (glfwGetKey(window, GLFW_KEY_RIGHT) == GLFW_PRESS)
2023-06-04 00:59:02 +08:00
model->setPosition(model->getPosition() + glm::vec3(d, 0, 0));
2023-06-03 01:04:32 +08:00
}