backup:
authorEleni Maria Stea <elene.mst@gmail.com>
Tue, 25 Jul 2017 11:08:28 +0000 (14:08 +0300)
committerEleni Maria Stea <elene.mst@gmail.com>
Tue, 25 Jul 2017 11:08:28 +0000 (14:08 +0300)
- removed renderer-gl (since mesh, shader, shaderprogram have their own
gl backends atm we can just call draw)
- added uniforms, camera, draw, matrices

src/global.h
src/main.cc
src/opengl/renderer-gl.cc [deleted file]
src/opengl/renderer-gl.h [deleted file]
src/renderer.cc
src/renderer.h

index f56b34c..1b9b66b 100644 (file)
@@ -4,4 +4,11 @@
 #include "shader_manager.h"
 extern ShaderManager *sdr_man;
 
+#include <gmath/gmath.h>
+extern Mat4 mprojection;
+
+extern float phi;
+extern float theta;
+extern float dist;
+
 #endif // GLOBAL_H_
\ No newline at end of file
index 50eea41..1e45c4b 100644 (file)
@@ -3,6 +3,8 @@
 #include <string.h>
 #include <vector>
 
+#include <gmath/gmath.h>
+
 #include "global.h"
 
 /* TODO: fix those */
@@ -22,24 +24,30 @@ static void cleanup();
 static void display();
 
 /* glfw callbacks */
-static void key_clbk(GLFWwindow *win, int key, int scancode, int action, int mods);
-static void motion_clbk(GLFWwindow *win, double x, double y);
-static void mouse_clbk(GLFWwindow *win, int button, int action, int mods);
+
+static void clbk_key(GLFWwindow *win, int key, int scancode, int action, int mods);
+static void clbk_motion(GLFWwindow *win, double x, double y);
+static void clbk_mouse(GLFWwindow *win, int button, int action, int mods);
+static void clbk_reshape(GLFWwindow *win, int width, int height);
 
 /* global variables */
+
 bool use_vulkan;
+Mat4 mprojection;
 
 GLFWwindow *win;
 int win_w = 800;
 int win_h = 600;
 
 OrbitCamera *camera;
+float phi = 25;
+float theta = 0;
+float dist = 4;
+
 ShaderManager *sdr_man;
 
 /* variables */
-static float phi = 25;
-static float theta = 0;
-static float dist = 4;
+static float aspect;
 
 // TODO: remove just for test:
 static Scene scene;
@@ -65,9 +73,10 @@ int main(int argc, char **argv)
                return 1;
        }
 
-       glfwSetKeyCallback(win, key_clbk);
-       glfwSetCursorPosCallback(win, motion_clbk);
-       glfwSetMouseButtonCallback(win, mouse_clbk);
+       glfwSetKeyCallback(win, clbk_key);
+       glfwSetCursorPosCallback(win, clbk_motion);
+       glfwSetMouseButtonCallback(win, clbk_mouse);
+       glfwSetWindowSizeCallback(win, clbk_reshape);
 
        while(!glfwWindowShouldClose(win)) {
                display();
@@ -82,13 +91,6 @@ 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;
@@ -108,6 +110,7 @@ static bool init()
                return false;
        }
 
+// TODO delete: debugging
        for(size_t i=0; i<scene.objects.size(); ++i) {
                printf("object: %d\n", (int)i);
                printf("mesh: %s\n", scene.objects[i]->mesh->name.c_str());
@@ -131,7 +134,7 @@ static void cleanup()
        }
 }
 
-static void key_clbk(GLFWwindow *win, int key, int scancode, int action, int mods)
+static void clbk_key(GLFWwindow *win, int key, int scancode, int action, int mods)
 {
        if(key == GLFW_KEY_ESCAPE && action == GLFW_PRESS) {
                glfwSetWindowShouldClose(win, GLFW_TRUE);
@@ -141,7 +144,7 @@ static void key_clbk(GLFWwindow *win, int key, int scancode, int action, int mod
 static double prev_x, prev_y;
 static int button;
 
-static void motion_clbk(GLFWwindow *win, double x, double y)
+static void clbk_motion(GLFWwindow *win, double x, double y)
 {
        switch(button) {
        case GLFW_MOUSE_BUTTON_LEFT:
@@ -167,12 +170,25 @@ static void motion_clbk(GLFWwindow *win, double x, double y)
        prev_y = y;
 }
 
-static void mouse_clbk(GLFWwindow *win, int bn, int action, int mods)
+static void clbk_mouse(GLFWwindow *win, int bn, int action, int mods)
 {
        button = bn;
        glfwGetCursorPos(win, &prev_x, &prev_y);
 }
 
+static void clbk_reshape(GLFWwindow *win, int width, int height)
+{
+       if(use_vulkan) {
+               //TODO
+               return;
+       }
+       else {
+               glViewport(0, 0, width, height);
+               aspect = (float)width / (float)height;
+               mprojection = calc_projection_matrix(1000, 0.5, aspect, 45);
+       }
+}
+
 static void display()
 {
        if(use_vulkan) {
diff --git a/src/opengl/renderer-gl.cc b/src/opengl/renderer-gl.cc
deleted file mode 100644 (file)
index 6eaafda..0000000
+++ /dev/null
@@ -1,47 +0,0 @@
-#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()
-{
-}
-
-bool RendererGL::create()
-{
-       return true;
-}
-
-void RendererGL::draw_object(Object *object) const
-{
-       object->mesh->draw();
-}
-
-void RendererGL::draw() const
-{
-       //TODO
-       /* set state, uniforms */
-       sprog->use();
-       for(size_t i=0; i<scene->objects.size(); i++) {
-               draw_object(scene->objects[i]);
-       }
-}
\ No newline at end of file
diff --git a/src/opengl/renderer-gl.h b/src/opengl/renderer-gl.h
deleted file mode 100644 (file)
index ec9357e..0000000
+++ /dev/null
@@ -1,25 +0,0 @@
-#ifndef RENDERER_GL_H_
-#define RENDERER_GL_H_
-
-#include "renderer.h"
-
-class Scene;
-class Object;
-class Camera;
-
-class RendererGL : public Renderer {
-protected:
-       virtual void draw_object(Object *object) const override;
-public:
-       RendererGL();
-       RendererGL(ShaderProgram *sprog, Scene *scene, Camera *camera);
-
-       virtual ~RendererGL();
-
-       bool load_program(ShaderProgram *sprog);
-
-       virtual bool create() override; // load shader prog, scene data etc
-       virtual void draw() const override; // set state from camera, set uniforms
-};
-
-#endif // RENDERER_GL_H_
\ No newline at end of file
index 353d0db..0251cb6 100644 (file)
@@ -1,6 +1,9 @@
+#include <GL/glew.h>
+
 #include "global.h"
 
 #include "camera.h"
+#include "mesh.h"
 #include "object.h"
 #include "renderer.h"
 #include "scene.h"
@@ -23,6 +26,7 @@ bool Renderer::create()
        if(!(sprog = sdr_man->create_shader_program("default.v.glsl", "default.f.glsl"))) {
                return false;
        }
+
        return true;
 }
 
@@ -31,9 +35,46 @@ void Renderer::draw() const
        if(!camera || !scene)
                return;
 
-       // sprog->set_uniform_matrix(mview_loc, camera->get_view_matrix());
+       int mview_loc;
+       if((mview_loc = sprog->get_uniform_location("mview")) != -1) {
+               sprog->set_uniform_matrix(mview_loc, camera->get_view_matrix());
+       }
+
+       int mproj_loc;
+       if((mproj_loc = sprog->get_uniform_location("mproj")) != -1) {
+               sprog->set_uniform_matrix(mproj_loc, mprojection);
+       }
+
+       glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
+       camera->set_orbit_params(phi, theta, dist);
+
+       for(size_t i=0; i<scene->objects.size(); i++) {
+               draw_object(scene->objects[i]);
+       }
 }
 
 void Renderer::draw_object(Object *object) const
 {
+       Material *m = object->material;
+       /* diffuse */
+       int diff_loc;
+       if((diff_loc = sprog->get_uniform_location("diffuse")) != -1) {
+               sprog->set_uniformf(diff_loc, m->diffuse.x, m->diffuse.y, m->diffuse.z);
+       }
+       /* specular */
+       int spec_loc;
+       if((spec_loc = sprog->get_uniform_location("specular")) != -1) {
+               sprog->set_uniformf(spec_loc, m->specular.x, m->specular.y, m->specular.z);
+       }
+       /* specular exponent */
+       int shin_loc;
+       if((shin_loc = sprog->get_uniform_location("shininess")) != -1) {
+               sprog->set_uniformf(shin_loc, m->shininess);
+       }
+
+       if (!sprog->link())
+               return;
+
+       sprog->use();
+       object->mesh->draw();
 }
\ No newline at end of file
index b3d2424..c33206b 100644 (file)
@@ -1,7 +1,7 @@
 #ifndef RENDERER_H_
 #define RENDERER_H_
 
-class Camera;
+class OrbitCamera;
 class Object;
 class Scene;
 class ShaderProgram;
@@ -13,7 +13,7 @@ protected:
 
 public:
        Scene *scene;
-       Camera *camera;
+       OrbitCamera *camera;
 
        Renderer();
        virtual ~Renderer();