townengine/townengine/rendering/quad_element_buffer.h
2024-07-31 11:32:55 +03:00

46 lines
1.4 KiB
C

/* a rendering.c mixin */
#ifndef QUAD_ELEMENT_BUFFER_H
#define QUAD_ELEMENT_BUFFER_H
#include "townengine/util.h"
#include <glad/glad.h>
#include <stddef.h>
#define QUAD_ELEMENT_BUFFER_LENGTH (65536 / 6)
static void bind_quad_element_buffer(void) {
static GLuint buffer = 0;
/* it's only generated once at runtime */
if (buffer == 0) {
glGenBuffers(1, &buffer);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffer);
glBufferData(GL_ELEMENT_ARRAY_BUFFER,
QUAD_ELEMENT_BUFFER_LENGTH * 6 * sizeof(uint16_t),
NULL,
GL_STATIC_DRAW);
uint16_t *const indices = glMapBuffer(GL_ELEMENT_ARRAY_BUFFER,
GL_WRITE_ONLY);
if (!indices)
CRY("Quad indices generation", "glMapBuffer() failed");
for (uint16_t i = 0; i < QUAD_ELEMENT_BUFFER_LENGTH; ++i) {
indices[i * 6 + 0] = (uint16_t)(i * 4 + 0);
indices[i * 6 + 1] = (uint16_t)(i * 4 + 1);
indices[i * 6 + 2] = (uint16_t)(i * 4 + 2);
indices[i * 6 + 3] = (uint16_t)(i * 4 + 2);
indices[i * 6 + 4] = (uint16_t)(i * 4 + 3);
indices[i * 6 + 5] = (uint16_t)(i * 4 + 0);
}
glUnmapBuffer(GL_ELEMENT_ARRAY_BUFFER);
} else
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffer);
}
#endif