Dumb boilerplate shit

main
E. Almqvist 2 years ago
parent 259c25a621
commit 1de6a7280b
  1. 20
      headers/renderer.hpp
  2. 26
      headers/window.hpp
  3. 14
      src/controller.cpp
  4. 34
      src/main.cpp
  5. 18
      src/renderer.cpp
  6. 36
      src/window.cpp

@ -5,9 +5,11 @@
#include <vector>
#include "shaders.hpp"
#include "GLFW/glfw3.h"
#include "textures.hpp"
#include "window.hpp"
#include "GLFW/glfw3.h"
#define VERTEX_ATTRIB_PTR_SIZE 8 * sizeof(float)
@ -50,13 +52,13 @@ namespace Renderer {
glm::mat4 projection = glm::mat4(1.0f);
glm::mat4 view = glm::mat4(1.0f);
Camera(GLFWwindow* win);
Camera(GLFWwindow* win, glm::vec3 pos);
Camera(GLFWwindow* win, glm::vec3 pos, glm::vec3 angle);
Camera(Window* win);
Camera(Window* win, glm::vec3 pos);
Camera(Window* win, glm::vec3 pos, glm::vec3 angle);
void setFOV(float deg);
protected:
GLFWwindow* window;
Window* window;
glm::vec3 front = glm::vec3(0.0f, 0.0f, 1.0f);
glm::vec3 up = glm::vec3(0.0f, 1.0f, 0.0f);
@ -67,7 +69,7 @@ namespace Renderer {
class RenderObject : public Object {
public:
RenderObject(std::vector<float> verts, std::vector<unsigned int> indices);
void render(GLFWwindow* win, Camera cam);
void render(Camera cam);
void preRenderHook();
private:
Shaders::Shader shader;
@ -82,8 +84,8 @@ namespace Renderer {
public:
float deltaTime = 0.0f;
Scene(GLFWwindow* win);
Scene(GLFWwindow* win, std::vector<RenderObject*> ROs);
Scene(Window* win);
Scene(Window* win, std::vector<RenderObject*> ROs);
void setCamera(Camera *cam);
void spawnObject(RenderObject *ro);
@ -92,7 +94,7 @@ namespace Renderer {
Camera *camera;
private:
std::vector<RenderObject*> renderObjects = std::vector<RenderObject*>();
GLFWwindow* window;
Window* window;
float lastFrame = 0.0f;
};

@ -1,13 +1,29 @@
#pragma once
#define WINDOW_WIDTH 640
#define WINDOW_HEIGHT 480
#include "GLFW/glfw3.h"
// Default window dimensions
#define D_WINDOW_TITLE "Euclid Engine"
#define D_WINDOW_WIDTH 640
#define D_WINDOW_HEIGHT 480
class Window {
public:
Window();
GLFWwindow* win;
protected:
const char* title = "Euclid Engine";
Window(const char* title);
Window(const char* title, unsigned int w, unsigned int h);
void spawn();
unsigned int width() { return _width; } // getters and dumb and
unsigned int height() { return _height; } // cpp should have readonly fields...
private:
const char* _title = D_WINDOW_TITLE;
unsigned int _width = D_WINDOW_WIDTH;
unsigned int _height = D_WINDOW_HEIGHT;
void updateSize();
void setWidth(unsigned int w) { _width = w; }
void setHeight(unsigned int h) { _height = h; }
};

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

@ -114,43 +114,29 @@ void processInput(GLFWwindow *win) {
}
int main() {
glfwInit(); glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
GLFWwindow* win = glfwCreateWindow(WINDOW_WIDTH, WINDOW_HEIGHT, "Euclid Engine: Demo", NULL, NULL);
if (win == NULL) {
printf("Failed to create a window.\n");
glfwTerminate();
return 1;
}
glfwMakeContextCurrent(win);
Window win("Euclid Engine: Demo");
win.spawn();
if ( !gladLoadGLLoader((GLADloadproc)glfwGetProcAddress) ) {
printf("Failed to init GLAD.\n");
return 1;
}
glViewport(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT);
glfwSetFramebufferSizeCallback(win, framebuffer_size_callback); // Framebuffer
glViewport(0, 0, win.width(), win.height());
glfwSetFramebufferSizeCallback(win.win, framebuffer_size_callback); // Framebuffer
glEnable(GL_DEPTH_TEST);
// Input
glfwSetInputMode(win, GLFW_CURSOR, GLFW_CURSOR_DISABLED); // Disable cursor
glfwSetInputMode(win.win, 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);
// Create scene
Renderer::Scene scene(win);
Renderer::Scene scene(&win);
Renderer::TexturedObject ro(verts, indices);
// ro.setPosition(glm::vec3(0.2f, -1.0f, -8.0f));
Renderer::TexturedObject ro2(verts, indices);
// ro2.setPosition(glm::vec3(0.5f, 0.0, -8.0f));
ro2.setTexture("assets/textures/meep.jpg"); // TODO: fix texture bug
ro.setTexture(RUSTY_METAL_TEXTURE);
@ -159,13 +145,13 @@ int main() {
scene.spawnObject(&ro2);
// Controller test
Controller player(win, glm::vec3(0.0f, 0.0f, -8.0f));
Controller player(&win, glm::vec3(0.0f, 0.0f, -8.0f));
scene.setCamera(&player);
while (!glfwWindowShouldClose(win)) {
while (!glfwWindowShouldClose(win.win)) {
// Handle input
player.processInput(scene.deltaTime);
processInput(win);
processInput(win.win);
ro.translate(glm::vec3(0.0f, 0.0f, 0.001f));
// ro2.setPosition(glm::vec3(0.0f, 0.0f, -1000.0f));
@ -174,7 +160,7 @@ int main() {
scene.render();
// glfw
glfwSwapBuffers(win);
glfwSwapBuffers(win.win);
glfwPollEvents();
}

@ -71,11 +71,11 @@ namespace Renderer {
}
// Scene
Scene::Scene(GLFWwindow* win) {
Scene::Scene(Window* win) {
window = win;
}
Scene::Scene(GLFWwindow* win, std::vector<RenderObject*> ROs) : Scene(win) {
Scene::Scene(Window* win, std::vector<RenderObject*> ROs) : Scene(win) {
renderObjects = ROs;
}
@ -96,30 +96,28 @@ namespace Renderer {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
for ( RenderObject *ro: renderObjects )
ro->render(window, *camera);
ro->render(*camera);
// Record the last frame
lastFrame = curFrame;
}
// Camera
Camera::Camera(GLFWwindow* win) {
Camera::Camera(Window* win) {
window = win;
setFOV(DEFAULT_FOV);
}
Camera::Camera(GLFWwindow* win, glm::vec3 pos) : Camera(win) {
Camera::Camera(Window* win, glm::vec3 pos) : Camera(win) {
setPosition(pos);
}
Camera::Camera(GLFWwindow* win, glm::vec3 pos, glm::vec3 angle) : Camera(win, pos) {
Camera::Camera(Window* win, glm::vec3 pos, glm::vec3 angle) : Camera(win, pos) {
setRotation(angle);
}
void Camera::setFOV(float fov) {
int width, height;
glfwGetWindowSize(window, &width, &height);
projection = glm::perspective(glm::radians(fov), (float)width / (float)height, NEAR_PLANE, FAR_PLANE);
projection = glm::perspective(glm::radians(fov), (float)window->width() / (float)window->height(), NEAR_PLANE, FAR_PLANE);
}
void Camera::updateCameraTransforms() {
@ -169,7 +167,7 @@ namespace Renderer {
void RenderObject::preRenderHook() {}
// TODO: Make prerender instead of render
void RenderObject::render(GLFWwindow* win, Camera cam) {
void RenderObject::render(Camera cam) {
shader.setMat4("modelPosition", positionTransform);
shader.setMat4("modelRotation", rotationTransform);
shader.setMat4("model", modelTransform);

@ -0,0 +1,36 @@
#include "window.hpp"
#include "GLFW/glfw3.h"
#include <cstdlib>
#include <stdio.h>
Window::Window(const char* title) {
this->_title = title;
}
Window::Window(const char* title, unsigned int w, unsigned int h) : Window(title) {
this->_width = w;
this->_height = h;
}
void Window::spawn() {
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
win = glfwCreateWindow(_width, _height, _title, NULL, NULL);
if (win == NULL) {
printf("[ERROR] Failed to create a window.\n");
glfwTerminate();
exit(1);
}
glfwMakeContextCurrent(win);
}
void Window::updateSize() {
int w, h;
glfwGetWindowSize(win, &w, &h);
_width = w;
_height = h;
}
Loading…
Cancel
Save