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 #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,37 +14,49 @@
#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(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 { 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 spawnObject(RenderObject ro);
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 : public RenderObject {
public: public:
Shaders::Shader shader; using RenderObject::RenderObject;
Textures::Texture2D texture;
Obj2D(unsigned int indices[], unsigned int icount, float verts[], unsigned int vcount);
void setTexture(const char* t_src); void setTexture(const char* t_src);
void transform(glm::mat4 T); void preRenderHook();
void render(GLFWwindow* win);
private: private:
Textures::Texture2D texture;
void bind_texture(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; in vec2 TexCoord;
uniform sampler2D inpTexture; uniform sampler2D inpTexture;
uniform vec3 rgbOffset = vec3(0.0, 0.0, 0.0);
uniform float r;
uniform float g;
uniform float b;
void main() { 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 vec4 VertexColor;
out vec2 TexCoord; out vec2 TexCoord;
uniform mat4 model = mat4(1.0f); uniform mat4 model = mat4(1.0);
uniform mat4 view = mat4(1.0f); uniform mat4 view = mat4(1.0);
uniform mat4 projection = mat4(1.0f); uniform mat4 projection = mat4(1.0);
void main() { void main() {
gl_Position = projection * view * model * vec4(aPos, 1.0); gl_Position = projection * view * model * vec4(aPos, 1.0);

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

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

@ -81,10 +81,10 @@ namespace Shaders {
Shader::Shader(const char* vertPath, const char* fragPath) { Shader::Shader(const char* vertPath, const char* fragPath) {
unsigned int vertShader = compileFromFile(GL_VERTEX_SHADER, vertPath); unsigned int vertShader = compileFromFile(GL_VERTEX_SHADER, vertPath);
unsigned int fragShader = compileFromFile(GL_FRAGMENT_SHADER, fragPath); unsigned int fragShader = compileFromFile(GL_FRAGMENT_SHADER, fragPath);
unsigned int shaders[2] = {vertShader, fragShader}; std::vector<unsigned int> shaders({vertShader, fragShader});
// Shader program ID // 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); } 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_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_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); unsigned char* data = stbi_load(texture_src, &width, &height, &nrChannels, 0);
if (data) { if (data) {
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
glGenerateMipmap(GL_TEXTURE_2D); glGenerateMipmap(GL_TEXTURE_2D);
printf("Loaded!\n");
loaded = true; loaded = true;
} else { } else {
printf("[ERROR] Failed to load texture \"%s\"\n", texture_src); printf("[ERROR] Failed to load texture \"%s\"\n", texture_src);

Loading…
Cancel
Save