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
#include <glm/fwd.hpp>
#include <glm/glm.hpp>
#include <vector>
#include "shaders.hpp"
#include "textures.hpp"
@ -12,21 +14,33 @@
#define VERT_SHADER_SRC_FILE "shaders/vertex.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 {
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 {
public:
Renderer3D();
Renderer3D(std::vector<RenderObject> ROs);
Renderer3D(GLFWwindow* win);
Renderer3D(GLFWwindow* win, std::vector<RenderObject> ROs);
void setCamera(glm::vec3 pos);
void setFOV(float deg);
void render(GLFWwindow* win);
void render();
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 {

@ -2,19 +2,45 @@
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include "GLFW/glfw3.h"
#include "renderer.hpp"
#include "shaders.hpp"
namespace Renderer {
Renderer3D::Renderer3D() {
// TODO: Make more OOP
// Renderer3D
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() {
RenderObjects = ROs;
void Renderer3D::render() {
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) {
shader.setMat4("model", T);
}
@ -49,20 +75,17 @@ namespace Renderer {
Obj2D::Obj2D(unsigned int indices[], unsigned int icount, float verts[], unsigned int vcount)
: shader(VERT_SHADER_SRC_FILE, FRAG_SHADER_SRC_FILE) {
// Vertex buffer object
glGenBuffers(1, &VBO);
// Vertex Array Object
glGenVertexArrays(1, &VAO); // gen the VAO
glBindVertexArray(VAO); // bind it
// Copy verts into buffer
// Copy the verts into the buffer
glGenBuffers(1, &VBO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
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);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, icount, indices, GL_STATIC_DRAW);

Loading…
Cancel
Save