scene: improve retexturing

main
Dominik Madarász 2024-08-24 11:49:16 +02:00
parent 3858f73f3f
commit d1cc36207f
12 changed files with 82 additions and 46 deletions

View File

@ -1422,7 +1422,7 @@ enum BILLBOARD_MODE {
void model_render_instanced(model_t, mat44 proj, mat44 view, mat44 *models, int shader, unsigned count); void model_render_instanced(model_t, mat44 proj, mat44 view, mat44 *models, int shader, unsigned count);
void model_render_instanced_pass(model_t m, mat44 proj, mat44 view, mat44* models, int shader, unsigned count, int pass); void model_render_instanced_pass(model_t m, mat44 proj, mat44 view, mat44* models, int shader, unsigned count, int pass);
void model_render_pass(model_t m, mat44 proj, mat44 view, mat44 model, int shader, int pass); void model_render_pass(model_t m, mat44 proj, mat44 view, mat44 model, int shader, int pass);
void model_set_texture(model_t, texture_t t); void model_set_texture(model_t*, texture_t t);
bool model_get_bone_pose(model_t m, unsigned joint, mat34 *out); bool model_get_bone_pose(model_t m, unsigned joint, mat34 *out);
void model_destroy(model_t); void model_destroy(model_t);
unsigned model_getpass(); unsigned model_getpass();
@ -1549,7 +1549,7 @@ typedef struct object_t {
mat44 transform; mat44 transform;
quat rot; quat rot;
vec3 sca, pos, euler, pivot; vec3 sca, pos, euler, pivot;
handle* textures; texture_t* textures;
model_t model; model_t model;
anim_t anim; anim_t anim;
float anim_speed; float anim_speed;

View File

@ -185,7 +185,7 @@ object_pivot(obj3, vec3(0,90,0));
// @todo: add shadertoy material // @todo: add shadertoy material
static model_t cube; do_once cube = model("cube.obj", 0); static model_t cube; do_once cube = model("cube.obj", 0);
static shadertoy_t s; do_once s = shadertoy("shadertoys/4ttGWM.fs", 256); static shadertoy_t s; do_once s = shadertoy("shadertoys/4ttGWM.fs", 256);
model_set_texture(cube, shadertoy_render(&s, window_delta())->tx); model_set_texture(&cube, shadertoy_render(&s, window_delta())->tx);
model_render(cube, cam.proj, cam.view, cube.pivot, 0); model_render(cube, cam.proj, cam.view, cube.pivot, 0);
while(window_swap() && !input(KEY_ESC)) { while(window_swap() && !input(KEY_ESC)) {
@ -203,7 +203,7 @@ while(window_swap() && !input(KEY_ESC)) {
// load static scene // load static scene
// model_t sponza = model("sponza.obj", MODEL_MATCAPS); // model_t sponza = model("sponza.obj", MODEL_MATCAPS);
// model_set_texture(sponza, texture("matcaps/normals", 0)); // model_set_texture(&sponza, texture("matcaps/normals", 0));
// translation44(sponza.pivot, 0,-1,0); // translation44(sponza.pivot, 0,-1,0);
// rotate44(sponza.pivot, -90,1,0,0); // rotate44(sponza.pivot, -90,1,0,0);
// scale44(sponza.pivot, 10,10,10); // scale44(sponza.pivot, 10,10,10);

View File

@ -20,7 +20,7 @@ int main() {
// config 3d model #1 // config 3d model #1
model_t witch = model("witch/witch.obj", 0); model_t witch = model("witch/witch.obj", 0);
model_set_texture(witch, texture("witch/witch_diffuse.tga.png", 0)); model_set_texture(&witch, texture("witch/witch_diffuse.tga.png", 0));
mat44 witch_pivot; vec3 witch_p = {-5,0,-5}, witch_r={-180,180,0}, witch_s={0.1,-0.1,0.1}; mat44 witch_pivot; vec3 witch_p = {-5,0,-5}, witch_r={-180,180,0}, witch_s={0.1,-0.1,0.1};
// config 3d model #2 // config 3d model #2

View File

@ -59,15 +59,15 @@ int main() {
if( flag("--matcaps") ) { if( flag("--matcaps") ) {
// patch models to use matcaps // patch models to use matcaps
model_set_texture(george, texture("matcaps/3B6E10_E3F2C3_88AC2E_99CE51-256px", 0)); // green model_set_texture(&george, texture("matcaps/3B6E10_E3F2C3_88AC2E_99CE51-256px", 0)); // green
model_set_texture(leela, texture("matcaps/39433A_65866E_86BF8B_BFF8D8-256px", 0)); model_set_texture(&leela, texture("matcaps/39433A_65866E_86BF8B_BFF8D8-256px", 0));
model_set_texture(mike, texture("matcaps/394641_B1A67E_75BEBE_7D7256-256px.png", 0)); model_set_texture(&mike, texture("matcaps/394641_B1A67E_75BEBE_7D7256-256px.png", 0));
model_set_texture(stan, texture("matcaps/test_steel", 0)); model_set_texture(&stan, texture("matcaps/test_steel", 0));
model_set_texture(girl, texture("matcaps/material3", 0)); model_set_texture(&girl, texture("matcaps/material3", 0));
model_set_texture(alien, texture("matcaps/material3", 0)); model_set_texture(&alien, texture("matcaps/material3", 0));
if( flag("--shaderball") ) if( flag("--shaderball") )
model_set_texture(shaderball, texture("matcaps/normals", 0)); model_set_texture(&shaderball, texture("matcaps/normals", 0));
} }
// camera // camera

View File

@ -39,7 +39,7 @@ int main()
unsigned char emissive[] = { 255, 180, 0, 255 }; unsigned char emissive[] = { 255, 180, 0, 255 };
texture_t emission = texture_create(1,1,4,emissive,TEXTURE_LINEAR); texture_t emission = texture_create(1,1,4,emissive,TEXTURE_LINEAR);
model_set_texture(litm, emission); model_set_texture(&litm, emission);
lightmap_t baker = lightmap(64, 0.01, 100, vec3(0,0,0), 2, 0.01, 0.0); lightmap_t baker = lightmap(64, 0.01, 100, vec3(0,0,0), 2, 0.01, 0.0);
lightmap_setup(&baker, 512, 512); lightmap_setup(&baker, 512, 512);

View File

@ -17687,7 +17687,7 @@ API void model_render_skeleton(model_t, mat44 model);
API void model_render_instanced(model_t, mat44 proj, mat44 view, mat44 *models, int shader, unsigned count); API void model_render_instanced(model_t, mat44 proj, mat44 view, mat44 *models, int shader, unsigned count);
API void model_render_instanced_pass(model_t m, mat44 proj, mat44 view, mat44* models, int shader, unsigned count, int pass); API void model_render_instanced_pass(model_t m, mat44 proj, mat44 view, mat44* models, int shader, unsigned count, int pass);
API void model_render_pass(model_t m, mat44 proj, mat44 view, mat44 model, int shader, int pass); API void model_render_pass(model_t m, mat44 proj, mat44 view, mat44 model, int shader, int pass);
API void model_set_texture(model_t, texture_t t); API void model_set_texture(model_t*, texture_t t);
API bool model_get_bone_pose(model_t m, unsigned joint, mat34 *out); API bool model_get_bone_pose(model_t m, unsigned joint, mat34 *out);
API void model_destroy(model_t); API void model_destroy(model_t);
@ -17878,7 +17878,7 @@ typedef struct object_t {
mat44 transform; mat44 transform;
quat rot; quat rot;
vec3 sca, pos, euler, pivot; vec3 sca, pos, euler, pivot;
array(handle) textures; array(texture_t) textures;
model_t model; model_t model;
anim_t anim; anim_t anim;
float anim_speed; float anim_speed;
@ -384813,12 +384813,14 @@ typedef struct iqm_t {
vec4 *colormaps; vec4 *colormaps;
} iqm_t; } iqm_t;
void model_set_texture(model_t m, texture_t t) { void model_set_texture(model_t *m, texture_t t) {
if(!m.iqm) return; if(!m->iqm) return;
iqm_t *q = m.iqm; iqm_t *q = m->iqm;
for( int i = 0; i < q->nummeshes; ++i) { // assume 1 texture per mesh for( int i = 0; i < q->nummeshes; ++i) { // assume 1 texture per mesh
q->textures[i] = t.id; q->textures[i] = t.id;
if (m->materials[i].layer[MATERIAL_CHANNEL_DIFFUSE].map.texture)
*m->materials[i].layer[MATERIAL_CHANNEL_DIFFUSE].map.texture = t;
} }
} }
@ -387792,7 +387794,7 @@ void object_anim(object_t *obj, anim_t anim, float speed) {
} }
void object_push_diffuse(object_t *obj, texture_t tex) { void object_push_diffuse(object_t *obj, texture_t tex) {
array_push(obj->textures, tex.id); array_push(obj->textures, tex);
} }
void object_pop_diffuse(object_t *obj) { void object_pop_diffuse(object_t *obj) {
@ -388047,13 +388049,18 @@ void scene_render(int flags) {
mat44 *views = (mat44*)(&cam->view); mat44 *views = (mat44*)(&cam->view);
// @todo: avoid heap allocs here? // @todo: avoid heap allocs here?
static array(handle) old_textures = 0; static array(handle) old_texture_ids = 0;
static array(texture_t) old_textures = 0;
int do_retexturing = model->iqm && array_count(obj->textures) > 0; int do_retexturing = model->iqm && model->shading != SHADING_PBR && array_count(obj->textures) > 0;
if( do_retexturing ) { if( do_retexturing ) {
for(int i = 0; i < model->iqm->nummeshes; ++i) { for(int i = 0; i < model->iqm->nummeshes; ++i) {
array_push(old_textures, model->iqm->textures[i]); array_push(old_texture_ids, model->iqm->textures[i]);
model->iqm->textures[i] = *array_back(obj->textures); model->iqm->textures[i] = (*array_back(obj->textures)).id;
if (model->materials[i].layer[MATERIAL_CHANNEL_DIFFUSE].map.texture) {
array_push(old_textures, *model->materials[i].layer[MATERIAL_CHANNEL_DIFFUSE].map.texture);
*model->materials[i].layer[MATERIAL_CHANNEL_DIFFUSE].map.texture = (*array_back(obj->textures));
}
} }
} }
@ -388083,8 +388090,13 @@ void scene_render(int flags) {
if( do_retexturing ) { if( do_retexturing ) {
for(int i = 0; i < model->iqm->nummeshes; ++i) { for(int i = 0; i < model->iqm->nummeshes; ++i) {
model->iqm->textures[i] = old_textures[i]; model->iqm->textures[i] = old_texture_ids[i];
if (i < array_count(old_textures)) {
if (model->materials[i].layer[MATERIAL_CHANNEL_DIFFUSE].map.texture)
*model->materials[i].layer[MATERIAL_CHANNEL_DIFFUSE].map.texture = old_textures[i];
} }
}
array_resize(old_texture_ids, 0);
array_resize(old_textures, 0); array_resize(old_textures, 0);
} }
} }

View File

@ -3173,12 +3173,14 @@ typedef struct iqm_t {
vec4 *colormaps; vec4 *colormaps;
} iqm_t; } iqm_t;
void model_set_texture(model_t m, texture_t t) { void model_set_texture(model_t *m, texture_t t) {
if(!m.iqm) return; if(!m->iqm) return;
iqm_t *q = m.iqm; iqm_t *q = m->iqm;
for( int i = 0; i < q->nummeshes; ++i) { // assume 1 texture per mesh for( int i = 0; i < q->nummeshes; ++i) { // assume 1 texture per mesh
q->textures[i] = t.id; q->textures[i] = t.id;
if (m->materials[i].layer[MATERIAL_CHANNEL_DIFFUSE].map.texture)
*m->materials[i].layer[MATERIAL_CHANNEL_DIFFUSE].map.texture = t;
} }
} }

View File

@ -719,7 +719,7 @@ API void model_render_skeleton(model_t, mat44 model);
API void model_render_instanced(model_t, mat44 proj, mat44 view, mat44 *models, int shader, unsigned count); API void model_render_instanced(model_t, mat44 proj, mat44 view, mat44 *models, int shader, unsigned count);
API void model_render_instanced_pass(model_t m, mat44 proj, mat44 view, mat44* models, int shader, unsigned count, int pass); API void model_render_instanced_pass(model_t m, mat44 proj, mat44 view, mat44* models, int shader, unsigned count, int pass);
API void model_render_pass(model_t m, mat44 proj, mat44 view, mat44 model, int shader, int pass); API void model_render_pass(model_t m, mat44 proj, mat44 view, mat44 model, int shader, int pass);
API void model_set_texture(model_t, texture_t t); API void model_set_texture(model_t*, texture_t t);
API bool model_get_bone_pose(model_t m, unsigned joint, mat34 *out); API bool model_get_bone_pose(model_t m, unsigned joint, mat34 *out);
API void model_destroy(model_t); API void model_destroy(model_t);

View File

@ -279,7 +279,7 @@ void object_anim(object_t *obj, anim_t anim, float speed) {
} }
void object_push_diffuse(object_t *obj, texture_t tex) { void object_push_diffuse(object_t *obj, texture_t tex) {
array_push(obj->textures, tex.id); array_push(obj->textures, tex);
} }
void object_pop_diffuse(object_t *obj) { void object_pop_diffuse(object_t *obj) {
@ -534,13 +534,18 @@ void scene_render(int flags) {
mat44 *views = (mat44*)(&cam->view); mat44 *views = (mat44*)(&cam->view);
// @todo: avoid heap allocs here? // @todo: avoid heap allocs here?
static array(handle) old_textures = 0; static array(handle) old_texture_ids = 0;
static array(texture_t) old_textures = 0;
int do_retexturing = model->iqm && array_count(obj->textures) > 0; int do_retexturing = model->iqm && model->shading != SHADING_PBR && array_count(obj->textures) > 0;
if( do_retexturing ) { if( do_retexturing ) {
for(int i = 0; i < model->iqm->nummeshes; ++i) { for(int i = 0; i < model->iqm->nummeshes; ++i) {
array_push(old_textures, model->iqm->textures[i]); array_push(old_texture_ids, model->iqm->textures[i]);
model->iqm->textures[i] = *array_back(obj->textures); model->iqm->textures[i] = (*array_back(obj->textures)).id;
if (model->materials[i].layer[MATERIAL_CHANNEL_DIFFUSE].map.texture) {
array_push(old_textures, *model->materials[i].layer[MATERIAL_CHANNEL_DIFFUSE].map.texture);
*model->materials[i].layer[MATERIAL_CHANNEL_DIFFUSE].map.texture = (*array_back(obj->textures));
}
} }
} }
@ -570,8 +575,13 @@ void scene_render(int flags) {
if( do_retexturing ) { if( do_retexturing ) {
for(int i = 0; i < model->iqm->nummeshes; ++i) { for(int i = 0; i < model->iqm->nummeshes; ++i) {
model->iqm->textures[i] = old_textures[i]; model->iqm->textures[i] = old_texture_ids[i];
if (i < array_count(old_textures)) {
if (model->materials[i].layer[MATERIAL_CHANNEL_DIFFUSE].map.texture)
*model->materials[i].layer[MATERIAL_CHANNEL_DIFFUSE].map.texture = old_textures[i];
} }
}
array_resize(old_texture_ids, 0);
array_resize(old_textures, 0); array_resize(old_textures, 0);
} }
} }

View File

@ -42,7 +42,7 @@ typedef struct object_t {
mat44 transform; mat44 transform;
quat rot; quat rot;
vec3 sca, pos, euler, pivot; vec3 sca, pos, euler, pivot;
array(handle) textures; array(texture_t) textures;
model_t model; model_t model;
anim_t anim; anim_t anim;
float anim_speed; float anim_speed;

View File

@ -19972,12 +19972,14 @@ typedef struct iqm_t {
vec4 *colormaps; vec4 *colormaps;
} iqm_t; } iqm_t;
void model_set_texture(model_t m, texture_t t) { void model_set_texture(model_t *m, texture_t t) {
if(!m.iqm) return; if(!m->iqm) return;
iqm_t *q = m.iqm; iqm_t *q = m->iqm;
for( int i = 0; i < q->nummeshes; ++i) { // assume 1 texture per mesh for( int i = 0; i < q->nummeshes; ++i) { // assume 1 texture per mesh
q->textures[i] = t.id; q->textures[i] = t.id;
if (m->materials[i].layer[MATERIAL_CHANNEL_DIFFUSE].map.texture)
*m->materials[i].layer[MATERIAL_CHANNEL_DIFFUSE].map.texture = t;
} }
} }
@ -22951,7 +22953,7 @@ void object_anim(object_t *obj, anim_t anim, float speed) {
} }
void object_push_diffuse(object_t *obj, texture_t tex) { void object_push_diffuse(object_t *obj, texture_t tex) {
array_push(obj->textures, tex.id); array_push(obj->textures, tex);
} }
void object_pop_diffuse(object_t *obj) { void object_pop_diffuse(object_t *obj) {
@ -23206,13 +23208,18 @@ void scene_render(int flags) {
mat44 *views = (mat44*)(&cam->view); mat44 *views = (mat44*)(&cam->view);
// @todo: avoid heap allocs here? // @todo: avoid heap allocs here?
static array(handle) old_textures = 0; static array(handle) old_texture_ids = 0;
static array(texture_t) old_textures = 0;
int do_retexturing = model->iqm && array_count(obj->textures) > 0; int do_retexturing = model->iqm && model->shading != SHADING_PBR && array_count(obj->textures) > 0;
if( do_retexturing ) { if( do_retexturing ) {
for(int i = 0; i < model->iqm->nummeshes; ++i) { for(int i = 0; i < model->iqm->nummeshes; ++i) {
array_push(old_textures, model->iqm->textures[i]); array_push(old_texture_ids, model->iqm->textures[i]);
model->iqm->textures[i] = *array_back(obj->textures); model->iqm->textures[i] = (*array_back(obj->textures)).id;
if (model->materials[i].layer[MATERIAL_CHANNEL_DIFFUSE].map.texture) {
array_push(old_textures, *model->materials[i].layer[MATERIAL_CHANNEL_DIFFUSE].map.texture);
*model->materials[i].layer[MATERIAL_CHANNEL_DIFFUSE].map.texture = (*array_back(obj->textures));
}
} }
} }
@ -23242,8 +23249,13 @@ void scene_render(int flags) {
if( do_retexturing ) { if( do_retexturing ) {
for(int i = 0; i < model->iqm->nummeshes; ++i) { for(int i = 0; i < model->iqm->nummeshes; ++i) {
model->iqm->textures[i] = old_textures[i]; model->iqm->textures[i] = old_texture_ids[i];
if (i < array_count(old_textures)) {
if (model->materials[i].layer[MATERIAL_CHANNEL_DIFFUSE].map.texture)
*model->materials[i].layer[MATERIAL_CHANNEL_DIFFUSE].map.texture = old_textures[i];
} }
}
array_resize(old_texture_ids, 0);
array_resize(old_textures, 0); array_resize(old_textures, 0);
} }
} }

View File

@ -3754,7 +3754,7 @@ API void model_render_skeleton(model_t, mat44 model);
API void model_render_instanced(model_t, mat44 proj, mat44 view, mat44 *models, int shader, unsigned count); API void model_render_instanced(model_t, mat44 proj, mat44 view, mat44 *models, int shader, unsigned count);
API void model_render_instanced_pass(model_t m, mat44 proj, mat44 view, mat44* models, int shader, unsigned count, int pass); API void model_render_instanced_pass(model_t m, mat44 proj, mat44 view, mat44* models, int shader, unsigned count, int pass);
API void model_render_pass(model_t m, mat44 proj, mat44 view, mat44 model, int shader, int pass); API void model_render_pass(model_t m, mat44 proj, mat44 view, mat44 model, int shader, int pass);
API void model_set_texture(model_t, texture_t t); API void model_set_texture(model_t*, texture_t t);
API bool model_get_bone_pose(model_t m, unsigned joint, mat34 *out); API bool model_get_bone_pose(model_t m, unsigned joint, mat34 *out);
API void model_destroy(model_t); API void model_destroy(model_t);
@ -3945,7 +3945,7 @@ typedef struct object_t {
mat44 transform; mat44 transform;
quat rot; quat rot;
vec3 sca, pos, euler, pivot; vec3 sca, pos, euler, pivot;
array(handle) textures; array(texture_t) textures;
model_t model; model_t model;
anim_t anim; anim_t anim;
float anim_speed; float anim_speed;