Browse Source

Move orthographic and perspective logic to cameracontroller.cpp

master
Riyyi 4 years ago
parent
commit
4baa7cd7df
  1. 4
      inferno/src/inferno/scene/scene.cpp
  2. 112
      inferno/src/inferno/script/cameracontroller.cpp
  3. 62
      inferno/src/inferno/script/cameracontroller.h
  4. 107
      inferno/src/inferno/systems/camera.cpp

4
inferno/src/inferno/scene/scene.cpp

@ -27,8 +27,8 @@ namespace Inferno {
uint32_t camera = createEntity("Camera Entity"); uint32_t camera = createEntity("Camera Entity");
auto& cameraTransform = getComponent<TransformComponent>(camera); auto& cameraTransform = getComponent<TransformComponent>(camera);
cameraTransform.translate.z = -1.0f; cameraTransform.translate.z = 1.0f;
addComponent<CameraComponent>(camera, CameraType::Orthographic); addComponent<CameraComponent>(camera, CameraType::Perspective);
addComponent<NativeScriptComponent>(camera).bind<CameraController>(); addComponent<NativeScriptComponent>(camera).bind<CameraController>();
RenderSystem* renderSystem = new RenderSystem(); RenderSystem* renderSystem = new RenderSystem();

112
inferno/src/inferno/script/cameracontroller.cpp

@ -2,6 +2,116 @@
namespace Inferno { namespace Inferno {
//.. void CameraController::updateOrthographic(float deltaTime)
{
// Update camera rotation
float cameraRotateSpeed = ROTATE_SPEED * deltaTime;
if (Input::isKeyPressed(KeyCode("GLFW_KEY_Q"))) {
transform->rotate.z += cameraRotateSpeed;
}
if (Input::isKeyPressed(KeyCode("GLFW_KEY_E"))) {
transform->rotate.z -= cameraRotateSpeed;
}
if (transform->rotate.z > 180.0f) {
transform->rotate.z -= 360.0f;
}
else if (transform->rotate.z <= -180.0f) {
transform->rotate.z += 360.0f;
}
// Update camera translation
float cameraTranslateSpeed = TRANSLATE_SPEED * deltaTime;
// WASD movement
if (Input::isKeyPressed(KeyCode("GLFW_KEY_W"))) {
transform->translate.x += -sin(glm::radians(transform->rotate.z)) * cameraTranslateSpeed;
transform->translate.y += cos(glm::radians(transform->rotate.z)) * cameraTranslateSpeed;
}
if (Input::isKeyPressed(KeyCode("GLFW_KEY_S"))) {
transform->translate.x -= -sin(glm::radians(transform->rotate.z)) * cameraTranslateSpeed;
transform->translate.y -= cos(glm::radians(transform->rotate.z)) * cameraTranslateSpeed;
}
if (Input::isKeyPressed(KeyCode("GLFW_KEY_A"))) {
transform->translate.x -= cos(glm::radians(transform->rotate.z)) * cameraTranslateSpeed;
transform->translate.y -= sin(glm::radians(transform->rotate.z)) * cameraTranslateSpeed;
}
if (Input::isKeyPressed(KeyCode("GLFW_KEY_D"))) {
transform->translate.x += cos(glm::radians(transform->rotate.z)) * cameraTranslateSpeed;
transform->translate.y += sin(glm::radians(transform->rotate.z)) * cameraTranslateSpeed;
}
// Update camera zoom
float zoomSpeed = ZOOM_SENSITIVITY * deltaTime;
if (Input::isKeyPressed(KeyCode("GLFW_KEY_EQUAL"))) {
m_camera->zoomLevel -= zoomSpeed;
}
if (Input::isKeyPressed(KeyCode("GLFW_KEY_MINUS"))) {
m_camera->zoomLevel += zoomSpeed;
}
m_camera->zoomLevel = std::max(m_camera->zoomLevel, 0.25f);
m_camera->zoomLevel = std::min(m_camera->zoomLevel, 10.0f);
}
void CameraController::updatePerspective(float deltaTime)
{
// Get mouse movement offset compared to last frame
float xOffset = Input::getXOffset() * MOUSE_SENSITIVITY;
float yOffset = Input::getYOffset() * MOUSE_SENSITIVITY;
m_camera->yaw += xOffset;
m_camera->pitch += yOffset;
// Prevent gimbal lock
if (m_camera->pitch > 89.0f) m_camera->pitch = 89.0f;
if (m_camera->pitch < -89.0f) m_camera->pitch = -89.0f;
// Update camera rotation, by calculating direction vector via yaw and pitch
transform->rotate = {
cos(glm::radians(m_camera->pitch)) * cos(glm::radians(m_camera->yaw)),
sin(glm::radians(m_camera->pitch)),
cos(glm::radians(m_camera->pitch)) * sin(glm::radians(m_camera->yaw))
};
transform->rotate = glm::normalize(transform->rotate);
// The direction vector is based on
// Camera direction (z): normalize(position - target)
// Right axis (x): normalize(cross(up, direction))
// Up axis (y): cross(direction, right)
// Source: https://learnopengl.com/img/getting-started/camera_axes.png
// Cross = combination of two vectors in 3D space,
// where result is always perpendicular to both of the vectors
// Update camera translation
float cameraSpeed = TRANSLATE_SPEED * deltaTime;
// WASD movement
if (Input::isKeyPressed(KeyCode("GLFW_KEY_W"))) {
transform->translate = { transform->translate + cameraSpeed * transform->rotate };
}
if (Input::isKeyPressed(KeyCode("GLFW_KEY_S"))) {
transform->translate = { transform->translate - cameraSpeed * transform->rotate };
}
if (Input::isKeyPressed(KeyCode("GLFW_KEY_A"))) {
transform->translate = { transform->translate -
glm::normalize(glm::cross(transform->rotate, m_camera->up)) * cameraSpeed };
}
if (Input::isKeyPressed(KeyCode("GLFW_KEY_D"))) {
transform->translate = { transform->translate +
glm::normalize(glm::cross(transform->rotate, m_camera->up)) * cameraSpeed };
}
// Up / down movement
if (Input::isKeyPressed(KeyCode("GLFW_KEY_SPACE"))) {
transform->translate = { transform->translate.x, transform->translate.y + cameraSpeed, transform->translate.z };
}
if (Input::isKeyPressed(KeyCode("GLFW_KEY_LEFT_SHIFT"))) {
transform->translate = { transform->translate.x, transform->translate.y - cameraSpeed, transform->translate.z };
}
}
} }

62
inferno/src/inferno/script/cameracontroller.h

@ -39,66 +39,8 @@ namespace Inferno {
} }
} }
void updateOrthographic(float deltaTime) void updateOrthographic(float deltaTime);
{ void updatePerspective(float deltaTime);
// Update camera rotation
float cameraRotateSpeed = ROTATE_SPEED * (1.0f/ 60.0f);
if (Input::isKeyPressed(KeyCode("GLFW_KEY_Q"))) {
transform->rotate.z += cameraRotateSpeed;
}
if (Input::isKeyPressed(KeyCode("GLFW_KEY_E"))) {
transform->rotate.z -= cameraRotateSpeed;
}
if (transform->rotate.z > 180.0f) {
transform->rotate.z -= 360.0f;
}
else if (transform->rotate.z <= -180.0f) {
transform->rotate.z += 360.0f;
}
// Update camera translation
float cameraTranslateSpeed = TRANSLATE_SPEED * deltaTime;
// WASD movement
if (Input::isKeyPressed(KeyCode("GLFW_KEY_W"))) {
transform->translate.x += -sin(glm::radians(transform->rotate.z)) * cameraTranslateSpeed;
transform->translate.y += cos(glm::radians(transform->rotate.z)) * cameraTranslateSpeed;
}
if (Input::isKeyPressed(KeyCode("GLFW_KEY_S"))) {
transform->translate.x -= -sin(glm::radians(transform->rotate.z)) * cameraTranslateSpeed;
transform->translate.y -= cos(glm::radians(transform->rotate.z)) * cameraTranslateSpeed;
}
if (Input::isKeyPressed(KeyCode("GLFW_KEY_A"))) {
transform->translate.x -= cos(glm::radians(transform->rotate.z)) * cameraTranslateSpeed;
transform->translate.y -= sin(glm::radians(transform->rotate.z)) * cameraTranslateSpeed;
}
if (Input::isKeyPressed(KeyCode("GLFW_KEY_D"))) {
transform->translate.x += cos(glm::radians(transform->rotate.z)) * cameraTranslateSpeed;
transform->translate.y += sin(glm::radians(transform->rotate.z)) * cameraTranslateSpeed;
}
// Update camera zoom
float zoomSpeed = ZOOM_SENSITIVITY * (1.0f / 60.0f);
if (Input::isKeyPressed(KeyCode("GLFW_KEY_EQUAL"))) {
m_camera->zoomLevel -= zoomSpeed;
}
if (Input::isKeyPressed(KeyCode("GLFW_KEY_MINUS"))) {
m_camera->zoomLevel += zoomSpeed;
}
m_camera->zoomLevel = std::max(m_camera->zoomLevel, 0.25f);
m_camera->zoomLevel = std::min(m_camera->zoomLevel, 10.0f);
}
void updatePerspective(float deltaTime)
{
}
private: private:
CameraComponent* m_camera; CameraComponent* m_camera;

107
inferno/src/inferno/systems/camera.cpp

@ -57,59 +57,6 @@ namespace Inferno {
void CameraSystem::updateOrthographic(TransformComponent& transform, CameraComponent& camera) void CameraSystem::updateOrthographic(TransformComponent& transform, CameraComponent& camera)
{ {
// Update camera rotation
float cameraRotateSpeed = ROTATE_SPEED * (1.0f/ 60.0f);
if (Input::isKeyPressed(KeyCode("GLFW_KEY_Q"))) {
transform.rotate.z += cameraRotateSpeed;
}
if (Input::isKeyPressed(KeyCode("GLFW_KEY_E"))) {
transform.rotate.z -= cameraRotateSpeed;
}
if (transform.rotate.z > 180.0f) {
transform.rotate.z -= 360.0f;
}
else if (transform.rotate.z <= -180.0f) {
transform.rotate.z += 360.0f;
}
// Update camera translation
float cameraTranslateSpeed = TRANSLATE_SPEED * (1.0f / 60.0f);
// WASD movement
if (Input::isKeyPressed(KeyCode("GLFW_KEY_W"))) {
transform.translate.x += -sin(glm::radians(transform.rotate.z)) * cameraTranslateSpeed;
transform.translate.y += cos(glm::radians(transform.rotate.z)) * cameraTranslateSpeed;
}
if (Input::isKeyPressed(KeyCode("GLFW_KEY_S"))) {
transform.translate.x -= -sin(glm::radians(transform.rotate.z)) * cameraTranslateSpeed;
transform.translate.y -= cos(glm::radians(transform.rotate.z)) * cameraTranslateSpeed;
}
if (Input::isKeyPressed(KeyCode("GLFW_KEY_A"))) {
transform.translate.x -= cos(glm::radians(transform.rotate.z)) * cameraTranslateSpeed;
transform.translate.y -= sin(glm::radians(transform.rotate.z)) * cameraTranslateSpeed;
}
if (Input::isKeyPressed(KeyCode("GLFW_KEY_D"))) {
transform.translate.x += cos(glm::radians(transform.rotate.z)) * cameraTranslateSpeed;
transform.translate.y += sin(glm::radians(transform.rotate.z)) * cameraTranslateSpeed;
}
// Update camera zoom
float zoomSpeed = ZOOM_SENSITIVITY * (1.0f / 60.0f);
if (Input::isKeyPressed(KeyCode("GLFW_KEY_EQUAL"))) {
camera.zoomLevel -= zoomSpeed;
}
if (Input::isKeyPressed(KeyCode("GLFW_KEY_MINUS"))) {
camera.zoomLevel += zoomSpeed;
}
camera.zoomLevel = std::max(camera.zoomLevel, 0.25f);
camera.zoomLevel = std::min(camera.zoomLevel, 10.0f);
// Update camera matrix // Update camera matrix
// Local space -> World space: model matrix // Local space -> World space: model matrix
@ -135,60 +82,6 @@ namespace Inferno {
void CameraSystem::updatePerspective(TransformComponent& transform, CameraComponent& camera) void CameraSystem::updatePerspective(TransformComponent& transform, CameraComponent& camera)
{ {
// Get mouse movement offset compared to last frame
float xOffset = Input::getXOffset() * MOUSE_SENSITIVITY;
float yOffset = Input::getYOffset() * MOUSE_SENSITIVITY;
camera.yaw += xOffset;
camera.pitch += yOffset;
// Prevent gimbal lock
if (camera.pitch > 89.0f) camera.pitch = 89.0f;
if (camera.pitch < -89.0f) camera.pitch = -89.0f;
// Update camera rotation, by calculating direction vector via yaw and pitch
transform.rotate = {
cos(glm::radians(camera.pitch)) * cos(glm::radians(camera.yaw)),
sin(glm::radians(camera.pitch)),
cos(glm::radians(camera.pitch)) * sin(glm::radians(camera.yaw))
};
transform.rotate = glm::normalize(transform.rotate);
// The direction vector is based on
// Camera direction (z): normalize(position - target)
// Right axis (x): normalize(cross(up, direction))
// Up axis (y): cross(direction, right)
// Source: https://learnopengl.com/img/getting-started/camera_axes.png
// Cross = combination of two vectors in 3D space,
// where result is always perpendicular to both of the vectors
// Update camera translation
float cameraSpeed = TRANSLATE_SPEED * (1.0f / 60.0f);
// WASD movement
if (Input::isKeyPressed(KeyCode("GLFW_KEY_W"))) {
transform.translate = { transform.translate + cameraSpeed * transform.rotate };
}
if (Input::isKeyPressed(KeyCode("GLFW_KEY_S"))) {
transform.translate = { transform.translate - cameraSpeed * transform.rotate };
}
if (Input::isKeyPressed(KeyCode("GLFW_KEY_A"))) {
transform.translate = { transform.translate -
glm::normalize(glm::cross(transform.rotate, camera.up)) * cameraSpeed };
}
if (Input::isKeyPressed(KeyCode("GLFW_KEY_D"))) {
transform.translate = { transform.translate +
glm::normalize(glm::cross(transform.rotate, camera.up)) * cameraSpeed };
}
// Up / down movement
if (Input::isKeyPressed(KeyCode("GLFW_KEY_SPACE"))) {
transform.translate = { transform.translate.x, transform.translate.y + cameraSpeed, transform.translate.z };
}
if (Input::isKeyPressed(KeyCode("GLFW_KEY_LEFT_SHIFT"))) {
transform.translate = { transform.translate.x, transform.translate.y - cameraSpeed, transform.translate.z };
}
// Update camera matrix // Update camera matrix
// Local space -> World space: model matrix // Local space -> World space: model matrix

Loading…
Cancel
Save