diff options
author | Joshua Rowe <17525998+omnisci3nce@users.noreply.github.com> | 2024-06-09 14:59:01 +1000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-06-09 14:59:01 +1000 |
commit | 9c79df522980eabdc5e52592cbd152e2a285c4cc (patch) | |
tree | 9082af194033e5e3e4a770456209d3bac7784943 /src/renderer/backends/opengl/opengl_helpers.h | |
parent | 8d116bd23d9441e33cb3377e90c08169109b438a (diff) | |
parent | d4ff15d9cd82a6e3bc71da9d04ee0f250460cef1 (diff) |
Merge pull request #16 from omnisci3nce/port-opengl-ral
Bring back OpenGL (part 1)
Diffstat (limited to 'src/renderer/backends/opengl/opengl_helpers.h')
-rw-r--r-- | src/renderer/backends/opengl/opengl_helpers.h | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/src/renderer/backends/opengl/opengl_helpers.h b/src/renderer/backends/opengl/opengl_helpers.h new file mode 100644 index 0000000..a3c4014 --- /dev/null +++ b/src/renderer/backends/opengl/opengl_helpers.h @@ -0,0 +1,73 @@ +#if defined(CEL_REND_BACKEND_OPENGL) +#pragma once +#include "backend_opengl.h" +#include "log.h" +#include "ral.h" +#include "ral_types.h" + +#include <glad/glad.h> +#include <glfw3.h> +#include "ral_types.h" +typedef struct opengl_vertex_attr { + u32 count; + GLenum data_type; +} opengl_vertex_attr; + +static opengl_vertex_attr format_from_vertex_attr(vertex_attrib_type attr) { + switch (attr) { + case ATTR_F32: + return (opengl_vertex_attr){ .count = 1, .data_type = GL_FLOAT }; + case ATTR_U32: + return (opengl_vertex_attr){ .count = 1, .data_type = GL_UNSIGNED_INT }; + case ATTR_I32: + return (opengl_vertex_attr){ .count = 1, .data_type = GL_INT }; + case ATTR_F32x2: + return (opengl_vertex_attr){ .count = 2, .data_type = GL_FLOAT }; + case ATTR_U32x2: + // return VK_FORMAT_R32G32_UINT; + case ATTR_I32x2: + // return VK_FORMAT_R32G32_UINT; + case ATTR_F32x3: + return (opengl_vertex_attr){ .count = 3, .data_type = GL_FLOAT }; + case ATTR_U32x3: + // return VK_FORMAT_R32G32B32_UINT; + case ATTR_I32x3: + // return VK_FORMAT_R32G32B32_SINT; + case ATTR_F32x4: + return (opengl_vertex_attr){ .count = 4, .data_type = GL_FLOAT }; + case ATTR_U32x4: + // return VK_FORMAT_R32G32B32A32_UINT; + case ATTR_I32x4: + return (opengl_vertex_attr){ .count = 4, .data_type = GL_INT }; + } +} + +static u32 opengl_bindcreate_vao(gpu_buffer* buf, vertex_description desc) { + // 1. Bind the buffer + glBindBuffer(GL_ARRAY_BUFFER, buf->id.vbo); + // 2. Create new VAO + u32 vao; + glGenVertexArrays(1, &vao); + glBindVertexArray(vao); + + // Attributes + u32 attr_count = desc.attributes_count; + printf("N attributes %d\n", attr_count); + u64 offset = 0; + size_t vertex_size = desc.use_full_vertex_size ? sizeof(vertex) : desc.stride; + for (u32 i = 0; i < desc.attributes_count; i++) { + opengl_vertex_attr format = format_from_vertex_attr(desc.attributes[i]); + glVertexAttribPointer(i, format.count, format.data_type, GL_FALSE, vertex_size, (void*)offset); + TRACE(" %d %d %d %d %d %s", i, format.count, format.data_type, vertex_size, offset, + desc.attr_names[i]); + glEnableVertexAttribArray(i); // nth index + size_t this_offset = vertex_attrib_size(desc.attributes[i]); + printf("offset total %lld this attr %ld\n", offset, this_offset); + offset += this_offset; + } + glBindBuffer(GL_ARRAY_BUFFER, 0); + + return vao; +} + +#endif |