Skip to content

Commit

Permalink
fix merge
Browse files Browse the repository at this point in the history
  • Loading branch information
Tumble committed Feb 16, 2021
2 parents 500cfdf + f2138aa commit fa56a00
Show file tree
Hide file tree
Showing 7 changed files with 168 additions and 12 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
10 changes: 3 additions & 7 deletions engine/core/tpnt_log.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,9 @@

#define TPNT_LOGGER_STR(X) (std::string("") + X).c_str()
#define TPNT_LOGGER_FORMAT "[%s][%s:%d|%s]: %s\n"
#define TPNT_LOGGER_FILE \
strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__
#define TPNT_LOGGER_FILE strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__
#define TPNT_LOGGER_CONTEXT TPNT_LOGGER_FILE, __LINE__, __FUNCTION__
#define TPNT_LOG(L, X) \
printf(TPNT_LOGGER_FORMAT, TPNT_LOGGER_STR(L), TPNT_LOGGER_CONTEXT, \
TPNT_LOGGER_STR(X))
#define TPNT_LOG(L, X) printf(TPNT_LOGGER_FORMAT, TPNT_LOGGER_STR(L), TPNT_LOGGER_CONTEXT,TPNT_LOGGER_STR(X))
#define TPNT_LOG_RESET "\033[0m"
#define TPNT_LOG_SUCCESS "\033[1;32m"
#define TPNT_LOG_WARN "\033[1;33m"
Expand All @@ -36,5 +33,4 @@
#define LOG_INFO(X) TPNT_LOG("INFO", X)
#define LOG_WARN(X) TPNT_LOG(TPNT_LOG_WARN + "WARN" + TPNT_LOG_RESET, X)
#define LOG_ERROR(X) TPNT_LOG(TPNT_LOG_ERROR + "ERROR" + TPNT_LOG_RESET, X)
#define LOG_SUCCESS(X) \
TPNT_LOG(TPNT_LOG_SUCCESS + "SUCCESS" + TPNT_LOG_RESET, X)
#define LOG_SUCCESS(X) TPNT_LOG(TPNT_LOG_SUCCESS + "SUCCESS" + TPNT_LOG_RESET, X)
53 changes: 52 additions & 1 deletion 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 @@ -97,6 +98,7 @@ int main(int argc, char* argv[]) {
ce::Mesh* mesh = new ce::Mesh(vertices, vertexCount, indices, indexCount);
mesh->sendToShader(shader);
ce::Texture* texture = new ce::Texture("uv-map.png");
<<<<<<< HEAD
shader->setInt("uTex", 0);

glm::mat4 proj = glm::perspective(
Expand All @@ -108,6 +110,18 @@ int main(int argc, char* argv[]) {
cameraUp(0.0f, 1.0f, 0.0f),
cameraRight = glm::normalize(glm::cross(cameraFront, cameraUp));

=======
shader->setInt("uTex",0);

glm::mat4 proj = glm::perspective(glm::radians(45.0f), window->getAspectRatio(), 0.1f, 100.0f);


float
mouseSensitivity = 0.1f;
ce::Camera* camera = new ce::Camera();
camera->getTransform()->setPosition(0.0f,0.0f,3.0f);
camera->getTransform()->setYaw(-90.0f);
>>>>>>> master
/*
* Game Loop
*/
Expand All @@ -123,31 +137,53 @@ int main(int argc, char* argv[]) {
break;
glm::vec2 mouseDelta(event.motion.xrel, event.motion.yrel);
mouseDelta *= mouseSensitivity;
<<<<<<< HEAD
cameraYaw += mouseDelta.x;
cameraPitch -= mouseDelta.y;
if (cameraPitch > 89.9f) // TODO: fix broken 90 degree pitch
cameraPitch = 89.9f;
if (cameraPitch < -89.9f)
cameraPitch = -89.9f;

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

// Transform
transform.roll(25.0f * time->getDeltaTime());
Expand All @@ -183,6 +220,20 @@ int main(int argc, char* argv[]) {
shader->setMat4("transform.view", view);
shader->setMat4("transform.proj", proj);

=======


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

// Camera
shader->setMat4("transform.proj",proj);
camera->sendToShader(shader);

>>>>>>> master
/* Render */
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
Expand Down
15 changes: 14 additions & 1 deletion engine/math/transform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,19 @@ 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);
}
42 changes: 42 additions & 0 deletions engine/math/transform.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ namespace ce {
public:
Transform();
~Transform();
<<<<<<< HEAD

glm::vec3 getPosition() { return m_pos; }
void setPosition(glm::vec3 pos) { m_pos = pos; }
Expand Down Expand Up @@ -59,5 +60,46 @@ namespace ce {
glm::mat4 getMatrix();

void saveToShader(ce::Shader* shader);
=======

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));}
void translate(glm::vec3 delta){m_pos+=delta;}
void translate(float x,float y,float z){translate(glm::vec3(x,y,z));}

void setPitch(float pitch){m_rot.x = pitch;}
void pitch(float delta){m_rot.x+= delta;}
float getPitch(){return m_rot.x;}

void setYaw(float yaw){m_rot.y = yaw;}
void yaw(float delta){m_rot.y += delta;}
float getYaw(){return m_rot.y;}

void setRoll(float roll) {m_rot.z = roll;}
void roll(float delta) {m_rot.z += delta;}
float getRoll(){return m_rot.z;}

glm::vec3 getRotation() {return m_rot;}
void setRotation(glm::vec3 rot){m_rot=rot;}
void setRotation(float x,float y,float z){ setRotation(glm::vec3(x,y,z));}
void rotate(glm::vec3 delta) {m_rot+=delta;}
void rotate(float x, float y, float z) {rotate(glm::vec3(x,y,z));}

glm::vec3 getScale() {return m_scale;}
void setScale(glm::vec3 scale){m_scale=scale;}
void setScale(float x,float y,float z){ setScale(glm::vec3(x,y,z));}
void setScale(float a){setScale(a,a,a);}
void scale(glm::vec3 delta) {m_scale*=delta;}
void scale(float x, float y, float z) {scale(glm::vec3(x,y,z));}
void scale(float a){scale(a,a,a);}

glm::mat4 getMatrix();

void sendToShader(ce::Shader* shader);
glm::vec3 getForward();
>>>>>>> master
};
}
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 fa56a00

Please sign in to comment.