-
-
Save 60fov/4f61eeb83188ccd46751fa6c7afc110e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import private/gl | |
import util, maths, shapes#, shader | |
# const | |
# DefVertSrc = slurp("private/shader/default.vert") | |
# DefFragSrc = slurp("private/shader/default.frag") | |
const VertexSize = sizeof(Vertex) | |
const DefMaxVertices = 50_000 | |
type | |
BufferBit* {.pure.} = enum | |
Color = GL_COLOR_BUFFER_BIT.BufferBit | |
Depth = GL_DEPTH_BUFFER_BIT.BufferBit | |
Stencil = GL_STENCIL_BUFFER_BIT.BufferBit | |
# var default_shader: Shader | |
var batch: tuple[vao, vbo: GLuint, size: uint, index: uint] | |
var clear_color: Vec4f | |
var clear_mask: BufferBit = ColorBufferBit or DepthBufferBit | |
proc r_init*() | |
proc r_destroy*() | |
proc r_batch_max_vert_count*(count: uint) = batch.size = count * VertexSize.uint | |
proc r_init() = | |
if batch.size == 0: batch.size = DefMaxVertices * VertexSize | |
# default_shader = create_shader(DefVertSrc, DefFragSrc) | |
glGenVertexArrays(1, addr batch.vao) | |
glBindVertexArray(batch.vao) | |
glGenBuffers(1, addr batch.vbo) | |
glBindBuffer(GL_ARRAY_BUFFER, batch.vbo) | |
glBufferData(GL_ARRAY_BUFFER, GLsizeiptr(batch.size), nil, GL_DYNAMIC_DRAW) | |
glVertexAttribPointer(0, 3, cGL_FLOAT, false, GLsizei(VertexSize), cast[pointer](offsetof(Vertex, pos))) | |
glVertexAttribPointer(1, 3, cGL_FLOAT, false, GLsizei(VertexSize), cast[pointer](offsetof(Vertex, norm))) | |
glVertexAttribPointer(2, 2, cGL_FLOAT, false, GLsizei(VertexSize), cast[pointer](offsetof(Vertex, uv))) | |
glEnableVertexAttribArray(0) | |
glEnableVertexAttribArray(1) | |
glEnableVertexAttribArray(2) | |
glBindVertexArray(0) | |
glBindBuffer(GL_ARRAY_BUFFER, 0) | |
proc r_destroy() = | |
# delete_shader(default_shader) | |
glDeleteVertexArrays(1, addr batch.vao) | |
glDeleteBuffers(1, addr batch.vbo) | |
proc clear_bits_set*(mask: BufferBit) = clear_mask = mask | |
proc clear_color_set*(color: Vec4f) = | |
clear_color = color | |
glClearColor(clear_color.r, clear_color.g, clear_color.b, clear_color.a) | |
proc clear*() = | |
glClear(@clear_mask) | |
proc clear*(color: Vec4f) = | |
clear_color_set(color) | |
glClear(@clear_mask) | |
proc draw_circle*(pos: Vec3f, radius: float, color: Vec4f) | |
# proc draw_circle(pos: Vec2f, radius: float, color: Vec4f) | |
# proc draw_circle(x, y: float, radius: float, color: Vec4f) | |
proc draw_circle(pos: Vec3f, radius: float, color: Vec4f) = | |
let vertices = gen_circle(radius, 32) | |
glBindBuffer(GL_ARRAY_BUFFER, batch.vbo) | |
# glBufferSubData(GL_ARRAY_BUFFER, batch.index.int * VertexSize, len(vertices) * VertexSize, unsafeAddr(vertices[0])) | |
batch.index += len(vertices).uint | |
proc r_flush*() = | |
# use(default_shader) | |
glBindVertexArray(batch.vao) | |
glDrawArrays(GL_TRIANGLES, 0, GLsizei(batch.index)) | |
batch.index = 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment