From 826b0cf7adaf8b1dc4c37f57a4a98c79a3995e21 Mon Sep 17 00:00:00 2001 From: Eleni Maria Stea Date: Wed, 12 Jul 2017 18:35:01 +0300 Subject: [PATCH] quick backup of changes: mouse handling, camera, shaderprog etc --- src/camera.cc | 20 +++++++++++++++++++ src/camera.h | 20 +++++++++++++++++++ src/common-ui.h | 7 ------- src/main.cc | 50 ++++++++++++++++++++++++++++++++++++++++++++++- src/opengl/opengl.cc | 15 +++++++++++++- src/opengl/shader-gl.cc | 5 +++++ src/opengl/shader-gl.h | 1 + src/shader.h | 5 +++++ 8 files changed, 114 insertions(+), 9 deletions(-) create mode 100644 src/camera.cc create mode 100644 src/camera.h delete mode 100644 src/common-ui.h diff --git a/src/camera.cc b/src/camera.cc new file mode 100644 index 0000000..d7839b5 --- /dev/null +++ b/src/camera.cc @@ -0,0 +1,20 @@ +#include +#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 index 0000000..8b3a895 --- /dev/null +++ b/src/camera.h @@ -0,0 +1,20 @@ +#ifndef CAMERA_H_ +#define CAMERA_H_ + +#include + +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 index addcbcf..0000000 --- a/src/common-ui.h +++ /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_ diff --git a/src/main.cc b/src/main.cc index dc3440d..4834548 100644 --- a/src/main.cc +++ b/src/main.cc @@ -3,8 +3,10 @@ #include #include -#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) { diff --git a/src/opengl/opengl.cc b/src/opengl/opengl.cc index f05ea61..d2aea3b 100644 --- a/src/opengl/opengl.cc +++ b/src/opengl/opengl.cc @@ -2,9 +2,15 @@ #include #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 diff --git a/src/opengl/shader-gl.cc b/src/opengl/shader-gl.cc index 334a1cf..c5b57a7 100644 --- a/src/opengl/shader-gl.cc +++ b/src/opengl/shader-gl.cc @@ -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 diff --git a/src/opengl/shader-gl.h b/src/opengl/shader-gl.h index a147c5d..ea3b1c1 100644 --- a/src/opengl/shader-gl.h +++ b/src/opengl/shader-gl.h @@ -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 diff --git a/src/shader.h b/src/shader.h index 1472fb3..28832f6 100644 --- a/src/shader.h +++ b/src/shader.h @@ -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_ -- 1.7.10.4