2024-09-16 13:17:00 +00:00
|
|
|
#include "twn_rendering_c.h"
|
|
|
|
#include "twn_engine_context_c.h"
|
|
|
|
#include "twn_util.h"
|
2024-07-31 08:32:55 +00:00
|
|
|
|
2024-09-16 06:07:01 +00:00
|
|
|
#ifdef EMSCRIPTEN
|
|
|
|
#include <GLES2/gl2.h>
|
|
|
|
#else
|
2024-07-27 12:10:19 +00:00
|
|
|
#include <glad/glad.h>
|
2024-09-16 06:07:01 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
void setup_viewport(int x, int y, int width, int height) {
|
|
|
|
glViewport(x, y, width, height);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-09-23 17:43:16 +00:00
|
|
|
VertexBuffer create_vertex_buffer(void) {
|
2024-09-16 06:07:01 +00:00
|
|
|
GLuint result;
|
|
|
|
glGenBuffers(1, &result);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-09-23 17:43:16 +00:00
|
|
|
void delete_vertex_buffer(VertexBuffer buffer) {
|
2024-09-16 06:07:01 +00:00
|
|
|
glDeleteBuffers(1, &buffer);
|
|
|
|
}
|
|
|
|
|
2024-07-27 12:10:19 +00:00
|
|
|
|
2024-09-23 17:43:16 +00:00
|
|
|
void specify_vertex_buffer(VertexBuffer buffer, void *data, size_t bytes) {
|
2024-09-16 06:07:01 +00:00
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, buffer);
|
|
|
|
glBufferData(GL_ARRAY_BUFFER, bytes, data, GL_STREAM_DRAW);
|
|
|
|
}
|
|
|
|
|
2024-07-27 12:10:19 +00:00
|
|
|
|
2024-09-16 06:07:01 +00:00
|
|
|
void bind_quad_element_buffer(void) {
|
2024-07-27 12:10:19 +00:00
|
|
|
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,
|
2024-07-31 08:32:55 +00:00
|
|
|
QUAD_ELEMENT_BUFFER_LENGTH * 6 * sizeof(uint16_t),
|
2024-07-27 12:10:19 +00:00
|
|
|
NULL,
|
|
|
|
GL_STATIC_DRAW);
|
|
|
|
|
|
|
|
uint16_t *const indices = glMapBuffer(GL_ELEMENT_ARRAY_BUFFER,
|
|
|
|
GL_WRITE_ONLY);
|
2024-07-31 08:32:55 +00:00
|
|
|
if (!indices)
|
|
|
|
CRY("Quad indices generation", "glMapBuffer() failed");
|
2024-07-27 12:10:19 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2024-09-16 06:07:01 +00:00
|
|
|
|
|
|
|
void clear_draw_buffer(void) {
|
|
|
|
glClearColor((1.0f / 255) * 230,
|
|
|
|
(1.0f / 255) * 230,
|
|
|
|
(1.0f / 255) * 230, 1);
|
|
|
|
|
2024-09-26 18:02:56 +00:00
|
|
|
/* TODO: don't clear color when skybox is applied? */
|
2024-09-16 06:07:01 +00:00
|
|
|
glClear(GL_COLOR_BUFFER_BIT |
|
|
|
|
GL_DEPTH_BUFFER_BIT |
|
|
|
|
GL_STENCIL_BUFFER_BIT);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void swap_buffers(void) {
|
|
|
|
SDL_GL_SwapWindow(ctx.window);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void set_depth_range(double low, double high) {
|
|
|
|
glDepthRange(low, high);
|
|
|
|
}
|