]> git.treefish.org Git - seamulator.git/blob - src/seamulator.cpp
Play note on velocity sign change
[seamulator.git] / src / seamulator.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 Seamulator.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 #include <ctime>
21 #include <cmath>
22 #include <iostream>
23 #include <memory>
24
25 #include <boost/program_options.hpp>
26 #include <GL/glut.h>
27
28 #include "sea.h"
29 #include "seaview.h"
30 #include "synthesizer.h"
31 #include "watersurface.h"
32
33 const int INIT_WINDOW_POS_X{50};
34 const int INIT_WINDOW_POS_Y{50};
35 const int INIT_WINDOW_WIDTH{800};
36 const int INIT_WINDOW_HEIGHT{600};
37 const double INIT_VIEW_AZIMUTH{0};
38 const double INIT_VIEW_ALTITUDE{M_PI / 4};
39 const char WINDOW_TITLE[]{"seamulator"};
40
41 SeaPtr sea;
42 WaterSurfacePtr surface;
43 std::unique_ptr<SeaView> seaView;
44 std::unique_ptr<Synthesizer> synthesizer;
45
46 struct Settings {
47   double windSpeed;
48   int latticeSize;
49   double latticeExtend;
50   double amplitudeFactor;
51 };
52
53 void glDisplayFunc();
54 void glReshapeFunc(int width, int height);
55 void glMouseFunc(int button, int state, int x, int y);
56 void glMotionFunc(int x, int y);
57 Settings parseArguments(int argc, char** argv);
58
59 namespace po = boost::program_options;
60
61 int main(int argc, char** argv)
62 {
63   Settings settings = parseArguments(argc, argv);
64
65   std::srand(std::time(0));
66   surface = std::make_shared<WaterSurface>(settings.latticeSize,
67                                            settings.latticeExtend);
68   sea = std::make_shared<Sea>(surface,
69                               settings.windSpeed,
70                               settings.amplitudeFactor);
71   seaView = std::make_unique<SeaView>(settings.latticeExtend * 1.5,
72                                       INIT_VIEW_AZIMUTH,
73                                       INIT_VIEW_ALTITUDE);
74   synthesizer = std::make_unique<Synthesizer>(surface);
75
76   glutInit(&argc, argv);
77   glutInitDisplayMode(GLUT_DOUBLE);
78   glutInitWindowSize(INIT_WINDOW_WIDTH, INIT_WINDOW_HEIGHT);
79   glutInitWindowPosition(INIT_WINDOW_POS_X, INIT_WINDOW_POS_Y);
80   glutCreateWindow(WINDOW_TITLE);
81   glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
82
83   glutDisplayFunc(glDisplayFunc);
84   glutReshapeFunc(glReshapeFunc);
85   glutMouseFunc(glMouseFunc);
86   glutMotionFunc(glMotionFunc);
87
88   glutMainLoop();
89
90   return 0;
91 }
92
93 Settings parseArguments(int argc, char** argv)
94 {
95   po::options_description desc("Available options");
96   desc.add_options()
97     ("help,h", "show this help")
98     ("windspeed,w", po::value<double>()->default_value(10),
99      "wind speed (m/s)")
100     ("size,s", po::value<char>()->default_value('s'),
101      "lattice size (s|l)")
102     ("amplitude,a", po::value<double>()->default_value(1),
103      "amplitude multiplicator")
104     ;
105
106   po::variables_map vm;
107   po::store(po::parse_command_line(argc, argv, desc), vm);
108   po::notify(vm);
109
110   if (vm.count("help")) {
111     std::cout << desc << "\n";
112     exit(1);
113   }
114
115   Settings settings;
116
117   settings.windSpeed = vm["windspeed"].as<double>();
118
119   if (vm["size"].as<char>() == 'l') {
120     settings.latticeSize = 256;
121     settings.latticeExtend = 20;
122     settings.amplitudeFactor = vm["amplitude"].as<double>() * 0.00000004;
123   }
124   else {
125     settings.latticeSize = 128;
126     settings.latticeExtend = 10;
127     settings.amplitudeFactor = vm["amplitude"].as<double>() * 0.0000001;
128   }
129
130   return settings;
131 }
132
133 void glDisplayFunc()
134 {
135   glClear(GL_COLOR_BUFFER_BIT);
136
137   seaView->setupView();
138   sea->update();
139   surface->draw();
140   synthesizer->tick();
141
142   glutSwapBuffers();
143   glutPostRedisplay();
144 }
145
146 void glReshapeFunc(int width, int height)
147 {
148   glMatrixMode(GL_PROJECTION);
149   glLoadIdentity();
150   gluPerspective(50.0, ((float)width/(float)height), 0, 1000.0);
151   glViewport(0, 0, width, height);
152 }
153
154 void glMouseFunc(int button, int state, int x, int y)
155 {
156   seaView->onMouseEvent(button, state, x, y);
157 }
158
159 void glMotionFunc(int x, int y)
160 {
161   seaView->onMouseMove(x, y);
162 }