LibGame  v0.4.0
The LG Game Engine - Copyright (C) 2024 ETMSoftware
lg_shader_progs.c File Reference

Functions

LG_ShaderProg LG_ShaderProg_create (const char **vertex_shader_code, const char **fragment_shader_code, LG_VAP_params *params)
 
void LG_ShaderProg_delete (LG_ShaderProg *shader_prog)
 
void LG_ShaderProg_use (LG_ShaderProg *shader_prog, zboolean gl_checking)
 
int LG_ShaderProg_get_attrib_loc (LG_ShaderProg *shader_prog, const char *attrib_name)
 
int LG_ShaderProg_get_uniform_loc (LG_ShaderProg *shader_prog, const char *uniform_name)
 
uint32_t lg_load_shader (int type, const char **shader_code)
 
int lg_check_gl_errors (long long log_time, const char *file, int line, const char *func)
 
void lg_shader_info_log (uint32_t shader)
 

Detailed Description

 === Wrapper functions to more easily create and manage shader progs ===

 LG_VAP_params = Vertex Attrib Pointer params

Function Documentation

◆ LG_ShaderProg_create()

LG_ShaderProg LG_ShaderProg_create ( const char **  vertex_shader_code,
const char **  fragment_shader_code,
LG_VAP_params params 
)

Create shader prog from vertex and fragment shader source code

Also set vertex attrib pointer params if params != NULL

Shader prog id set to LG_INVALID_SHADER_PROG_ID on error

Parameters
vertex_shader_codeGLSL source code
fragment_shader_codeGLSL source code
paramsAn array of LG_VAP_params, may be NULL - VAP stands for Vertex Attrib Pointer

Example code:

const char *vert_shader[] = {
"uniform mat4 u_mvp_matrix;"
"attribute vec4 a_position;"
"attribute vec2 a_texcoords;"
"varying vec2 v_texcoords;"
"void main() {"
" gl_Position = u_mvp_matrix * a_position;"
" v_texcoords = a_texcoords;"
"}"
};
const char *frag_shader[] = {
"uniform sampler2D u_texture0;"
"varying mediump vec2 v_texcoords;"
"void main() {"
" gl_FragColor = texture2D(u_texture0, vec2(v_texcoords.x, 1.0 - v_texcoords.y));"
"}"
};
LG_VAP_params draw_params[] = {
{0, "a_position", 3, GL_FLOAT, FALSE, sizeof(Vertex_uv_n), (void *)0},
{1, "a_texcoords", 2, GL_UNSIGNED_SHORT, TRUE, sizeof(Vertex_uv_n), (void *)offsetof(Vertex_uv_n, u)},
VAP_END
};
LG_ShaderProg shader_prog = LG_ShaderProg_create(vert_shader, frag_shader, draw_params);
Returns
A new LG_ShaderProg

◆ LG_ShaderProg_delete()

void LG_ShaderProg_delete ( LG_ShaderProg shader_prog)

Delete shader prog

Parameters
shader_progAddr of shader prog

◆ LG_ShaderProg_use()

void LG_ShaderProg_use ( LG_ShaderProg shader_prog,
zboolean  gl_checking 
)

Use shader prog

Parameters
shader_progAddr of shader prog
gl_checkingEnable (TRUE) / disable (FALSE) GL checking

◆ LG_ShaderProg_get_attrib_loc()

int LG_ShaderProg_get_attrib_loc ( LG_ShaderProg shader_prog,
const char *  attrib_name 
)

Get the location of an attribute variable in the shader prog

Return -1 on error

From: https://docs.gl/es2/glGetAttribLocation
"If the named attribute variable is not an active attribute in the specified program object
or if name starts with the reserved prefix 'gl_', a value of -1 is returned."
Parameters
shader_prog
attrib_name
Returns
Attribute location, set to -1 on error

◆ LG_ShaderProg_get_uniform_loc()

int LG_ShaderProg_get_uniform_loc ( LG_ShaderProg shader_prog,
const char *  uniform_name 
)

Get the location of a uniform variable in the shader prog

Return -1 on error

From: https://docs.gl/es2/glGetUniformLocation
"This function returns -1 if name does not correspond to an active uniform variable in program
or if name starts with the reserved prefix 'gl_'."
Parameters
shader_prog
uniform_name
Returns
Uniform location, set to -1 on error

◆ lg_load_shader()

uint32_t lg_load_shader ( int  type,
const char **  shader_code 
)

Load and compile shader from source

Parameters
typeGL_VERTEX_SHADER or GL_FRAGMENT_SHADER
shader_codeGLSL source code
Returns
Shader or 0 on error

◆ lg_check_gl_errors()

int lg_check_gl_errors ( long long  log_time,
const char *  file,
int  line,
const char *  func 
)

Check GL errors

Parameters
log_timelg_log_time()
file__ FILE __ (without spaces)
line__ LINE __ (without spaces)
func__ func __ (without spaces)
Returns
LG_OK if OK

◆ lg_shader_info_log()

void lg_shader_info_log ( uint32_t  shader)

Print out shader compilation log

Parameters
shader
LG_VAP_params
Definition: lg_shader_progs.h:32
LG_ShaderProg
Definition: lg_shader_progs.h:11
Vertex_uv_n
Definition: lg_vertex.h:44
LG_ShaderProg_create
LG_ShaderProg LG_ShaderProg_create(const char **vertex_shader_code, const char **fragment_shader_code, LG_VAP_params *params)
Definition: lg_shader_progs.c:63