1 /*
2  * Copyright (C) 2006-2020 by the Widelands Development Team
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17  *
18  */
19 
20 #ifndef WL_GRAPHIC_GL_TERRAIN_PROGRAM_H
21 #define WL_GRAPHIC_GL_TERRAIN_PROGRAM_H
22 
23 #include "base/vector.h"
24 #include "graphic/gl/fields_to_draw.h"
25 #include "graphic/gl/utils.h"
26 #include "logic/map_objects/description_maintainer.h"
27 #include "logic/map_objects/world/terrain_description.h"
28 
29 class TerrainProgram {
30 public:
31 	// Compiles the program. Throws on errors.
32 	TerrainProgram();
33 
34 	// Draws the terrain.
35 	void draw(uint32_t gametime,
36 	          const Widelands::DescriptionMaintainer<Widelands::TerrainDescription>& terrains,
37 	          const FieldsToDraw& fields_to_draw,
38 	          float z_value);
39 
40 private:
41 	struct PerVertexData {
42 		float gl_x;
43 		float gl_y;
44 		float brightness;
45 		float texture_x;
46 		float texture_y;
47 		float texture_offset_x;
48 		float texture_offset_y;
49 	};
50 	static_assert(sizeof(PerVertexData) == 28, "Wrong padding.");
51 
52 	void gl_draw(int gl_texture, float texture_w, float texture_h, float z_value);
53 
54 	// Adds a vertex to the end of vertices with data from 'field' and 'texture_coordinates'.
55 	void add_vertex(const FieldsToDraw::Field& field, const Vector2f& texture_coordinates);
56 
57 	// The program used for drawing the terrain.
58 	Gl::Program gl_program_;
59 
60 	// The buffer that will contain 'vertices_' for rendering.
61 	Gl::Buffer<PerVertexData> gl_array_buffer_;
62 
63 	// Attributes.
64 	GLint attr_brightness_;
65 	GLint attr_position_;
66 	GLint attr_texture_offset_;
67 	GLint attr_texture_position_;
68 
69 	// Uniforms.
70 	GLint u_terrain_texture_;
71 	GLint u_texture_dimensions_;
72 	GLint u_z_value_;
73 
74 	// Objects below are kept around to avoid memory allocations on each frame.
75 	// They could theoretically also be recreated.
76 	std::vector<PerVertexData> vertices_;
77 
78 	DISALLOW_COPY_AND_ASSIGN(TerrainProgram);
79 };
80 
81 #endif  // end of include guard: WL_GRAPHIC_GL_TERRAIN_PROGRAM_H
82