diff --git a/headers/renderer.hpp b/headers/renderer.hpp index c7165e9..578887f 100644 --- a/headers/renderer.hpp +++ b/headers/renderer.hpp @@ -1,9 +1,12 @@ #pragma once #include +#include #include "shaders.hpp" #include "textures.hpp" +#include "GLFW/glfw3.h" + #define VERTEX_ATTRIB_PTR_SIZE 8 * sizeof(float) #define VERT_SHADER_SRC_FILE "shaders/vertex.glsl" @@ -12,16 +15,32 @@ #define RUSTY_METAL_TEXTURE "assets/textures/rusty_metal.jpg" namespace Renderer { + class RenderObject {}; + + class Renderer3D { + public: + Renderer3D(); + Renderer3D(std::vector ROs); + + void setCamera(glm::vec3 pos); + void setFOV(float deg); + void render(GLFWwindow* win); + private: + std::vector RenderObjects; + }; + class Obj2D { public: - Obj2D(unsigned int indices[], unsigned int icount, float verts[], unsigned int vcount); Shaders::Shader shader; Textures::Texture2D texture; + Obj2D(unsigned int indices[], unsigned int icount, float verts[], unsigned int vcount); + + void setTexture(const char* t_src); void transform(glm::mat4 T); - void bind_texture(Textures::Texture2D texture); - void render(); + void render(GLFWwindow* win); private: + void bind_texture(Textures::Texture2D texture); unsigned int EBO; unsigned int VBO; unsigned int VAO; diff --git a/headers/textures.hpp b/headers/textures.hpp index 5029cfd..ce94df9 100644 --- a/headers/textures.hpp +++ b/headers/textures.hpp @@ -4,11 +4,14 @@ namespace Textures { class Texture2D { public: unsigned int id; + const char* texture_src; + bool loaded = false; + + Texture2D(); Texture2D(const char* t_src); void load(); void bind(); private: - const char* texture_src; int width, height, nrChannels; }; } diff --git a/shaders/vertex.glsl b/shaders/vertex.glsl index db110e6..d798e11 100644 --- a/shaders/vertex.glsl +++ b/shaders/vertex.glsl @@ -6,15 +6,12 @@ layout (location = 2) in vec2 aTexCoord; out vec4 VertexColor; out vec2 TexCoord; -uniform mat4 transform = mat4( - 1.0, 0.0, 0.0, 0.0, - 0.0, 1.0, 0.0, 0.0, - 0.0, 0.0, 1.0, 0.0, - 0.0, 0.0, 0.0, 1.0 - ); +uniform mat4 model = mat4(1.0f); +uniform mat4 view = mat4(1.0f); +uniform mat4 projection = mat4(1.0f); void main() { - gl_Position = transform * vec4(aPos, 1.0); + gl_Position = projection * view * model * vec4(aPos, 1.0); VertexColor = vec4(aColor, 1.0); TexCoord = aTexCoord; } diff --git a/src/main.cpp b/src/main.cpp index b84b401..dfc6bf1 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -8,6 +8,7 @@ #include #include "../headers/renderer.hpp" +#include "glm/trigonometric.hpp" // #include "../headers/shaders.hpp" // #include "../headers/textures.hpp" @@ -56,10 +57,10 @@ int main() { } float verts[] = { - 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, -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, 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 @@ -75,7 +76,9 @@ int main() { glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, borderColor); Renderer::Obj2D ro(indices, sizeof(indices), verts, sizeof(verts)); + ro.setTexture(RUSTY_METAL_TEXTURE); + // Window width & height while (!glfwWindowShouldClose(win)) { // Handle input processInput(win); @@ -91,17 +94,10 @@ int main() { float rotang = time; 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(1.0, 0.0, 1.0)); T = glm::scale(T, glm::vec3(0.5, 0.5, 0.5)); ro.transform(T); - ro.render(); - - glm::mat4 T2 = glm::mat4(1.0f); - 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)); - ro.transform(T2); - ro.render(); + ro.render(win); // glfw glfwSwapBuffers(win); diff --git a/src/renderer.cpp b/src/renderer.cpp index 6f76410..65acf58 100644 --- a/src/renderer.cpp +++ b/src/renderer.cpp @@ -6,20 +6,48 @@ #include "shaders.hpp" namespace Renderer { + + Renderer3D::Renderer3D() { + // TODO: Make more OOP + } + + Renderer3D::Renderer3D(std::vector ROs) : Renderer3D() { + RenderObjects = ROs; + } + void Obj2D::transform(glm::mat4 T) { - shader.setMat4("transform", T); + shader.setMat4("model", T); + } + + void Obj2D::setTexture(const char* t_src) { + texture.texture_src = t_src; + texture.load(); } - void Obj2D::render() { + void Obj2D::render(GLFWwindow* win) { + int width, height; + + // 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(); - texture.bind(); + + if (texture.loaded) + 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) { + : shader(VERT_SHADER_SRC_FILE, FRAG_SHADER_SRC_FILE) { // Vertex buffer object glGenBuffers(1, &VBO); @@ -51,4 +79,8 @@ namespace Renderer { glEnableVertexAttribArray(2); } + // Private stuff + void Obj2D::bind_texture(Textures::Texture2D new_texture) { + texture = new_texture; + } } diff --git a/src/textures.cpp b/src/textures.cpp index a75cc71..c19fa30 100644 --- a/src/textures.cpp +++ b/src/textures.cpp @@ -6,9 +6,17 @@ #include "../extern/stb/stb_image.h" namespace Textures { + Texture2D::Texture2D() { + texture_src = ""; + } + Texture2D::Texture2D(const char* t_src) { texture_src = t_src; + // Load texture source etc + load(); + } + void Texture2D::load() { // Bind this texture glGenTextures(1, &id); glBindTexture(GL_TEXTURE_2D, id); @@ -21,11 +29,6 @@ namespace Textures { glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); - // Load texture source etc - load(); - } - - void Texture2D::load() { printf("[%i] \"%s\" - ", id, texture_src); unsigned char* data = stbi_load(texture_src, &width, &height, &nrChannels, 0); @@ -33,6 +36,7 @@ namespace Textures { 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); }