Added Camera and first-person and UI camera types; Added mouse tracking to aid this

This commit is contained in:
grayson committed 2021-09-01 02:04:44 -05:00
1 parent d628bbb184
commit 48821afd76
18 files changed
+291 -42

No files matched your search

+106
View File
@@ -0,0 +1,106 @@
#include "Camera.h"
#include "glm/glm.hpp"
#include "Application.h"
#include "Shader.h"
namespace nf {
Camera::Camera(Application* app) :
m_app(app)
{
m_type = Type::NF_CAMERA_UI;
}
void Camera::setType(Type cameraType) {
m_type = cameraType;
if (m_type == Type::NF_CAMERA_FIRST_PERSON || m_type == Type::NF_CAMERA_ORBIT)
m_app->trackMouse(true);
else
m_app->trackMouse(false);
}
Camera::Type Camera::getType() const {
return m_type;
}
void Camera::moveForward(double speed) {
Vec3 temp = m_front * speed;
m_position = { m_position.x + temp.x, m_position.y + temp.y, m_position.z + temp.z };
}
void Camera::moveBackward(double speed) {
Vec3 temp = m_front * speed;
m_position = { m_position.x - temp.x, m_position.y - temp.y, m_position.z - temp.z };
}
void Camera::moveRight(double speed) {
glm::vec3 front = { m_front.x, m_front.y, m_front.z };
glm::vec3 temp = glm::normalize(glm::cross(front, glm::vec3(0.0, 1.0, 0.0))) * (float)speed;
m_position = { m_position.x + temp.x, m_position.y + temp.y, m_position.z + temp.z };
}
void Camera::moveLeft(double speed) {
glm::vec3 front = { m_front.x, m_front.y, m_front.z };
glm::vec3 temp = glm::normalize(glm::cross(front, glm::vec3(0.0, 1.0, 0.0))) * (float)speed;
m_position = { m_position.x - temp.x, m_position.y - temp.y, m_position.z - temp.z };
}
void Camera::setPosition(double x, double y, double z) {
m_position = { x, y, z };
}
void Camera::setPosition(const Vec3& position) {
m_position = position;
}
void Camera::bind(Shader* shader) {
glm::mat4 view;
switch (m_type) {
case Type::NF_CAMERA_UI: {
view = glm::mat4(1.0);
break;
}
case Type::NF_CAMERA_FIRST_PERSON: {
int mouseDiffx = 0;
int mouseDiffy = 0;
m_app->getMouseDiff(mouseDiffx, mouseDiffy);
float mouseX = (float)mouseDiffx * 0.1f;
float mouseY = (float)mouseDiffy * 0.1f;
static float yaw = -90.0f;
static float pitch = 0.0f;
yaw += mouseX;
pitch += mouseY;
if (pitch > 89.0f)
pitch = 89.0f;
if (pitch < -89.0f)
pitch = -89.0f;
glm::vec3 rotation;
rotation.x = std::cos(glm::radians(yaw)) * std::cos(glm::radians(pitch));
rotation.y = std::sin(glm::radians(pitch));
rotation.z = std::sin(glm::radians(yaw)) * std::cos(glm::radians(pitch));
rotation = glm::normalize(rotation);
m_front = { rotation.x, rotation.y, rotation.z };
glm::vec3 position(m_position.x, m_position.y, m_position.z);
glm::vec3 up(0.0, 1.0, 0.0);
view = glm::lookAt(position, position + rotation, up);
break;
}
case Type::NF_CAMERA_ORBIT: {
break;
}
case Type::NF_CAMERA_FIXED: {
break;
}
}
shader->setUniform("view", view);
}
Camera::~Camera() {
}
}
+12 -2
View File
@@ -2,8 +2,6 @@
#include<vector>
#include "Utility.h"
namespace nf {
Entity::Entity() :
m_model(nullptr),
@@ -39,10 +37,18 @@ namespace nf {
m_position = { x, y, z };
}
void Entity::setPosition(const Vec3& position) {
m_position = position;
}
void Entity::setRotation(double x, double y, double z) {
m_rotation = { x, y, z };
}
void Entity::setRotation(const Vec3& rotation) {
m_rotation = rotation;
}
void Entity::setScale(double x) {
m_rotation = { x, x, x };
}
@@ -51,6 +57,10 @@ namespace nf {
m_scale = { x, y, z };
}
void Entity::setScale(const Vec3& scale) {
m_scale = scale;
}
void Entity::bind(Shader* shader) {
m_model->bind();
shader->bind();
@@ -1,6 +1,10 @@
#include "Model.h"
#include "glm/glm.hpp"
#include "glm/gtc/matrix_transform.hpp"
#include "Utility.h"
#include "Texture.h"
namespace nf {
Model::Model() {
+6 -2
View File
@@ -2,6 +2,7 @@
#include "GL/glew.h"
#include "GL\wglew.h"
#include "glm/glm.hpp"
#include "Application.h"
#include "Utility.h"
@@ -51,6 +52,7 @@ namespace nf {
glDepthFunc(GL_LESS);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_CULL_FACE);
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
Win32Res vs(IDR_DEFAULTVERTEX);
@@ -68,15 +70,17 @@ namespace nf {
m_lGame.push_back(&in);
}
void Renderer::doFrame() {
void Renderer::doFrame(Camera* camera) {
glViewport(0, 0, m_app->getConfig().width, m_app->getConfig().height);
proj = glm::perspective(glm::radians(45.0f), (float)m_app->getConfig().width / (float)m_app->getConfig().height, 0.1f, 100000.0f);
glm::mat4 proj = glm::perspective(glm::radians(45.0f), (float)m_app->getConfig().width / (float)m_app->getConfig().height, 0.1f, 100000.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
for (Entity* draw : m_lGame) {
Entity& curr = *draw;
curr.bind(m_defaultShader);
m_defaultShader->setUniform("proj", proj);
camera->bind(m_defaultShader);
//TODO: Clean this up a bit
glDrawElements(GL_TRIANGLES, curr.getModel()->getIndexCount(), GL_UNSIGNED_INT, nullptr);
}
m_lGame.clear();