diff --git a/headers/renderer.hpp b/headers/renderer.hpp index dbf023a..7de6d36 100644 --- a/headers/renderer.hpp +++ b/headers/renderer.hpp @@ -37,10 +37,10 @@ namespace Renderer { std::vector indicesVec; }; - class Renderer3D { + class Scene { public: - Renderer3D(GLFWwindow* win); - Renderer3D(GLFWwindow* win, std::vector ROs); + Scene(GLFWwindow* win); + Scene(GLFWwindow* win, std::vector ROs); void setCamera(glm::vec3 pos); void setFOV(float deg); @@ -53,7 +53,7 @@ namespace Renderer { glm::mat4 projectionTransform = glm::mat4(1.0f); }; - class Obj2D : public RenderObject { + class TexturedObject : public RenderObject { public: using RenderObject::RenderObject; void setTexture(const char* t_src); diff --git a/src/main.cpp b/src/main.cpp index 3e1bd91..f22d279 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -7,17 +7,11 @@ #include #include -#include "../headers/renderer.hpp" -#include "glm/trigonometric.hpp" -// #include "../headers/shaders.hpp" -// #include "../headers/textures.hpp" +#include "renderer.hpp" #define WINDOW_WIDTH 640 #define WINDOW_HEIGHT 480 -#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" void framebuffer_size_callback(GLFWwindow* win, int w, int h) { @@ -49,7 +43,7 @@ int main() { return 1; } - Renderer::Renderer3D renderer(win); + Renderer::Scene scene(win); glfwMakeContextCurrent(win); @@ -58,13 +52,6 @@ int main() { return 1; } - // std::vector 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, 0.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, 1.0f, 0.0f, - // }); - std::vector verts({ -0.5f, -0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.5f, -0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, @@ -161,16 +148,18 @@ int main() { float borderColor[] = {1.0f, 1.0f, 1.0f, 1.0f}; glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, borderColor); - Renderer::Obj2D ro(verts, indices); - Renderer::Obj2D ro2(verts, indices); - ro.position = glm::vec3(-1.0f, -1.0f, -4.0f); + Renderer::TexturedObject ro(verts, indices); + ro.position = glm::vec3(0.2f, -1.0f, -4.0f); + + Renderer::TexturedObject ro2(verts, indices); + ro2.position = glm::vec3(0.5f, 0.0, -2.0f); ro2.setTexture("assets/textures/meep.jpg"); ro.setTexture(RUSTY_METAL_TEXTURE); - renderer.spawnObject(ro); - // renderer.spawnObject(ro2); - renderer.setCamera(glm::vec3(0.0f, 0.0f, -4.0f)); + scene.spawnObject(ro); + scene.spawnObject(ro2); + scene.setCamera(glm::vec3(0.0f, 0.0f, -8.0f)); // Window width & height while (!glfwWindowShouldClose(win)) { @@ -183,18 +172,16 @@ int main() { /* OBJECT RENDERING */ float time = glfwGetTime(); float gVal = sin(time) / 10.5f; - renderer.setCamera(glm::vec3(0.0f, 0.0f, gVal/10.0f)); + scene.setCamera(glm::vec3(gVal/10.0f, 0.0f, 0.0f)); // Transformation float rotang = time; glm::mat4 T = glm::mat4(1.0f); 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); - // ro2.transform(T); - renderer.render(); + scene.render(); // glfw glfwSwapBuffers(win); diff --git a/src/renderer.cpp b/src/renderer.cpp index 107577b..93aa933 100644 --- a/src/renderer.cpp +++ b/src/renderer.cpp @@ -10,32 +10,32 @@ namespace Renderer { - // Renderer3D - Renderer3D::Renderer3D(GLFWwindow* win) { + // Scene + Scene::Scene(GLFWwindow* win) { window = win; setFOV(DEFAULT_FOV); } - Renderer3D::Renderer3D(GLFWwindow* win, std::vector ROs) : Renderer3D(win) { + Scene::Scene(GLFWwindow* win, std::vector ROs) : Scene(win) { renderObjects = ROs; } - void Renderer3D::spawnObject(RenderObject ro) { + void Scene::spawnObject(RenderObject ro) { renderObjects.push_back(ro); } - void Renderer3D::setFOV(float fov) { + void Scene::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) { + void Scene::setCamera(glm::vec3 pos) { cameraTransform = glm::translate(cameraTransform, pos); } - void Renderer3D::render() { + void Scene::render() { for ( RenderObject ro: renderObjects ) ro.render(window, cameraTransform, projectionTransform); } @@ -95,7 +95,6 @@ namespace Renderer { preRenderHook(); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO); - // glDrawElements(GL_TRIANGLES, indicesVec.size(), GL_UNSIGNED_INT, 0); glDrawArrays(GL_TRIANGLES, 0, indicesVec.size()); } @@ -103,21 +102,21 @@ namespace Renderer { shader.setMat4("model", T); } - // Obj2D - void Obj2D::setTexture(const char* t_src) { + // TexturedObject + void TexturedObject::setTexture(const char* t_src) { texture.texture_src = t_src; texture.load(); } - void Obj2D::preRenderHook() { - printf("OBJ2D was here!\n"); + void TexturedObject::preRenderHook() { + printf("TexturedObject was here!\n"); printf("Now has texture: %s", texture.texture_src); if (texture.loaded) texture.bind(); } // Private stuff - void Obj2D::bind_texture(Textures::Texture2D new_texture) { + void TexturedObject::bind_texture(Textures::Texture2D new_texture) { texture = new_texture; } }