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