]> git.treefish.org Git - seamulator.git/blob - src/watersurface.cpp
Fixed license text
[seamulator.git] / src / watersurface.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 "watersurface.h"
21
22 #include <GL/glut.h>
23
24 WaterSurface::WaterSurface(int size, double extend) :
25   m_size{size},
26   m_extend{extend}
27 {
28   m_points.resize(size*size);
29 }
30
31 SurfacePoint& WaterSurface::at(int x, int y)
32 {
33   return m_points.at(x + m_size*y);
34 }
35
36 const SurfacePoint& WaterSurface::at(int x, int y) const
37 {
38   return m_points.at(x + m_size*y);
39 }
40
41 int WaterSurface::size() const
42 {
43   return m_size;
44 }
45
46 double WaterSurface::extend() const
47 {
48   return m_extend;
49 }
50
51 void WaterSurface::draw() const
52 {
53   const double scaleFactor{m_extend / m_size};
54
55   glScalef(scaleFactor, scaleFactor, 1.0f);
56   glTranslatef(-(float)(m_size - 1) / 2, -(float)(m_size - 1) / 2, 0);
57
58   for (int y = 0; y < m_size - 1; ++y) {
59     for (int x = 0; x < m_size - 1; ++x) {
60       drawSingleTile(x, y);
61     }
62   }
63 }
64
65 void WaterSurface::drawSingleTile(int x, int y) const
66 {
67   glBegin(GL_TRIANGLES);
68
69   glVertex3f(x, y, at(x, y).getHeight());
70   glVertex3f(x+1, y, at(x+1, y).getHeight());
71   glVertex3f(x+1, y+1, at(x+1, y+1).getHeight());
72
73   glVertex3f(x, y, at(x, y).getHeight());
74   glVertex3f(x, y+1, at(x, y+1).getHeight());
75   glVertex3f(x+1, y+1, at(x+1, y+1).getHeight());
76
77   glEnd();
78 }