Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cube fix and more formatting #4

Merged
merged 6 commits into from
Feb 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ Pull requests are welcome. For major changes, please open an issue first to disc
- [x] 3D Cube
- [x] 3D Transforms
- [x] WASD
- [x] basic input handling
- [x] Mouse controlled camers
- [x] Basic input handling
- [x] Mouse controlled camera
- [x] Window Class
- [x] Resize context on window resize
- [x] Time Class
Expand All @@ -92,4 +92,4 @@ Pull requests are welcome. For major changes, please open an issue first to disc
- [ ] Scene Graph
- [ ] XML based scene graph import
- [ ] Test ON Windows
- [ ] Port to Emscripten
- [ ] Port to Emscripten
7 changes: 2 additions & 5 deletions engine/ce_assets.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#ifndef _CE_ASSETS_H_
#define _CE_ASSETS_H_
#pragma once

#include <string>
#include <ce_math.h>
Expand Down Expand Up @@ -31,6 +30,4 @@ namespace ce {
specularTex = "missing.png",
shader = "";
};
}

#endif // !_CE_ASSETS_H_
}
5 changes: 3 additions & 2 deletions engine/core/time.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
float ce::Time::getTime() {
return SDL_GetTicks() / 1000.0f;
}
ce::Time::Time() : m_last(getTime()), m_now(0), m_dt(0), m_fps(0) {}
ce::Time::Time()
: m_last(getTime()), m_now(0), m_dt(0), m_fps(0) {}

ce::Time::~Time() {}

Expand All @@ -14,4 +15,4 @@ void ce::Time::update() {
m_dt = m_now - m_last;
m_fps = 1 / m_dt;
m_last = m_now;
}
}
4 changes: 2 additions & 2 deletions engine/core/tpnt_log.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
#define TPNT_LOGGER_FORMAT "[%s][%s:%d|%s]: %s\n"
#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 @@ -33,4 +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)
22 changes: 10 additions & 12 deletions engine/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,18 +91,18 @@ int main(int argc, char* argv[]) {
ce::RenderingEngine* renderingEngine = new ce::RenderingEngine();
renderingEngine->setFOV(75.0f);
renderingEngine->setSize(window->getWindowSize());
renderingEngine->setClipRange(0.1f,100.0f);
renderingEngine->setClipRange(0.1f, 100.0f);

ce::Transform* transform = new ce::Transform();
ce::Mesh* mesh = new ce::Mesh(vertices, vertexCount, indices, indexCount);
ce::Shader* shader = new ce::Shader("basic");
ce::Material* material = new ce::Material(shader);
ce::Material* planeMaterial = new ce::Material(shader);
material->setTexture(new ce::Texture("uv-map.png"));

ce::Mesh* plane = new ce::Mesh(planeVertices,planeIndexCount,planeIndices,planeVertexCount);

ce::Material* planeMaterial = new ce::Material(shader);
ce::Mesh* plane = new ce::Mesh(planeVertices, planeIndexCount, planeIndices, planeVertexCount);
ce::Transform* planeTransform = new ce::Transform();

float mouseSensitivity = 0.1f;
ce::Camera* camera = new ce::Camera();
// Seperate so i can put in a player class later
Expand Down Expand Up @@ -159,10 +159,8 @@ int main(int argc, char* argv[]) {
if ((event.key.keysym.sym == SDLK_a && cameraVelocity.x < 0) ||
(event.key.keysym.sym == SDLK_d && cameraVelocity.x > 0))
cameraVelocity.x = 0;
if ((event.key.keysym.sym == SDLK_SPACE &&
cameraVelocity.y > 0) ||
(event.key.keysym.sym == SDLK_LSHIFT &&
cameraVelocity.y < 0))
if ((event.key.keysym.sym == SDLK_SPACE && cameraVelocity.y > 0) ||
(event.key.keysym.sym == SDLK_LSHIFT && cameraVelocity.y < 0))
cameraVelocity.y = 0;
break;
}
Expand All @@ -180,7 +178,7 @@ int main(int argc, char* argv[]) {
transform->roll(25.0f * time->getDeltaTime());
transform->yaw(50.0f * time->getDeltaTime());
transform->pitch(100.0f * time->getDeltaTime());

// Camera
glm::vec3
cameraFront = camera->getTransform()->getForward(),
Expand All @@ -190,10 +188,10 @@ int main(int argc, char* argv[]) {
(cameraFront * cameraVelocity.z) +
(cameraRight * cameraVelocity.x) +
(cameraUp * cameraVelocity.y));

/* Render */
renderingEngine->registerCommand({transform, material, mesh, mesh->GetIndexCount()});
renderingEngine->registerCommand({planeTransform,planeMaterial,plane,plane->GetIndexCount()});
renderingEngine->registerCommand({planeTransform, planeMaterial, plane, plane->GetIndexCount()});
renderingEngine->render();

window->swapBuffers();
Expand Down
5 changes: 2 additions & 3 deletions engine/managers/asset_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ std::string ce::AssetManager::load_text_file(std::string path) {
text = filestream.str();
LOG_SUCCESS("LOADED_FILE: " + path);
} catch (std::fstream::failure e) {
LOG_ERROR(
"FILE_NOT_SUCCESSFULLY_READ: (" + path + ") " + std::string(e.what()));
LOG_ERROR("FILE_NOT_SUCCESSFULLY_READ: (" + path + ") " + std::string(e.what()));
}
return text;
}
Expand Down Expand Up @@ -65,4 +64,4 @@ ce::TextureFile ce::AssetManager::getTextureFile(std::string filename) {

void ce::AssetManager::freeTextureFile(ce::TextureFile textureFile) {
stbi_image_free(textureFile.data);
}
}
5 changes: 3 additions & 2 deletions engine/managers/asset_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
namespace ce {
class AssetManager {
private:
inline static const std::string SHADER_FOLDER = "shaders",
TEXTURE_FOLDER = "textures";
inline static const std::string
SHADER_FOLDER = "shaders",
TEXTURE_FOLDER = "textures";

static std::string load_text_file(std::string file);

Expand Down
8 changes: 3 additions & 5 deletions engine/math/transform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,13 @@ void ce::Transform::sendToShader(ce::Shader* shader) {
shader->setMat4("transform.model", getMatrix());
}

glm::vec3 ce::Transform::getForward()
{
glm::vec3 ce::Transform::getForward() {
float
yaw = glm::radians(getYaw()),
pitch = glm::radians(getPitch());
glm::vec3 dir(
cos(yaw)*cos(pitch),
cos(yaw) * cos(pitch),
sin(pitch),
sin(yaw)*cos(pitch)
);
sin(yaw) * cos(pitch));
return glm::normalize(dir);
}
16 changes: 5 additions & 11 deletions engine/math/transform.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,15 @@ namespace ce {
glm::vec3 m_pos, m_rot, m_scale;

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

Transform();
~Transform();
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 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 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; }
Expand All @@ -43,9 +39,7 @@ namespace ce {

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 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)); }

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

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

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

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

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

void ce::Camera::sendToShader(ce::Shader* shader)
{
shader->setMat4("transform.view",getView());
void ce::Camera::sendToShader(ce::Shader* shader) {
shader->setMat4("transform.view", getView());
}

void ce::Camera::boundPitch()
{
void ce::Camera::boundPitch() {
float cameraPitch = m_transform->getPitch();
if (cameraPitch > 89.9f) // TODO: fix broken 90 degree pitch
cameraPitch = 89.9f;
if (cameraPitch < -89.9f)
cameraPitch = -89.9f;
m_transform->setPitch(cameraPitch);
}

}
14 changes: 8 additions & 6 deletions engine/rendering/camera.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,20 @@

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

public:
Camera();
~Camera();

glm::mat4 getView();
glm::vec3 getRight();
void sendToShader(ce::Shader* shader);
Transform* getTransform(){return m_transform;}
Transform* getTransform() { return m_transform; }
void boundPitch();
};
}
Expand Down
3 changes: 1 addition & 2 deletions engine/rendering/material.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ ce::Material::Material(Shader* shader)
update();
}

ce::Material::~Material()
{
ce::Material::~Material() {
delete m_shader;
delete m_texture;
}
Expand Down
55 changes: 30 additions & 25 deletions engine/rendering/mesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,46 +7,51 @@ ce::Mesh::Mesh(Vertex* vertexArray, const unsigned vertexCount,
GLuint* indexArray, const unsigned indexCount)
: vertexCount(vertexCount), indexCount(indexCount), VAO(0), VBO(0), EBO(0) {
glGenVertexArrays(1, &VAO);
bind();
this->initVAO(vertexArray, indexArray);
unbind();
glBindVertexArray(VAO);
initVAO(vertexArray, indexArray);
glBindVertexArray(0);
}

ce::Mesh::~Mesh() {
glDeleteVertexArrays(1, &VAO);
glDeleteBuffers(1, &this->VBO);
glDeleteBuffers(1, &this->EBO);
glDeleteBuffers(1, &VBO);
glDeleteBuffers(1, &EBO);
}

void ce::Mesh::bind() {
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
}

void ce::Mesh::unbind() {
glBindVertexArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
}

void ce::Mesh::sendToShader(ce::Shader* shader) {
bind();
shader->vertexAttribPointer("aPos", 3, GL_FLOAT, GL_FALSE, sizeof(Vertex),
(GLvoid*)offsetof(Vertex, position));
shader->vertexAttribPointer("aColor", 4, GL_FLOAT, GL_FALSE, sizeof(Vertex),
(GLvoid*)offsetof(Vertex, color));
shader->vertexAttribPointer("aTexCoord", 2, GL_FLOAT, GL_FALSE,
sizeof(Vertex), (GLvoid*)offsetof(Vertex, texCoord));
unbind();
void ce::Mesh::sendToShader(ce::Shader* shader, bool bind) {
if (bind) {
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
}
shader->vertexAttribPointer("aPos", 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*)offsetof(Vertex, position));
shader->vertexAttribPointer("aColor", 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*)offsetof(Vertex, color));
shader->vertexAttribPointer("aTexCoord", 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*)offsetof(Vertex, texCoord));
if (bind) {
glBindVertexArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
}

void ce::Mesh::initVAO(Vertex* vertexArray, GLuint* indexArray) {
glGenBuffers(1, &this->VBO);
glBindBuffer(GL_ARRAY_BUFFER, this->VBO);
glBufferData(GL_ARRAY_BUFFER, this->vertexCount * sizeof(Vertex),
vertexArray, GL_STATIC_DRAW);
glGenBuffers(1, &VBO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, vertexCount * sizeof(Vertex), vertexArray, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);

if (indexCount > 0) {
glGenBuffers(1, &this->EBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, this->EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, this->indexCount * sizeof(GLuint),
indexArray, GL_STATIC_DRAW);
glGenBuffers(1, &EBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indexCount * sizeof(GLuint), indexArray, GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
}
}
}
3 changes: 1 addition & 2 deletions engine/rendering/mesh.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

namespace ce {
class Mesh {

private:
unsigned vertexCount, indexCount;
GLuint VAO, VBO, EBO;
Expand All @@ -17,7 +16,7 @@ namespace ce {
Mesh(Vertex* vertexArray, const unsigned vertexCount,
GLuint* indexArray = NULL, const unsigned indexCount = 0);
~Mesh();
void sendToShader(class Shader* shader);
void sendToShader(class Shader* shader, bool bind = true);

unsigned GetIndexCount() { return indexCount; };
void bind(), unbind();
Expand Down
Loading