From 0da7a98f74d00bfa6cf0d47fd7cf0f687eeba5f6 Mon Sep 17 00:00:00 2001 From: Eleni Maria Stea Date: Fri, 14 Jul 2017 11:52:29 +0300 Subject: [PATCH] quick backup of the renderer - things missing --- src/main.cc | 9 ++- src/mesh.h | 3 +- src/object.h | 6 -- src/opengl/mesh-gl.cc | 161 ++++++++++++++++++++++----------------------- src/opengl/mesh-gl.h | 4 +- src/opengl/opengl.cc | 10 --- src/opengl/renderer-gl.cc | 87 ++++++++++++++++++++++++ src/opengl/renderer-gl.h | 21 ++++++ src/opengl/shader-gl.h | 10 +-- src/opengl/texture-gl.cc | 6 ++ src/opengl/texture-gl.h | 2 + src/renderer.cc | 20 ++++++ src/renderer.h | 37 +++++++++++ src/scene.cc | 23 +++---- src/scene.h | 2 - src/texture.h | 1 + src/vulkan/mesh-vk.cc | 4 ++ src/vulkan/mesh-vk.h | 1 + src/vulkan/texture-vk.cc | 4 ++ src/vulkan/texture-vk.h | 4 +- 20 files changed, 290 insertions(+), 125 deletions(-) create mode 100644 src/opengl/renderer-gl.cc create mode 100644 src/opengl/renderer-gl.h create mode 100644 src/renderer.cc create mode 100644 src/renderer.h diff --git a/src/main.cc b/src/main.cc index 4834548..4e6677a 100644 --- a/src/main.cc +++ b/src/main.cc @@ -74,6 +74,13 @@ int main(int argc, char **argv) static bool init() { + /* TODO */ + /* + TODO changes: + 1- create cam + 2- scene + 3- renderers + */ if(use_vulkan) { if(!init_vulkan()) return false; @@ -159,4 +166,4 @@ static void display() else { display_opengl(); } -} +} \ No newline at end of file diff --git a/src/mesh.h b/src/mesh.h index 6482bde..a7fb327 100644 --- a/src/mesh.h +++ b/src/mesh.h @@ -28,10 +28,11 @@ public: std::string name; unsigned int mat_idx; - unsigned int which_mask; Mesh(); virtual ~Mesh() = 0; + + virtual void draw() const = 0; }; #endif // MESH_H_ \ No newline at end of file diff --git a/src/object.h b/src/object.h index 57aef84..98a2595 100644 --- a/src/object.h +++ b/src/object.h @@ -9,12 +9,6 @@ class Mesh; class ShaderProgram; class Texture; -enum OType { - OBJ_MESH, - OBJ_PT_LIGHT, - OBJ_CAMERA -}; - struct Material { Vec3 diffuse; Vec3 specular; diff --git a/src/opengl/mesh-gl.cc b/src/opengl/mesh-gl.cc index a6efb7d..e7cf44d 100644 --- a/src/opengl/mesh-gl.cc +++ b/src/opengl/mesh-gl.cc @@ -1,15 +1,15 @@ #include +#include #include "mesh-gl.h" MeshGL::MeshGL() { - which_mask = 0; - vao = 0; vbo_vertices = 0; vbo_normals = 0; + vbo_tex_coords = 0; ibo = 0; num_vertices = 0; @@ -21,9 +21,11 @@ MeshGL::MeshGL(const MeshGL &mesh) indices = mesh.indices; vertices = mesh.vertices; normals = mesh.normals; + tex_coords = mesh.tex_coords; vbo_vertices = 0; vbo_normals = 0; + vbo_tex_coords = 0; ibo = 0; /* @@ -48,6 +50,7 @@ MeshGL &MeshGL::operator=(const MeshGL &mesh) indices = mesh.indices; vertices = mesh.vertices; normals = mesh.normals; + tex_coords = mesh.tex_coords; return *this; } @@ -60,36 +63,16 @@ MeshGL::~MeshGL() normals.clear(); } -/* void MeshGL::draw() const { - // save the previously bound vao, vbo just in case - int curr_vao; - int curr_vbo; - glGetIntegerv(GL_VERTEX_ARRAY_BINDING, &curr_vao); - glGetIntegerv(GL_ELEMENT_ARRAY_BUFFER_BINDING, &curr_vbo); - glBindVertexArray(vao); - glBindBuffer(GL_ARRAY_BUFFER, vbo_vertices); - glVertexPointer(3, GL_FLOAT, 0, 0); - glBindBuffer(GL_ARRAY_BUFFER, vbo_normals); - glNormalPointer(GL_FLOAT, 0, 0); - glBindBuffer(GL_ARRAY_BUFFER, 0); - - glEnableClientState(GL_VERTEX_ARRAY); - glEnableClientState(GL_NORMAL_ARRAY); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo); - glDrawElements(GL_TRIANGLES, indices.size(), GL_UNSIGNED_SHORT, 0); + glDrawElements(GL_TRIANGLES, num_indices, GL_UNSIGNED_INT, 0); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); - glDisableClientState(GL_VERTEX_ARRAY); - glDisableClientState(GL_NORMAL_ARRAY); - // return to previous state - glBindBuffer(GL_ARRAY_BUFFER, curr_vbo); - glBindVertexArray(curr_vao); + glBindVertexArray(0); } -*/ void MeshGL::update_vertex_data() { @@ -98,67 +81,76 @@ void MeshGL::update_vertex_data() void MeshGL::update_vbo() { - // save the previously bound vao, vbo just in case - int curr_vao; - int curr_vbo; - glGetIntegerv(GL_VERTEX_ARRAY_BINDING, &curr_vao); - glGetIntegerv(GL_ELEMENT_ARRAY_BUFFER_BINDING, &curr_vbo); - + /* vao */ if(!vao) glGenVertexArrays(1, &vao); - glBindVertexArray(vao); - if(which_mask & MESH_NORMAL) { - if(!vbo_normals) { - glGenBuffers(1, &vbo_normals); - } - glBindBuffer(GL_ARRAY_BUFFER, vbo_normals); - if(num_vertices != (int)normals.size()) { - glBufferData(GL_ARRAY_BUFFER, normals.size() * 3 * sizeof(float), - &normals[0], GL_STREAM_DRAW); - } - else { - glBufferSubData(GL_ARRAY_BUFFER, 0, normals.size() * 3 * sizeof(float), - &normals[0]); - } - } - - if(which_mask & MESH_VERTEX) { - if(!vbo_vertices) { - glGenBuffers(1, &vbo_vertices); - } - glBindBuffer(GL_ARRAY_BUFFER, vbo_vertices); - if(num_vertices != (int)vertices.size()) { - glBufferData(GL_ARRAY_BUFFER, vertices.size() * 3 * sizeof(float), - &vertices[0], GL_STREAM_DRAW); - } - else { - glBufferSubData(GL_ARRAY_BUFFER, 0, vertices.size() * 3 * sizeof(float), - &vertices[0]); - } - num_vertices = vertices.size(); - } - - if(which_mask & MESH_INDEX) { - if(!ibo) { - glGenBuffers(1, &ibo); - } - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo); - if(num_indices != (int)indices.size()) { - glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * 2, - &indices[0], GL_STATIC_DRAW); - } - else { - glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, 0, indices.size() * 2, - &indices[0]); - } - num_indices = indices.size(); - } - - /* bind previously bound vbo */ - glBindBuffer(GL_ARRAY_BUFFER, curr_vbo); - glBindVertexArray(curr_vao); + /* vertices */ + + if(!vbo_vertices) + glGenBuffers(1, &vbo_vertices); + glBindBuffer(GL_ARRAY_BUFFER, vbo_vertices); + if(num_vertices != (int)vertices.size()) + glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(float) * 3, + &vertices, GL_STATIC_DRAW); + else + glBufferSubData(GL_ARRAY_BUFFER, 0, vertices.size() * sizeof(float) * 3, + &vertices); + num_vertices = vertices.size(); + glBindBuffer(GL_ARRAY_BUFFER, 0); + + /* normals */ + + if(!vbo_normals) + glGenBuffers(1, &vbo_normals); + glBindBuffer(GL_ARRAY_BUFFER, vbo_normals); + if(num_vertices != (int)normals.size()) + glBufferData(GL_ARRAY_BUFFER, normals.size() * sizeof(float) * 3, + &normals, GL_STATIC_DRAW); + else + glBufferSubData(GL_ARRAY_BUFFER, 0, normals.size() * sizeof(float) * 3, + &normals); + glBindBuffer(GL_ARRAY_BUFFER, 0); + + /* texture coordinates */ + + if(!vbo_tex_coords) + glGenBuffers(1, &vbo_tex_coords); + if(num_vertices != (int)tex_coords.size()) + glBufferData(GL_ARRAY_BUFFER, tex_coords.size() * sizeof(float) * 2, &tex_coords, GL_STATIC_DRAW); + else + glBufferSubData(GL_ARRAY_BUFFER, 0, tex_coords.size() * sizeof(float) * 2, &tex_coords); + glBindBuffer(GL_ARRAY_BUFFER, 0); + + /* indices */ + + if(!ibo) + glGenBuffers(1, &ibo); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo); + if(num_indices != (int)indices.size()) + glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * 2, + &indices[0], GL_STATIC_DRAW); + else + glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, 0, indices.size() * 2, + &indices[0]); + num_indices = indices.size(); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); + + glEnableVertexAttribArray(MESH_VERTEX); + glEnableVertexAttribArray(MESH_NORMAL); + glEnableVertexAttribArray(MESH_TEXTURE); + + glBindBuffer(GL_ARRAY_BUFFER, vbo_vertices); + glVertexAttribPointer(MESH_VERTEX, 3, GL_FLOAT, GL_FALSE, sizeof(Vec3), 0); + + glBindBuffer(GL_ARRAY_BUFFER, vbo_normals); + glVertexAttribPointer(MESH_NORMAL, 3, GL_FLOAT, GL_FALSE, sizeof(Vec3), 0); + + glBindBuffer(GL_ARRAY_BUFFER, vbo_tex_coords); + glVertexAttribPointer(MESH_TEXTURE, 2, GL_FLOAT, GL_FALSE, sizeof(Vec2), 0); + + glBindVertexArray(0); } void MeshGL::destroy_vbo() @@ -167,8 +159,9 @@ void MeshGL::destroy_vbo() glDeleteBuffers(1, &vbo_vertices); if(vbo_normals) glDeleteBuffers(1, &vbo_normals); + if(vbo_tex_coords) + glDeleteBuffers(1, &vbo_tex_coords); if(ibo) - glDeleteBuffers(1, &ibo); - if(vao) - glDeleteVertexArrays(1, &vao); + if(vao) + glDeleteVertexArrays(1, &vao); } \ No newline at end of file diff --git a/src/opengl/mesh-gl.h b/src/opengl/mesh-gl.h index 912257c..f935a59 100644 --- a/src/opengl/mesh-gl.h +++ b/src/opengl/mesh-gl.h @@ -9,6 +9,8 @@ private: unsigned int vbo_vertices; unsigned int vbo_normals; + unsigned int vbo_tex_coords; + unsigned int ibo; int num_vertices; @@ -21,7 +23,7 @@ public: virtual ~MeshGL(); - // virtual void draw() const override; + virtual void draw() const override; virtual void update_vertex_data() override; void update_vbo(); diff --git a/src/opengl/opengl.cc b/src/opengl/opengl.cc index d2aea3b..a7ca8c1 100644 --- a/src/opengl/opengl.cc +++ b/src/opengl/opengl.cc @@ -7,10 +7,6 @@ extern GLFWwindow *win; extern int win_h; extern int win_w; -/* static test_* functions are going to be removed: just to test the shaders */ -static void test_draw(); -static void test_torus(); - bool init_opengl() { if(!glfwInit()) { @@ -44,10 +40,4 @@ void display_opengl() glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glClearColor(0.5, 0.5, 0.5, 1.0); -} - -static void test_draw() -{ - /* this function is going to be removed, it's here only to test the shaders */ - } \ No newline at end of file diff --git a/src/opengl/renderer-gl.cc b/src/opengl/renderer-gl.cc new file mode 100644 index 0000000..5b81be3 --- /dev/null +++ b/src/opengl/renderer-gl.cc @@ -0,0 +1,87 @@ +#include + +#include "object.h" +#include "scene.h" + +#include "opengl/mesh-gl.h" +#include "opengl/renderer-gl.h" +#include "opengl/shader-gl.h" +#include "opengl/texture-gl.h" + +RendererGL::RendererGL() +{ + sprog = 0; + scene = 0; + camera = 0; +} + +RendererGL::RendererGL(ShaderProgram *sprog, Scene *scene, Camera *camera) +{ + this->sprog = sprog; + this->scene = scene; + this->camera = camera; +} + +RendererGL::~RendererGL() +{ + destroy_shaders(); +} + +void RendererGL::draw() const +{ + /* use shaders */ + if(!sprog) { + fprintf(stderr, "No active shaders found. Using default.\n"); + glUseProgram(0); + } + else { + sprog->use(); + } + + /* draw all scene components */ + for(size_t i=0; iobjects.size(); ++i) { + draw_object(scene->objects[i]); + } +} + + +bool RendererGL::init_shaders(const char *vfname, const char *ffname) +{ + if(sprog) + delete sprog; + + sprog = new ShaderProgramGL; + if(!sprog->load(vfname, ffname)) + goto error; + + if(!sprog->link()) + goto error; + + sprog->use(); + + glEnable(GL_DEPTH_TEST); + glEnable(GL_CULL_FACE); + + return true; + +error: + destroy_shaders(); + return false; +} + +void RendererGL::destroy_shaders() +{ + if(sprog) { + delete sprog; + sprog = 0; + } +} + +void RendererGL::draw_object(Object *object) const +{ + object->material->dtex->bind(); + + + + object->mesh->draw(); +} \ No newline at end of file diff --git a/src/opengl/renderer-gl.h b/src/opengl/renderer-gl.h new file mode 100644 index 0000000..195df58 --- /dev/null +++ b/src/opengl/renderer-gl.h @@ -0,0 +1,21 @@ +#ifndef RENDERER_GL_H_ +#define RENDERER_GL_H_ + +#include "renderer.h" + +class RendererGL : public Renderer { +protected: + virtual void draw_object(Object *object) const override; +public: + RendererGL(); + RendererGL(ShaderProgram *sprog, Scene *scene, Camera *camera); + + virtual ~RendererGL(); + + virtual bool init_shaders(const char *vname, const char *fname) override; + virtual void destroy_shaders() override; + + virtual void draw() const override; +}; + +#endif // RENDERER_GL_H_ \ No newline at end of file diff --git a/src/opengl/shader-gl.h b/src/opengl/shader-gl.h index ea3b1c1..e5e6390 100644 --- a/src/opengl/shader-gl.h +++ b/src/opengl/shader-gl.h @@ -13,8 +13,8 @@ public: ShaderGL(); virtual ~ShaderGL(); - virtual void destroy(); - virtual void attach(unsigned int prog); + virtual void destroy() override; + virtual void attach(unsigned int prog) override; }; class ShaderProgramGL : public ShaderProgram { @@ -27,9 +27,9 @@ public: void destroy(); void delete_shaders(); - virtual bool link(); - virtual bool load(const char *vfname, const char *ffname); - virtual void use(); + virtual bool link() override; + virtual bool load(const char *vfname, const char *ffname) override; + virtual void use() override; }; #endif // SHADER_GL_H_ \ No newline at end of file diff --git a/src/opengl/texture-gl.cc b/src/opengl/texture-gl.cc index 0143a6e..a2072e3 100644 --- a/src/opengl/texture-gl.cc +++ b/src/opengl/texture-gl.cc @@ -31,4 +31,10 @@ void TextureGL::update() glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h, 0, GL_RGB, GL_UNSIGNED_BYTE, pixels); glGenerateMipmap(GL_TEXTURE_2D); +} + + +void TextureGL::bind() +{ + glBindTexture(GL_TEXTURE_2D, tex); } \ No newline at end of file diff --git a/src/opengl/texture-gl.h b/src/opengl/texture-gl.h index 8265e54..b443f09 100644 --- a/src/opengl/texture-gl.h +++ b/src/opengl/texture-gl.h @@ -11,6 +11,8 @@ private: public: TextureGL(); virtual ~TextureGL(); + + virtual void bind() override; }; #endif // TEXTURE_GL_H_ diff --git a/src/renderer.cc b/src/renderer.cc new file mode 100644 index 0000000..2c9b4c3 --- /dev/null +++ b/src/renderer.cc @@ -0,0 +1,20 @@ +#include "renderer.h" +#include "shader.h" + +Renderer::Renderer() +{ + scene = 0; + camera = 0; + sprog = 0; +} + +Renderer::Renderer(ShaderProgram *sprog, Scene *scene, Camera *camera) +{ + this->scene = scene; + this->sprog = sprog; + this->camera = camera; +} + +Renderer::~Renderer() +{ +} \ No newline at end of file diff --git a/src/renderer.h b/src/renderer.h new file mode 100644 index 0000000..6ea7c02 --- /dev/null +++ b/src/renderer.h @@ -0,0 +1,37 @@ +#ifndef RENDERER_H_ +#define RENDERER_H_ + +/* + this might change: + atm we are going to have 1 renderer per scene and 1 shader program + for the scene + */ +class Camera; +class Scene; +class ShaderProgram; +class Object; + +class Renderer { +protected: + ShaderProgram *sprog; + virtual void draw_object(Object *object) const = 0; + +public: + Scene *scene; + Camera *camera; + + Renderer(); + Renderer(ShaderProgram *sprog, Scene *scene, Camera *camera); + virtual ~Renderer(); + + /* for the moment each Renderer creates and destroys the ShaderProgram + because we are using only a couple of shaders, in the future we might need a shader + manager that stores the shaders and replace these functions with something like: + void set_shader_program(ShaderProgram *sprog) */ + virtual bool init_shaders(const char *vfname, const char *ffname) = 0; + virtual void destroy_shaders() = 0; + + virtual void draw() const = 0; +}; + +#endif // RENDERER_H_ \ No newline at end of file diff --git a/src/scene.cc b/src/scene.cc index 08689f6..c640847 100644 --- a/src/scene.cc +++ b/src/scene.cc @@ -145,12 +145,10 @@ static Mesh *load_mesh(const aiScene *scene, unsigned int index) } mesh->name = std::string(amesh->mName.data); - mesh->which_mask = 0; for(unsigned int i=0; imNumVertices; ++i) { /* vertices */ if(amesh->HasPositions()) { - mesh->which_mask |= MESH_VERTEX; Vec3 vertex = Vec3(amesh->mVertices[i].x, amesh->mVertices[i].y, amesh->mVertices[i].z); @@ -164,7 +162,6 @@ static Mesh *load_mesh(const aiScene *scene, unsigned int index) /* normals */ if(amesh->HasNormals()) { - mesh->which_mask |= MESH_NORMAL; Vec3 normal = Vec3(amesh->mNormals[i].x, amesh->mNormals[i].y, amesh->mNormals[i].z); mesh->normals.push_back(normal); @@ -177,7 +174,6 @@ static Mesh *load_mesh(const aiScene *scene, unsigned int index) /* texture coordinates */ if(amesh->mTextureCoords[0]) { - mesh->which_mask |= MESH_TEXTURE; Vec2 tex_coord = Vec2(amesh->mTextureCoords[0][i].x, amesh->mTextureCoords[0][i].y); mesh->tex_coords.push_back(tex_coord); } @@ -186,19 +182,18 @@ static Mesh *load_mesh(const aiScene *scene, unsigned int index) } /* tangents */ - if(amesh->mTangents) { - mesh->which_mask |= MESH_TANGENT; - Vec3 tangent = Vec3(amesh->mTangents[i].x, amesh->mTangents[i].y, - amesh->mTangents[i].z); - mesh->tangents.push_back(tangent); - } - else { - mesh->tangents.push_back(Vec3(1, 0, 0)); - } + // if(amesh->mTangents) { + // mesh->which_mask |= MESH_TANGENT; + // Vec3 tangent = Vec3(amesh->mTangents[i].x, amesh->mTangents[i].y, + // amesh->mTangents[i].z); + // mesh->tangents.push_back(tangent); + // } + // else { + // mesh->tangents.push_back(Vec3(1, 0, 0)); + // } } /* indices (called faces in assimp) */ if(amesh->HasFaces()) { - mesh->which_mask |= MESH_INDEX; for(unsigned int i=0; imNumFaces; ++i) { mesh->indices.push_back(amesh->mFaces[i].mIndices[0]); mesh->indices.push_back(amesh->mFaces[i].mIndices[1]); diff --git a/src/scene.h b/src/scene.h index 1633fd1..483cdd8 100644 --- a/src/scene.h +++ b/src/scene.h @@ -21,8 +21,6 @@ public: std::vector meshes; std::vector materials; std::vector textures; - // std::vectorlights; - // std::vectorcamera; std::vector objects; Scene(); diff --git a/src/texture.h b/src/texture.h index d386eee..bcf9a92 100644 --- a/src/texture.h +++ b/src/texture.h @@ -19,6 +19,7 @@ public: virtual ~Texture(); virtual bool load(const char *fname); + virtual void bind() = 0; }; #endif // TEXTURE_H_ \ No newline at end of file diff --git a/src/vulkan/mesh-vk.cc b/src/vulkan/mesh-vk.cc index d9d6665..4bd4791 100644 --- a/src/vulkan/mesh-vk.cc +++ b/src/vulkan/mesh-vk.cc @@ -29,4 +29,8 @@ MeshVK::~MeshVK() void MeshVK::update_vertex_data() { +} + +void MeshVK::draw() const +{ } \ No newline at end of file diff --git a/src/vulkan/mesh-vk.h b/src/vulkan/mesh-vk.h index 3e305cd..30593e9 100644 --- a/src/vulkan/mesh-vk.h +++ b/src/vulkan/mesh-vk.h @@ -12,6 +12,7 @@ public: MeshVK& operator=(const MeshVK &mesh); virtual ~MeshVK(); + virtual void draw() const override; }; #endif // MESH_VK_H_ \ No newline at end of file diff --git a/src/vulkan/texture-vk.cc b/src/vulkan/texture-vk.cc index b20277b..af67c82 100644 --- a/src/vulkan/texture-vk.cc +++ b/src/vulkan/texture-vk.cc @@ -11,3 +11,7 @@ TextureVK::~TextureVK() void TextureVK::update() { } + +void TextureVK::bind() +{ +} \ No newline at end of file diff --git a/src/vulkan/texture-vk.h b/src/vulkan/texture-vk.h index f31b4a7..bc99c83 100644 --- a/src/vulkan/texture-vk.h +++ b/src/vulkan/texture-vk.h @@ -10,6 +10,8 @@ private: public: TextureVK(); virtual ~TextureVK(); + + virtual void bind() override; }; -#endif // TEXTURE_VK_H_ +#endif // TEXTURE_VK_H_ \ No newline at end of file -- 1.7.10.4