2020-01-23 23:04:46 +03:00
|
|
|
// Copyright (c) 2019-2020 Alexander Medvednikov. All rights reserved.
|
2019-06-23 05:21:30 +03:00
|
|
|
// Use of this source code is governed by an MIT license
|
|
|
|
// that can be found in the LICENSE file.
|
|
|
|
|
2019-06-22 22:24:49 +03:00
|
|
|
module gl
|
|
|
|
|
2020-05-10 17:49:29 +03:00
|
|
|
import glm
|
|
|
|
|
2019-07-10 14:27:35 +03:00
|
|
|
#flag -I @VROOT/thirdparty/glad
|
2019-06-22 22:27:59 +03:00
|
|
|
#include "glad.h"
|
2019-08-23 00:00:31 +03:00
|
|
|
#flag @VROOT/thirdparty/glad/glad.o
|
2019-06-23 14:17:33 +03:00
|
|
|
|
2020-05-10 17:49:29 +03:00
|
|
|
|
2019-09-23 13:42:20 +03:00
|
|
|
// joe-c: fix & remove
|
2020-01-21 20:47:52 +03:00
|
|
|
pub enum TmpGlImportHack{ non_empty }
|
2019-09-23 13:42:20 +03:00
|
|
|
|
2020-04-16 12:37:59 +03:00
|
|
|
fn C.gladLoadGL() int
|
|
|
|
|
2020-04-02 02:45:22 +03:00
|
|
|
fn C.glDisable()
|
|
|
|
fn C.glEnable()
|
|
|
|
fn C.glScissor()
|
|
|
|
fn C.glVertexAttribPointer()
|
|
|
|
fn C.glGenBuffers()
|
|
|
|
fn C.glEnableVertexAttribArray()
|
2020-05-20 16:24:37 +03:00
|
|
|
fn C.glDisableVertexAttribArray()
|
2020-04-02 02:45:22 +03:00
|
|
|
fn C.glGenVertexArrays()
|
|
|
|
fn C.glDrawElements()
|
|
|
|
fn C.glUseProgram()
|
2020-05-20 16:24:37 +03:00
|
|
|
fn C.glValidateProgram()
|
2020-04-02 02:45:22 +03:00
|
|
|
fn C.glDrawArrays()
|
|
|
|
fn C.glBufferData()
|
|
|
|
fn C.glGenerateMipmap()
|
|
|
|
fn C.glTexParameteri()
|
|
|
|
fn C.glDeleteTextures()
|
|
|
|
fn C.glBindTexture()
|
|
|
|
fn C.glActiveTexture()
|
|
|
|
fn C.glGenTextures()
|
|
|
|
fn C.glBindBuffer()
|
|
|
|
fn C.glBindVertexArray()
|
|
|
|
fn C.glGetProgramInfoLog()
|
|
|
|
fn C.glGetShaderInfoLog()
|
|
|
|
fn C.glDeleteShader()
|
|
|
|
fn C.glGetProgramiv()
|
|
|
|
fn C.glLinkProgram()
|
|
|
|
fn C.glAttachShader()
|
2020-05-20 16:24:37 +03:00
|
|
|
fn C.glDetachShader()
|
2020-04-02 02:45:22 +03:00
|
|
|
fn C.glGetShaderiv()
|
|
|
|
fn C.glCompileShader()
|
|
|
|
fn C.glShaderSource()
|
2020-04-04 13:56:31 +03:00
|
|
|
fn C.glCreateProgram() int
|
2020-05-20 16:24:37 +03:00
|
|
|
fn C.glDeleteProgram()
|
2020-04-02 02:45:22 +03:00
|
|
|
fn C.glClear()
|
2020-04-04 13:56:31 +03:00
|
|
|
fn C.glCreateShader() int
|
2020-04-02 02:45:22 +03:00
|
|
|
fn C.glClearColor()
|
|
|
|
fn C.glViewport()
|
|
|
|
fn C.glTexImage2D()
|
|
|
|
fn C.glPixelStorei()
|
|
|
|
fn C.glBlendFunc()
|
2020-04-02 03:02:12 +03:00
|
|
|
fn C.glPolygonMode()
|
|
|
|
fn C.glDeleteBuffers()
|
2020-05-20 16:24:37 +03:00
|
|
|
fn C.glDeleteVertexArrays()
|
2020-05-10 17:49:29 +03:00
|
|
|
fn C.glGetUniformLocation() int
|
|
|
|
fn C.glGetAttribLocation() int
|
2020-05-20 16:24:37 +03:00
|
|
|
fn C.glBindAttribLocation()
|
2020-04-02 02:45:22 +03:00
|
|
|
|
|
|
|
|
2019-06-26 18:49:50 +03:00
|
|
|
pub fn init_glad() {
|
2019-06-22 22:27:59 +03:00
|
|
|
ok := C.gladLoadGL()
|
2020-04-16 12:37:59 +03:00
|
|
|
if ok == 0 {
|
2019-06-23 11:41:42 +03:00
|
|
|
println('Failed to initialize glad OpenGL context')
|
|
|
|
exit(1)
|
2019-06-22 22:27:59 +03:00
|
|
|
}
|
2019-06-22 22:24:49 +03:00
|
|
|
}
|
|
|
|
|
2019-06-26 17:39:40 +03:00
|
|
|
pub fn viewport(a, b, c, d int) {
|
2019-06-22 22:24:49 +03:00
|
|
|
C.glViewport(a, b, c, d)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn clear_color(r, g, b, a int) {
|
2019-06-28 14:49:27 +03:00
|
|
|
C.glClearColor(f32(r)/255.0, f32(g)/255.0, f32(b)/255.0, f32(a)/255.0)
|
2019-06-22 22:24:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn clear() {
|
2019-08-23 00:00:31 +03:00
|
|
|
C.glClear(C.GL_COLOR_BUFFER_BIT | C.GL_DEPTH_BUFFER_BIT | C.GL_STENCIL_BUFFER_BIT)
|
2019-06-22 22:24:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn create_shader(typ int) int {
|
|
|
|
return C.glCreateShader(typ)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn create_program() int {
|
|
|
|
return C.glCreateProgram()
|
|
|
|
}
|
|
|
|
|
2020-05-20 16:24:37 +03:00
|
|
|
pub fn delete_program(program int) {
|
|
|
|
C.glDeleteProgram(program)
|
|
|
|
}
|
|
|
|
|
2019-06-26 17:39:40 +03:00
|
|
|
pub fn shader_source(shader, a int, source string, b int) {
|
2019-06-22 22:24:49 +03:00
|
|
|
C.glShaderSource(shader, a, &source.str, b)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn compile_shader(shader int) {
|
|
|
|
C.glCompileShader(shader)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn shader_compile_status(shader int) int {
|
|
|
|
success := 0
|
2019-08-23 00:00:31 +03:00
|
|
|
C.glGetShaderiv(shader, C.GL_COMPILE_STATUS, &success)
|
2019-06-22 22:24:49 +03:00
|
|
|
return success
|
|
|
|
}
|
|
|
|
|
2019-06-26 17:39:40 +03:00
|
|
|
pub fn attach_shader(program, shader int) {
|
2019-06-22 22:24:49 +03:00
|
|
|
// fn (s Shader) attach(program int) {
|
|
|
|
C.glAttachShader(program, shader)
|
|
|
|
}
|
|
|
|
|
2020-05-20 16:24:37 +03:00
|
|
|
pub fn detach_shader(program, shader int) {
|
|
|
|
C.glDetachShader(program, shader)
|
|
|
|
}
|
|
|
|
|
2019-06-22 22:24:49 +03:00
|
|
|
pub fn link_program(program int) {
|
|
|
|
C.glLinkProgram(program)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_program_link_status(program int) int {
|
|
|
|
success := 0
|
2019-08-23 00:00:31 +03:00
|
|
|
C.glGetProgramiv(program, C.GL_LINK_STATUS, &success)
|
2019-06-22 22:24:49 +03:00
|
|
|
return success
|
|
|
|
}
|
|
|
|
|
2020-05-20 16:24:37 +03:00
|
|
|
pub fn validate_program(program int) {
|
|
|
|
C.glValidateProgram(program)
|
|
|
|
}
|
|
|
|
|
2019-06-22 22:24:49 +03:00
|
|
|
pub fn delete_shader(shader int) {
|
|
|
|
C.glDeleteShader(shader)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn shader_info_log(shader int) string {
|
2019-07-15 23:44:26 +03:00
|
|
|
info_log := [512]byte
|
|
|
|
C.glGetShaderInfoLog(shader, 512, 0, info_log)
|
|
|
|
return tos_clone(info_log)
|
2019-06-22 22:24:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_program_info_log(program int) string {
|
2019-07-15 23:44:26 +03:00
|
|
|
info_log := [1024]byte
|
|
|
|
C.glGetProgramInfoLog(program, 1024, 0, info_log)
|
|
|
|
return tos_clone(info_log)
|
2019-06-22 22:24:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn bind_vao(vao u32) {
|
|
|
|
C.glBindVertexArray(vao)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn bind_buffer(typ int, vbo u32) {
|
|
|
|
C.glBindBuffer(typ, vbo)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn gen_texture() u32 {
|
|
|
|
res := u32(0)
|
|
|
|
C.glGenTextures(1, &res)
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn active_texture(t int) {
|
|
|
|
C.glActiveTexture(t)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn bind_2d_texture(texture u32) {
|
2019-08-23 00:00:31 +03:00
|
|
|
C.glBindTexture(C.GL_TEXTURE_2D, texture)
|
2019-06-22 22:24:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn delete_texture(texture u32) {
|
|
|
|
C.glDeleteTextures(1, &texture)
|
|
|
|
}
|
|
|
|
|
2019-06-26 17:39:40 +03:00
|
|
|
pub fn buffer_data(typ, size int, arr voidptr, draw_typ int) {
|
2019-06-22 22:24:49 +03:00
|
|
|
C.glBufferData(typ, size, arr, draw_typ)
|
|
|
|
}
|
|
|
|
|
2019-06-30 14:06:46 +03:00
|
|
|
pub fn buffer_data_int(typ int, vertices []int, draw_typ int) {
|
2019-06-22 22:24:49 +03:00
|
|
|
size := sizeof(int) * vertices.len
|
|
|
|
C.glBufferData(typ, size, vertices.data, draw_typ)
|
|
|
|
}
|
|
|
|
|
2019-06-30 14:06:46 +03:00
|
|
|
pub fn buffer_data_f32(typ int, vertices []f32, draw_typ int) {
|
2019-06-25 22:36:44 +03:00
|
|
|
size := sizeof(f32) * vertices.len
|
2019-06-22 22:24:49 +03:00
|
|
|
C.glBufferData(typ, size, vertices.data, draw_typ)
|
|
|
|
}
|
|
|
|
|
2019-06-30 14:06:46 +03:00
|
|
|
pub fn set_vbo(vbo u32, vertices []f32, draw_typ int) {
|
2019-08-23 00:00:31 +03:00
|
|
|
gl.bind_buffer(C.GL_ARRAY_BUFFER, vbo)
|
|
|
|
gl.buffer_data_f32(C.GL_ARRAY_BUFFER, vertices, draw_typ)
|
2019-06-22 22:24:49 +03:00
|
|
|
}
|
|
|
|
|
2019-06-30 14:06:46 +03:00
|
|
|
pub fn set_ebo(ebo u32, indices []int, draw_typ int) {
|
2019-08-23 00:00:31 +03:00
|
|
|
gl.bind_buffer(C.GL_ELEMENT_ARRAY_BUFFER, ebo)
|
2019-06-22 22:24:49 +03:00
|
|
|
// gl.buffer_data_int(GL_ELEMENT_ARRAY_BUFFER, indices, draw_typ)
|
2019-08-23 00:00:31 +03:00
|
|
|
gl.buffer_data_int(C.GL_ELEMENT_ARRAY_BUFFER, indices, draw_typ)
|
2019-06-22 22:24:49 +03:00
|
|
|
}
|
|
|
|
|
2020-05-20 16:24:37 +03:00
|
|
|
pub fn delete_buffer(vbo u32) {
|
|
|
|
C.glDeleteBuffers(1, vbo)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn delete_vao(vao u32) {
|
|
|
|
C.glDeleteVertexArrays(1, vao)
|
|
|
|
}
|
|
|
|
|
2019-06-22 22:24:49 +03:00
|
|
|
// /////////////////////
|
|
|
|
// fn gen_vertex_arrays(a int, vao uint) {
|
|
|
|
// # glGenVertexArrays(a, &VAO);
|
|
|
|
// }
|
2020-05-10 17:49:29 +03:00
|
|
|
|
|
|
|
// gets the uniform location for key
|
|
|
|
pub fn get_uniform_location(program int, key string) int {
|
|
|
|
return C.glGetUniformLocation(program, key.str)
|
|
|
|
}
|
|
|
|
|
|
|
|
//gets the attribute location for key
|
|
|
|
pub fn get_attrib_location(program int, key string) int {
|
|
|
|
return C.glGetAttribLocation(program, key.str)
|
|
|
|
}
|
|
|
|
|
2020-05-20 16:24:37 +03:00
|
|
|
pub fn bind_attrib_location(program int, index int, name string) {
|
|
|
|
C.glBindAttribLocation(program, index, name.str)
|
|
|
|
}
|
|
|
|
|
2019-06-22 22:24:49 +03:00
|
|
|
pub fn draw_arrays(typ, start, len int) {
|
|
|
|
C.glDrawArrays(typ, start, len)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn draw_elements(mode, count, typ, indices int) {
|
|
|
|
C.glDrawElements(mode, count, typ, indices)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn use_program(program int) {
|
|
|
|
C.glUseProgram(program)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn gen_vertex_array() u32 {
|
2019-07-15 23:44:26 +03:00
|
|
|
vao := u32(0)
|
|
|
|
C.glGenVertexArrays(1, &vao)
|
2019-08-23 00:00:31 +03:00
|
|
|
return vao
|
2019-06-22 22:24:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn enable_vertex_attrib_array(n int) {
|
|
|
|
C.glEnableVertexAttribArray(n)
|
|
|
|
}
|
|
|
|
|
2020-05-20 16:24:37 +03:00
|
|
|
pub fn disable_vertex_attrib_array(n int) {
|
|
|
|
C.glDisableVertexAttribArray(n)
|
|
|
|
}
|
|
|
|
|
2019-06-22 22:24:49 +03:00
|
|
|
pub fn gen_buffer() u32 {
|
2019-07-15 23:44:26 +03:00
|
|
|
vbo := u32(0)
|
|
|
|
C.glGenBuffers(1, &vbo)
|
2019-08-23 00:00:31 +03:00
|
|
|
return vbo
|
2019-06-22 22:24:49 +03:00
|
|
|
}
|
|
|
|
|
2019-08-23 00:00:31 +03:00
|
|
|
pub fn vertex_attrib_pointer(index, size int, typ int, normalized bool, _stride int, _ptr int) {
|
|
|
|
mut stride := _stride
|
|
|
|
mut ptr := _ptr
|
|
|
|
if typ == C.GL_FLOAT {
|
2019-06-25 22:36:44 +03:00
|
|
|
stride *= sizeof(f32)
|
|
|
|
ptr *= sizeof(f32)
|
2019-06-22 22:24:49 +03:00
|
|
|
}
|
|
|
|
C.glVertexAttribPointer(index, size, typ, normalized, stride, ptr)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn tex_param(key, val int) {
|
2019-08-23 00:00:31 +03:00
|
|
|
C.glTexParameteri(C.GL_TEXTURE_2D, key, val)
|
2019-06-22 22:24:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn enable(val int) {
|
|
|
|
C.glEnable(val)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn disable(val int) {
|
|
|
|
C.glDisable(val)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn scissor(a, b, c, d int) {
|
|
|
|
C.glScissor(a, b, c, d)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn generate_mipmap(typ int) {
|
|
|
|
C.glGenerateMipmap(typ)
|
|
|
|
}
|
|
|
|
|
2020-05-10 17:49:29 +03:00
|
|
|
// set mat4 at uniform location
|
|
|
|
pub fn set_mat4fv(loc, count int, transpose bool, val glm.Mat4) {
|
|
|
|
C.glUniformMatrix4fv(loc, count, transpose, val.data)
|
|
|
|
}
|