This repository has been archived by the owner on Oct 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGLhelpers.h
180 lines (141 loc) · 4.22 KB
/
GLhelpers.h
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
#pragma once
#include "defines.h"
#include <array>
#include "GL/glut.h"
#include "functions.h"
extern int currentWindowWidth;
extern int currentWindowHeight;
static void drawStringOnWindow(float x, float y, float z, void* font, const std::vector<std::string>& lines, int corner = 2, int lineSpace = 10)
{
corner = corner % 4;
int lineHeight = lineSpace; // + glutBitmapHeight(font);
if (!lines.empty())
{
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
glOrtho(0.0f, currentWindowWidth, 0.0f, currentWindowHeight, 1.0f, -1.0f);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
// glDisable(GL_LIGHTING);
size_t linesCount = lines.size();
size_t lineNum = 1;
for (const std::string& line : lines)
{
// 1|0
// -+-
// 2|3
int xstr, ystr;
int lineWidth = 0;
for (char const& c : line)
lineWidth += glutBitmapWidth(font, c);
if (corner == 0 || corner == 3)
xstr = currentWindowWidth - x - lineWidth;
else
xstr = x;
if (corner == 0 || corner == 1)
ystr = currentWindowHeight - y - lineHeight * lineNum;
else
ystr = y + lineHeight * (linesCount - lineNum);
glColor4f(0.0f, 0.0f, 0.0f, 0.3f);
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
glBegin(GL_QUADS);
glVertex3f(xstr - 1, ystr - 1, z + FLT_EPSILON);
glVertex3f(xstr + lineWidth, ystr - 1, z + FLT_EPSILON);
glVertex3f(xstr + lineWidth, ystr + lineHeight - 1, z + FLT_EPSILON);
glVertex3f(xstr - 1, ystr + lineHeight - 1, z + FLT_EPSILON);
glEnd();
glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
glRasterPos3i(xstr, ystr, z);
for (char const& c : line)
glutBitmapCharacter(font, c);
++lineNum;
}
// glMatrixMode(GL_MODELVIEW);
glPopMatrix();
glMatrixMode(GL_PROJECTION);
glPopMatrix();
}
}
static void drawStringOnWindow(float x, float y, float z, void* font, const std::string& str, int corner = 2, int lineSpace = 10)
{
std::vector<std::string> lines;
std::string lnbr("\n");
std::size_t start = 0;
std::size_t end = 0;
do
{
end = str.find(lnbr, start);
lines.push_back(str.substr(start, end - start));
start = end + 1;
} while (end != std::string::npos);
drawStringOnWindow(x, y, z, font, lines, corner, lineSpace);
}
static inline void drawStringOnWindow(float x, float y, float z, void* font, const char* str, int corner = 2, int lineSpace = 10)
{
drawStringOnWindow(x, y, z, font, std::string(str), corner, lineSpace);
}
template <typename T = uint8_t, std::enable_if_t<std::is_arithmetic<T>::value, bool> = true>
class Color
{
public:
T R;
T G;
T B;
T A;
};
#if HEIGHT_ITPL_GAUSS
static constexpr std::array<float, BIOME_ITPL_RDS + 1> generate_biome_weights()
{
constexpr float sigmas = 2.5f;
constexpr float std = (BIOME_ITPL_RDS ? BIOME_ITPL_RDS : 1) / sigmas;
constexpr float DCoffset = gauss_pdf_dscrt(-2, BIOME_ITPL_RDS, std); // -1?
std::array<float, BIOME_ITPL_RDS + 1> multipliers{ };
size_t i = 0;
float sum = 0.0f;
for (size_t i = 0; i < BIOME_ITPL_RDS; ++i)
{
multipliers[i] = gauss_pdf_dscrt(i, BIOME_ITPL_RDS, std) - DCoffset;
sum += 2 * multipliers[i];
}
multipliers[BIOME_ITPL_RDS] = gauss_pdf_dscrt(BIOME_ITPL_RDS, BIOME_ITPL_RDS, std) - DCoffset;
sum += multipliers[BIOME_ITPL_RDS];
for (size_t i = 0; i <= BIOME_ITPL_RDS; ++i)
multipliers[i] /= sum;
return multipliers;
}
#endif
constexpr auto generate_vertices()
{
std::array<GLfloat, CHUNK_VERTNUM * 3> points{};
size_t i = 0;
for (BlkCrd y = 0; y <= CHUNK_HEIGHT; ++y)
{
for (BlkCrd x = 0; x < CHUNK_WIDTH; ++x)
{
points[i++] = static_cast<GLfloat>(x);
points[i++] = static_cast<GLfloat>(y);
}
points[i++] = nextafter(static_cast<GLfloat>(CHUNK_WIDTH));
points[i++] = static_cast<GLfloat>(y);
}
return points;
}
constexpr auto generate_faces()
{
typedef GLushort DtTp;
std::array<DtTp, CHUNK_BLOCKNUM * 4> points{}; // GLubyte, GLushort, GLuint
size_t i = 0;
for (BlkCrd y = 0; y < CHUNK_HEIGHT; ++y)
{
for (BlkCrd x = 0; x < CHUNK_WIDTH; ++x)
{
points[i++] = static_cast<DtTp>(y * (CHUNK_WIDTH + 1) + x);
points[i++] = static_cast<DtTp>(y * (CHUNK_WIDTH + 1) + x + 1);
points[i++] = static_cast<DtTp>((y + 1) * (CHUNK_WIDTH + 1) + x + 1);
points[i++] = static_cast<DtTp>((y + 1) * (CHUNK_WIDTH + 1) + x);
}
}
return points;
}