]> git.treefish.org Git - seamulator.git/blob - seamulator.cpp
3d6a5397125afd4ae82ff378489d02f57299250e
[seamulator.git] / seamulator.cpp
1 #include <ctime>
2 #include <cmath>
3
4 #include <GL/glut.h>
5
6 #include "sea.h"
7 #include "watersurface.h"
8
9 const int LATTICE_SIZE = 10;
10 const double LATTICE_UNIT = 1;
11
12 SeaPtr sea;
13 WaterSurfacePtr surface;
14
15 struct
16 {
17   float lookRadius = LATTICE_SIZE*LATTICE_UNIT*1.5;
18   float lookAz = 0;
19   float lookAlt = M_PI/4;
20   float fstMouseAngle[2], oldLookAz, oldLookAlt;
21 } view;
22
23 void glDisplayFunc();
24 void glReshapeFunc(int width, int height);
25 void glMouseFunc(int button, int state, int x, int y);
26 void glMotionFunc(int x, int y);
27
28 int main(int argc, char** argv)
29 {
30   std::srand(std::time(0));
31
32   surface = std::make_shared<WaterSurface>(LATTICE_SIZE, LATTICE_UNIT);
33   sea = std::make_shared<Sea>(surface);
34
35   glutInit(&argc, argv);
36   glutInitDisplayMode(GLUT_DOUBLE);
37   glutInitWindowSize(300, 300);
38   glutInitWindowPosition(100, 100);
39   glutCreateWindow("seamulator");
40
41   glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
42
43   glutDisplayFunc(glDisplayFunc);
44   glutReshapeFunc(glReshapeFunc);
45   glutMouseFunc(glMouseFunc);
46   glutMotionFunc(glMotionFunc);
47
48   glutMainLoop();
49
50   return 0;
51 }
52
53 void glDisplayFunc()
54 {
55   glMatrixMode(GL_MODELVIEW);
56   glLoadIdentity();
57
58   glClear(GL_COLOR_BUFFER_BIT);
59
60   sea->update();
61   surface->draw();
62
63   glutSwapBuffers();
64
65   glutPostRedisplay();
66 }
67
68 void updateView(int width, int height)
69 {
70   glMatrixMode(GL_PROJECTION);
71   glLoadIdentity();
72
73   gluPerspective(50.0, ((float)width/(float)height), 0, 1000.0);
74   glViewport(0, 0, width, height);
75
76   float eyePos[3];
77   eyePos[0] = view.lookRadius*cos(view.lookAlt)*sin(view.lookAz);
78   eyePos[1] = view.lookRadius*cos(view.lookAlt)*cos(view.lookAz);
79   eyePos[2] = view.lookRadius*sin(view.lookAlt);
80
81   gluLookAt(eyePos[0], eyePos[1], eyePos[2],
82             0, 0, 0,
83             0, 0, 1);
84 }
85
86 void updateView()
87 {
88   updateView(glutGet(GLUT_WINDOW_WIDTH), glutGet(GLUT_WINDOW_HEIGHT));
89 }
90
91 void glReshapeFunc(int width, int height)
92 {
93   updateView(width, height);
94 }
95
96 void glMouseFunc(int button, int state, int x, int y)
97 {
98   if (button == 3 && state == 0)
99     {
100       view.lookRadius += view.lookRadius*0.1;
101       updateView();
102     }
103
104   else if (button == 4 && state == 0)
105     {
106       view.lookRadius -= view.lookRadius*0.1;
107       updateView();
108     }
109
110   else if (button == 0 && state == 0) {
111     view.fstMouseAngle[0] = x;
112     view.fstMouseAngle[1] = y;
113     view.oldLookAz = view.lookAz;
114     view.oldLookAlt = view.lookAlt;
115   }
116
117 }
118
119 void glMotionFunc(int x, int y)
120 {
121   view.lookAlt =
122     fmod(view.oldLookAlt +
123          (float)((y-view.fstMouseAngle[1])*2*M_PI/glutGet(GLUT_WINDOW_HEIGHT)),
124          (float)(2*M_PI));
125
126   view.lookAz =
127     fmod(view.oldLookAz +
128          (float)((x-view.fstMouseAngle[0])*2*M_PI/glutGet(GLUT_WINDOW_WIDTH)),
129          (float)(2*M_PI));
130
131   updateView();
132 }