quick backup of the renderer - things missing
authorEleni Maria Stea <estea@igalia.com>
Fri, 14 Jul 2017 08:52:29 +0000 (11:52 +0300)
committerEleni Maria Stea <estea@igalia.com>
Fri, 14 Jul 2017 08:52:29 +0000 (11:52 +0300)
20 files changed:
src/main.cc
src/mesh.h
src/object.h
src/opengl/mesh-gl.cc
src/opengl/mesh-gl.h
src/opengl/opengl.cc
src/opengl/renderer-gl.cc [new file with mode: 0644]
src/opengl/renderer-gl.h [new file with mode: 0644]
src/opengl/shader-gl.h
src/opengl/texture-gl.cc
src/opengl/texture-gl.h
src/renderer.cc [new file with mode: 0644]
src/renderer.h [new file with mode: 0644]
src/scene.cc
src/scene.h
src/texture.h
src/vulkan/mesh-vk.cc
src/vulkan/mesh-vk.h
src/vulkan/texture-vk.cc
src/vulkan/texture-vk.h

index 4834548..4e6677a 100644 (file)
@@ -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
index 6482bde..a7fb327 100644 (file)
@@ -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
index 57aef84..98a2595 100644 (file)
@@ -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;
index a6efb7d..e7cf44d 100644 (file)
@@ -1,15 +1,15 @@
 #include <GL/glew.h>
+#include <gmath/gmath.h>
 
 #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
index 912257c..f935a59 100644 (file)
@@ -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();
index d2aea3b..a7ca8c1 100644 (file)
@@ -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 (file)
index 0000000..5b81be3
--- /dev/null
@@ -0,0 +1,87 @@
+#include <GL/glew.h>
+
+#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; i<scene->objects.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 (file)
index 0000000..195df58
--- /dev/null
@@ -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
index ea3b1c1..e5e6390 100644 (file)
@@ -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
index 0143a6e..a2072e3 100644 (file)
@@ -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
index 8265e54..b443f09 100644 (file)
@@ -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 (file)
index 0000000..2c9b4c3
--- /dev/null
@@ -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 (file)
index 0000000..6ea7c02
--- /dev/null
@@ -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
index 08689f6..c640847 100644 (file)
@@ -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; i<amesh->mNumVertices; ++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; i<amesh->mNumFaces; ++i) {
                        mesh->indices.push_back(amesh->mFaces[i].mIndices[0]);
                        mesh->indices.push_back(amesh->mFaces[i].mIndices[1]);
index 1633fd1..483cdd8 100644 (file)
@@ -21,8 +21,6 @@ public:
        std::vector<Mesh *> meshes;
        std::vector<Material *> materials;
        std::vector<Texture *> textures;
-       // std::vector<Light *>lights;
-       // std::vector<Camera *>camera;
        std::vector<Object *> objects;
 
        Scene();
index d386eee..bcf9a92 100644 (file)
@@ -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
index d9d6665..4bd4791 100644 (file)
@@ -29,4 +29,8 @@ MeshVK::~MeshVK()
 
 void MeshVK::update_vertex_data()
 {
+}
+
+void MeshVK::draw() const
+{
 }
\ No newline at end of file
index 3e305cd..30593e9 100644 (file)
@@ -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
index b20277b..af67c82 100644 (file)
@@ -11,3 +11,7 @@ TextureVK::~TextureVK()
 void TextureVK::update()
 {
 }
+
+void TextureVK::bind()
+{
+}
\ No newline at end of file
index f31b4a7..bc99c83 100644 (file)
@@ -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