Minor refactor

pull/6/head
E. Almqvist 2 years ago
parent deeb54913f
commit 218d6a7fe3
  1. 17
      headers/window.hpp
  2. 20
      src/controller.cpp
  3. 222
      src/main.cpp
  4. 2
      src/renderer.cpp
  5. 4
      src/textures.cpp
  6. 18
      src/window.cpp

@ -1,7 +1,7 @@
#pragma once
// #include "renderer.hpp"
#include <glad/glad.h>
#include "GLFW/glfw3.h"
#include <map>
// Default window dimensions
#define D_WINDOW_TITLE "Euclid Engine"
@ -11,19 +11,24 @@
class Window {
public:
GLFWwindow* win;
// Renderer::Camera cam;
Window(const char* title);
Window(const char* title, unsigned int w, unsigned int h);
~Window();
void spawn();
unsigned int width() { return _width; } // getters and dumb and
unsigned int height() { return _height; } // cpp should have readonly fields...
unsigned int getWidth() { return _width; } // getters and dumb and
unsigned int getHeight() { return _height; } // cpp should have readonly fields...
GLFWwindow* getWindow() { return _win; }
void updateSize(int w, int h);
void makeCurrent();
void swapBuffers();
private:
GLFWwindow* _win;
static std::map<GLFWwindow*, Window*> windowMap;
const char* _title = D_WINDOW_TITLE;
unsigned int _width = D_WINDOW_WIDTH;
unsigned int _height = D_WINDOW_HEIGHT;

@ -10,15 +10,15 @@
void Controller::processMouseInput(float deltaTime) {
double x, y;
glfwGetCursorPos(window->win, &x, &y);
glfwGetCursorPos(window->getWindow(), &x, &y);
if (firstMouseInput) {
lastX = x;
lastY = y;
firstMouseInput = false;
}
}
float xOffset = x - lastX;
float xOffset = x - lastX;
float yOffset = y - lastY;
lastX = x;
lastY = y;
@ -49,24 +49,24 @@ void Controller::processMouseInput(float deltaTime) {
void Controller::processInput(float deltaTime) {
processMouseInput(deltaTime);
float cameraSpeed = deltaTime * 2.5f;
float cameraSpeed = deltaTime * 2.5f;
if (glfwGetKey(window->win, GLFW_KEY_W) == GLFW_PRESS) {
if (glfwGetKey(window->getWindow(), GLFW_KEY_W) == GLFW_PRESS) {
translate(cameraSpeed * front);
}
if (glfwGetKey(window->win, GLFW_KEY_S) == GLFW_PRESS) {
if (glfwGetKey(window->getWindow(), GLFW_KEY_S) == GLFW_PRESS) {
translate(-cameraSpeed * front);
}
if (glfwGetKey(window->win, GLFW_KEY_A) == GLFW_PRESS) {
if (glfwGetKey(window->getWindow(), GLFW_KEY_A) == GLFW_PRESS) {
translate(-glm::normalize(glm::cross(front, up)) * cameraSpeed);
}
if (glfwGetKey(window->win, GLFW_KEY_D) == GLFW_PRESS) {
if (glfwGetKey(window->getWindow(), GLFW_KEY_D) == GLFW_PRESS) {
translate(glm::normalize(glm::cross(front, up)) * cameraSpeed);
}
if (glfwGetKey(window->win, GLFW_KEY_SPACE) == GLFW_PRESS) {
if (glfwGetKey(window->getWindow(), GLFW_KEY_SPACE) == GLFW_PRESS) {
translate(cameraSpeed * up);
}
if (glfwGetKey(window->win, GLFW_KEY_LEFT_SHIFT) == GLFW_PRESS) {
if (glfwGetKey(window->getWindow(), GLFW_KEY_LEFT_SHIFT) == GLFW_PRESS) {
translate(cameraSpeed * -up);
}
}

@ -14,92 +14,92 @@
#define RUSTY_METAL_TEXTURE "assets/textures/rusty_metal.jpg"
std::vector<float> verts({
-0.5f, -0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f,
0.5f, -0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f,
0.5f, 0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f,
-0.5f, -0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f,
0.5f, -0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f,
0.5f, 0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f,
0.5f, 0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f,
-0.5f, 0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f,
-0.5f, -0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f,
0.5f, 0.5f, -0.5f, 1.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, 1, 3,
1, 2, 3,
5, 6, 7,
7, 8, 9,
9, 10, 11,
11, 12, 13,
0, 1, 3,
3, 4, 5,
5, 6, 7,
7, 8, 9,
9, 10, 11,
11, 12, 13,
0, 1, 3,
3, 4, 5,
5, 6, 7,
7, 8, 9,
9, 10, 11,
11, 12, 13,
0, 1, 3,
3, 4, 5,
5, 6, 7,
7, 8, 9,
9, 10, 11,
11, 12, 13,
0, 1, 3,
3, 4, 5,
5, 6, 7,
7, 8, 9,
9, 10, 11,
11, 12, 13,
0, 1, 3,
1, 2, 3,
5, 6, 7,
7, 8, 9,
9, 10, 11,
11, 12, 13,
0, 1, 3,
3, 4, 5,
5, 6, 7,
7, 8, 9,
9, 10, 11,
11, 12, 13,
0, 1, 3,
3, 4, 5,
5, 6, 7,
7, 8, 9,
9, 10, 11,
11, 12, 13,
0, 1, 3,
3, 4, 5,
5, 6, 7,
7, 8, 9,
9, 10, 11,
11, 12, 13,
0, 1, 3,
3, 4, 5,
5, 6, 7,
7, 8, 9,
9, 10, 11,
11, 12, 13,
});
@ -108,68 +108,68 @@ Window win("Euclid Engine: Demo");
Renderer::Scene scene(&win);
void framebuffer_size_callback(GLFWwindow* glfwWindow, int w, int h) {
win.updateSize(w, h);
win.updateSize(w, h);
}
void processInput(GLFWwindow *win) {
int action = glfwGetKey(win, GLFW_KEY_ESCAPE);
if (action == GLFW_PRESS) {
glfwSetWindowShouldClose(win, true);
}
int action = glfwGetKey(win, GLFW_KEY_ESCAPE);
if (action == GLFW_PRESS) {
glfwSetWindowShouldClose(win, true);
}
}
int main() {
// Spawn window
win.spawn();
// Spawn window
win.spawn();
if ( !gladLoadGLLoader((GLADloadproc)glfwGetProcAddress) ) {
printf("Failed to init GLAD.\n");
return 1;
}
if ( !gladLoadGLLoader((GLADloadproc)glfwGetProcAddress) ) {
printf("Failed to init GLAD.\n");
return 1;
}
glViewport(0, 0, win.width(), win.height());
glfwSetFramebufferSizeCallback(win.win, framebuffer_size_callback); // Framebuffer
glViewport(0, 0, win.getWidth(), win.getHeight());
glfwSetFramebufferSizeCallback(win.getWindow(), framebuffer_size_callback); // Framebuffer
glEnable(GL_DEPTH_TEST);
glEnable(GL_DEPTH_TEST);
// Input
glfwSetInputMode(win.win, GLFW_CURSOR, GLFW_CURSOR_DISABLED); // Disable cursor
// Input
glfwSetInputMode(win.getWindow(), GLFW_CURSOR, GLFW_CURSOR_DISABLED); // Disable cursor
float borderColor[] = {1.0f, 1.0f, 1.0f, 1.0f};
glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, borderColor);
float borderColor[] = {1.0f, 1.0f, 1.0f, 1.0f};
glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, borderColor);
// Create scene
Renderer::TexturedObject ro(verts, indices);
Renderer::TexturedObject ro2(verts, indices);
// Create scene
Renderer::TexturedObject ro(verts, indices);
Renderer::TexturedObject ro2(verts, indices);
ro2.setTexture("assets/textures/meep.jpg"); // TODO: fix texture bug
ro.setTexture(RUSTY_METAL_TEXTURE);
ro2.setTexture("assets/textures/meep.jpg"); // TODO: fix texture bug
ro.setTexture(RUSTY_METAL_TEXTURE);
scene.spawnObject(&ro);
scene.spawnObject(&ro2);
scene.spawnObject(&ro);
scene.spawnObject(&ro2);
// Controller test
Controller player(&win, glm::vec3(0.0f, 0.0f, 8.0f));
scene.setCamera(&player);
// Controller test
Controller player(&win, glm::vec3(0.0f, 0.0f, 8.0f));
scene.setCamera(&player);
while (!glfwWindowShouldClose(win.win)) {
// Handle input
player.processInput(scene.deltaTime);
processInput(win.win);
while (!glfwWindowShouldClose(win.getWindow())) {
// Handle input
player.processInput(scene.deltaTime);
processInput(win.getWindow());
ro.translate(glm::vec3(0.0f, 0.0f, 0.001f));
// ro2.translate(glm::vec3(0.0f, -0.01f, 0.01f));
ro2.translate(glm::vec3(0.0f, 0.0f, -0.001f));
ro2.rotate(glm::vec3(1.01f, 1.0f, 1.0f));
ro.translate(glm::vec3(0.0f, 0.0f, 0.001f));
// ro2.translate(glm::vec3(0.0f, -0.01f, 0.01f));
// ro2.translate(glm::vec3(0.0f, 0.0f, -0.001f));
ro2.rotate(glm::vec3(1.01f, 1.0f, 1.0f));
// Render new frame
scene.render();
// Render new frame
scene.render();
// glfw
glfwSwapBuffers(win.win);
glfwPollEvents();
}
// glfw
glfwSwapBuffers(win.getWindow());
glfwPollEvents();
}
glfwTerminate();
return 0;
glfwTerminate();
return 0;
}

@ -120,7 +120,7 @@ namespace Renderer {
}
glm::mat4 Camera::getProjection() {
projection = glm::perspective(glm::radians(FOV), (float)window->width() / (float)window->height(), NEAR_PLANE, FAR_PLANE);
projection = glm::perspective(glm::radians(FOV), (float)window->getWidth() / (float)window->getHeight(), NEAR_PLANE, FAR_PLANE);
return projection;
}

@ -1,5 +1,5 @@
#include <stdio.h>
#include "../headers/textures.hpp"
#include "textures.hpp"
#include <glad/glad.h>
#define STB_IMAGE_IMPLEMENTATION
@ -30,7 +30,7 @@ namespace Textures {
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
unsigned char* data = stbi_load(texture_src, &width, &height, &nrChannels, 0);
if (data) {
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
glGenerateMipmap(GL_TEXTURE_2D);

@ -13,6 +13,10 @@ Window::Window(const char* title, unsigned int w, unsigned int h) : Window(title
this->_height = h;
}
Window::~Window() {
glfwDestroyWindow(_win);
}
void Window::spawn() {
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
@ -20,14 +24,14 @@ void Window::spawn() {
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_FLOATING, GL_TRUE);
win = glfwCreateWindow(_width, _height, _title, NULL, NULL);
if (win == NULL) {
_win = glfwCreateWindow(_width, _height, _title, NULL, NULL);
if (_win == NULL) {
printf("[ERROR] Failed to create a window.\n");
glfwTerminate();
exit(1);
}
glfwMakeContextCurrent(win);
glfwMakeContextCurrent(_win);
}
void Window::updateSize(int w, int h) {
@ -35,3 +39,11 @@ void Window::updateSize(int w, int h) {
_height = h;
glViewport(0, 0, w, h);
}
void Window::makeCurrent() {
glfwMakeContextCurrent(_win);
}
void Window::swapBuffers() {
glfwSwapBuffers(_win);
}

Loading…
Cancel
Save