v4k-git-backup/demos/99-lmap.c

64 lines
2.5 KiB
C
Raw Normal View History

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-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);
shader_float("u_litboost", 4.0);
model_render(litm, proj, view, litm.pivot, lm->shader);
2023-11-30 13:33:49 +00:00
}
int main()
{
window_create(0.5, WINDOW_VSYNC_DISABLED);
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);
litm = model("cube.obj", MODEL_MATCAPS);
2023-11-30 13:33:49 +00:00
{
2023-12-01 15:05:38 +00:00
mat44 lp; scaling44(lp, 0.3, 0.3, 0.3); translate44(lp, 8,4,0);
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);
unsigned char emissive[] = { 255, 180, 0, 255 };
texture_t emission = texture_create(1,1,4,emissive,TEXTURE_LINEAR);
model_set_texture(litm, emission);
lightmap_t baker = lightmap(64, 0.001, 1000, vec3(0,0,0), 2, 0.01, 0.0);
lightmap_setup(&baker, 640, 640);
array_push(baker.models, &mdl);
2023-11-30 13:33:49 +00:00
window_title("AO Baking demo");
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:05:38 +00:00
int b=2;
if( ui_button(va("Bake %d light bounce", b)) ) {
lightmap_bake(&baker, b, bakedrawmodel, 0);
2023-11-30 13:33:49 +00:00
}
ui_panel_end();
}
}
}