Merge pull request #1 from E-Almqvist/refactor

Renderer Refactor
pull/2/head
E. Almqvist 2 years ago committed by GitHub
commit 92b0e38468
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 48
      headers/renderer.hpp
  2. 7
      shaders/fragment.glsl
  3. 6
      shaders/vertex.glsl
  4. 20
      src/main.cpp
  5. 101
      src/renderer.cpp
  6. 4
      src/shaders.cpp
  7. 2
      src/textures.cpp

@ -1,7 +1,9 @@
#pragma once
#include <glm/fwd.hpp>
#include <glm/glm.hpp>
#include <vector>
#include "shaders.hpp"
#include "textures.hpp"
@ -12,37 +14,49 @@
#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(std::vector<float> verts, std::vector<unsigned int> indices);
void render(GLFWwindow* win, glm::mat4 cameraTransform, glm::mat4 projectionTransform);
void transform(glm::mat4 T);
void preRenderHook();
private:
Shaders::Shader shader;
unsigned int EBO;
unsigned int VBO;
unsigned int VAO;
std::vector<float> vertsVec;
std::vector<unsigned int> indicesVec;
};
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 spawnObject(RenderObject ro);
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 {
class Obj2D : public RenderObject {
public:
Shaders::Shader shader;
Textures::Texture2D texture;
Obj2D(unsigned int indices[], unsigned int icount, float verts[], unsigned int vcount);
using RenderObject::RenderObject;
void setTexture(const char* t_src);
void transform(glm::mat4 T);
void render(GLFWwindow* win);
void preRenderHook();
private:
Textures::Texture2D texture;
void bind_texture(Textures::Texture2D texture);
unsigned int EBO;
unsigned int VBO;
unsigned int VAO;
};
}

@ -6,11 +6,8 @@ in vec4 VertexColor;
in vec2 TexCoord;
uniform sampler2D inpTexture;
uniform float r;
uniform float g;
uniform float b;
uniform vec3 rgbOffset = vec3(0.0, 0.0, 0.0);
void main() {
FragColor = texture(inpTexture, TexCoord) * (VertexColor + vec4(r, g, b, 1.0f));
FragColor = texture(inpTexture, TexCoord) * (VertexColor + vec4(rgbOffset, 1.0));
}

@ -6,9 +6,9 @@ layout (location = 2) in vec2 aTexCoord;
out vec4 VertexColor;
out vec2 TexCoord;
uniform mat4 model = mat4(1.0f);
uniform mat4 view = mat4(1.0f);
uniform mat4 projection = mat4(1.0f);
uniform mat4 model = mat4(1.0);
uniform mat4 view = mat4(1.0);
uniform mat4 projection = mat4(1.0);
void main() {
gl_Position = projection * view * model * vec4(aPos, 1.0);

@ -49,6 +49,8 @@ int main() {
return 1;
}
Renderer::Renderer3D renderer(win);
glfwMakeContextCurrent(win);
if ( !gladLoadGLLoader((GLADloadproc)glfwGetProcAddress) ) {
@ -56,18 +58,18 @@ int main() {
return 1;
}
float verts[] = {
std::vector<float> verts({
0.5f, 0.5f, 0.0f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f,
0.5f, -0.5f, 0.0f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f,
-0.5f, -0.5f, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f,
-0.5f, 0.5f, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f
};
});
// Vert struc: x y z r g b tx ty
unsigned int indices[] = {
std::vector<unsigned int> indices({
0, 1, 3,
1, 2, 3
};
});
glViewport(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT);
glfwSetFramebufferSizeCallback(win, framebuffer_size_callback);
@ -75,7 +77,9 @@ int main() {
float borderColor[] = {1.0f, 1.0f, 1.0f, 1.0f};
glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, borderColor);
Renderer::Obj2D ro(indices, sizeof(indices), verts, sizeof(verts));
Renderer::Obj2D ro(verts, indices);
renderer.spawnObject(ro);
renderer.setCamera(glm::vec3(0.0f, 0.0f, -8.0f));
ro.setTexture(RUSTY_METAL_TEXTURE);
// Window width & height
@ -88,7 +92,8 @@ int main() {
/* OBJECT RENDERING */
float time = glfwGetTime();
float gVal = (sin(time) / 1.5f) + 0.5f;
float gVal = sin(time) / 10.5f;
renderer.setCamera(glm::vec3(0.0f, 0.0f, -gVal));
// Transformation
float rotang = time;
@ -97,7 +102,8 @@ int main() {
T = glm::rotate(T, rotang, glm::vec3(1.0, 0.0, 1.0));
T = glm::scale(T, glm::vec3(0.5, 0.5, 0.5));
ro.transform(T);
ro.render(win);
renderer.render();
// glfw
glfwSwapBuffers(win);

@ -2,70 +2,71 @@
#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;
Renderer3D::Renderer3D(std::vector<RenderObject> ROs) : Renderer3D() {
RenderObjects = ROs;
setFOV(DEFAULT_FOV);
}
void Obj2D::transform(glm::mat4 T) {
shader.setMat4("model", T);
Renderer3D::Renderer3D(GLFWwindow* win, std::vector<RenderObject> ROs) : Renderer3D(win) {
renderObjects = ROs;
}
void Obj2D::setTexture(const char* t_src) {
texture.texture_src = t_src;
texture.load();
void Renderer3D::spawnObject(RenderObject ro) {
renderObjects.push_back(ro);
}
void Obj2D::render(GLFWwindow* win) {
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);
}
// Camera
glm::mat4 view = glm::mat4(1.0f);
view = glm::translate(view, glm::vec3(0.0f, 0.0f, -8.0f)); // Camera position
shader.setMat4("view", view);
glfwGetWindowSize(win, &width, &height);
glm::mat4 projection = glm::perspective(glm::radians(45.0f), (float)width / (float)height, 0.1f, 100.0f);
shader.setMat4("projection", projection);
// Use the shader etc
shader.use();
if (texture.loaded)
texture.bind();
void Renderer3D::setCamera(glm::vec3 pos) {
cameraTransform = glm::translate(cameraTransform, pos);
}
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
void Renderer3D::render() {
for ( RenderObject ro: renderObjects )
ro.render(window, cameraTransform, projectionTransform);
}
Obj2D::Obj2D(unsigned int indices[], unsigned int icount, float verts[], unsigned int vcount)
// RenderObject
RenderObject::RenderObject(std::vector<float> verts, std::vector<unsigned int> indices)
: shader(VERT_SHADER_SRC_FILE, FRAG_SHADER_SRC_FILE) {
vertsVec = verts;
indicesVec = indices;
// Vertex buffer object
glGenBuffers(1, &VBO);
float vertsArray[vertsVec.size()];
std::copy(vertsVec.begin(), vertsVec.end(), vertsArray);
unsigned int indicesArray[indicesVec.size()];
std::copy(indicesVec.begin(), indicesVec.end(), indicesArray);
// 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);
glBufferData(GL_ARRAY_BUFFER, vertsVec.size() * sizeof(vertsVec[0]), vertsArray, GL_DYNAMIC_DRAW); // for moving stuff
// printf("%u\n", sizeof(vertsVec[0]) * vertsVec.size());
// 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);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indicesVec.size() * sizeof(indicesVec[0]), indicesArray, GL_STATIC_DRAW);
// printf("%u\n",indicesVec.size() * sizeof(indicesVec[0]));
// Shader stuff
// Pos
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, VERTEX_ATTRIB_PTR_SIZE, (void*)0);
glEnableVertexAttribArray(0);
@ -79,6 +80,36 @@ namespace Renderer {
glEnableVertexAttribArray(2);
}
void RenderObject::preRenderHook() {}
void RenderObject::render(GLFWwindow* win, glm::mat4 cameraTransform, glm::mat4 projectionTransform) {
shader.setMat4("view", cameraTransform);
shader.setMat4("projection", projectionTransform);
shader.use();
preRenderHook();
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glDrawElements(GL_TRIANGLES, indicesVec.size(), GL_UNSIGNED_INT, 0);
}
void RenderObject::transform(glm::mat4 T) {
shader.setMat4("model", T);
}
// Obj2D
void Obj2D::setTexture(const char* t_src) {
texture.texture_src = t_src;
texture.load();
}
void Obj2D::preRenderHook() {
printf("OBJ2D was here!\n");
if (texture.loaded)
texture.bind();
}
// Private stuff
void Obj2D::bind_texture(Textures::Texture2D new_texture) {
texture = new_texture;

@ -81,10 +81,10 @@ namespace Shaders {
Shader::Shader(const char* vertPath, const char* fragPath) {
unsigned int vertShader = compileFromFile(GL_VERTEX_SHADER, vertPath);
unsigned int fragShader = compileFromFile(GL_FRAGMENT_SHADER, fragPath);
unsigned int shaders[2] = {vertShader, fragShader};
std::vector<unsigned int> shaders({vertShader, fragShader});
// Shader program ID
id = Shaders::createProgram( std::vector<unsigned int> (std::begin(shaders), std::end(shaders)) );
id = Shaders::createProgram(shaders);
}
void Shader::use() { glUseProgram(id); }

@ -29,13 +29,11 @@ namespace Textures {
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
printf("[%i] \"%s\" - ", id, texture_src);
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);
printf("Loaded!\n");
loaded = true;
} else {
printf("[ERROR] Failed to load texture \"%s\"\n", texture_src);

Loading…
Cancel
Save