2023-11-30 13:33:49 +00:00
# include "v4k.h"
2023-12-01 15:05:38 +00:00
skybox_t sky ;
model_t litm ;
2023-12-01 21:19:41 +00:00
lightmap_t baker ;
2023-11-30 13:33:49 +00:00
2023-12-01 15:05:38 +00:00
void bakedrawmodel ( lightmap_t * lm , model_t * m , float * view , float * proj , void * userdata ) {
shader_bind ( lm - > shader ) ;
model_render ( * m , proj , view , m - > pivot , lm - > shader ) ;
2023-12-01 21:19:41 +00:00
shader_float ( " u_litboost " , 8.0 ) ;
2023-12-01 15:05:38 +00:00
model_render ( litm , proj , view , litm . pivot , lm - > shader ) ;
2023-11-30 13:33:49 +00:00
}
2023-12-01 15:54:40 +00:00
void progressupdate ( float progress ) {
static double lastUpdateTime = 0.0 ;
double time = time_ss ( ) ;
if ( time - lastUpdateTime > 1.0 ) {
lastUpdateTime = time ;
PRINTF ( " progress: %.02f%% " , progress * 100 ) ;
}
// window_swap();
}
2023-11-30 13:33:49 +00:00
int main ( )
{
2023-12-01 15:54:40 +00:00
window_create ( 0.5 , 0 ) ;
window_title ( __FILE__ ) ;
2023-12-01 15:05:38 +00:00
camera_t cam = camera ( ) ;
sky = skybox ( 0 , 0 ) ; skybox_mie_calc_sh ( & sky , 2.0f ) ;
model_t mdl = model ( " gazebo.obj " , 0 ) ;
2023-12-01 21:19:41 +00:00
litm = model ( " kgirls01.fbx " , 0 ) ;
2023-11-30 13:33:49 +00:00
{
2023-12-01 21:19:41 +00:00
mat44 lp ; id44 ( lp ) ;
// scaling44(lp, 10, 10, 10);
translate44 ( lp , 0 , 2 , 0 ) ;
2023-12-01 15:05:38 +00:00
copy44 ( litm . pivot , lp ) ;
2023-11-30 13:33:49 +00:00
}
2023-12-01 15:05:38 +00:00
rotate44 ( mdl . pivot , - 90 , 1 , 0 , 0 ) ;
scale44 ( mdl . pivot , 4 , 4 , 4 ) ;
shader_bind ( mdl . program ) ;
shader_vec3v ( " u_coefficients_sh " , 9 , sky . cubemap . sh ) ;
// shader_bool("u_texmod", 0);
2023-12-01 21:19:41 +00:00
// unsigned char emissive[] = { 255, 180, 0, 255 };
// texture_t emission = texture_create(1,1,4,emissive,TEXTURE_LINEAR);
// model_set_texture(litm, emission);
2023-12-01 15:05:38 +00:00
2023-12-01 21:19:41 +00:00
baker = lightmap ( 64 , 0.01 , 100 , vec3 ( 0 , 0 , 0 ) , 2 , 0.01 , - 1.0 ) ;
2023-12-01 15:54:40 +00:00
lightmap_setup ( & baker , 512 , 512 ) ;
2023-12-01 15:05:38 +00:00
array_push ( baker . models , & mdl ) ;
2023-11-30 13:33:49 +00:00
2023-12-01 15:54:40 +00:00
bool do_bake = 0 ;
int b = 1 ;
2023-11-30 13:33:49 +00:00
2023-12-01 15:05:38 +00:00
while ( window_swap ( ) & & ! input ( KEY_ESC ) ) {
bool active = ui_active ( ) | | ui_hover ( ) | | gizmo_active ( ) ? false : input ( MOUSE_L ) | | input ( MOUSE_M ) | | input ( MOUSE_R ) ;
if ( active ) cam . speed = clampf ( cam . speed + input_diff ( MOUSE_W ) / 10 , 0.05f , 5.0f ) ;
vec2 mouse = scale2 ( vec2 ( input_diff ( MOUSE_X ) , - input_diff ( MOUSE_Y ) ) , 0.2f * active ) ;
vec3 wasdecq = scale3 ( vec3 ( input ( KEY_D ) - input ( KEY_A ) , input ( KEY_E ) - ( input ( KEY_C ) | | input ( KEY_Q ) ) , input ( KEY_W ) - input ( KEY_S ) ) , cam . speed ) ;
camera_moveby ( & cam , wasdecq ) ;
camera_fps ( & cam , mouse . x , mouse . y ) ;
window_cursor ( ! active ) ;
skybox_render ( & sky , cam . proj , cam . view ) ;
model_render ( mdl , cam . proj , cam . view , mdl . pivot , 0 ) ;
model_render ( litm , cam . proj , cam . view , litm . pivot , 0 ) ;
2023-11-30 13:33:49 +00:00
if ( ui_panel ( " Lightmapper " , PANEL_OPEN ) ) {
ui_label2 ( " Freecam " , " Mouse + W/A/S/D/E/Q keys " ) ;
ui_label ( " Warning " ICON_MD_WARNING " @This will take a few seconds and bake a lightmap illuminated by: The mesh itself (initially black) + A white sky (1.0f, 1.0f, 1.0f) " ) ;
2023-12-01 15:54:40 +00:00
ui_int ( " Bounces " , & b ) ;
2023-12-01 15:05:38 +00:00
if ( ui_button ( va ( " Bake %d light bounce " , b ) ) ) {
2023-12-01 15:54:40 +00:00
do_bake = 1 ;
2023-11-30 13:33:49 +00:00
}
ui_panel_end ( ) ;
}
2023-12-01 15:54:40 +00:00
if ( do_bake ) {
do_bake = 0 ;
lightmap_bake ( & baker , b , bakedrawmodel , progressupdate , 0 ) ;
}
2023-11-30 13:33:49 +00:00
}
}