]> git.treefish.org Git - seamulator.git/blob - watersurface.cpp
Modified ignores
[seamulator.git] / watersurface.cpp
1 #include "watersurface.h"
2
3 #include <GL/glut.h>
4
5 WaterSurface::WaterSurface(int size, double extend) :
6   m_size{size},
7   m_extend{extend}
8 {
9   m_points.resize(size*size);
10 }
11
12 SurfacePoint& WaterSurface::at(int x, int y)
13 {
14   return m_points.at(x + m_size*y);
15 }
16
17 const SurfacePoint& WaterSurface::at(int x, int y) const
18 {
19   return m_points.at(x + m_size*y);
20 }
21
22 int WaterSurface::size() const
23 {
24   return m_size;
25 }
26
27 double WaterSurface::extend() const
28 {
29   return m_extend;
30 }
31
32 void WaterSurface::draw() const
33 {
34   const double scaleFactor{m_extend / m_size};
35
36   glScalef(scaleFactor, scaleFactor, 1.0f);
37   glTranslatef(-(float)(m_size - 1) / 2, -(float)(m_size - 1) / 2, 0);
38
39   for (int y = 0; y < m_size - 1; ++y) {
40     for (int x = 0; x < m_size - 1; ++x) {
41       drawSingleTile(x, y);
42     }
43   }
44 }
45
46 void WaterSurface::drawSingleTile(int x, int y) const
47 {
48   glBegin(GL_TRIANGLES);
49
50   glVertex3f(x, y, at(x, y).getHeight());
51   glVertex3f(x+1, y, at(x+1, y).getHeight());
52   glVertex3f(x+1, y+1, at(x+1, y+1).getHeight());
53
54   glVertex3f(x, y, at(x, y).getHeight());
55   glVertex3f(x, y+1, at(x, y+1).getHeight());
56   glVertex3f(x+1, y+1, at(x+1, y+1).getHeight());
57
58   glEnd();
59 }