mirror of https://github.com/bjornbytes/lovr.git
Improve Model texture loading;
- Only loads textures used by materials - Creates color/glow textures as sRGB
This commit is contained in:
parent
c2dd7281cc
commit
1df537294a
|
@ -2288,39 +2288,52 @@ Model* lovrModelCreate(const ModelInfo* info) {
|
||||||
model->info = *info;
|
model->info = *info;
|
||||||
lovrRetain(info->data);
|
lovrRetain(info->data);
|
||||||
|
|
||||||
// Textures
|
// Materials and Textures
|
||||||
model->textures = malloc(data->imageCount * sizeof(Texture*));
|
model->textures = calloc(data->imageCount, sizeof(Texture*));
|
||||||
lovrAssert(model->textures, "Out of memory");
|
|
||||||
for (uint32_t i = 0; i < data->imageCount; i++) {
|
|
||||||
model->textures[i] = lovrTextureCreate(&(TextureInfo) {
|
|
||||||
.type = TEXTURE_2D,
|
|
||||||
.usage = TEXTURE_SAMPLE,
|
|
||||||
.format = lovrImageGetFormat(data->images[i]),
|
|
||||||
.width = lovrImageGetWidth(data->images[i], 0),
|
|
||||||
.height = lovrImageGetHeight(data->images[i], 0),
|
|
||||||
.depth = 1,
|
|
||||||
.mipmaps = info->mipmaps || lovrImageGetLevelCount(data->images[i]) > 1 ? ~0u : 1,
|
|
||||||
.samples = 1,
|
|
||||||
.srgb = lovrImageIsSRGB(data->images[i]),
|
|
||||||
.images = &data->images[i],
|
|
||||||
.imageCount = 1
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
// Materials
|
|
||||||
model->materials = malloc(data->materialCount * sizeof(Material*));
|
model->materials = malloc(data->materialCount * sizeof(Material*));
|
||||||
lovrAssert(model->materials, "Out of memory");
|
lovrAssert(model->textures && model->materials, "Out of memory");
|
||||||
for (uint32_t i = 0; i < data->materialCount; i++) {
|
for (uint32_t i = 0; i < data->materialCount; i++) {
|
||||||
MaterialInfo material;
|
MaterialInfo material;
|
||||||
ModelMaterial* properties = &data->materials[i];
|
ModelMaterial* properties = &data->materials[i];
|
||||||
memcpy(&material.data, properties, sizeof(MaterialData));
|
memcpy(&material.data, properties, sizeof(MaterialData));
|
||||||
material.texture = properties->texture == ~0u ? NULL : model->textures[properties->texture];
|
|
||||||
material.glowTexture = properties->glowTexture == ~0u ? NULL : model->textures[properties->glowTexture];
|
struct { uint32_t index; Texture** texture; } textures[] = {
|
||||||
material.occlusionTexture = properties->occlusionTexture == ~0u ? NULL : model->textures[properties->occlusionTexture];
|
{ properties->texture, &material.texture },
|
||||||
material.metalnessTexture = properties->metalnessTexture == ~0u ? NULL : model->textures[properties->metalnessTexture];
|
{ properties->glowTexture, &material.glowTexture },
|
||||||
material.roughnessTexture = properties->roughnessTexture == ~0u ? NULL : model->textures[properties->roughnessTexture];
|
{ properties->occlusionTexture, &material.occlusionTexture },
|
||||||
material.clearcoatTexture = properties->clearcoatTexture == ~0u ? NULL : model->textures[properties->clearcoatTexture];
|
{ properties->metalnessTexture, &material.metalnessTexture },
|
||||||
material.normalTexture = properties->normalTexture == ~0u ? NULL : model->textures[properties->normalTexture];
|
{ properties->roughnessTexture, &material.roughnessTexture },
|
||||||
|
{ properties->clearcoatTexture, &material.clearcoatTexture },
|
||||||
|
{ properties->normalTexture, &material.normalTexture }
|
||||||
|
};
|
||||||
|
|
||||||
|
for (uint32_t t = 0; t < COUNTOF(textures); t++) {
|
||||||
|
uint32_t index = textures[t].index;
|
||||||
|
Texture** texture = textures[t].texture;
|
||||||
|
|
||||||
|
if (index == ~0u) {
|
||||||
|
*texture = NULL;
|
||||||
|
} else {
|
||||||
|
if (!model->textures[index]) {
|
||||||
|
model->textures[index] = lovrTextureCreate(&(TextureInfo) {
|
||||||
|
.type = TEXTURE_2D,
|
||||||
|
.usage = TEXTURE_SAMPLE,
|
||||||
|
.format = lovrImageGetFormat(data->images[index]),
|
||||||
|
.width = lovrImageGetWidth(data->images[index], 0),
|
||||||
|
.height = lovrImageGetHeight(data->images[index], 0),
|
||||||
|
.depth = 1,
|
||||||
|
.mipmaps = info->mipmaps || lovrImageGetLevelCount(data->images[index]) > 1 ? ~0u : 1,
|
||||||
|
.samples = 1,
|
||||||
|
.srgb = texture == &material.texture || texture == &material.glowTexture,
|
||||||
|
.images = &data->images[index],
|
||||||
|
.imageCount = 1
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
*texture = model->textures[index];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
model->materials[i] = lovrMaterialCreate(&material);
|
model->materials[i] = lovrMaterialCreate(&material);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue