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

Functions

LG_Light lg_light (lg_light_type type, vec3_t pos_or_dir, LG_Color_f color)
 
void lg_set_light_src (LG_Light *light, vec3_t pos)
 
vec3_t lg_get_light_src (LG_Light *light)
 
void lg_move_light_src (LG_Light *light, vec3_t delta)
 
void lg_set_light_dir (LG_Light *light, vec3_t dir)
 
vec3_t lg_get_light_dir (LG_Light *light)
 
void lg_change_light_dir (LG_Light *light, LG_EulerAng angle, const char *rot_order)
 

Detailed Description

=== It's all about scene lights position, direction, color ===

Use an array of LG_Light structs if you want to have several different lights in a scene

Lighting is actually a bit complicated:

  • Ambiant lighting -> small constant color
  • Diffuse lighting ->
    • Directional lighting, using surface normal
      • vec3_t as an orientation
      • Lambert cosine law
    • Point lighting, using surface normal AND distance
      • vec3_t as a position
      • dist = len(light pos - obj pos)
      • attenuation = 1.0 / (light K + light linear K * dist + light quadratic K * pow2(dist))
  • Specular lighting ->
    • ...
  • Spot lighting
  • Combined -> Gouraud / Phong / Blinn-Phong shading
  • Gamma correction
(From https://learnopengl.com/Lighting/Materials)
uniform Material material;
#version 330 core
struct Material {
vec3 ambient;
vec3 diffuse;
vec3 specular;
float shininess;
};
void main()
{
// ambient
vec3 ambient = lightColor * material.ambient;
// diffuse
vec3 norm = normalize(Normal);
vec3 lightDir = normalize(lightPos - FragPos);
float diff = max(dot(norm, lightDir), 0.0);
vec3 diffuse = lightColor * (diff * material.diffuse);
// specular
vec3 viewDir = normalize(viewPos - FragPos);
vec3 reflectDir = reflect(-lightDir, norm);
float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess);
vec3 specular = lightColor * (spec * material.specular);
vec3 result = ambient + diffuse + specular;
FragColor = vec4(result, 1.0);
}
uniform Light light;
struct Light {
vec3 position;
vec3 ambient;
vec3 diffuse;
vec3 specular;
};
Lighting maps
...
Specular maps
...

TODO

  • Should LG_Light pos and dir be in a union or not ?
  • Complete this

Function Documentation

◆ lg_light()

LG_Light lg_light ( lg_light_type  type,
vec3_t  pos_or_dir,
LG_Color_f  color 
)

Return a new LG_Light

Parameters
typeLight type (lg_light_type enum), one of POINT, DIRECTIONAL, SPOTLIGHT, AMBIENT, DIFFUSE, EMISSIVE, SPECULAR
pos_or_dirSource of light (vec3_t) in case of POINT light or light orientation (vec3_t) in case of DIRECTIONAL light
colorLight color
Returns
A new LG_Light

◆ lg_set_light_src()

void lg_set_light_src ( LG_Light light,
vec3_t  pos 
)

Set light source position

Parameters
lightA LG_Light
posLight source pos

◆ lg_get_light_src()

vec3_t lg_get_light_src ( LG_Light light)

Read light source position

Parameters
lightA LG_Light
Returns
Light source pos

◆ lg_move_light_src()

void lg_move_light_src ( LG_Light light,
vec3_t  delta 
)

Move light source position by delta

Parameters
lightA LG_Light
deltaTranslation vector

◆ lg_set_light_dir()

void lg_set_light_dir ( LG_Light light,
vec3_t  dir 
)

Set light orientation

Parameters
lightA LG_Light
dirLight dir

◆ lg_get_light_dir()

vec3_t lg_get_light_dir ( LG_Light light)

Read light orientation

Parameters
lightA LG_Light
Returns
Light dir

◆ lg_change_light_dir()

void lg_change_light_dir ( LG_Light light,
LG_EulerAng  angle,
const char *  rot_order 
)

Change light orientation by rotating by Euler angle

Parameters
lightA LG_Light
rotRotation to apply (Euler angle)
rot_orderRotation order, one of "XYZ", "YXZ", "ZXY", "ZYX", "YZX", "XZY"