Skip to content

Commit

Permalink
template: add rotate/translate
Browse files Browse the repository at this point in the history
  • Loading branch information
PottierLoic committed Mar 19, 2024
1 parent 3dc3fa1 commit 4bfb09d
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 78 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Ignore space build
/build
**/build

# TODO: Remove this and bring models once the default ones (cubes etc.. are done)
/models
Expand All @@ -8,3 +8,4 @@
.vscode/
.idea/
*.DS_Store

1 change: 1 addition & 0 deletions core/scene/scene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Scene::Scene() {
Entity* defaultCamera = new Entity("Default Camera");
defaultCamera->addComponent<Camera>();
this->addEntity(defaultCamera);
this->selectedCamera = defaultCamera->getComponent<Camera>();
}

void Scene::addEntity(Entity* ent) {
Expand Down
12 changes: 8 additions & 4 deletions core/scene/scene.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,22 @@ class Scene{
// Vector of pointers to Entity contained within the scene
std::vector<Entity*> entities;

Camera selectedCamera = nullptr; // Main camera component in the scene. Used to render game.
// Main camera component in the scene. Used to render game.
Camera* selectedCamera = nullptr;

Scene(); // Initializes a new empty scene with a default name.
/**
* @brief Initialize a Scene with default name.
*/
Scene();

/**
* Add an Entity to the scene.
* @brief Add an Entity to the scene.
* @param ent: Reference to the entity.
*/
void addEntity(Entity* ent);

/**
* Remove an Entity from the scene.
* @brief Remove an Entity from the scene.
* @param ent: Reference to the entity.
*/
void removeEntity(Entity* ent);
Expand Down
Empty file added core/shader/grid/grid_shader.fs
Empty file.
Empty file added core/shader/grid/grid_shader.vs
Empty file.
12 changes: 6 additions & 6 deletions editor/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ int main() {

// TODO: REMOVE OPENGL TESTS
// TODO: Find a way to use better path.
Shader testShader("../../shaders/test.vs", "../../shaders/test.fs", nullptr);
Shader shader("../../shaders/test.vs", "../../shaders/test.fs", nullptr);

// Main loop
while (!glfwWindowShouldClose(window)) {
Expand All @@ -191,11 +191,11 @@ int main() {
glClearColor(0.45f, 0.55f, 0.60f, 1.00f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

testShader.use();
shader.use();
glm::mat4 projection = glm::perspective(glm::radians(camera.zoom), 1280.0f / 720.0f, 0.1f, 100.0f);
glm::mat4 view = camera.getViewMatrix();
testShader.setMat4("projection", projection);
testShader.setMat4("view", view);
shader.setMat4("projection", projection);
shader.setMat4("view", view);

for (auto& entity : scene.entities) {
ModelRenderer* modelRenderer = entity->getComponent<ModelRenderer>();
Expand All @@ -207,8 +207,8 @@ int main() {
model = glm::rotate(model, static_cast<float>(glm::radians(tf->rotation.x)), glm::vec3(1.0f, 0.0f, 0.0f));
model = glm::rotate(model, static_cast<float>(glm::radians(tf->rotation.y)), glm::vec3(0.0f, 1.0f, 0.0f));
model = glm::rotate(model, static_cast<float>(glm::radians(tf->rotation.z)), glm::vec3(0.0f, 0.0f, 1.0f));
testShader.setMat4("model", model);
modelRenderer->model->draw(testShader);
shader.setMat4("model", model);
modelRenderer->model->draw(shader);
}
}

Expand Down
2 changes: 1 addition & 1 deletion project_template/build-mingw.bat
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
@echo off
cmake -DBUILD_SHARED_LIBS=OFF -G "MinGW Makefiles" -B "./build" . -DFLAGS=%1
cmake -DBUILD_SHARED_LIBS=OFF -G "MinGW Makefiles" -B "./build" .

cd build
make
Expand Down
96 changes: 30 additions & 66 deletions project_template/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

#include "shader/shader.hpp"
#include "scene/scene.hpp"
#include "camera/camera.hpp" // maybe not stay here
#include "model/model.hpp" // maybe removed soon

/* TODO: REMOVE */
Expand All @@ -15,6 +14,9 @@
#include "component/component.hpp"
#include "component/transform.hpp"
#include "component/physic.hpp"
#include "component/model_renderer.hpp"
#include "component/camera.hpp"


// OPENGL TEST TODO REMOVE
#define STB_IMAGE_IMPLEMENTATION
Expand All @@ -26,55 +28,16 @@

using namespace SpaceEngine;

// Camera
Camera camera(glm::vec3(0.0f, 0.0f, 3.0f));
float lastX = 1280.0f / 2.0;
float lastY = 720.0 / 2.0;
bool firstMouse = true;

// timing
float deltaTime = 0.0f;
float lastFrame = 0.0f;

void processInput(GLFWwindow* window) {
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
glfwSetWindowShouldClose(window, true);
if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS)
camera.processKeyboard(FORWARD, deltaTime);
if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS)
camera.processKeyboard(BACKWARD, deltaTime);
if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS)
camera.processKeyboard(LEFT, deltaTime);
if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS)
camera.processKeyboard(RIGHT, deltaTime);
}

/* DEBUG */

void framebufferSizeCallback(GLFWwindow* /*window*/, int width, int height) {
glViewport(0, 0, width, height);
}

void mouseCallback(GLFWwindow* /*window*/, double xpos, double ypos) {
if (firstMouse) {
lastX = xpos;
lastY = xpos;
firstMouse = false;
}

float xoffset = xpos - lastX;
float yoffset = lastY - ypos;

lastX = xpos;
lastY = ypos;

camera.processMouseMovement(xoffset, yoffset);
}

void scrollCallback(GLFWwindow* /*window*/, double /*xoffset*/, double yoffset) {
camera.processMouseScroll(static_cast<float>(yoffset));
}

int main() {
// Initialize GLFW
if (!glfwInit()) {
Expand All @@ -89,12 +52,10 @@ int main() {
glfwTerminate();
return -1;
}

glfwMakeContextCurrent(window);
// glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
glfwSetFramebufferSizeCallback(window, framebufferSizeCallback);
glfwSetCursorPosCallback(window, mouseCallback);
glfwSetScrollCallback(window, scrollCallback);


// Initialize Glad
if (!gladLoadGLLoader(reinterpret_cast<GLADloadproc>(glfwGetProcAddress))) {
Expand All @@ -109,52 +70,55 @@ int main() {
stbi_set_flip_vertically_on_load(true);

glEnable(GL_DEPTH_TEST);
// TODO: REMOVE
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);

//TODO : Remove too
// Scene creation
Scene scene = Scene();
scene.addEntity(new Entity("Test1"));
scene.entities[0]->addComponent<Physic>();
scene.addEntity(new Entity("Backpack"));
scene.entities[1]->addComponent<ModelRenderer>();
scene.entities[1]->getComponent<ModelRenderer>()->model = new Model("../../models/backpack/backpack.obj");

// TODO: REMOVE OPENGL TESTS
// TODO: Find a way to use better path.
Shader testShader("../../shaders/test.vs", "../../shaders/test.fs", nullptr);
Shader shader("../../shaders/test.vs", "../../shaders/test.fs", nullptr);

// TODO: REMOVE TEST MODEL IMPORT
// TODO: Find a way to use better path.
Model testModel("../../models/backpack/backpack.obj");


// Main loop
while (!glfwWindowShouldClose(window)) {
float currentFrame = static_cast<float>(glfwGetTime());
deltaTime = currentFrame - lastFrame;
lastFrame = currentFrame;

processInput(window);


int display_w, display_h;
glfwGetFramebufferSize(window, &display_w, &display_h);
glViewport(0, 0, display_w, display_h);
glClearColor(0.45f, 0.55f, 0.60f, 1.00f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

testShader.use();

glm::mat4 projection = glm::perspective(glm::radians(camera.zoom), 1280.0f / 720.0f, 0.1f, 100.0f);
glm::mat4 view = camera.getViewMatrix();
testShader.setMat4("projection", projection);
testShader.setMat4("view", view);

// render model
glm::mat4 model = glm::mat4(1.0f);
model = glm::translate(model, glm::vec3(0.0f, 0.0f, 0.0f));
model = glm::scale(model, glm::vec3(1.0f, 1.0f, 1.0f));
testShader.setMat4("model", model);
testModel.draw(testShader);
shader.use();

glm::mat4 projection = glm::perspective(glm::radians(scene.selectedCamera->zoom), 1280.0f / 720.0f, 0.1f, 100.0f);
glm::mat4 view = scene.selectedCamera->getViewMatrix();
shader.setMat4("projection", projection);
shader.setMat4("view", view);

for (auto& entity : scene.entities) {
ModelRenderer* modelRenderer = entity->getComponent<ModelRenderer>();
if (modelRenderer != nullptr) {
Transform* tf = entity->getComponent<Transform>();
glm::mat4 model = glm::mat4(1.0f);
model = glm::translate(model, glm::vec3(tf->position.x, tf->position.y, tf->position.z));
model = glm::scale(model, glm::vec3(tf->scale.x, tf->scale.y, tf->scale.z));
model = glm::rotate(model, static_cast<float>(glm::radians(tf->rotation.x)), glm::vec3(1.0f, 0.0f, 0.0f));
model = glm::rotate(model, static_cast<float>(glm::radians(tf->rotation.y)), glm::vec3(0.0f, 1.0f, 0.0f));
model = glm::rotate(model, static_cast<float>(glm::radians(tf->rotation.z)), glm::vec3(0.0f, 0.0f, 1.0f));
shader.setMat4("model", model);
modelRenderer->model->draw(shader);
}
}

glfwSwapBuffers(window);
glfwPollEvents();
Expand Down

0 comments on commit 4bfb09d

Please sign in to comment.