File: InMapDrawView.cpp

package info (click to toggle)
spring 105.0.1%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 108,860 kB
  • sloc: cpp: 467,785; ansic: 302,607; python: 12,925; java: 12,201; awk: 5,889; sh: 2,371; xml: 655; perl: 405; php: 276; objc: 194; makefile: 75; sed: 2
file content (259 lines) | stat: -rw-r--r-- 8,444 bytes parent folder | download | duplicates (3)
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */


#include "InMapDrawView.h"
#include "Rendering/Colors.h"
#include "Rendering/GlobalRendering.h"
#include "Rendering/Fonts/glFont.h"
#include "Rendering/GL/RenderDataBuffer.hpp"
#include "Rendering/GL/WideLineAdapter.hpp"

#include "Game/Camera.h"
#include "Game/InMapDrawModel.h"
#include "Map/ReadMap.h"
#include "Sim/Misc/TeamHandler.h"

CInMapDrawView* inMapDrawerView = nullptr;


CInMapDrawView::CInMapDrawView()
{
	unsigned char tex[64][128][4];

	std::memset(tex, 0, sizeof(tex));

	for (int y = 0; y < 64; y++) {
		// circular thingy
		for (int x = 0; x < 64; x++) {
			const float dist = std::sqrt((float)(x - 32) * (x - 32) + (y - 32) * (y - 32));

			if (dist > 31.0f) {
				// do nothing - leave transparent
			} else if (dist > 30.0f) {
				// interpolate (outline -> nothing)
				tex[y][x][3] = mix(255,   0, dist - 30.0f);
			} else if (dist > 24.0f) {
				// black outline
				tex[y][x][3] = 255;
			} else if (dist > 23.0f) {
				// interpolate (inner -> outline)
				tex[y][x][0] = mix(255,   0, dist - 23.0f);
				tex[y][x][1] = mix(255,   0, dist - 23.0f);
				tex[y][x][2] = mix(255,   0, dist - 23.0f);
				tex[y][x][3] = mix(200, 255, dist - 23.0f);
			} else {
				tex[y][x][0] = 255;
				tex[y][x][1] = 255;
				tex[y][x][2] = 255;
				tex[y][x][3] = 200;
			}
		}
	}
	for (int y = 0; y < 64; y++) {
		// linear falloff
		for (int x = 64; x < 128; x++) {
			const float dist = std::abs(y - 32);

			if (dist > 31.0f) {
				// do nothing - leave transparent
			} else if (dist > 30.0f) {
				// interpolate (outline -> nothing)
				tex[y][x][3] = mix(255,   0, dist - 30.0f);
			} else if (dist > 24.0f) {
				// black outline
				tex[y][x][3] = 255;
			} else if (dist > 23.0f) {
				// interpolate (inner -> outline)
				tex[y][x][0] = mix(255,   0, dist - 23.0f);
				tex[y][x][1] = mix(255,   0, dist - 23.0f);
				tex[y][x][2] = mix(255,   0, dist - 23.0f);
				tex[y][x][3] = mix(200, 255, dist - 23.0f);
			} else {
				tex[y][x][0] = 255;
				tex[y][x][1] = 255;
				tex[y][x][2] = 255;
				tex[y][x][3] = 200;
			}
		}
	}

	glGenTextures(1, &texture);
	glBindTexture(GL_TEXTURE_2D, texture);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
	glBuildMipmaps(GL_TEXTURE_2D, GL_RGBA8, 128, 64, GL_RGBA, GL_UNSIGNED_BYTE, tex[0]);
}


CInMapDrawView::~CInMapDrawView()
{
	glDeleteTextures(1, &texture);
}


struct InMapDraw_QuadDrawer: public CReadMap::IQuadDrawer
{
	std::vector<const CInMapDrawModel::MapPoint*>* visibleLabels = nullptr;

	GL::RenderDataBufferTC* pointBuffer = nullptr;
	GL::WideLineAdapterC* linesAdapter = nullptr;

	void ResetState() override {
		pointBuffer = nullptr;
		linesAdapter = nullptr;
	}
	void DrawQuad(int x, int y) override ;

private:
	void DrawPoint(const CInMapDrawModel::MapPoint* point, GL::RenderDataBufferTC* buffer) const;
	void DrawLine(const CInMapDrawModel::MapLine* line, GL::WideLineAdapterC* wla) const;
};



void InMapDraw_QuadDrawer::DrawPoint(const CInMapDrawModel::MapPoint* point, GL::RenderDataBufferTC* buffer) const
{
	const float3& pos = point->GetPos();
	const float3 zdir = (pos - camera->GetPos()).ANormalize();
	const float3 xdir = (zdir.cross(UpVector)).ANormalize();
	const float3 ydir = (zdir.cross(xdir));


	const unsigned char* pcolor = point->IsBySpectator() ? color4::white : teamHandler.Team(point->GetTeamID())->color;
	const unsigned char color[4] = {pcolor[0], pcolor[1], pcolor[2], 200};

	constexpr float size = 6.0f;
	const float3 pos1(pos.x,  pos.y  +   5.0f, pos.z);
	const float3 pos2(pos1.x, pos1.y + 100.0f, pos1.z);

	{
		buffer->SafeAppend({pos1 - xdir * size,               0.25f, 0.0f, color}); // tl
		buffer->SafeAppend({pos1 + xdir * size,               0.25f, 1.0f, color}); // tr
		buffer->SafeAppend({pos1 + xdir * size + ydir * size, 0.00f, 1.0f, color}); // br

		buffer->SafeAppend({pos1 + xdir * size + ydir * size, 0.00f, 1.0f, color}); // br
		buffer->SafeAppend({pos1 - xdir * size + ydir * size, 0.00f, 0.0f, color}); // bl
		buffer->SafeAppend({pos1 - xdir * size,               0.25f, 0.0f, color}); // tl
	}
	{
		buffer->SafeAppend({pos1 - xdir * size,               0.75f, 0.0f, color});
		buffer->SafeAppend({pos1 + xdir * size,               0.75f, 1.0f, color});
		buffer->SafeAppend({pos2 + xdir * size,               0.75f, 1.0f, color});

		buffer->SafeAppend({pos2 + xdir * size,               0.75f, 1.0f, color});
		buffer->SafeAppend({pos2 - xdir * size,               0.75f, 0.0f, color});
		buffer->SafeAppend({pos1 - xdir * size,               0.75f, 0.0f, color});
	}
	{
		buffer->SafeAppend({pos2 - xdir * size,               0.25f, 0.0f, color});
		buffer->SafeAppend({pos2 + xdir * size,               0.25f, 1.0f, color});
		buffer->SafeAppend({pos2 + xdir * size - ydir * size, 0.00f, 1.0f, color});

		buffer->SafeAppend({pos2 + xdir * size - ydir * size, 0.00f, 1.0f, color});
		buffer->SafeAppend({pos2 - xdir * size - ydir * size, 0.00f, 0.0f, color});
		buffer->SafeAppend({pos2 - xdir * size,               0.25f, 0.0f, color});
	}

	if (point->GetLabel().empty())
		return;

	visibleLabels->push_back(point);
}

void InMapDraw_QuadDrawer::DrawLine(const CInMapDrawModel::MapLine* line, GL::WideLineAdapterC* wla) const
{
	const unsigned char* color = line->IsBySpectator() ? color4::white : teamHandler.Team(line->GetTeamID())->color;
	wla->SafeAppend({line->GetPos1() - (line->GetPos1() - camera->GetPos()).ANormalize() * 26, color});
	wla->SafeAppend({line->GetPos2() - (line->GetPos2() - camera->GetPos()).ANormalize() * 26, color});
}



void InMapDraw_QuadDrawer::DrawQuad(int x, int y)
{
	const CInMapDrawModel::DrawQuad* dq = inMapDrawerModel->GetDrawQuad(x, y);

	//! draw point markers
	for (const CInMapDrawModel::MapPoint& pi: dq->points) {
		if (pi.IsVisibleToPlayer(inMapDrawerModel->GetAllMarksVisible())) {
			DrawPoint(&pi, pointBuffer);
		}
	}

	//! draw line markers
	for (const CInMapDrawModel::MapLine& li: dq->lines) {
		if (li.IsVisibleToPlayer(inMapDrawerModel->GetAllMarksVisible())) {
			DrawLine(&li, linesAdapter);
		}
	}
}



void CInMapDrawView::Draw()
{
	GL::RenderDataBufferTC* pointBuffer = GL::GetRenderBufferTC();
	GL::RenderDataBufferC* linesBuffer = GL::GetRenderBufferC();
	GL::WideLineAdapterC* wla = GL::GetWideLineAdapterC();

	Shader::IProgramObject* pointShader = pointBuffer->GetShader();
	Shader::IProgramObject* linesShader = linesBuffer->GetShader();
	wla->Setup(linesBuffer, globalRendering->viewSizeX, globalRendering->viewSizeY, 3.0f, camera->GetViewProjectionMatrix());

	InMapDraw_QuadDrawer drawer;
	drawer.visibleLabels = &visibleLabels;
	drawer.pointBuffer = pointBuffer;
	drawer.linesAdapter = wla;

	readMap->GridVisibility(nullptr, &drawer, 1e9, CInMapDrawModel::DRAW_QUAD_SIZE);


	glAttribStatePtr->DisableDepthMask();
	glAttribStatePtr->BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
	glAttribStatePtr->EnableBlendMask();


	{
		// draw lines
		linesShader->Enable();
		linesShader->SetUniformMatrix4x4<float>("u_movi_mat", false, camera->GetViewMatrix());
		linesShader->SetUniformMatrix4x4<float>("u_proj_mat", false, camera->GetProjectionMatrix());
		wla->Submit(GL_LINES);
		linesShader->Disable();
	}
	{
		// draw points
		glBindTexture(GL_TEXTURE_2D, texture);
		pointShader->Enable();
		pointShader->SetUniformMatrix4x4<float>("u_movi_mat", false, camera->GetViewMatrix());
		pointShader->SetUniformMatrix4x4<float>("u_proj_mat", false, camera->GetProjectionMatrix());
		pointBuffer->Submit(GL_TRIANGLES);
		pointShader->Disable();
		glBindTexture(GL_TEXTURE_2D, 0);
	}


	if (!visibleLabels.empty()) {
		font->SetColors(); // default
		font->glWorldBegin();

		// draw point labels
		for (const CInMapDrawModel::MapPoint* point: visibleLabels) {
			const float3 pos = point->GetPos() + UpVector * 111.0f;

			const CTeam* team = teamHandler.Team(point->GetTeamID());
			const unsigned char* color = point->IsBySpectator() ? color4::white : team->color;

			font->SetTextColor(color[0] / 255.0f, color[1] / 255.0f, color[2] / 255.0f, 1.0f);
			font->glWorldPrint(pos, 26.0f, point->GetLabel());
		}

		font->glWorldEnd();
		visibleLabels.clear();
	}

	glAttribStatePtr->EnableDepthMask();
}