forked from Manan07Ag/LogicLabs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrid.cpp
80 lines (63 loc) · 2.11 KB
/
grid.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
#include <GLFW/glfw3.h>
#include <cstdlib>
int windowWidth = 500;
int windowHeight = 500;
struct Color {
float r, g, b;
};
struct Cell {
int x, y;
Color color;
void randomizeColor() {
color.r = rand() % 256 / 255.0f;
color.g = rand() % 256 / 255.0f;
color.b = rand() % 256 / 255.0f;
}
};
void framebuffer_size_callback(GLFWwindow* window, int width, int height) {
windowWidth = width;
windowHeight = height;
glViewport(0, 0, windowWidth, windowHeight);
}
void drawGrid() {
int cellSize = 50; // Desired size of each cell
int numCellsX = windowWidth / cellSize;
int numCellsY = windowHeight / cellSize;
float normalizedCellWidth = 2.0f / numCellsX; // Normalized width of each cell in OpenGL coords
float normalizedCellHeight = 2.0f / numCellsY; // Normalized height of each cell in OpenGL coords
for (int x = 0; x < numCellsX; ++x) {
for (int y = 0; y < numCellsY; ++y) {
Cell cell;
cell.x = x;
cell.y = y;
cell.randomizeColor();
glColor3f(cell.color.r, cell.color.g, cell.color.b);
glBegin(GL_QUADS);
glVertex2f(x * normalizedCellWidth - 1.0f, y * normalizedCellHeight - 1.0f);
glVertex2f((x + 1) * normalizedCellWidth - 1.0f, y * normalizedCellHeight - 1.0f);
glVertex2f((x + 1) * normalizedCellWidth - 1.0f, (y + 1) * normalizedCellHeight - 1.0f);
glVertex2f(x * normalizedCellWidth - 1.0f, (y + 1) * normalizedCellHeight - 1.0f);
glEnd();
}
}
}
int main() {
if (!glfwInit()) {
return -1;
}
GLFWwindow* window = glfwCreateWindow(windowWidth, windowHeight, "Colored Grid", NULL, NULL);
if (!window) {
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
while (!glfwWindowShouldClose(window)) {
glClear(GL_COLOR_BUFFER_BIT);
drawGrid();
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwTerminate();
return 0;
}