pull/1/head
E. Almqvist 2 years ago
parent d184564062
commit e38192b594
  1. 26
      headers/renderer.hpp
  2. 43
      src/renderer.cpp

@ -1,7 +1,9 @@
#pragma once #pragma once
#include <glm/fwd.hpp> #include <glm/fwd.hpp>
#include <glm/glm.hpp>
#include <vector> #include <vector>
#include "shaders.hpp" #include "shaders.hpp"
#include "textures.hpp" #include "textures.hpp"
@ -12,21 +14,33 @@
#define VERT_SHADER_SRC_FILE "shaders/vertex.glsl" #define VERT_SHADER_SRC_FILE "shaders/vertex.glsl"
#define FRAG_SHADER_SRC_FILE "shaders/fragment.glsl" #define FRAG_SHADER_SRC_FILE "shaders/fragment.glsl"
#define RUSTY_METAL_TEXTURE "assets/textures/rusty_metal.jpg" #define DEFAULT_FOV 45.0f
#define NEAR_PLANE 0.1f
#define FAR_PLANE 100.0f
namespace Renderer { namespace Renderer {
class RenderObject {}; class RenderObject {
public:
RenderObject();
void render(GLFWwindow* win, glm::mat4 cameraTransform, glm::mat4 projectionTransform);
private:
std::vector<float> verts;
std::vector<unsigned int> indices;
};
class Renderer3D { class Renderer3D {
public: public:
Renderer3D(); Renderer3D(GLFWwindow* win);
Renderer3D(std::vector<RenderObject> ROs); Renderer3D(GLFWwindow* win, std::vector<RenderObject> ROs);
void setCamera(glm::vec3 pos); void setCamera(glm::vec3 pos);
void setFOV(float deg); void setFOV(float deg);
void render(GLFWwindow* win); void render();
private: private:
std::vector<RenderObject> RenderObjects; std::vector<RenderObject> renderObjects = std::vector<RenderObject>();
GLFWwindow* window;
glm::mat4 cameraTransform = glm::mat4(1.0f);
glm::mat4 projectionTransform = glm::mat4(1.0f);
}; };
class Obj2D { class Obj2D {

@ -2,19 +2,45 @@
#include <glm/glm.hpp> #include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp> #include <glm/gtc/matrix_transform.hpp>
#include "GLFW/glfw3.h"
#include "renderer.hpp" #include "renderer.hpp"
#include "shaders.hpp" #include "shaders.hpp"
namespace Renderer { namespace Renderer {
Renderer3D::Renderer3D() { // Renderer3D
// TODO: Make more OOP Renderer3D::Renderer3D(GLFWwindow* win) {
window = win;
setFOV(DEFAULT_FOV);
}
Renderer3D::Renderer3D(GLFWwindow* win, std::vector<RenderObject> ROs) : Renderer3D(win) {
renderObjects = ROs;
}
void Renderer3D::setFOV(float fov) {
int width, height;
glfwGetWindowSize(window, &width, &height);
projectionTransform = glm::perspective(glm::radians(fov), (float)width / (float)height, NEAR_PLANE, FAR_PLANE);
}
void Renderer3D::setCamera(glm::vec3 pos) {
cameraTransform = glm::translate(cameraTransform, pos);
} }
Renderer3D::Renderer3D(std::vector<RenderObject> ROs) : Renderer3D() { void Renderer3D::render() {
RenderObjects = ROs; for ( RenderObject ro: renderObjects )
ro.render(window, cameraTransform, projectionTransform);
} }
// RenderObject
RenderObject::RenderObject() {
}
void RenderObject::render(GLFWwindow* win, glm::mat4 cameraTransform, glm::mat4 projectionTransform) {}
void Obj2D::transform(glm::mat4 T) { void Obj2D::transform(glm::mat4 T) {
shader.setMat4("model", T); shader.setMat4("model", T);
} }
@ -49,20 +75,17 @@ namespace Renderer {
Obj2D::Obj2D(unsigned int indices[], unsigned int icount, float verts[], unsigned int vcount) Obj2D::Obj2D(unsigned int indices[], unsigned int icount, float verts[], unsigned int vcount)
: shader(VERT_SHADER_SRC_FILE, FRAG_SHADER_SRC_FILE) { : shader(VERT_SHADER_SRC_FILE, FRAG_SHADER_SRC_FILE) {
// Vertex buffer object
glGenBuffers(1, &VBO);
// Vertex Array Object // Vertex Array Object
glGenVertexArrays(1, &VAO); // gen the VAO glGenVertexArrays(1, &VAO); // gen the VAO
glBindVertexArray(VAO); // bind it glBindVertexArray(VAO); // bind it
// Copy verts into buffer // Copy the verts into the buffer
glGenBuffers(1, &VBO);
glBindBuffer(GL_ARRAY_BUFFER, VBO); glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, vcount, verts, GL_DYNAMIC_DRAW); // for moving stuff glBufferData(GL_ARRAY_BUFFER, vcount, verts, GL_DYNAMIC_DRAW); // for moving stuff
//glBufferData(GL_ARRAY_BUFFER, sizeof(verts), verts, GL_STATIC_DRAW);
// Copy the indices for the verts into another buffer
glGenBuffers(1, &EBO); glGenBuffers(1, &EBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, icount, indices, GL_STATIC_DRAW); glBufferData(GL_ELEMENT_ARRAY_BUFFER, icount, indices, GL_STATIC_DRAW);

Loading…
Cancel
Save