]> git.treefish.org Git - seamulator.git/blob - watersurface.cpp
Cleaning up
[seamulator.git] / watersurface.cpp
1 #include "watersurface.h"
2
3 #include <GL/glut.h>
4
5 WaterSurface::WaterSurface(int size, double unitLength) :
6   m_size{size},
7   m_unitLength{unitLength}
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::unitLength() const
28 {
29   return m_unitLength;
30 }
31
32 void WaterSurface::draw() const
33 {
34   glScalef(m_unitLength, m_unitLength, 1.0f);
35   glTranslatef(-(float)(m_size - 1) / 2, -(float)(m_size - 1) / 2, 0);
36
37   for (int y = 0; y < m_size - 1; ++y) {
38     for (int x = 0; x < m_size - 1; ++x) {
39       drawSingleTile(x, y);
40     }
41   }
42 }
43
44 void WaterSurface::drawSingleTile(int x, int y) const
45 {
46   glBegin(GL_TRIANGLES);
47
48   glVertex3f(x, y, at(x, y).getHeight());
49   glVertex3f(x+1, y, at(x+1, y).getHeight());
50   glVertex3f(x+1, y+1, at(x+1, y+1).getHeight());
51
52   glVertex3f(x, y, at(x, y).getHeight());
53   glVertex3f(x, y+1, at(x, y+1).getHeight());
54   glVertex3f(x+1, y+1, at(x+1, y+1).getHeight());
55
56   glEnd();
57 }