diff options
Diffstat (limited to 'src/renderer/backends')
-rw-r--r-- | src/renderer/backends/backend_opengl.c | 82 |
1 files changed, 82 insertions, 0 deletions
diff --git a/src/renderer/backends/backend_opengl.c b/src/renderer/backends/backend_opengl.c index 6022dbf..ea6cb00 100644 --- a/src/renderer/backends/backend_opengl.c +++ b/src/renderer/backends/backend_opengl.c @@ -2,6 +2,7 @@ #define CEL_PLATFORM_LINUX #include "defines.h" +#include "file.h" #include "log.h" #include "maths_types.h" #include "render_types.h" @@ -59,4 +60,85 @@ void clear_screen(vec3 colour) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); } +void bind_texture(shader s, texture *tex, u32 slot) { + // printf("bind texture slot %d with texture id %d \n", slot, tex->texture_id); + glActiveTexture(GL_TEXTURE0 + slot); + glBindTexture(GL_TEXTURE_2D, tex->texture_id); +} + +void bind_mesh_vertex_buffer(void *_backend, mesh *mesh) { glBindVertexArray(mesh->vao); } + +static inline GLenum to_gl_prim_topology(enum cel_primitive_topology primitive) { + switch (primitive) { + case CEL_PRIMITIVE_TOPOLOGY_TRIANGLE: + return GL_TRIANGLES; + case CEL_PRIMITIVE_TOPOLOGY_POINT: + case CEL_PRIMITIVE_TOPOLOGY_LINE: + case CEL_PRIMITIVE_TOPOLOGY_LINE_STRIP: + case CEL_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP: + case CEL_PRIMITIVE_TOPOLOGY_COUNT: + break; + } +} + +void draw_primitives(cel_primitive_topology primitive, u32 start_index, u32 count) { + u32 gl_primitive = to_gl_prim_topology(primitive); + glDrawArrays(gl_primitive, start_index, count); +} + +shader shader_create_separate(const char *vert_shader, const char *frag_shader) { + INFO("Load shaders at %s and %s", vert_shader, frag_shader); + int success; + char info_log[512]; + + u32 vertex = glCreateShader(GL_VERTEX_SHADER); + const char *vertex_shader_src = string_from_file(vert_shader); + if (vertex_shader_src == NULL) { + ERROR("EXIT: couldnt load shader"); + exit(-1); + } + glShaderSource(vertex, 1, &vertex_shader_src, NULL); + glCompileShader(vertex); + glGetShaderiv(vertex, GL_COMPILE_STATUS, &success); + if (!success) { + glGetShaderInfoLog(vertex, 512, NULL, info_log); + printf("%s\n", info_log); + ERROR("EXIT: vertex shader compilation failed"); + exit(-1); + } + + // fragment shader + u32 fragment = glCreateShader(GL_FRAGMENT_SHADER); + const char *fragment_shader_src = string_from_file(frag_shader); + if (fragment_shader_src == NULL) { + ERROR("EXIT: couldnt load shader"); + exit(-1); + } + glShaderSource(fragment, 1, &fragment_shader_src, NULL); + glCompileShader(fragment); + glGetShaderiv(fragment, GL_COMPILE_STATUS, &success); + if (!success) { + glGetShaderInfoLog(fragment, 512, NULL, info_log); + printf("%s\n", info_log); + ERROR("EXIT: fragment shader compilation failed"); + exit(-1); + } + + u32 shader_prog; + shader_prog = glCreateProgram(); + + glAttachShader(shader_prog, vertex); + glAttachShader(shader_prog, fragment); + glLinkProgram(shader_prog); + glDeleteShader(vertex); + glDeleteShader(fragment); + free((char *)vertex_shader_src); + free((char *)fragment_shader_src); + + shader s = { .program_id = shader_prog }; + return s; +} + +void set_shader(shader s) { glUseProgram(s.program_id); } + #endif
\ No newline at end of file |