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