-
Notifications
You must be signed in to change notification settings - Fork 0
/
grid.h
92 lines (69 loc) · 2.81 KB
/
grid.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
#ifndef _GRID_H_
#define _GRID_H_
#include "typedefs.h"
#include "util.h"
#include <glm/glm.hpp>
ObjectData configure_grid(glm::ivec2 window_size) {
ObjectData grid;
float cell_width = (float)window_size.x / CELL_COUNT;
float cell_height = (float)window_size.y / CELL_COUNT;
/*
const i32 grid_vertices_count = (CELL_COUNT - 1) * 4 + 8;
Vertex grid_vertices[grid_vertices_count];
for (size_t i = 0; i < CELL_COUNT - 1; i++) {
float x = cell_width * (i + 1) / (float)window_size.x * 2 - 1;
float y = cell_height * (i + 1) / (float)window_size.y * 2 - 1;
grid_vertices[i * 4 + 0] = { { x, 1 } };
grid_vertices[i * 4 + 1] = { { x, -1 } };
grid_vertices[i * 4 + 2] = { { -1, y } };
grid_vertices[i * 4 + 3] = { { 1, y } };
}
*/
const i32 grid_vertices_count = 8;
Vertex grid_vertices[grid_vertices_count];
float limit = 0.999f;
glm::vec2 top_left = { -limit, limit };
glm::vec2 top_right = { limit, limit };
glm::vec2 bottom_left = { -limit, -limit };
glm::vec2 bottom_right = { limit, -limit };
{
/*
i32 offset = 1;
#define SET_BORDER_VERTICES(from, to) \
grid_vertices[grid_vertices_count - offset++] = {(from)}; \
grid_vertices[grid_vertices_count - offset++] = {(to)}
*/
i32 offset = 0;
#define SET_BORDER_VERTICES(from, to) \
grid_vertices[offset++] = {(from)}; \
grid_vertices[offset++] = {(to)}
SET_BORDER_VERTICES(top_left, top_right);
SET_BORDER_VERTICES(top_right, bottom_right);
SET_BORDER_VERTICES(bottom_right, bottom_left);
SET_BORDER_VERTICES(bottom_left, top_left);
}
grid.vertex_count = grid_vertices_count;
grid.primitive = GL_LINES;
glGenBuffers(1, &grid.vbo);
glGenVertexArrays(1, &grid.vao);
glBindVertexArray(grid.vao);
glBindBuffer(GL_ARRAY_BUFFER, grid.vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(grid_vertices), grid_vertices, GL_STATIC_DRAW);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, pos));
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
u32 grid_shader = glCreateProgram();
u32 grid_vertex_shader = glCreateShader(GL_VERTEX_SHADER);
u32 grid_fragment_shader = glCreateShader(GL_FRAGMENT_SHADER);
compile_shader_file(grid_vertex_shader, "./shaders/grid.vert");
compile_shader_file(grid_fragment_shader, "./shaders/grid.frag");
glAttachShader(grid_shader, grid_vertex_shader);
glAttachShader(grid_shader, grid_fragment_shader);
glLinkProgram(grid_shader);
grid.shader = grid_shader;
glDeleteShader(grid_vertex_shader);
glDeleteShader(grid_fragment_shader);
return grid;
}
#endif