]> git.treefish.org Git - seamulator.git/commitdiff
Moved view code to separate class
authorAlexander Schmidt <alex@treefish.org>
Fri, 15 Jul 2016 21:52:25 +0000 (23:52 +0200)
committerAlexander Schmidt <alex@treefish.org>
Fri, 15 Jul 2016 21:52:25 +0000 (23:52 +0200)
CMakeLists.txt
seamulator.cpp
seaview.cpp [new file with mode: 0644]
seaview.h [new file with mode: 0644]

index aca8dab2ed1e1a5421b970f8720482e2eac1723f..63f645f322b95023485476aef13ab23b23b3fa3f 100644 (file)
@@ -6,5 +6,8 @@ find_package(OpenGL REQUIRED)
 find_package(GLUT REQUIRED)
 include_directories(${OPENGL_INCLUDE_DIRS} ${GLUT_INCLUDE_DIRS})
 
-add_executable(seamulator seamulator.cpp sea.cpp watersurface.cpp surfacepoint.cpp)
+add_executable(seamulator seamulator.cpp
+                         sea.cpp watersurface.cpp surfacepoint.cpp
+                         seaview.cpp)
+
 target_link_libraries(seamulator ${OPENGL_LIBRARIES} ${GLUT_LIBRARY})
index 3d6a5397125afd4ae82ff378489d02f57299250e..d3ac1c48172ed34cd6d27c9a8fee73e76f53879c 100644 (file)
@@ -1,9 +1,11 @@
 #include <ctime>
 #include <cmath>
+#include <memory>
 
 #include <GL/glut.h>
 
 #include "sea.h"
+#include "seaview.h"
 #include "watersurface.h"
 
 const int LATTICE_SIZE = 10;
@@ -11,14 +13,7 @@ const double LATTICE_UNIT = 1;
 
 SeaPtr sea;
 WaterSurfacePtr surface;
-
-struct
-{
-  float lookRadius = LATTICE_SIZE*LATTICE_UNIT*1.5;
-  float lookAz = 0;
-  float lookAlt = M_PI/4;
-  float fstMouseAngle[2], oldLookAz, oldLookAlt;
-} view;
+std::unique_ptr<SeaView> seaView;
 
 void glDisplayFunc();
 void glReshapeFunc(int width, int height);
@@ -31,13 +26,14 @@ int main(int argc, char** argv)
 
   surface = std::make_shared<WaterSurface>(LATTICE_SIZE, LATTICE_UNIT);
   sea = std::make_shared<Sea>(surface);
+  seaView = std::make_unique<SeaView>(LATTICE_SIZE * LATTICE_UNIT * 1.5,
+                                     0, M_PI/4);
 
   glutInit(&argc, argv);
   glutInitDisplayMode(GLUT_DOUBLE);
   glutInitWindowSize(300, 300);
   glutInitWindowPosition(100, 100);
   glutCreateWindow("seamulator");
-
   glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
 
   glutDisplayFunc(glDisplayFunc);
@@ -52,81 +48,30 @@ int main(int argc, char** argv)
 
 void glDisplayFunc()
 {
-  glMatrixMode(GL_MODELVIEW);
-  glLoadIdentity();
-
   glClear(GL_COLOR_BUFFER_BIT);
 
+  seaView->setupView();
   sea->update();
   surface->draw();
 
   glutSwapBuffers();
-
   glutPostRedisplay();
 }
 
-void updateView(int width, int height)
+void glReshapeFunc(int width, int height)
 {
   glMatrixMode(GL_PROJECTION);
   glLoadIdentity();
-
   gluPerspective(50.0, ((float)width/(float)height), 0, 1000.0);
   glViewport(0, 0, width, height);
-
-  float eyePos[3];
-  eyePos[0] = view.lookRadius*cos(view.lookAlt)*sin(view.lookAz);
-  eyePos[1] = view.lookRadius*cos(view.lookAlt)*cos(view.lookAz);
-  eyePos[2] = view.lookRadius*sin(view.lookAlt);
-
-  gluLookAt(eyePos[0], eyePos[1], eyePos[2],
-           0, 0, 0,
-           0, 0, 1);
-}
-
-void updateView()
-{
-  updateView(glutGet(GLUT_WINDOW_WIDTH), glutGet(GLUT_WINDOW_HEIGHT));
-}
-
-void glReshapeFunc(int width, int height)
-{
-  updateView(width, height);
 }
 
 void glMouseFunc(int button, int state, int x, int y)
 {
-  if (button == 3 && state == 0)
-    {
-      view.lookRadius += view.lookRadius*0.1;
-      updateView();
-    }
-
-  else if (button == 4 && state == 0)
-    {
-      view.lookRadius -= view.lookRadius*0.1;
-      updateView();
-    }
-
-  else if (button == 0 && state == 0) {
-    view.fstMouseAngle[0] = x;
-    view.fstMouseAngle[1] = y;
-    view.oldLookAz = view.lookAz;
-    view.oldLookAlt = view.lookAlt;
-  }
-
+  seaView->onMouseEvent(button, state, x, y);
 }
 
 void glMotionFunc(int x, int y)
 {
-  view.lookAlt =
-    fmod(view.oldLookAlt +
-        (float)((y-view.fstMouseAngle[1])*2*M_PI/glutGet(GLUT_WINDOW_HEIGHT)),
-        (float)(2*M_PI));
-
-  view.lookAz =
-    fmod(view.oldLookAz +
-        (float)((x-view.fstMouseAngle[0])*2*M_PI/glutGet(GLUT_WINDOW_WIDTH)),
-        (float)(2*M_PI));
-
-  updateView();
+  seaView->onMouseMove(x, y);
 }
diff --git a/seaview.cpp b/seaview.cpp
new file mode 100644 (file)
index 0000000..035f557
--- /dev/null
@@ -0,0 +1,62 @@
+#include "seaview.h"
+
+#include <cmath>
+
+#include <GL/glut.h>
+
+SeaView::SeaView(double distance, double azimuth, double altitude) :
+  m_distance{distance},
+  m_azimuth{azimuth},
+  m_altitude{altitude}
+{
+}
+
+void SeaView::onMouseEvent(int button, int state, int x, int y)
+{
+  if (button == 3 && state == 0) {
+    m_distance += m_distance*DISTANCE_MULTIPLIER;
+    glutPostRedisplay();
+  }
+  else if (button == 4 && state == 0) {
+    m_distance -= m_distance*DISTANCE_MULTIPLIER;
+    glutPostRedisplay();
+  }
+  else if (button == 0 && state == 0) {
+    m_mouseDownPos[0] = x;
+    m_mouseDownPos[1] = y;
+    m_mouseDownAzimuth = m_azimuth;
+    m_mouseDownAltitude = m_altitude;
+  }
+}
+
+void SeaView::onMouseMove(int x, int y)
+{
+  m_altitude =
+    fmod(m_mouseDownAltitude +
+        (double)((y - m_mouseDownPos[1]) *
+                 2*M_PI / glutGet(GLUT_WINDOW_HEIGHT)),
+        (double)(2*M_PI));
+
+  m_azimuth =
+    fmod(m_mouseDownAzimuth +
+        (double)((x - m_mouseDownPos[0]) *
+                 2*M_PI / glutGet(GLUT_WINDOW_WIDTH)),
+        (double)(2*M_PI));
+
+  glutPostRedisplay();
+}
+
+void SeaView::setupView() const
+{
+  glMatrixMode(GL_MODELVIEW);
+  glLoadIdentity();
+
+  const double eyePos[3] =
+    {m_distance * cos(m_altitude) * sin(m_azimuth),
+     m_distance * cos(m_altitude) * cos(m_azimuth),
+     m_distance * sin(m_altitude)};
+
+  gluLookAt(eyePos[0], eyePos[1], eyePos[2],
+           0, 0, 0,
+           0, 0, 1);
+}
diff --git a/seaview.h b/seaview.h
new file mode 100644 (file)
index 0000000..213bce0
--- /dev/null
+++ b/seaview.h
@@ -0,0 +1,20 @@
+#pragma once
+
+class SeaView
+{
+ public:
+  SeaView(double distance, double azimuth, double altitude);
+  void onMouseEvent(int button, int state, int x, int y);
+  void onMouseMove(int x, int y);
+  void setupView() const;
+
+ private:
+  static constexpr double DISTANCE_MULTIPLIER = 0.1f;
+
+  double m_distance;
+  double m_azimuth;
+  double m_altitude;
+  int m_mouseDownPos[2];
+  double m_mouseDownAltitude;
+  double m_mouseDownAzimuth;
+};