-
Notifications
You must be signed in to change notification settings - Fork 0
/
HexGrid.cpp
519 lines (435 loc) · 13.7 KB
/
HexGrid.cpp
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Name: HexGrid.cpp
// Purpose: Procedural Hexagonal Grid generation
// Author: Allen "z64555" Babb ( [email protected] )
// Modified by:
// Created: 30/11/2016 (DD/MM/YYYY)
// Page width: 120 char
// Tab width: 4 spaces
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include <cassert>
#include <iostream>
#include <vector>
#include <cmath>
#include <GL/glew.h>
#include <SDL.h>
#include <glm/glm.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <glm/gtc/matrix_transform.hpp>
using namespace std;
typedef union vec3d
{
struct
{
float x;
float y;
float z;
};
float a1d[3];
};
typedef union vec4d
{
struct
{
float x;
float y;
float z;
float w;
};
float a1d[4];
} vec4d;
struct face
{
vec3d* v[3];
};
class HexGrid
{
public:
/**
* @brief Generates a hexagonal grid
*
* @param[in] maj_orig Origin offset of the major axis
* @param[in] min_orig Origin offset of the minor axis
* @param[in] grid_size Maximum value for both axes
* @param[in] n Number of divisions along the major axis
* @param[in] centered If true, the grid will be centered around the origin. Else, the grid will have one corner
* in the origin.
* @details The major axis is the one where adjacent hexagons share an edge, and the minor axis is the one where adjacent hexagons share only a vertex.
*/
void generate(float maj_orig, float min_orig, float grid_size, int n, bool centered);
/**
* @brief Rounds the incoming coordinates to the nearest hexagon center
* @param[in/out] maj Major coordinate
* @param[in/out] min Minor coordinate
* @param[in] dir Direction to round. False is towards grid origin, True is away from grid origin
*/
void round(float &maj, float &min);
// The grid's origin
struct
{
float major;
float minor;
} origin;
// Hexagon center-to-center distances
struct
{
float major; // If the hexagon is regular, this unit's value = 2 * apothem
float minor; // If the hexagon is regular, this unit's value = (3 / 2) * radius
} offset;
vector<float> major_axis;
vector<float> minor_axis;
};
class HexMesh
{
public:
void generate();
/**
* Tesselates the mesh centered at (x, y) with radius r
*
* @param[in] x X coordinate of the target hexagon
* @param[in] y Y coordinate of the target hexagon
* @param[in] r Radius, in number of hexagons, around the target hexagon to tesselate. A value of 0 means only the target hexagon will be tesselated
*/
void tesselate(float x, float y, int r);
HexGrid Grid;
vector<vec3d> v_arr; // Vertex array
vector<face> f_arr; // Face array
size_t x_size; // Number of gridlines along the X axis
size_t y_size; // Number of gridlines along the Y axis
bool x_major; // True if the x axis is major, false if the y axis is the major line
};
// Rendering globals
GLuint program; // For shaders
GLint attribute_coord2d; //
GLint uniform_mvp; // Transformation matrix
const int screen_width = 640;
const int screen_height = 480;
HexMesh Mesh;
void HexGrid::generate(float maj_orig, float min_orig, float grid_size, int n, bool centered) {
const float sin60 = 0.8660254; // sin(60deg) = 0.8660254
float maj_off = (grid_size / (n * 2)); // Offsets along the major axis
float r = (grid_size / (n * 2)) / sin60; // Radius of the hexagon
float min_off = r / 2; // Offsets along the minor axis
int m;
origin.major = maj_orig;
origin.minor = min_orig;
offset.major = grid_size / n;
offset.minor = (3 / 2) * r;
// From here on, min_origin and maj_origin are re-defined as the lower-left corner of the grid
if (centered) {
maj_orig -= grid_size / 2;
min_orig -= (r * (2 + 3 * (int)(n / 2))) / 2;
}
// Major axis
m = ((n * 2) + 1);
for (int i = 0; i < m; ++i) {
// Number of major graduations per hexagon (Lines where vertices are)) = 3;
// Each additional hexagon adds 2 graduations, so the total is (n * 2) + 1
major_axis.push_back(maj_orig + (maj_off * i));
}
// Minor axis
// First calculate number of hexagons to plot
if (centered) {
// odd values of n give a nice super-hexagon, while
// even values of n give a parallelogram
m = n | 1; // +1 to even numbers to make them odd.
} else {
// Try to fit within square
if (n == 1) {
// Special case. Hexagon will spill outside the square along the minor edge
m = 1;
} else {
// A regular hexagon's diameter is (1 / sin60) * h;
// If its height is 1.0f, then its width is 1.155. Likewise, if we have a maximum of n cells on the major axis, then we have (1.155 * n) cells on the minor axis that will fit in a square
m = int(float(n) / sin60);
}
}
// Then, calculate number of graduations
m = (m * 3) + 2;
for (int i = 0; i < m; ++i) {
// Number of minor graduataions per hexagon = 4 + 1
// Each additional hexagon adds 2 + 1 graduations, so the total is (m * 3) + 2
// Doing a few tricks here. A regular hexagon can be composed of 6 equilateral triangles.
// If an eqilateral triangle has its base along an axis, such as the x axis, the apex is located exactly in between the two foot vertices. (x, y) = (b/2, h);
// Thus, a hexagon can be formed from (m * 3) + 1 graduations, plus one "phantom" graduation in the dead center of each hexagon. (m * 4) + 1;
if ((i + 1) % 3 == 0) {
// Skip every third multiple, the "phantom" graduation
continue;
}
minor_axis.push_back(min_orig + (min_off * i));
}
}
void HexGrid::round(float &maj, float &min) {
float major = maj - origin.major;
float minor = min - origin.minor;
bool is_odd = false;
minor /= offset.minor;
roundf(minor);
is_odd = int(minor) % 2;
min = minor * offset.minor;
min += origin.minor;
// Scale down
major /= offset.major;
// Shift away from center
if (is_odd) {
if (major > 0) {
major += 0.5f;
} else {
major -= 0.5f;
}
}
// Snap to nearest integer
roundf(major);
// Shift towards center
if (is_odd) {
if (major > 0) {
major -= 0.5f;
} else {
major += 0.5f;
}
}
// Scale up
maj = major * offset.major;
maj += origin.major;
}
void HexMesh::generate() {
int n = 9;
Grid.generate(0.0f, 0.0f, 2.0f, n, true);
//Grid.generate(-1.0f,-1.0f, 2.0f, n, false);
vec3d vert = { 0.0f, 0.0f, 0.0f };
size_t major_size = Grid.major_axis.size(); // Number of vertices along the Y (major) axis
size_t minor_size = Grid.minor_axis.size(); // Number of vertices along the X (minor) axis
y_size = major_size / 2;
x_size = minor_size / 2;
v_arr.reserve(major_size * minor_size); // TODO: Find a closer approximation. This one will reserve way more than what's needed
// Create vertex strips up the major axis
int j = 0;
bool zig = ((j % 2) == 1);;
for (; j < (minor_size - 1); j += 2) {
for (int i = 0; i < major_size; ++i) {
if (zig) {
vert.x = Grid.minor_axis[j]; // Minor axis
vert.y = Grid.major_axis[i]; // Major axis
} else {
vert.x = Grid.minor_axis[j + 1]; // Minor axis
vert.y = Grid.major_axis[i]; // Major axis
}
v_arr.push_back(vert);
zig = !zig;
}
// zig = !zig;
}
}
bool init_resources() {
GLint compile_ok = GL_FALSE, link_ok = GL_FALSE;
// Vertex shader
GLuint vs = glCreateShader(GL_VERTEX_SHADER);
const char *vs_source =
//"#version 100\n" // OpenGL ES 2.0
"#version 120\n" // OpenGL 2.1
"attribute vec2 coord2d; "
"uniform mat4 mvp; "
"void main(void) { "
" gl_Position = mvp * vec4(coord2d, 0.0, 1.0); "
"}";
glShaderSource(vs, 1, &vs_source, NULL);
glCompileShader(vs);
glGetShaderiv(vs, GL_COMPILE_STATUS, &compile_ok);
if (!compile_ok) {
cerr << "Error in vertex shader" << endl;
return false;
}
// Fragment shader
GLuint fs = glCreateShader(GL_FRAGMENT_SHADER);
const char *fs_source =
//"#version 100\n" // OpenGL ES 2.0
"#version 120\n" // OpenGL 2.1
"void main(void) { "
" gl_FragColor[0] = 0.1; "
" gl_FragColor[1] = 1.0; "
" gl_FragColor[2] = 0.1; "
"}";
glShaderSource(fs, 1, &fs_source, NULL);
glCompileShader(fs);
glGetShaderiv(fs, GL_COMPILE_STATUS, &compile_ok);
if (!compile_ok) {
cerr << "Error in fragment shader" << endl;
return false;
}
// Linking
program = glCreateProgram();
glAttachShader(program, vs);
glAttachShader(program, fs);
glLinkProgram(program);
glGetProgramiv(program, GL_LINK_STATUS, &link_ok);
if (!link_ok) {
cerr << "Error in glLinkProgram" << endl;
return false;
}
// Bind handles to GLSL code
const char* attribute_name = "coord2d";
attribute_coord2d = glGetAttribLocation(program, attribute_name);
if (attribute_coord2d == -1) {
cerr << "Could not bind attribute " << attribute_name << endl;
return false;
}
const char* uniform_name = "mvp";
uniform_mvp = glGetUniformLocation(program, uniform_name);
if (uniform_mvp == -1) {
cerr << "Could not bind uniform " << uniform_name << endl;
return false;
}
return true;
}
void free_resources() {
glDeleteProgram(program);
}
void logic() {
glm::mat4 ident(1.0f);
glm::mat4 model = glm::translate(glm::mat4(1.0f), glm::vec3(0.0, 0.0, -2.0));
glm::vec3 eye(0.0, 0.0, 0.0);
glm::vec3 center(0.0, 0.0, -2.0);
glm::vec3 uvec(0.0, 1.0, 0.0);
glm::mat4 view = glm::lookAt(eye, center, uvec);
glm::mat4 projection = glm::perspective(45.0f, (1.0f * (float(screen_width) / float(screen_height))), 0.1f, 10.0f); // (1.0f * (screen_width / screen_height))
glm::mat4 mvp;
mvp = projection * view * model;
glUniformMatrix4fv(uniform_mvp, 1, GL_FALSE, glm::value_ptr(mvp));
};
void render(SDL_Window* window) {
// Background as black
glClearColor(0.0, 0.0, 0.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
glUseProgram(program);
glEnableVertexAttribArray(attribute_coord2d);
// Draw Vertices and major lines
glVertexAttribPointer(
attribute_coord2d, // attribute
3, // number of elements per vertex, here (x,y)
GL_FLOAT, // the type of each element
GL_FALSE, // take our values as-is
0, // no extra data between each position
&Mesh.v_arr[0] // pointer to the C array
);
size_t major_size = Mesh.Grid.major_axis.size();
size_t minor_size = Mesh.Grid.minor_axis.size();
size_t count = minor_size / 2;
glDrawArrays(GL_POINTS, 0, Mesh.v_arr.size());
for (int i = 0; i < (count - 1); ++i) {
glDrawArrays(GL_LINE_STRIP, (major_size * i), major_size);
}
if (count % 2) {
// Odd number of major lines, so skip the first and last segments
glDrawArrays(GL_LINE_STRIP, (major_size * (count - 1)) + 1, major_size - 2);
} else {
// Even number of major lines, draw the last line normally
glDrawArrays(GL_LINE_STRIP, (major_size * (count - 1)), major_size);
}
// Draw minor lines
for (int j = 0; j < major_size; j += 2) {
glVertexAttribPointer(
attribute_coord2d, // attribute
3, // number of elements per vertex, here (x,y)
GL_FLOAT, // the type of each element
GL_FALSE, // take our values as-is
sizeof(vec3d) * major_size, // Stride between verts is is major_size
&Mesh.v_arr[j] // pointer to the C array
);
glDrawArrays(GL_LINES, 0, count);
}
count += count % 2;
for (int j = 1; j < major_size; j += 2) {
glVertexAttribPointer(
attribute_coord2d, // attribute
3, // number of elements per vertex, here (x,y)
GL_FLOAT, // the type of each element
GL_FALSE, // take our values as-is
sizeof(vec3d) * major_size, // Stride between verts is is major_size
&Mesh.v_arr[j] // pointer to the C array
);
glDrawArrays(GL_LINES, 1, count - 2);
}
// Draw reference square
GLfloat square[] = {
-1.0f, -1.0f,
-1.0f, 1.0f,
1.0f, 1.0f,
1.0f, -1.0f
};
glVertexAttribPointer(
attribute_coord2d, // attribute
2, // number of elements per vertex, here (x,y)
GL_FLOAT, // the type of each element
GL_FALSE, // take our values as-is
0, // No data between verts
square // pointer to the C array
);
glDrawArrays(GL_LINE_LOOP, 0, 4);
// Draw reference cross
GLfloat cross[] = {
-1.0f, -1.0f,
1.0f, 1.0f,
-1.0f, 1.0f,
1.0f, -1.0f
};
glVertexAttribPointer(
attribute_coord2d, // attribute
2, // number of elements per vertex, here (x,y)
GL_FLOAT, // the type of each element
GL_FALSE, // take our values as-is
0, // No data between verts
cross // pointer to the C array
);
glDrawArrays(GL_LINES, 0, 4);
glDisableVertexAttribArray(attribute_coord2d);
/* Display the result */
SDL_GL_SwapWindow(window);
}
void mainLoop(SDL_Window* window) {
while (true) {
SDL_Event ev;
while (SDL_PollEvent(&ev)) {
if (ev.type == SDL_QUIT)
return;
}
logic();
render(window);
}
}
int main(int argc, char argv[]) {
SDL_Init(SDL_INIT_VIDEO);
SDL_Window* window = SDL_CreateWindow("HexGrid",
SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
screen_width, screen_height,
SDL_WINDOW_RESIZABLE | SDL_WINDOW_OPENGL);
SDL_GL_CreateContext(window);
GLenum glew_status = glewInit();
if (glew_status != GLEW_OK) {
cerr << "Error: glewInit:" << glewGetErrorString(glew_status) << endl;
return EXIT_FAILURE;
}
// Init GL stuff
if (!init_resources()) {
return EXIT_FAILURE;
}
// Init stuff to test:
Mesh.generate();
// Run and cleanup, etc.
mainLoop(window);
free_resources();
return EXIT_SUCCESS;
}
/*
// Render minor lines
for (int i = 0; i < Mesh.y_size; ++i) {
for (int j = 0; j < Mesh.x_size; ++j) {
Mesh.v_arr[i + (Mesh.x_size * j)]; // Render me!
}
}
// Render major lines
for (int j = 0; j < Mesh.x_size; ++j) {
for (int i = 0; i < Mesh.y_size; ++i) {
Mesh.v_arr[i + (Mesh.y_size * j)]; // Render me!
}
}
*/