Skip to content

Commit

Permalink
Merge pull request #14 from Pinkolik/issue-12
Browse files Browse the repository at this point in the history
Refactoring and cleaning up
  • Loading branch information
Pinkolik authored Dec 15, 2022
2 parents 018fcb3 + a753e15 commit e0fb76d
Show file tree
Hide file tree
Showing 30 changed files with 290 additions and 258 deletions.
8 changes: 4 additions & 4 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[submodule "glfw"]
path = glfw
[submodule "dependencies/glfw"]
path = dependencies/glfw
url = [email protected]:glfw/glfw.git
[submodule "glm"]
path = glm
[submodule "dependencies/glm"]
path = dependencies/glm
url = [email protected]:g-truc/glm.git
44 changes: 22 additions & 22 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED True)
set(CMAKE_EXPORT_COMPILE_COMMANDS On)

add_subdirectory(glfw)
add_subdirectory(glm)
add_subdirectory(dependencies/glfw)
add_subdirectory(dependencies/glm)

# file(COPY resources/shaders resources/textures DESTINATION resources)

Expand All @@ -18,26 +18,26 @@ add_executable(Game
src/helpers/stb_image_write.h
src/helpers/json.hpp
src/helpers/tiny_gltf.h
src/engine/shader.h
src/engine/shader.cpp
src/engine/mesh.h
src/engine/mesh.cpp
src/engine/map.cpp
src/engine/map.h
src/engine/model.cpp
src/engine/model.h
src/engine/node.h
src/engine/node.cpp
src/engine/primitive.h
src/engine/primitive.cpp
src/engine/position_struct.h
src/engine/player.h
src/engine/player.cpp
src/engine/intersection/intersection_util.h
src/engine/intersection/intersection_util.cpp
src/engine/intersection/projection.h
src/engine/intersection/projection.cpp
)
src/shader/Shader.h
src/shader/Shader.cpp
src/engine/model/Mesh.h
src/engine/model/Mesh.cpp
src/engine/map/Map.cpp
src/engine/map/Map.h
src/engine/model/Model.cpp
src/engine/model/Model.h
src/engine/model/Node.h
src/engine/model/Node.cpp
src/engine/model/Primitive.h
src/engine/model/Primitive.cpp
src/engine/model/PositionStruct.h
src/engine/player/Player.h
src/engine/player/Player.cpp
src/engine/intersection/IntersectionUtil.h
src/engine/intersection/IntersectionUtil.cpp
src/engine/intersection/Projection.h
src/engine/intersection/Projection.cpp
src/engine/game/GameInstance.cpp src/engine/game/GameInstance.h)
add_custom_command(
TARGET Game POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_directory
Expand Down
Submodule glfw updated from 000000 to dd8a67
Submodule glm updated from 000000 to fc8f4b
68 changes: 68 additions & 0 deletions src/engine/game/GameInstance.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
//
// Created by pinkolik on 12/15/22.
//

#include "GameInstance.h"
#include <iostream>

GameInstance::GameInstance(const char *mapModelPath, const char *playerModelPath, const int width,
const int height) : width(width), height(height) {
map = new Map(mapModelPath);
player = new Player(playerModelPath, map->getSpawnPos());
}

void GameInstance::draw() {
shader.use();
shader.setBool("debug", false);
glm::mat4 projection = glm::perspective(
glm::radians(45.0f), (float) width / (float) height, 0.1f, 100.0f);
shader.setMatrix4f("projection", projection);

glm::mat4 view = player->getViewMatrix();
shader.setMatrix4f("view", view);

map->getModel().draw(shader);

shader.setBool("debug", true);
player->getModel().draw(shader);
shader.setBool("debug", false);
}

void GameInstance::tick(GLFWwindow *window, const float deltaTime) {
glm::vec3 gravity = glm::vec3(0, GRAVITY * fallTime * fallTime, 0);
std::cout << "Fall time: " << fallTime << std::endl;
glm::vec3 move = player->processKeyboard(window, deltaTime);
player->applyForce(gravity);
player->applyForce(move);

bool first = true;
while (true) {
glm::vec3 *mtv = map->getModel().getMinimumTranslationVec(player->getModel(), gravity);
if (mtv != nullptr) {
std::cout << "Applying force: " << mtv->x << ", " << mtv->y << ", " << mtv->z << std::endl;
player->applyForce(*mtv);
delete mtv;
fallTime = 0.1f;
} else if (first) {
fallTime += deltaTime;
first = false;
}
mtv = map->getModel().getMinimumTranslationVec(player->getModel(), move);
if (mtv != nullptr) {
std::cout << "Applying force: " << mtv->x << "," << mtv->y << "," << mtv->z << std::endl;
player->applyForce(*mtv);
delete mtv;
} else {
break;
}
}
}

void GameInstance::processMouseMovement(float xOffset, float yOffset) {
player->processMouseMovement(xOffset, yOffset);
}

void GameInstance::processResize(const int newWidth, const int newHeight) {
width = newWidth;
height = newHeight;
}
41 changes: 41 additions & 0 deletions src/engine/game/GameInstance.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
//
// Created by pinkolik on 12/15/22.
//

#ifndef GAME_GAMEINSTANCE_H
#define GAME_GAMEINSTANCE_H


#include "../map/Map.h"
#include "../player/Player.h"
#include "../../shader/Shader.h"

class GameInstance {
public:
GameInstance(const char *mapModelPath, const char *playerModelPath, int width, int height);

void draw();

void tick(GLFWwindow *window, float deltaTime);

void processMouseMovement(float xOffset, float yOffset);

void processResize(int newWidth, int newHeight);

private:
const float GRAVITY = -0.05f;

int width;
int height;

Map *map;
Player *player;

float fallTime = 0.1f;

Shader shader = Shader("resources/shaders/vShader.glsl",
"resources/shaders/fShader.glsl");
};


#endif //GAME_GAMEINSTANCE_H
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "intersection_util.h"
#include "IntersectionUtil.h"
#include "glm/geometric.hpp"
#include "projection.h"
#include "Projection.h"
#include <cmath>
#include <glm/glm.hpp>
#include <vector>
Expand All @@ -27,7 +27,7 @@ glm::vec3 *IntersectionUtil::getMinimumTranslationVec(
Projection firstProj = Projection(axis, firstTriangle);
Projection secondProj = Projection(axis, secondTriangle);
if (!firstProj.isIntersecting(secondProj)) {
return NULL;
return nullptr;
}
float intersectionLen = firstProj.findIntersectionLength(secondProj);
if (axis == firstTriangleNormal) {
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "projection.h"
#include "Projection.h"
#include <cmath>
#include <glm/glm.hpp>

Expand All @@ -14,10 +14,6 @@ Projection::Projection(glm::vec3 axis, std::vector<glm::vec3> &triangle) {
}
}

float Projection::getMin() { return min; }

float Projection::getMax() { return max; }

bool Projection::isIntersecting(Projection &other) {
if (min < other.max) {
return max >= other.min;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@ class Projection {
public:
Projection(glm::vec3 axis, std::vector<glm::vec3> &triangle);

float getMin();

float getMax();

bool isIntersecting(Projection &other);

float findIntersectionLength(Projection &other);
Expand Down
2 changes: 0 additions & 2 deletions src/engine/map.cpp

This file was deleted.

18 changes: 0 additions & 18 deletions src/engine/map.h

This file was deleted.

14 changes: 14 additions & 0 deletions src/engine/map/Map.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include "Map.h"

Map::Map(const char *mapModelPath) :
model(Model(mapModelPath)) {
model.buffer();
}

Model &Map::getModel() {
return model;
}

glm::vec3 Map::getSpawnPos() {
return model.getSpawnPos();
}
19 changes: 19 additions & 0 deletions src/engine/map/Map.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#ifndef MAP_H
#define MAP_H

#include "../model/Model.h"

class Map {
public:
explicit Map(const char *mapModelPath);

glm::vec3 getSpawnPos();

Model &getModel();

private:

Model model;
};

#endif
2 changes: 1 addition & 1 deletion src/engine/mesh.cpp → src/engine/model/Mesh.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "mesh.h"
#include "Mesh.h"

Mesh::Mesh(std::vector<Primitive> &primitives) : primitives(primitives) {}

Expand Down
4 changes: 2 additions & 2 deletions src/engine/mesh.h → src/engine/model/Mesh.h
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#ifndef MESH_H
#define MESH_H

#include "primitive.h"
#include "Primitive.h"
#include <vector>

class Mesh {
public:
Mesh(std::vector<Primitive> &primitives);
explicit Mesh(std::vector<Primitive> &primitives);

std::vector<Primitive> &getPrimitives();

Expand Down
30 changes: 14 additions & 16 deletions src/engine/model.cpp → src/engine/model/Model.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "model.h"
#include "Model.h"

#include "vertex.h"
#include "Vertex.h"
#include <iostream>
#include <stdexcept>
#include <string>
Expand All @@ -9,7 +9,7 @@
#define STB_IMAGE_IMPLEMENTATION
#define STB_IMAGE_WRITE_IMPLEMENTATION

#include "../helpers/tiny_gltf.h"
#include "../../helpers/tiny_gltf.h"

Model::Model(const char *path) { load(path); }

Expand All @@ -24,7 +24,7 @@ glm::vec3 Model::getSpawnPos() {
}

glm::vec3 *Model::getMinimumTranslationVec(Model &other, glm::vec3 direction) {
glm::vec3 *res = NULL;
glm::vec3 *res = nullptr;
float minDot = INFINITY;
int i = 0;
glm::vec3 normDir = glm::normalize(direction);
Expand All @@ -39,7 +39,7 @@ glm::vec3 *Model::getMinimumTranslationVec(Model &other, glm::vec3 direction) {
std::cout << "dot " << i++ << " = " << dot << std::endl;
if (dot < minDot) {
minDot = dot;
if (res != NULL) {
if (res != nullptr) {
delete res;
}
res = mtv;
Expand Down Expand Up @@ -88,8 +88,8 @@ void Model::load(const char *path) {
}

tinygltf::Scene &scene = gltfModel.scenes[gltfModel.defaultScene];
for (size_t i = 0; i < scene.nodes.size(); i++) {
tinygltf::Node &gltfNode = gltfModel.nodes[scene.nodes[i]];
for (int i : scene.nodes) {
tinygltf::Node &gltfNode = gltfModel.nodes[i];
Node node = processNode(gltfModel, gltfNode);
nodes.push_back(node);
}
Expand All @@ -104,8 +104,8 @@ Node Model::processNode(tinygltf::Model &gltfModel, tinygltf::Node &gltfNode) {
Mesh mesh = processMesh(gltfModel, gltfMesh);

Node node = Node(rotation, scale, translation, mesh);
for (size_t i = 0; i < gltfNode.children.size(); i++) {
tinygltf::Node &gltfChildNode = gltfModel.nodes[gltfNode.children[i]];
for (int i : gltfNode.children) {
tinygltf::Node &gltfChildNode = gltfModel.nodes[i];
Node childNode = processNode(gltfModel, gltfChildNode);
node.addChild(childNode);
}
Expand All @@ -114,20 +114,18 @@ Node Model::processNode(tinygltf::Model &gltfModel, tinygltf::Node &gltfNode) {

Mesh Model::processMesh(tinygltf::Model &gltfModel, tinygltf::Mesh &gltfMesh) {
std::vector<Primitive> primitives;
for (size_t i = 0; i < gltfMesh.primitives.size(); i++) {
tinygltf::Primitive &gltfPrimitive = gltfMesh.primitives[i];

for (auto & gltfPrimitive : gltfMesh.primitives) {
std::vector<std::vector<float>> normals;
std::vector<std::vector<float>> positions;
std::vector<std::vector<float>> texcoord;
for (auto &attrib: gltfPrimitive.attributes) {
std::cout << attrib.first << std::endl;
unsigned int accessor = attrib.second;
if (attrib.first.compare("NORMAL") == 0) {
if (attrib.first == "NORMAL") {
normals = createFloatArrayVector(gltfModel, accessor, 3);
} else if (attrib.first.compare("POSITION") == 0) {
} else if (attrib.first == "POSITION") {
positions = createFloatArrayVector(gltfModel, accessor, 3);
} else if (attrib.first.compare("TEXCOORD_0") == 0) {
} else if (attrib.first == "TEXCOORD_0") {
texcoord = createFloatArrayVector(gltfModel, accessor, 2);
}
}
Expand All @@ -136,7 +134,7 @@ Mesh Model::processMesh(tinygltf::Model &gltfModel, tinygltf::Mesh &gltfMesh) {

std::vector<Vertex> vertices;
for (size_t j = 0; j < normals.size(); j++) {
Vertex vertex;
Vertex vertex{};
vertex.position =
glm::vec3(positions[j][0], positions[j][1], positions[j][2]);
vertex.normal = glm::vec3(normals[j][0], normals[j][1], normals[j][2]);
Expand Down
Loading

0 comments on commit e0fb76d

Please sign in to comment.