I'm trying to render a 2d grid using opengl. My code is as follows
Shaders
const char *vertex_shader_source = "#version 330 core\n"
"layout (location = 0) in vec3 aPos;\n"
"uniform mat4 projection;\n"
"void main()\n"
"{\n"
" gl_Position = projection * vec4(aPos.x, aPos.y, aPos.z, 1.0);\n"
"}\0";
const char *fragment_shader_source = "#version 330 core\n"
"out vec4 FragColor;\n"
"uniform vec3 color;\n"
"void main()\n"
"{\n"
" FragColor = vec4(color, 1.0f);\n"
"}\n\0";
Drawing:
void
square_draw(vec2 tl, vec2 br, vec3 color) {
glUseProgram(shader_program);
mat4 projection;
glm_ortho(0.0f, WIDTH, HEIGHT, 0.0f, -1.0f, 1.0f, projection);
glUniformMatrix4fv(glGetUniformLocation(shader_program, "projection"), 1, GL_FALSE, (const float *)&projection[0][0]);
glUniform3f(glGetUniformLocation(shader_program, "color"), color[0], color[1], color[2]);
const float vertices[] = {
br[0], tl[1], 0.0, // top right
br[0], br[1], 0.0, // bottom right
tl[0], br[1], 0.0, // bottom left
tl[0], tl[1], 0.0 // top left
};
const float indices[] = {
0, 1, 3,
1, 2, 3
};
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
}
Main:
int main() {
init();
while (!glfwWindowShouldClose(window)) {
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
for (int y = 0; y < MAP_Y; y++) {
for (int x = 0; x < MAP_X; x++) {
vec3 color = {0, 0, 0};
if (map[y * MAP_X + x] == 1) {
glm_vec3_copy((vec3){1, 1, 1}, color);
}
const int xo = x * MAP_S;
const int yo = y * MAP_S;
square_draw(
(vec2){ xo + 1, yo + 1 },
(vec2){ MAP_S + xo - 1, MAP_S + yo -1 },
color);
}
}
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwTerminate();
return 0;
}
However nomatter what i try it always renders like this:

My guess is an error within the square_draw function but the vertices contain the correct points but they dont seem to be setting correctly?
I'm not sure what exactly i am doing wrong