quick backup of changes:
authorEleni Maria Stea <estea@igalia.com>
Wed, 12 Jul 2017 15:35:01 +0000 (18:35 +0300)
committerEleni Maria Stea <estea@igalia.com>
Wed, 12 Jul 2017 15:35:01 +0000 (18:35 +0300)
mouse handling, camera, shaderprog etc

src/camera.cc [new file with mode: 0644]
src/camera.h [new file with mode: 0644]
src/common-ui.h [deleted file]
src/main.cc
src/opengl/opengl.cc
src/opengl/shader-gl.cc
src/opengl/shader-gl.h
src/shader.h

diff --git a/src/camera.cc b/src/camera.cc
new file mode 100644 (file)
index 0000000..d7839b5
--- /dev/null
@@ -0,0 +1,20 @@
+#include <gmath/gmath.h>
+#include "camera.h"
+
+Camera::Camera()
+{
+       phi = theta = distance = 0;
+       fov = 0;
+       m_projection = Mat4::identity;
+}
+
+Camera::Camera(float phi, float theta, float distance, float fov)
+{
+       this->phi = phi;
+       this->theta = theta;
+       this->distance = distance;
+
+       this->fov = fov;
+}
+
+Camera::~Camera() {}
\ No newline at end of file
diff --git a/src/camera.h b/src/camera.h
new file mode 100644 (file)
index 0000000..8b3a895
--- /dev/null
@@ -0,0 +1,20 @@
+#ifndef CAMERA_H_
+#define CAMERA_H_
+
+#include <gmath/gmath.h>
+
+class Camera {
+public:
+       float phi;
+       float theta;
+       float distance;
+
+       Mat4 m_projection;
+       float fov;
+
+       Camera();
+       Camera(float phi, float theta, float distance, float fov);
+       ~Camera();
+};
+
+#endif // CAMERA_H_
\ No newline at end of file
diff --git a/src/common-ui.h b/src/common-ui.h
deleted file mode 100644 (file)
index addcbcf..0000000
+++ /dev/null
@@ -1,7 +0,0 @@
-#ifndef COMMON_UI_H_
-#define COMMON_UI_H_
-
-int win_w = 800;
-int win_h = 600;
-
-#endif // COMMON_UI_H_
index dc3440d..4834548 100644 (file)
@@ -3,8 +3,10 @@
 #include <string.h>
 #include <vector>
 
-#include "object.h"
+/* TODO: fix those */
+#include "camera.h"
 #include "mesh.h"
+#include "object.h"
 #include "scene.h"
 
 #include "opengl/opengl.h"
@@ -18,12 +20,20 @@ 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);
 
 /* global variables */
 bool use_vulkan;
+
 GLFWwindow *win;
+int win_w = 800;
+int win_h = 600;
+
+Camera *camera;
 
 /* variables */
+// TODO: remove just for test:
 static Scene scene;
 
 int main(int argc, char **argv)
@@ -48,6 +58,8 @@ int main(int argc, char **argv)
        }
 
        glfwSetKeyCallback(win, key_clbk);
+       glfwSetCursorPosCallback(win, motion_clbk);
+       glfwSetMouseButtonCallback(win, mouse_clbk);
 
        while(!glfwWindowShouldClose(win)) {
                display();
@@ -71,6 +83,8 @@ static bool init()
                        return false;
        }
 
+       camera = new Camera(25, 25, 4, 45);
+
        if(!scene.load("data/spot/spot_control_mesh.obj")) {
                fprintf(stderr, "Failed to load scene.\n");
                return false;
@@ -94,6 +108,8 @@ static void cleanup()
        else {
                cleanup_opengl();
        }
+
+       delete camera;
 }
 
 static void key_clbk(GLFWwindow *win, int key, int scancode, int action, int mods)
@@ -103,6 +119,38 @@ static void key_clbk(GLFWwindow *win, int key, int scancode, int action, int mod
        }
 }
 
+static double prev_x, prev_y;
+static int bnstate[8];
+
+static void motion_clbk(GLFWwindow *win, double x, double y)
+{
+       int dx = x - prev_x;
+       int dy = y - prev_y;
+
+       prev_x = x;
+       prev_y = y;
+
+       if(!dx && !dy) return;
+
+       if(bnstate[0]) {
+               camera->theta += dx * 0.5;
+               camera->phi += dy * 0.5;
+
+               if(camera->phi < -90) camera->phi = -90;
+               if(camera->phi > 90) camera->phi = 90;
+       }
+       if(bnstate[2]) {
+               camera->distance += dy * 0.1;
+               if(camera->distance < 0.0) camera->distance = 0.0;
+       }
+}
+
+static void mouse_clbk(GLFWwindow *win, int bn, int action, int mods)
+{
+       bnstate[bn - GLFW_MOUSE_BUTTON_LEFT] = action == GLFW_PRESS ? 1 : 0;
+       glfwGetCursorPos(win, &prev_x, &prev_y);
+}
+
 static void display()
 {
        if(use_vulkan) {
index f05ea61..d2aea3b 100644 (file)
@@ -2,9 +2,15 @@
 #include <stdio.h>
 
 #include "opengl/opengl.h"
-#include "common-ui.h"
 
 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()) {
@@ -37,4 +43,11 @@ 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
index 334a1cf..c5b57a7 100644 (file)
@@ -166,4 +166,9 @@ bool ShaderProgramGL::load(const char *vfname, const char *ffname)
        shaders[1] = fsdr;
 
        return true;
+}
+
+void ShaderProgramGL::use()
+{
+       glUseProgram(prog);
 }
\ No newline at end of file
index a147c5d..ea3b1c1 100644 (file)
@@ -29,6 +29,7 @@ public:
        void delete_shaders();
        virtual bool link();
        virtual bool load(const char *vfname, const char *ffname);
+       virtual void use();
 };
 
 #endif // SHADER_GL_H_
\ No newline at end of file
index 1472fb3..28832f6 100644 (file)
@@ -33,6 +33,11 @@ public:
        virtual void add_shader(Shader *sdr);
        virtual bool link() = 0;
        virtual bool load(const char *vfname, const char *ffname) = 0;
+       virtual void use() = 0;
+
+       /* THIS PART IS GOING TO BE CHANGED: on vulkan we set the uniforms
+          using descriptor sets. The current design is suitable for OpenGL and
+          it has to become more generic to work with both APIs later. */
 };
 
 #endif // SHADER_H_