File: LineDrawer.cpp

package info (click to toggle)
spring 106.0%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 55,316 kB
  • sloc: cpp: 543,954; ansic: 44,800; python: 12,575; java: 12,201; awk: 5,889; sh: 1,796; asm: 1,546; xml: 655; perl: 405; php: 211; objc: 194; makefile: 76; sed: 2
file content (115 lines) | stat: -rw-r--r-- 3,248 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
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */

#include "LineDrawer.h"

#include <cmath>

#include "Game/Camera.h"
#include "Game/UI/CommandColors.h"
#include "Game/UI/MiniMap.h"
#include "Rendering/GL/myGL.h"
#include "Rendering/GlobalRendering.h"
#include "Rendering/GL/RenderDataBuffer.hpp"
#include "Rendering/GL/WideLineAdapter.hpp"
#include "System/ContainerUtil.h"

CLineDrawer lineDrawer;


void CLineDrawer::UpdateLineStipple()
{
	stippleTimer += (globalRendering->lastFrameTime * 0.001f * cmdColors.StippleSpeed());
	stippleTimer = std::fmod(stippleTimer, (16.0f / 20.0f));
}


void CLineDrawer::SetupLineStipple()
{
	#if 0
	const unsigned int stipPat = (0xffff & cmdColors.StipplePattern());

	if (!(lineStipple = ((stipPat != 0x0000) && (stipPat != 0xffff))))
		return;

	const unsigned int fullPat = (stipPat << 16) | (stipPat & 0x0000ffff);
	const int shiftBits = 15 - (int(stippleTimer * 20.0f) % 16);

	glLineStipple(cmdColors.StippleFactor(), (fullPat >> shiftBits));
	#endif
}


void CLineDrawer::Restart()
{
	const int idx = width * 2 + useColorRestarts;
	Line& line = spring::VectorEmplaceBack(lineStipple? stippleLines[idx]: regularLines[idx]);

	if (useColorRestarts)
		return;

	line.push_back({lastPos, lastColor});
}


void CLineDrawer::DrawAll(bool onMiniMap)
{
	if (!HaveRegularLines() && !HaveStippleLines())
		return;

	glAttribStatePtr->PushEnableBit();
	glAttribStatePtr->DisableDepthTest();


	GL::RenderDataBufferC* buffer = GL::GetRenderBufferC();
	Shader::IProgramObject* shader = buffer->GetShader();
	GL::WideLineAdapterC* wla = GL::GetWideLineAdapterC();

	const CMatrix44f& projMat = onMiniMap? minimap->GetProjMat(0): camera->GetProjectionMatrix();
	const CMatrix44f& viewMat = onMiniMap? minimap->GetViewMat(0): camera->GetViewMatrix();
	const int xScale = onMiniMap? minimap->GetSizeX(): globalRendering->viewSizeX;
	const int yScale = onMiniMap? minimap->GetSizeY(): globalRendering->viewSizeY;
	wla->Setup(buffer, xScale, yScale, onMiniMap? 2.5f : 1.0f, projMat * viewMat, onMiniMap);

	const auto DrawLines = [&](const std::vector<Line>& lines, unsigned int glType) {
		for (const auto& line: lines) {
			if (line.empty())
				continue;

			wla->SafeAppend(line.data(), line.size());
		}

		wla->Submit(glType);
	};

	shader->Enable();
	shader->SetUniformMatrix4x4<float>("u_proj_mat", false, projMat);
	shader->SetUniformMatrix4x4<float>("u_movi_mat", false, viewMat);

	DrawLines(regularLines[0], GL_LINE_LOOP);
	DrawLines(regularLines[1], GL_LINES    );
	// [deprecated]
	// glAttribStatePtr->Enable(GL_LINE_STIPPLE);
	DrawLines(stippleLines[0], GL_LINE_LOOP);
	DrawLines(stippleLines[1], GL_LINES    );
	// glAttribStatePtr->Disable(GL_LINE_STIPPLE);

	if (!onMiniMap)
		wla->SetWidth(cmdColors.QueuedLineWidth());

	DrawLines(regularLines[2], GL_LINE_LOOP);
	DrawLines(regularLines[3], GL_LINES    );
	// [deprecated]
	// glAttribStatePtr->Enable(GL_LINE_STIPPLE);
	DrawLines(stippleLines[2], GL_LINE_LOOP);
	DrawLines(stippleLines[3], GL_LINES    );
	// glAttribStatePtr->Disable(GL_LINE_STIPPLE);

	shader->Disable();
	glAttribStatePtr->PopBits();

	for (size_t i = 0; i < regularLines.size(); i++) {
		regularLines[i].clear();
		stippleLines[i].clear();
	}
}