Skip to content

Commit

Permalink
camera class
Browse files Browse the repository at this point in the history
  • Loading branch information
Tumble committed Feb 16, 2021
1 parent cd07f01 commit 771655e
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 40 deletions.
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# Cinnabar Engine



## Prerequisites

Use the build tool [CMake](https://cmake.org/install/) to build cinnabar.
Expand Down Expand Up @@ -49,7 +47,7 @@ Pull requests are welcome. For major changes, please open an issue first to disc

## Todo
* ~~Window Class~~
* Resize context on window resize
* ~~Resize context on window resize~~
* ~~Time Class~~
* Camera Class
* Input System
Expand Down
51 changes: 16 additions & 35 deletions engine/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

//Rendering
#include "ce_render_fundementals.h"
#include "rendering/camera.h"

/*
* Vertices
Expand Down Expand Up @@ -103,17 +104,10 @@ int main(int argc, char* argv[]) {


float
mouseSensitivity = 0.1f,
cameraPitch = 0.0f,
cameraYaw = -90.0f;

glm::vec3
cameraPos(0.0f, 0.0f, 3.0f),
cameraFront(0.0f,0.0f,-1.0f),
cameraUp(0.0f,1.0f,0.0f),
cameraRight = glm::normalize(glm::cross(cameraFront, cameraUp));


mouseSensitivity = 0.1f;
ce::Camera* camera = new ce::Camera();
camera->getTransform()->setPosition(0.0f,0.0f,3.0f);
camera->getTransform()->setYaw(-90.0f);
/*
* Game Loop
*/
Expand All @@ -132,13 +126,8 @@ int main(int argc, char* argv[]) {
if(window->mouseVisible())break;
glm::vec2 mouseDelta(event.motion.xrel,event.motion.yrel);
mouseDelta *= mouseSensitivity;
cameraYaw += mouseDelta.x;
cameraPitch -= mouseDelta.y;
if(cameraPitch>89.0f)
cameraPitch = 89.0f;
if(cameraPitch<-89.0f)
cameraPitch = -89.0f;

camera->getTransform()->yaw(mouseDelta.x);
camera->getTransform()->pitch(-mouseDelta.y);
break;
}
case SDL_MOUSEBUTTONDOWN:
Expand All @@ -148,15 +137,17 @@ int main(int argc, char* argv[]) {
break;
case SDL_KEYDOWN:
{
float cameraSpeed = 2.5f*time->getDeltaTime(); // adjust accordingly
glm::vec3 cameraFront = camera->getTransform()->getForward(),
cameraRight = camera->getRight();
float cameraSpeed = 2.5f*time->getDeltaTime();
if (event.key.keysym.sym == SDLK_w)
cameraPos += cameraFront * cameraSpeed;
camera->getTransform()->translate(cameraFront * cameraSpeed);
if (event.key.keysym.sym == SDLK_s)
cameraPos -= cameraFront * cameraSpeed;
camera->getTransform()->translate(-cameraFront * cameraSpeed);
if (event.key.keysym.sym == SDLK_a)
cameraPos -= cameraRight * cameraSpeed;
camera->getTransform()->translate(-cameraRight * cameraSpeed);
if (event.key.keysym.sym == SDLK_d)
cameraPos += cameraRight * cameraSpeed;
camera->getTransform()->translate(cameraRight * cameraSpeed);
if(event.key.keysym.sym == SDLK_ESCAPE){
window->setMouseVisibility(true);
}
Expand All @@ -178,25 +169,15 @@ int main(int argc, char* argv[]) {
}



// Transform
transform.roll(25.0f*time->getDeltaTime());
transform.yaw(50.0f*time->getDeltaTime());
transform.pitch(100.0f*time->getDeltaTime());
transform.saveToShader(shader);
transform.sendToShader(shader);

// Camera
glm::vec3
cameraDirection(
cos(glm::radians(cameraYaw))*cos(glm::radians(cameraPitch)),
sin(glm::radians(cameraPitch)),
sin(glm::radians(cameraYaw)) * cos(glm::radians(cameraPitch))
);
cameraFront = glm::normalize(cameraDirection);
cameraRight = glm::normalize(glm::cross(cameraFront, cameraUp));
glm::mat4 view = glm::lookAt(cameraPos,cameraPos + cameraFront,cameraUp);
shader->setMat4("transform.view",view);
shader->setMat4("transform.proj",proj);
camera->sendToShader(shader);

/* Render */
glClearColor(0.0f,0.0f,0.0f,1.0f);
Expand Down
15 changes: 14 additions & 1 deletion engine/math/transform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,20 @@ glm::mat4 ce::Transform::getMatrix()
return transform;
}

void ce::Transform::saveToShader(ce::Shader* shader)
void ce::Transform::sendToShader(ce::Shader* shader)
{
shader->setMat4("transform.model",getMatrix());
}

glm::vec3 ce::Transform::getForward()
{
float
yaw = glm::radians(getYaw()),
pitch = glm::radians(getPitch());
glm::vec3 dir(
cos(yaw)*cos(pitch),
sin(pitch),
sin(yaw)*cos(pitch)
);
return glm::normalize(dir);
}
5 changes: 4 additions & 1 deletion engine/math/transform.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ namespace ce {
Transform();
~Transform();

static glm::vec3 getGloablUp() {return glm::vec3(0.0f,1.0f,0.0f);}

glm::vec3 getPosition() {return m_pos;}
void setPosition(glm::vec3 pos){m_pos=pos;}
void setPosition(float x,float y,float z){ setPosition(glm::vec3(x,y,z));}
Expand Down Expand Up @@ -55,7 +57,8 @@ namespace ce {

glm::mat4 getMatrix();

void saveToShader(ce::Shader* shader);
void sendToShader(ce::Shader* shader);
glm::vec3 getForward();
};
}

Expand Down
33 changes: 33 additions & 0 deletions engine/rendering/camera.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#include "camera.h"

ce::Camera::Camera()
:m_transform(NULL)
{
m_transform = new Transform();
}

ce::Camera::~Camera()
{
delete m_transform;
}

glm::mat4 ce::Camera::getView()
{
glm::vec3
pos = m_transform->getPosition(),
forward = m_transform->getForward();
return glm::lookAt(pos,pos+forward,ce::Transform::getGloablUp());
}

glm::vec3 ce::Camera::getRight()
{
glm::vec3
forward = m_transform->getForward(),
up = ce::Transform::getGloablUp();
return glm::normalize(glm::cross(forward,up));
}

void ce::Camera::sendToShader(ce::Shader* shader)
{
shader->setMat4("transform.view",getView());
}
23 changes: 23 additions & 0 deletions engine/rendering/camera.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#ifndef _CE_CAMERA_H_
#define _CE_CAMERA_H_

#include <math/transform.h>

namespace ce {
class Camera {
private:
ce::Transform* m_transform;
float speed,
fov;
public:
Camera();
~Camera();

glm::mat4 getView();
glm::vec3 getRight();
void sendToShader(ce::Shader* shader);
Transform* getTransform(){return m_transform;}
};
}

#endif //_CE_CAMERA_H_

0 comments on commit 771655e

Please sign in to comment.