File: SmoothHeightMeshDrawer.cpp

package info (click to toggle)
spring 88.0%2Bdfsg1-1.1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 41,524 kB
  • sloc: cpp: 343,114; ansic: 38,414; python: 12,257; java: 12,203; awk: 5,748; sh: 1,204; xml: 997; perl: 405; objc: 192; makefile: 181; php: 134; sed: 2
file content (87 lines) | stat: -rwxr-xr-x 2,876 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */

#include "SmoothHeightMeshDrawer.h"
#include "Game/UnsyncedGameCommands.h"
#include "Sim/Misc/SmoothHeightMesh.h"
#include "Rendering/GL/VertexArray.h"
#include "System/float3.h"


namespace { // prevents linking problems in case of duplicate symbols

class AirMeshActionExecutor : public IUnsyncedActionExecutor {
public:
	static const std::string COMMAND_NAME;

	AirMeshActionExecutor() : IUnsyncedActionExecutor(COMMAND_NAME,
			"Show/Hide the smooth air-mesh map overlay") {}

	bool Execute(const UnsyncedAction& action) const {
		SetBoolArg(smoothHeightMeshDrawer->DrawEnabled(), action.GetArgs());
		LogSystemStatus("smooth air-mesh map overlay", smoothHeightMeshDrawer->DrawEnabled());
		return true;
	}
};

const std::string AirMeshActionExecutor::COMMAND_NAME = "AirMesh";

} // namespace (unnamed)


SmoothHeightMeshDrawer::SmoothHeightMeshDrawer()
		: drawEnabled(false)
{
	unsyncedGameCommands->AddActionExecutor(new AirMeshActionExecutor());
}

SmoothHeightMeshDrawer::~SmoothHeightMeshDrawer() {

	if (unsyncedGameCommands != NULL) {
		unsyncedGameCommands->RemoveActionExecutor(AirMeshActionExecutor::COMMAND_NAME);
	}
}

SmoothHeightMeshDrawer* SmoothHeightMeshDrawer::GetInstance() {
	static SmoothHeightMeshDrawer drawer;
	return &drawer;
}

void SmoothHeightMeshDrawer::Draw(float yoffset) {
	if (!drawEnabled)
		return;

	glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
	glLineWidth(1.0f);
	glDisable(GL_TEXTURE_2D);
	glActiveTexture(GL_TEXTURE0);
	glDisable(GL_TEXTURE_1D);
	glDisable(GL_CULL_FACE);

	const unsigned char color[4] = {0, 255, 0, 255};
	const float quadSize = 4.0f * smoothGround->GetResolution();
	const unsigned int numQuadsX = smoothGround->GetFMaxX() / quadSize;
	const unsigned int numQuadsZ = smoothGround->GetFMaxY() / quadSize;

	CVertexArray* va = GetVertexArray();
	va->Initialize();
	va->EnlargeArrays((numQuadsX + 1) * (numQuadsZ + 1) * 4, 0, VA_SIZE_C);

	for (unsigned int zq = 0; zq <= numQuadsZ; zq++) {
		for (unsigned int xq = 0; xq <= numQuadsX; xq++) {
			const float x = xq * quadSize;
			const float z = zq * quadSize;
			const float h1 = smoothGround->GetHeightAboveWater(x,            z           ) + yoffset;
			const float h2 = smoothGround->GetHeightAboveWater(x + quadSize, z           ) + yoffset;
			const float h3 = smoothGround->GetHeightAboveWater(x + quadSize, z + quadSize) + yoffset;
			const float h4 = smoothGround->GetHeightAboveWater(x,            z + quadSize) + yoffset;

			va->AddVertexQC(float3(x,            h1, z           ), color);
			va->AddVertexQC(float3(x + quadSize, h2, z           ), color);
			va->AddVertexQC(float3(x + quadSize, h3, z + quadSize), color);
			va->AddVertexQC(float3(x,            h4, z + quadSize), color);
		}
	}

	va->DrawArrayC(GL_QUADS);
	glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
}