v/vlib/gl/gl.v

399 lines
11 KiB
V
Raw Normal View History

2020-01-23 21:04:46 +01:00
// Copyright (c) 2019-2020 Alexander Medvednikov. All rights reserved.
2019-06-23 04:21:30 +02:00
// Use of this source code is governed by an MIT license
// that can be found in the LICENSE file.
2019-06-22 21:24:49 +02:00
module gl
2020-05-10 16:49:29 +02:00
import glm
#flag -I @VROOT/thirdparty/glad
2019-06-22 21:27:59 +02:00
#include "glad.h"
2019-08-22 23:00:31 +02:00
#flag @VROOT/thirdparty/glad/glad.o
2019-06-23 13:17:33 +02:00
2020-05-10 16:49:29 +02:00
2019-09-23 12:42:20 +02:00
// joe-c: fix & remove
pub enum TmpGlImportHack{ non_empty }
2019-09-23 12:42:20 +02:00
fn C.gladLoadGL() int
2020-04-02 01:45:22 +02:00
fn C.glDisable()
fn C.glEnable()
fn C.glScissor()
fn C.glVertexAttribPointer()
fn C.glGenBuffers()
fn C.glEnableVertexAttribArray()
fn C.glDisableVertexAttribArray()
2020-04-02 01:45:22 +02:00
fn C.glGenVertexArrays()
fn C.glDrawElements()
fn C.glUseProgram()
fn C.glValidateProgram()
2020-04-02 01:45:22 +02: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()
fn C.glDetachShader()
2020-04-02 01:45:22 +02:00
fn C.glGetShaderiv()
fn C.glCompileShader()
fn C.glShaderSource()
2020-04-04 12:56:31 +02:00
fn C.glCreateProgram() int
fn C.glDeleteProgram()
2020-04-02 01:45:22 +02:00
fn C.glClear()
2020-04-04 12:56:31 +02:00
fn C.glCreateShader() int
2020-04-02 01:45:22 +02:00
fn C.glClearColor()
fn C.glViewport()
fn C.glPixelStorei()
fn C.glBlendFunc()
2020-04-02 02:02:12 +02:00
fn C.glPolygonMode()
fn C.glDeleteBuffers()
fn C.glDeleteVertexArrays()
2020-05-10 16:49:29 +02:00
fn C.glGetUniformLocation() int
fn C.glGetAttribLocation() int
fn C.glBindAttribLocation()
2020-04-02 01:45:22 +02:00
fn C.glUniform1f()
2020-04-02 01:45:22 +02:00
2020-06-05 15:57:45 +02:00
// init_glad initializes glad, which is needed to use other functions.
2019-06-26 17:49:50 +02:00
pub fn init_glad() {
2019-06-22 21:27:59 +02:00
ok := C.gladLoadGL()
if ok == 0 {
2019-06-23 10:41:42 +02:00
println('Failed to initialize glad OpenGL context')
exit(1)
2019-06-22 21:27:59 +02:00
}
2019-06-22 21:24:49 +02:00
}
2020-06-05 15:57:45 +02:00
// viewport declares the area, which should be rendered in the window.
// Should be used on resizing the window.
pub fn viewport(x, y, width, height int) {
C.glViewport(x, y, width, height)
2019-06-22 21:24:49 +02:00
}
2020-06-05 15:57:45 +02:00
// clear_color sets the color, which will be the background, where you can draw.
// Need only set once.
2019-06-22 21:24:49 +02:00
pub fn clear_color(r, g, b, a int) {
C.glClearColor(f32(r)/255.0, f32(g)/255.0, f32(b)/255.0, f32(a)/255.0)
2019-06-22 21:24:49 +02:00
}
2020-06-05 15:57:45 +02:00
// clear clears the bits of the last frame (ColorBufferBit, DepthBufferBit, StencilBufferBit) to store new data.
// Should be done every frame.
2019-06-22 21:24:49 +02:00
pub fn clear() {
2019-08-22 23:00:31 +02:00
C.glClear(C.GL_COLOR_BUFFER_BIT | C.GL_DEPTH_BUFFER_BIT | C.GL_STENCIL_BUFFER_BIT)
2019-06-22 21:24:49 +02:00
}
2020-06-05 15:57:45 +02:00
// create_shader creates a shader in OpenGL and returns the id.
2019-06-22 21:24:49 +02:00
pub fn create_shader(typ int) int {
return C.glCreateShader(typ)
}
2020-06-05 15:57:45 +02:00
// Create a program in OpenGL and returns the id.
// A shader can be attached to a program, which is required to use them.
2019-06-22 21:24:49 +02:00
pub fn create_program() int {
return C.glCreateProgram()
}
2020-06-05 15:57:45 +02:00
// Delete a program by id.
// Cleanup method, should be used after the main game loop
pub fn delete_program(program int) {
C.glDeleteProgram(program)
}
2020-06-05 15:57:45 +02:00
// shader_source attaches source code to the shader via the shaderID.
// Could be also used to load multiple sources into a shader.
// To just add one source code to one shader use: `gl.shader_source(shader, 1, src, 0)`
pub fn shader_source(shader, count int, source string, length int) {
C.glShaderSource(shader, count, &source.str, length)
2019-06-22 21:24:49 +02:00
}
2020-06-05 15:57:45 +02:00
// compile_shader compiles the shader's source code.
// OpenGL compiles the source code at runtime.
2019-06-22 21:24:49 +02:00
pub fn compile_shader(shader int) {
C.glCompileShader(shader)
}
2020-06-05 15:57:45 +02:00
// shader_compile_status returns the compile status of the shader compilation.
// Can be used to check the compilation and see for errors in the shader code via `gl.shader_info_log()`
2019-06-22 21:24:49 +02:00
pub fn shader_compile_status(shader int) int {
success := 0
2019-08-22 23:00:31 +02:00
C.glGetShaderiv(shader, C.GL_COMPILE_STATUS, &success)
2019-06-22 21:24:49 +02:00
return success
}
2020-06-05 15:57:45 +02:00
// attach_shader attaches a shader to a program.
// Required for drawing things on the screen
pub fn attach_shader(program, shader int) {
2019-06-22 21:24:49 +02:00
C.glAttachShader(program, shader)
}
2020-06-05 15:57:45 +02:00
// detach_shader detaches a shader of a program
// Cleanup method
pub fn detach_shader(program, shader int) {
C.glDetachShader(program, shader)
}
2020-06-05 15:57:45 +02:00
// link_program links a program as target.
// This let OpenGL know, which program has to be use.
2019-06-22 21:24:49 +02:00
pub fn link_program(program int) {
C.glLinkProgram(program)
}
2020-06-05 15:57:45 +02:00
// get_program_link_status returns the link status of linking the program
2019-06-22 21:24:49 +02:00
pub fn get_program_link_status(program int) int {
success := 0
2019-08-22 23:00:31 +02:00
C.glGetProgramiv(program, C.GL_LINK_STATUS, &success)
2019-06-22 21:24:49 +02:00
return success
}
2020-06-05 15:57:45 +02:00
// validate_program checks that the shaders in the program can be executed
pub fn validate_program(program int) {
C.glValidateProgram(program)
}
2020-06-05 15:57:45 +02:00
// delete_shader deletes a shader via the shaderID
// Cleanup method
2019-06-22 21:24:49 +02:00
pub fn delete_shader(shader int) {
C.glDeleteShader(shader)
}
2020-06-05 15:57:45 +02:00
// shader_info_log returns a info log of the shader.
// Can be used to print compilation errors.
2019-06-22 21:24:49 +02:00
pub fn shader_info_log(shader int) string {
2019-07-15 22:44:26 +02:00
info_log := [512]byte
C.glGetShaderInfoLog(shader, 512, 0, info_log)
return tos_clone(info_log)
2019-06-22 21:24:49 +02:00
}
2020-06-05 15:57:45 +02:00
// get_program_info_log returns a info log of the program.
// Can be used to print linking errors etc.
2019-06-22 21:24:49 +02:00
pub fn get_program_info_log(program int) string {
2019-07-15 22:44:26 +02:00
info_log := [1024]byte
C.glGetProgramInfoLog(program, 1024, 0, info_log)
return tos_clone(info_log)
2019-06-22 21:24:49 +02:00
}
2020-06-05 15:57:45 +02:00
// bind_vao binds a vertex array buffer to OpenGL.
// Says OpenGL which vao is the target.
2019-06-22 21:24:49 +02:00
pub fn bind_vao(vao u32) {
C.glBindVertexArray(vao)
}
2020-06-05 15:57:45 +02:00
// bind_buffer binds a vertex buffer object to OpenGL.
// Says OpenGL which vbo is the target.
2019-06-22 21:24:49 +02:00
pub fn bind_buffer(typ int, vbo u32) {
C.glBindBuffer(typ, vbo)
}
2020-06-05 15:57:45 +02:00
// gen_texture generates a textureID.
// Needed to use texturing.
2019-06-22 21:24:49 +02:00
pub fn gen_texture() u32 {
res := u32(0)
C.glGenTextures(1, &res)
return res
}
2020-06-05 15:57:45 +02:00
// active_texture activates a texture.
// If you don't do this, texture isn't working.
2019-06-22 21:24:49 +02:00
pub fn active_texture(t int) {
C.glActiveTexture(t)
}
2020-06-05 15:57:45 +02:00
// bind_2d_texture binds the activated texture as a 2D texture.
// Helper method.
2019-06-22 21:24:49 +02:00
pub fn bind_2d_texture(texture u32) {
2019-08-22 23:00:31 +02:00
C.glBindTexture(C.GL_TEXTURE_2D, texture)
2019-06-22 21:24:49 +02:00
}
2020-06-05 15:57:45 +02:00
// bind_texture binds the activated texture to a texture type.
// Defines the type for texture.
pub fn bind_texture(texture, typ u32) {
C.glBindTexture(typ, texture)
}
// delete_texture deletes a texture by ID.
// Cleanup method.
2019-06-22 21:24:49 +02:00
pub fn delete_texture(texture u32) {
C.glDeleteTextures(1, &texture)
}
2020-06-05 15:57:45 +02:00
// buffer_data puts data into a buffer.
// With these methods, data can put into a buffer.
// Common usage for C.GL_ARRAY_BUFFER or C.GL_ELEMENT_ARRAY_BUFFER.
pub fn buffer_data(typ, size int, arr voidptr, draw_typ int) {
2019-06-22 21:24:49 +02:00
C.glBufferData(typ, size, arr, draw_typ)
}
2020-06-05 15:57:45 +02:00
// buffer_data_int puts int into a buffer.
2019-06-30 13:06:46 +02:00
pub fn buffer_data_int(typ int, vertices []int, draw_typ int) {
size := sizeof(int) * u32(vertices.len)
2019-06-22 21:24:49 +02:00
C.glBufferData(typ, size, vertices.data, draw_typ)
}
2020-06-05 15:57:45 +02:00
// buffer_data_f32 puts float into a buffer.
2019-06-30 13:06:46 +02:00
pub fn buffer_data_f32(typ int, vertices []f32, draw_typ int) {
size := sizeof(f32) * u32(vertices.len)
2019-06-22 21:24:49 +02:00
C.glBufferData(typ, size, vertices.data, draw_typ)
}
2020-06-05 15:57:45 +02:00
// set_vbo sets vertices into a vertex buffer object.
// Helper method.
2019-06-30 13:06:46 +02:00
pub fn set_vbo(vbo u32, vertices []f32, draw_typ int) {
2019-08-22 23:00:31 +02:00
gl.bind_buffer(C.GL_ARRAY_BUFFER, vbo)
gl.buffer_data_f32(C.GL_ARRAY_BUFFER, vertices, draw_typ)
2019-06-22 21:24:49 +02:00
}
2020-06-05 15:57:45 +02:00
// set_ebo sets indices into a element array buffer.
// Helper method.
2019-06-30 13:06:46 +02:00
pub fn set_ebo(ebo u32, indices []int, draw_typ int) {
2019-08-22 23:00:31 +02:00
gl.bind_buffer(C.GL_ELEMENT_ARRAY_BUFFER, ebo)
gl.buffer_data_int(C.GL_ELEMENT_ARRAY_BUFFER, indices, draw_typ)
2019-06-22 21:24:49 +02:00
}
2020-06-05 15:57:45 +02:00
// delete_buffer deletes a vertex buffer object.
// Cleanup method.
pub fn delete_buffer(vbo u32) {
C.glDeleteBuffers(1, vbo)
}
2020-06-05 15:57:45 +02:00
// delete_vao deletes a vertex array object.
// Cleanup method.
pub fn delete_vao(vao u32) {
C.glDeleteVertexArrays(1, vao)
}
2020-06-05 15:57:45 +02:00
// get_uniform_location gets the uniform location for key in program.
// Required to put uniform data in shader at runtime.
2020-05-10 16:49:29 +02:00
pub fn get_uniform_location(program int, key string) int {
return C.glGetUniformLocation(program, key.str)
}
2020-06-05 15:57:45 +02:00
// get_attrib_location gets the attribute location for key in program.
// Required to put attrib data in shader at runtime.
2020-05-10 16:49:29 +02:00
pub fn get_attrib_location(program int, key string) int {
return C.glGetAttribLocation(program, key.str)
}
2020-06-05 15:57:45 +02:00
// bind_attrib_location binds a attrib on index in program to a name.
// Used to send data into a shader.
pub fn bind_attrib_location(program int, index int, name string) {
C.glBindAttribLocation(program, index, name.str)
}
2020-06-05 15:57:45 +02:00
// draw_arrays draws the vertex buffer object on screen.
// Commonly start is 0 and len 3 (without textures) or 5 (with textures).
// Mode commonly C.GL_TRIANGLES.
pub fn draw_arrays(mode, start, len int) {
C.glDrawArrays(mode, start, len)
2019-06-22 21:24:49 +02:00
}
2020-06-05 15:57:45 +02:00
// draw_elements draws the element object buffer on screen.
// Commonly typ is C.GL_UNSIGNED_INT and mode C.GL_TRIANGLES.
2019-06-22 21:24:49 +02:00
pub fn draw_elements(mode, count, typ, indices int) {
C.glDrawElements(mode, count, typ, indices)
}
2020-06-05 15:57:45 +02:00
// use_program binds program to OpenGL.
// Defines the program which is the target.
2019-06-22 21:24:49 +02:00
pub fn use_program(program int) {
C.glUseProgram(program)
}
2020-06-05 15:57:45 +02:00
// gen_vertex_array generates a vertex array ID.
// Linked to an empty vertex array.
2019-06-22 21:24:49 +02:00
pub fn gen_vertex_array() u32 {
2019-07-15 22:44:26 +02:00
vao := u32(0)
C.glGenVertexArrays(1, &vao)
2019-08-22 23:00:31 +02:00
return vao
2019-06-22 21:24:49 +02:00
}
2020-06-05 15:57:45 +02:00
// enable_vertex_attrib_array enables a vertex attrib array by index.
2019-06-22 21:24:49 +02:00
pub fn enable_vertex_attrib_array(n int) {
C.glEnableVertexAttribArray(n)
}
2020-06-05 15:57:45 +02:00
// disable_vertex_attrib_array disabled a vertex attrib array by index.
pub fn disable_vertex_attrib_array(n int) {
C.glDisableVertexAttribArray(n)
}
2020-06-05 15:57:45 +02:00
// gen_buffer generates an buffer ID.
// Linked to an empty buffer-
2019-06-22 21:24:49 +02:00
pub fn gen_buffer() u32 {
2019-07-15 22:44:26 +02:00
vbo := u32(0)
C.glGenBuffers(1, &vbo)
2019-08-22 23:00:31 +02:00
return vbo
2019-06-22 21:24:49 +02:00
}
2020-06-05 15:57:45 +02:00
// vertex_attrib_pointer defines the activated array by index.
2019-08-22 23:00:31 +02:00
pub fn vertex_attrib_pointer(index, size int, typ int, normalized bool, _stride int, _ptr int) {
mut stride := u32(_stride)
2019-08-22 23:00:31 +02:00
mut ptr := _ptr
if typ == C.GL_FLOAT {
2019-06-25 21:36:44 +02:00
stride *= sizeof(f32)
ptr *= int(sizeof(f32))
2019-06-22 21:24:49 +02:00
}
C.glVertexAttribPointer(index, size, typ, normalized, stride, ptr)
}
2020-06-05 15:57:45 +02:00
// tex_param attachs texture value as int to a texture by ID.
2019-06-22 21:24:49 +02:00
pub fn tex_param(key, val int) {
2019-08-22 23:00:31 +02:00
C.glTexParameteri(C.GL_TEXTURE_2D, key, val)
2019-06-22 21:24:49 +02:00
}
2020-06-05 15:57:45 +02:00
// enable enables various capabilities for OpenGL.
2019-06-22 21:24:49 +02:00
pub fn enable(val int) {
C.glEnable(val)
}
2020-06-05 15:57:45 +02:00
// disable disables various capabilities for OpenGL.
2019-06-22 21:24:49 +02:00
pub fn disable(val int) {
C.glDisable(val)
}
2020-06-05 15:57:45 +02:00
// scissor defines a rectangle in the window.
pub fn scissor(x, y, width, height int) {
C.glScissor(x, y, height, height)
2019-06-22 21:24:49 +02:00
}
pub fn generate_mipmap(typ int) {
C.glGenerateMipmap(typ)
}
2020-06-05 15:57:45 +02:00
// set_mat4fv sets a mat4 at uniform location.
// Used for almost every view stuff in OpenGL.
2020-05-10 16:49:29 +02:00
pub fn set_mat4fv(loc, count int, transpose bool, val glm.Mat4) {
C.glUniformMatrix4fv(loc, count, transpose, val.data)
}
2020-06-05 15:57:45 +02:00
// set_f32 sets a float at uniform location.
// Usable for global lighing.
pub fn set_f32(loc int, val f32) {
C.glUniform1f(loc, val)
}
2020-06-05 15:57:45 +02:00
// set_vec sets a vec3 at uniform location.
// Usable to set locations or colors.
pub fn set_vec(loc int, x, y, z f32) {
C.glUniform3f(loc, x, y, z)
}
2020-06-05 15:57:45 +02:00
// set_bool sets a bool at uniform location.
// Send states to the shader.
pub fn set_bool(loc int, val bool) {
if val {
set_f32(loc, 1)
} else {
set_f32(loc, 0)
}
}