From 26d75ac0ced7a40f08a67e85870d27d792f7a105 Mon Sep 17 00:00:00 2001
From: bobarna <barnabas.borcsok@gmail.com>
Date: Thu, 25 Feb 2021 14:04:27 +0100
Subject: [PATCH] Particle class and other work

---
 .gitignore               | 22 +++++++++++++++
 CMakeLists.txt           |  4 ++-
 src/application.cpp      |  5 ++++
 src/application.h        | 11 ++++++--
 src/constants.h          |  6 ++--
 src/main.cpp             |  1 +
 src/rendering/camera.cpp | 59 ++++++++++++++++++++++++++++++++++++++++
 src/rendering/camera.h   | 31 +++++++++++++++++++++
 src/scene.h              |  7 +++++
 src/sph/sph_particle.cpp |  3 ++
 src/sph/sph_particle.h   | 44 ++++++++++++++++++++++++++++++
 11 files changed, 187 insertions(+), 6 deletions(-)
 create mode 100644 src/rendering/camera.cpp
 create mode 100644 src/rendering/camera.h
 create mode 100644 src/sph/sph_particle.cpp
 create mode 100644 src/sph/sph_particle.h

diff --git a/.gitignore b/.gitignore
index 7ffc87e..6274a35 100644
--- a/.gitignore
+++ b/.gitignore
@@ -174,3 +174,25 @@ fabric.properties
 
 # Android studio 3.1+ serialized cache file
 .idea/caches/build_file_checksums.ser
+
+### Vim
+# Swap
+[._]*.s[a-v][a-z]
+!*.svg  # comment out if you don't need vector files
+[._]*.sw[a-p]
+[._]s[a-rt-v][a-z]
+[._]ss[a-gi-z]
+[._]sw[a-p]
+
+# Session
+Session.vim
+Sessionx.vim
+*.vim
+
+# Temporary
+.netrwhist
+*~
+# Auto-generated tag files
+tags
+# Persistent undo
+[._]*.un~
diff --git a/CMakeLists.txt b/CMakeLists.txt
index b0ccef2..ecec384 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -3,7 +3,7 @@ project(madid)
 
 set(CMAKE_CXX_STANDARD 17)
 #set(CMAKE_CXX_FLAGS "-Wall -Wextra -Werror -pedantic")
-set(CMAKE_CXX_FLAGS "-Wall -Wextra -pedantic")
+set(CMAKE_CXX_FLAGS "-Wall -Wextra -pedantic -Wunused-parameter")
 
 add_executable(${PROJECT_NAME}
         src/main.cpp
@@ -13,6 +13,8 @@ add_executable(${PROJECT_NAME}
         src/application.h
         src/scene.cpp
         src/scene.h
+        src/rendering/camera.cpp
+        src/rendering/camera.h
         )
 
 
diff --git a/src/application.cpp b/src/application.cpp
index 30fcc0c..c32a513 100644
--- a/src/application.cpp
+++ b/src/application.cpp
@@ -23,6 +23,8 @@ void Application::init() {
     glDepthMask(GL_TRUE);
     glLoadIdentity();
     glClearColor(255.0f / 255.0f, 25.0f / 255.0f, 25.0f / 255.0f, 1.0f);
+
+    camera.glSetupCamera();
 }
 
 void Application::error_callback(int error, const char* description) {
@@ -101,9 +103,12 @@ void Application::run() {
     }
 
     // TODO think about rendering only in 24 FPS
+    // TODO guarantee delta_time to be infinitesimal
     scene->update(delta_time);
     //clear color and depth buffer
     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
     glLoadIdentity(); //load identity matrix
+    camera->glSetupCamera();
     scene->render();
+
 }
diff --git a/src/application.h b/src/application.h
index 9cebea9..0e57061 100644
--- a/src/application.h
+++ b/src/application.h
@@ -16,8 +16,6 @@ class Application {
     int width = WIDTH;
     int height = HEIGHT;
 
-    char *name = "Madid";
-
     static bool dragging;
     // Callback functions
     static void error_callback(int error, const char* description);
@@ -32,6 +30,15 @@ class Application {
 
     void run();
     double time_since_last_frame;
+
+    std::shared_ptr<Camera> camera = std::make_shared<Camera>();
+    // TODO
+    //boolean flag, indicates whether a screen capture event should be performed
+    //on the next display event.
+    /* bool screen_capture; */ 
+    /* bool zoom_mode; */ 
+    /* bool pan_mode; */
+    /* bool trackball_mode; */
 public:
     Application();
     ~Application();
diff --git a/src/constants.h b/src/constants.h
index feb637e..a34aed8 100644
--- a/src/constants.h
+++ b/src/constants.h
@@ -1,9 +1,9 @@
 #ifndef MADID_CONSTANTS_H
 #define MADID_CONSTANTS_H
 
-static int WIDTH = 500;
-static int HEIGHT = 500;
+const int WIDTH = 500;
+const int HEIGHT = 500;
 
-static char *NAME = "Madid";
+char * const NAME = "Madid";
 
 #endif
diff --git a/src/main.cpp b/src/main.cpp
index 3d6914d..203a60c 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -7,6 +7,7 @@
 #include "application.h"
 #include "constants.h"
 
+
 int main(int argc, char **argv) {
 
     // start GL context and O/S window using the GLFW helper library
diff --git a/src/rendering/camera.cpp b/src/rendering/camera.cpp
new file mode 100644
index 0000000..fa0b3fd
--- /dev/null
+++ b/src/rendering/camera.cpp
@@ -0,0 +1,59 @@
+#include "camera.h"
+
+Camera::Camera(void):
+    aspect_ratio(1),
+    pos(0, 25, -50),
+    forward(0,0,1),
+    right(-1,0,0),
+    speed(10) 
+{
+}
+
+Camera::~Camera(void) {
+}
+
+
+void Camera::control(float delta_time, bool* inputs) {
+    // process camera keys
+    for(int i = 0; i < 256; i++) {
+        if(!inputs[i]) continue;
+        switch(i)
+        {
+            case 'w': pos += forward * speed * delta_time; break;
+            case 's': pos -= forward * speed * delta_time; break;
+            case 'a': pos -= right * speed * delta_time; break;
+            case 'd': pos += right * speed * delta_time; break;
+            case 'q': pos += glm::vec3(0,1,0) * speed * delta_time; break;
+            case 'e': pos += glm::vec3(0,1,0) * speed * delta_time; break;
+        }
+    }
+}
+
+void Camera::startDrag(int x, int y) {
+   drag_start = glm::vec3(0,0);
+}
+
+void Camera::drag(int x, int y) {
+    glm::vec2 d(drag_start.x - x, drag_start.y - y);
+    
+    forward = glm::normalize(forward);
+    right = cross(forward, glm::vec3(0,1,0));
+    right = glm::normalize(right);
+
+    // TODO 
+}
+
+
+void Camera::glSetupCamera() {
+    glMatrixMode(GL_PROJECTION);
+    glLoadIdentity();
+    gluPerspective(45.0f, aspect_ratio, 1.0f, 1000.0f);
+
+    glMatrixMode(GL_MODELVIEW);
+    glLoadIdentity();
+    gluLookAt(
+            pos.x, pos.y, pos.z,
+            pos.x + forward.x, pos.y + forward.y, pos.z + forward.z, 
+            0.0f, 1.0f, 0.0f);
+}
+
diff --git a/src/rendering/camera.h b/src/rendering/camera.h
new file mode 100644
index 0000000..8d2e91e
--- /dev/null
+++ b/src/rendering/camera.h
@@ -0,0 +1,31 @@
+#ifndef MADID_SCENE_H
+#define MADID_SCENE_H
+
+#include <glm/glm.hpp>
+#include "utility/gl.h"
+
+class Camera {
+    float aspect_ratio;
+    glm::vec3 pos;
+    glm::vec3 forward;
+    glm::vec3 right;
+    float speed;
+
+    glm::vec2 drag_start(0,0);
+public:
+    void setAspectRatio(float ar) {
+        aspect_ratio = ar;
+    }
+
+    void control(float delta_time, bool* inputs);
+    void startDrag(int x, int y);
+    void drag(int x, int y);
+
+    void glSetupCamera();
+
+    Camera(void);
+    ~Camera(void);
+
+}
+
+#endif
diff --git a/src/scene.h b/src/scene.h
index 0f845aa..d1a07d8 100644
--- a/src/scene.h
+++ b/src/scene.h
@@ -4,6 +4,13 @@
 #include <iostream>
 
 class Scene {
+    bool draw_normals;
+    bool draw_surface;
+    bool draw_velocity;
+    bool render;
+    bool use_emitter;
+    
+    std::shared_ptr<Scene> scene = std::make_shared<Scene>();
 public:
     Scene();
     ~Scene();
diff --git a/src/sph/sph_particle.cpp b/src/sph/sph_particle.cpp
new file mode 100644
index 0000000..c83c664
--- /dev/null
+++ b/src/sph/sph_particle.cpp
@@ -0,0 +1,3 @@
+#include "scene.h"
+
+using namespace sph::Particl
diff --git a/src/sph/sph_particle.h b/src/sph/sph_particle.h
new file mode 100644
index 0000000..ee0ad85
--- /dev/null
+++ b/src/sph/sph_particle.h
@@ -0,0 +1,44 @@
+#ifndef MADID_SCENE_H
+#define MADID_SCENE_H
+
+#include <glm/glm.hpp>
+
+namespace sph {
+    class Particle {
+        public:
+            typedef glm::vec3 vector;
+            typedef vector point_type;
+
+            Particle();
+        private:
+            float radius; // radius of the particle
+            float radius_sqr; // squared radius of the particle
+            point_type pos; // position of the particle
+            point_type old_pos; // old position of the particle
+            vector v; // current velocity
+            vector a; // current acceleration
+            vector f; // current sum of external forces acting on the particle
+            vector n; // surface normal if close to/on the surface, else 0
+            float m; // mass (constant)
+            float rho; // density (varies)
+            float p; // pressure (varies)
+            bool fixed; // Is this particled fixed (locked)?
+        public:
+            // will need these for hashing
+            point_type min() const;
+            point_type max() const;
+
+            // checks if two particles (actually two positions) are within an
+            // allowed radius.
+            bool check(const point_type& candidate) const;
+
+            // get position (read only)
+            const point_type& position() const;
+            // get position
+            point_type& position();
+
+            
+    }
+}
+
+#endif
-- 
GitLab