]> git.treefish.org Git - seamulator.git/blob - src/seaview.cpp
34944d6caea754072d788f6bd01b9c77b94ef3ab
[seamulator.git] / src / seaview.cpp
1 /**
2  * Copyright (C) 2016  Alexander Schmidt
3  *
4  * This file is part of Seamulator.
5  *
6  * Seamulator is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * Seamulator is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with Foobar.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 #include "seaview.h"
21
22 #include <cmath>
23
24 #include <GL/glut.h>
25
26 SeaView::SeaView(double distance, double azimuth, double altitude) :
27   m_distance{distance},
28   m_azimuth{azimuth},
29   m_altitude{altitude}
30 {
31 }
32
33 void SeaView::onMouseEvent(int button, int state, int x, int y)
34 {
35   if (button == 3 && state == 0) {
36     m_distance += m_distance*DISTANCE_MULTIPLIER;
37     glutPostRedisplay();
38   }
39   else if (button == 4 && state == 0) {
40     m_distance -= m_distance*DISTANCE_MULTIPLIER;
41     glutPostRedisplay();
42   }
43   else if (button == 0 && state == 0) {
44     m_mouseDownPos[0] = x;
45     m_mouseDownPos[1] = y;
46     m_mouseDownAzimuth = m_azimuth;
47     m_mouseDownAltitude = m_altitude;
48   }
49 }
50
51 void SeaView::onMouseMove(int x, int y)
52 {
53   m_altitude =
54     fmod(m_mouseDownAltitude +
55          (double)((y - m_mouseDownPos[1]) *
56                   2*M_PI / glutGet(GLUT_WINDOW_HEIGHT)),
57          (double)(2*M_PI));
58
59   m_azimuth =
60     fmod(m_mouseDownAzimuth +
61          (double)((x - m_mouseDownPos[0]) *
62                   2*M_PI / glutGet(GLUT_WINDOW_WIDTH)),
63          (double)(2*M_PI));
64
65   glutPostRedisplay();
66 }
67
68 void SeaView::setupView() const
69 {
70   glMatrixMode(GL_MODELVIEW);
71   glLoadIdentity();
72
73   const double eyePos[3] =
74     {m_distance * cos(m_altitude) * sin(m_azimuth),
75      m_distance * cos(m_altitude) * cos(m_azimuth),
76      m_distance * sin(m_altitude)};
77
78   gluLookAt(eyePos[0], eyePos[1], eyePos[2],
79             0, 0, 0,
80             0, 0, 1);
81 }