Hash :
f23d563b
Author :
Thomas de Grivel
Date :
2024-03-24T14:53:08
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
/* c3
* Copyright 2022-2024 kmx.io <contact@kmx.io>
*
* Permission is hereby granted to use this software granted the above
* copyright notice and this permission paragraph are included in all
* copies and substantial portions of this software.
*
* THIS SOFTWARE IS PROVIDED "AS-IS" WITHOUT ANY GUARANTEE OF
* PURPOSE AND PERFORMANCE. IN NO EVENT WHATSOEVER SHALL THE
* AUTHOR BE CONSIDERED LIABLE FOR THE USE AND PERFORMANCE OF
* THIS SOFTWARE.
*/
#include <libc3/c3.h>
#include "gl_lines.h"
#include "gl_vertex.h"
s_gl_lines * gl_lines_allocate (s_gl_lines *lines, uw lines_count)
{
uw vertex_count;
assert(lines);
assert(lines_count);
vertex_count = lines_count * 2;
if (! array_init(&lines->vertex, sym_1("GL.Vertex[]"), 1,
&vertex_count) ||
! array_allocate(&lines->vertex))
return NULL;
return lines;
}
void gl_lines_clean (s_gl_lines *lines)
{
assert(lines);
array_clean(&lines->vertex);
glDeleteVertexArrays(1, &lines->gl_vao);
glDeleteBuffers(1, &lines->gl_vbo);
}
s_gl_lines * gl_lines_init (s_gl_lines *lines)
{
s_gl_lines tmp = {0};
assert(glGetError() == GL_NO_ERROR);
glGenVertexArrays(1, &tmp.gl_vao);
glGenBuffers(1, &tmp.gl_vbo);
assert(glGetError() == GL_NO_ERROR);
*lines = tmp;
return lines;
}
void gl_lines_render (const s_gl_lines *lines, uw lines_count)
{
assert(lines);
if (lines_count > lines->vertex.count / 2)
lines_count = lines->vertex.count / 2;
assert(glGetError() == GL_NO_ERROR);
glBindVertexArray(lines->gl_vao);
assert(glGetError() == GL_NO_ERROR);
glBindVertexArray(lines->gl_vao);
assert(glGetError() == GL_NO_ERROR);
glDrawArrays(GL_LINES, 0, lines_count * 2);
assert(glGetError() == GL_NO_ERROR);
}
bool gl_lines_update (s_gl_lines *lines, uw lines_count)
{
//GLenum gl_error;
assert(lines);
assert(lines->gl_vao);
assert(lines->gl_vbo);
assert(lines->vertex.data);
assert(glGetError() == GL_NO_ERROR);
if (lines_count > lines->vertex.count / 2)
lines_count = lines->vertex.count / 2;
glBindVertexArray(lines->gl_vao);
assert(glGetError() == GL_NO_ERROR);
glBindBuffer(GL_ARRAY_BUFFER, lines->gl_vbo);
assert(glGetError() == GL_NO_ERROR);
glBufferData(GL_ARRAY_BUFFER, lines_count * 2 * sizeof(s_gl_vertex),
lines->vertex.data, GL_DYNAMIC_DRAW);
assert(glGetError() == GL_NO_ERROR);
gl_vertex_attrib();
assert(glGetError() == GL_NO_ERROR);
return true;
}