Refactor & added renderer

pull/2/head
E. Almqvist 2 years ago
parent 73ff2b5619
commit 4cdea48675
  1. 8
      CMakeLists.txt
  2. 29
      headers/renderer.hpp
  3. 2
      headers/shaders.hpp
  4. 4
      headers/textures.hpp
  5. 79
      src/main.cpp
  6. 54
      src/renderer.cpp

@ -3,9 +3,13 @@ cmake_minimum_required(VERSION 3.24.2)
project(Hohmann VERSION 1.0) project(Hohmann VERSION 1.0)
#### Sources #### Sources
file(GLOB SOURCES src/*.cpp headers/*.hpp) file(GLOB HEADERS headers/*.hpp headers/*.h)
add_executable(Hohmann ${SOURCES}) file(GLOB SOURCES src/*.cpp src/*.c)
add_executable(Hohmann src/main.cpp)
include_directories(headers)
target_sources(Hohmann PRIVATE ${SOURCES})
#### Libs #### Libs
# GLFW # GLFW

@ -0,0 +1,29 @@
#pragma once
#include <glm/fwd.hpp>
#include "shaders.hpp"
#include "textures.hpp"
#define VERTEX_ATTRIB_PTR_SIZE 8 * sizeof(float)
#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"
namespace Renderer {
class Obj2D {
public:
Obj2D(unsigned int indices[], unsigned int icount, float verts[], unsigned int vcount);
Shaders::Shader shader;
Textures::Texture2D texture;
void transform(glm::mat4 T);
void bind_texture(Textures::Texture2D texture);
void render();
private:
unsigned int EBO;
unsigned int VBO;
unsigned int VAO;
};
}

@ -1,3 +1,5 @@
#pragma once
#include "glm/fwd.hpp" #include "glm/fwd.hpp"
#include <glad/glad.h> #include <glad/glad.h>
#include <string> #include <string>

@ -1,5 +1,4 @@
#ifndef TEXTURES_HPP #pragma once
#define TEXTURES_HPP
namespace Textures { namespace Textures {
class Texture2D { class Texture2D {
@ -14,4 +13,3 @@ namespace Textures {
}; };
} }
#endif

@ -7,8 +7,9 @@
#include <vector> #include <vector>
#include <math.h> #include <math.h>
#include "../headers/shaders.hpp" #include "../headers/renderer.hpp"
#include "../headers/textures.hpp" // #include "../headers/shaders.hpp"
// #include "../headers/textures.hpp"
#define WINDOW_WIDTH 640 #define WINDOW_WIDTH 640
#define WINDOW_HEIGHT 480 #define WINDOW_HEIGHT 480
@ -29,70 +30,12 @@ void processInput(GLFWwindow *win) {
} }
} }
struct RenderObj {
unsigned int EBO;
Shaders::Shader shader;
Textures::Texture2D texture;
};
RenderObj preRenderCallback(unsigned int indices[], unsigned int indices_count, float verts[], unsigned int verts_count) {
// Vertex buffer object
unsigned int VBO;
glGenBuffers(1, &VBO);
// Vertex Array Object
unsigned int VAO;
glGenVertexArrays(1, &VAO); // gen the VAO
glBindVertexArray(VAO); // bind it
// Copy verts into buffer
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, verts_count, verts, GL_DYNAMIC_DRAW); // for moving stuff
//glBufferData(GL_ARRAY_BUFFER, sizeof(verts), verts, GL_STATIC_DRAW);
unsigned int EBO;
glGenBuffers(1, &EBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices_count, indices, GL_STATIC_DRAW);
// Set attrib pointers
#define VERTEX_ATTRIB_PTR_SIZE 8 * sizeof(float)
// Pos
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, VERTEX_ATTRIB_PTR_SIZE, (void*)0);
glEnableVertexAttribArray(0);
// Color
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, VERTEX_ATTRIB_PTR_SIZE, (void*)(3*sizeof(float)));
glEnableVertexAttribArray(1);
// Texture
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, VERTEX_ATTRIB_PTR_SIZE, (void*)(6*sizeof(float)));
glEnableVertexAttribArray(2);
Shaders::Shader shader(VERT_SHADER_SRC_FILE, FRAG_SHADER_SRC_FILE);
Textures::Texture2D texture(RUSTY_METAL_TEXTURE);
return RenderObj {EBO, shader, texture};
}
void renderCallback() { void renderCallback() {
// Make background // Make background
glClearColor(0.1f, 0.1f, 0.1f, 1.0f); glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT);
} }
void renderObject(RenderObj ro, glm::mat4 T) {
// Bind the texture
ro.texture.bind();
ro.shader.setMat4("transform", T);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ro.EBO);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
}
int main() { int main() {
glfwInit(); glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); glfwInit(); glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
@ -131,7 +74,7 @@ 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);
RenderObj ro = preRenderCallback(indices, sizeof(indices), verts, sizeof(verts)); Renderer::Obj2D ro(indices, sizeof(indices), verts, sizeof(verts));
while (!glfwWindowShouldClose(win)) { while (!glfwWindowShouldClose(win)) {
// Handle input // Handle input
@ -144,25 +87,21 @@ int main() {
float time = glfwGetTime(); float time = glfwGetTime();
float gVal = (sin(time) / 1.5f) + 0.5f; float gVal = (sin(time) / 1.5f) + 0.5f;
// Coloring uniform
ro.shader.use();
// ro.shader.setFloat("r", gVal);
// ro.shader.setFloat("g", gVal);
// ro.shader.setFloat("b", gVal);
// Transformation // Transformation
float rotang = time; float rotang = time;
glm::mat4 T = glm::mat4(1.0f); glm::mat4 T = glm::mat4(1.0f);
T = glm::rotate(T, rotang, glm::vec3(0.0, 0.707, 0.707)); T = glm::rotate(T, rotang, glm::vec3(0.0, 0.707, 0.707));
T = glm::scale(T, glm::vec3(0.5, 0.5, 0.5)); T = glm::scale(T, glm::vec3(0.5, 0.5, 0.5));
renderObject(ro, T); ro.transform(T);
ro.render();
glm::mat4 T2 = glm::mat4(1.0f); glm::mat4 T2 = glm::mat4(1.0f);
T2 = glm::translate(T2, glm::vec3(-0.5, 0.4, 0.0));
T2 = glm::rotate(T2, rotang, glm::vec3(0.707, 0.707, 0.0)); T2 = glm::rotate(T2, rotang, glm::vec3(0.707, 0.707, 0.0));
T2 = glm::translate(T2, glm::vec3(-0.5, 0.4, 0.0));
T2 = glm::scale(T2, glm::vec3(0.5, 0.5, 0.2)); T2 = glm::scale(T2, glm::vec3(0.5, 0.5, 0.2));
renderObject(ro, T2); ro.transform(T2);
ro.render();
// glfw // glfw
glfwSwapBuffers(win); glfwSwapBuffers(win);

@ -0,0 +1,54 @@
#include <glad/glad.h>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include "renderer.hpp"
#include "shaders.hpp"
namespace Renderer {
void Obj2D::transform(glm::mat4 T) {
shader.setMat4("transform", T);
}
void Obj2D::render() {
shader.use();
texture.bind();
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
}
Obj2D::Obj2D(unsigned int indices[], unsigned int icount, float verts[], unsigned int vcount)
: shader(VERT_SHADER_SRC_FILE, FRAG_SHADER_SRC_FILE), texture(RUSTY_METAL_TEXTURE) {
// Vertex buffer object
glGenBuffers(1, &VBO);
// Vertex Array Object
glGenVertexArrays(1, &VAO); // gen the VAO
glBindVertexArray(VAO); // bind it
// Copy verts into buffer
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);
glGenBuffers(1, &EBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, icount, indices, GL_STATIC_DRAW);
// Pos
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, VERTEX_ATTRIB_PTR_SIZE, (void*)0);
glEnableVertexAttribArray(0);
// Color
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, VERTEX_ATTRIB_PTR_SIZE, (void*)(3*sizeof(float)));
glEnableVertexAttribArray(1);
// Texture
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, VERTEX_ATTRIB_PTR_SIZE, (void*)(6*sizeof(float)));
glEnableVertexAttribArray(2);
}
}
Loading…
Cancel
Save