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
|
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */
#ifndef _LINE_DRAWER_H
#define _LINE_DRAWER_H
#include <vector>
#include "Game/UI/CursorIcons.h"
#include "Rendering/GL/myGL.h"
class CLineDrawer {
public:
CLineDrawer();
~CLineDrawer() {};
void Configure(bool useColorRestarts, bool useRestartColor,
const float* restartColor, float restartAlpha);
void SetupLineStipple();
void UpdateLineStipple();
void StartPath(const float3& pos, const float* color);
void FinishPath() const;
void DrawLine(const float3& endPos, const float* color);
void DrawLineAndIcon(int cmdID, const float3& endPos, const float* color);
void DrawIconAtLastPos(int cmdID);
void Break(const float3& endPos, const float* color);
void Restart();
/// now same as restart
void RestartSameColor();
void RestartWithColor(const float* color);
const float3& GetLastPos() const { return lastPos; }
void DrawAll();
private:
bool lineStipple;
bool useColorRestarts;
bool useRestartColor;
float restartAlpha;
const float* restartColor;
float3 lastPos;
const float* lastColor;
float stippleTimer;
// queue all lines and draw them in one go later
struct LinePair {
GLenum type;
std::vector<GLfloat> verts;
std::vector<GLfloat> colors;
};
std::vector<LinePair> lines;
std::vector<LinePair> stippled;
};
extern CLineDrawer lineDrawer;
/******************************************************************************/
//
// Inlines
//
inline void CLineDrawer::Configure(bool ucr, bool urc,
const float* rc, float ra)
{
restartAlpha = ra;
restartColor = rc;
useRestartColor = urc;
useColorRestarts = ucr;
}
inline void CLineDrawer::FinishPath() const
{
// noop, left for compatibility
}
inline void CLineDrawer::Break(const float3& endPos, const float* color)
{
lastPos = endPos;
lastColor = color;
}
inline void CLineDrawer::Restart()
{
LinePair *ptr;
if (lineStipple) {
stippled.push_back(LinePair());
ptr = &stippled.back();
} else {
lines.push_back(LinePair());
ptr = &lines.back();
}
LinePair& p = *ptr;
if (!useColorRestarts) {
p.type = GL_LINE_STRIP;
p.colors.push_back(lastColor[0]);
p.colors.push_back(lastColor[1]);
p.colors.push_back(lastColor[2]);
p.colors.push_back(lastColor[3]);
p.verts.push_back(lastPos[0]);
p.verts.push_back(lastPos[1]);
p.verts.push_back(lastPos[2]);
} else {
p.type = GL_LINES;
}
}
inline void CLineDrawer::RestartWithColor(const float *color)
{
lastColor = color;
Restart();
}
inline void CLineDrawer::RestartSameColor()
{
// the only way for this to work would be using glGet AFAIK
// so it's left broken
Restart();
}
inline void CLineDrawer::StartPath(const float3& pos, const float* color)
{
lastPos = pos;
lastColor = color;
Restart();
}
inline void CLineDrawer::DrawLine(const float3& endPos, const float* color)
{
LinePair *ptr;
if (lineStipple) {
ptr = &stippled.back();
} else {
ptr = &lines.back();
}
LinePair& p = *ptr;
if (!useColorRestarts) {
p.colors.push_back(color[0]);
p.colors.push_back(color[1]);
p.colors.push_back(color[2]);
p.colors.push_back(color[3]);
p.verts.push_back(endPos.x);
p.verts.push_back(endPos.y);
p.verts.push_back(endPos.z);
} else {
if (useRestartColor) {
p.colors.push_back(restartColor[0]);
p.colors.push_back(restartColor[1]);
p.colors.push_back(restartColor[2]);
p.colors.push_back(restartColor[3]);
} else {
p.colors.push_back(color[0]);
p.colors.push_back(color[1]);
p.colors.push_back(color[2]);
p.colors.push_back(color[3] * restartAlpha);
}
p.verts.push_back(lastPos.x);
p.verts.push_back(lastPos.y);
p.verts.push_back(lastPos.z);
p.colors.push_back(color[0]);
p.colors.push_back(color[1]);
p.colors.push_back(color[2]);
p.colors.push_back(color[3]);
p.verts.push_back(endPos.x);
p.verts.push_back(endPos.y);
p.verts.push_back(endPos.z);
}
lastPos = endPos;
lastColor = color;
}
inline void CLineDrawer::DrawLineAndIcon(
int cmdID, const float3& endPos, const float* color)
{
cursorIcons.AddIcon(cmdID, endPos);
DrawLine(endPos, color);
}
inline void CLineDrawer::DrawIconAtLastPos(int cmdID)
{
cursorIcons.AddIcon(cmdID, lastPos);
}
#endif // _LINE_DRAWER_H
|