mirror of https://github.com/bjornbytes/lovr.git
Add materialMap to ModelData;
This commit is contained in:
parent
83b2f8d745
commit
bd773d2d6c
|
@ -24,6 +24,7 @@ void lovrModelDataDestroy(void* ref) {
|
||||||
lovrRelease(TextureData, model->textures[i]);
|
lovrRelease(TextureData, model->textures[i]);
|
||||||
}
|
}
|
||||||
map_deinit(&model->animationMap);
|
map_deinit(&model->animationMap);
|
||||||
|
map_deinit(&model->materialMap);
|
||||||
map_deinit(&model->nodeMap);
|
map_deinit(&model->nodeMap);
|
||||||
free(model->data);
|
free(model->data);
|
||||||
}
|
}
|
||||||
|
@ -62,5 +63,6 @@ void lovrModelDataAllocate(ModelData* model) {
|
||||||
model->joints = (uint32_t*) (p + offset), offset += sizes[11];
|
model->joints = (uint32_t*) (p + offset), offset += sizes[11];
|
||||||
|
|
||||||
map_init(&model->animationMap);
|
map_init(&model->animationMap);
|
||||||
|
map_init(&model->materialMap);
|
||||||
map_init(&model->nodeMap);
|
map_init(&model->nodeMap);
|
||||||
}
|
}
|
||||||
|
|
|
@ -172,6 +172,8 @@ typedef struct {
|
||||||
float* inverseBindMatrices;
|
float* inverseBindMatrices;
|
||||||
} ModelSkin;
|
} ModelSkin;
|
||||||
|
|
||||||
|
typedef map_t(uint32_t) map_u32_t;
|
||||||
|
|
||||||
typedef struct ModelData {
|
typedef struct ModelData {
|
||||||
void* data;
|
void* data;
|
||||||
struct Blob** blobs;
|
struct Blob** blobs;
|
||||||
|
@ -202,8 +204,9 @@ typedef struct ModelData {
|
||||||
uint32_t childCount;
|
uint32_t childCount;
|
||||||
uint32_t jointCount;
|
uint32_t jointCount;
|
||||||
|
|
||||||
map_t(uint32_t) animationMap;
|
map_u32_t animationMap;
|
||||||
map_t(uint32_t) nodeMap;
|
map_u32_t materialMap;
|
||||||
|
map_u32_t nodeMap;
|
||||||
} ModelData;
|
} ModelData;
|
||||||
|
|
||||||
ModelData* lovrModelDataInit(ModelData* model, struct Blob* blob);
|
ModelData* lovrModelDataInit(ModelData* model, struct Blob* blob);
|
||||||
|
|
|
@ -724,6 +724,11 @@ ModelData* lovrModelDataInitGltf(ModelData* model, Blob* source) {
|
||||||
material->colors[COLOR_EMISSIVE].r = NOM_FLOAT(json, token);
|
material->colors[COLOR_EMISSIVE].r = NOM_FLOAT(json, token);
|
||||||
material->colors[COLOR_EMISSIVE].g = NOM_FLOAT(json, token);
|
material->colors[COLOR_EMISSIVE].g = NOM_FLOAT(json, token);
|
||||||
material->colors[COLOR_EMISSIVE].b = NOM_FLOAT(json, token);
|
material->colors[COLOR_EMISSIVE].b = NOM_FLOAT(json, token);
|
||||||
|
} else if (STR_EQ(key, "name")) {
|
||||||
|
gltfString name = NOM_STR(json, token);
|
||||||
|
name.data[name.length] = '\0';
|
||||||
|
map_set(&model->materialMap, name.data, model->materialCount - i);
|
||||||
|
name.data[name.length] = '"';
|
||||||
} else {
|
} else {
|
||||||
token += NOM_VALUE(json, token);
|
token += NOM_VALUE(json, token);
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,7 +11,7 @@
|
||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
int material;
|
uint32_t material;
|
||||||
int start;
|
int start;
|
||||||
int count;
|
int count;
|
||||||
} objGroup;
|
} objGroup;
|
||||||
|
@ -22,7 +22,7 @@ typedef arr_t(objGroup, 1) arr_group_t;
|
||||||
|
|
||||||
#define STARTS_WITH(a, b) !strncmp(a, b, strlen(b))
|
#define STARTS_WITH(a, b) !strncmp(a, b, strlen(b))
|
||||||
|
|
||||||
static void parseMtl(char* path, arr_texturedata_t* textures, arr_material_t* materials, map_int_t* names, char* base) {
|
static void parseMtl(char* path, arr_texturedata_t* textures, arr_material_t* materials, map_u32_t* names, char* base) {
|
||||||
size_t length = 0;
|
size_t length = 0;
|
||||||
char* data = lovrFilesystemRead(path, -1, &length);
|
char* data = lovrFilesystemRead(path, -1, &length);
|
||||||
lovrAssert(data && length > 0, "Unable to read mtl from '%s'", path);
|
lovrAssert(data && length > 0, "Unable to read mtl from '%s'", path);
|
||||||
|
@ -35,7 +35,7 @@ static void parseMtl(char* path, arr_texturedata_t* textures, arr_material_t* ma
|
||||||
char name[128];
|
char name[128];
|
||||||
bool hasName = sscanf(s + 7, "%s\n%n", name, &lineLength);
|
bool hasName = sscanf(s + 7, "%s\n%n", name, &lineLength);
|
||||||
lovrAssert(hasName, "Bad OBJ: Expected a material name");
|
lovrAssert(hasName, "Bad OBJ: Expected a material name");
|
||||||
map_set(names, name, (int) materials->length);
|
map_set(names, name, (uint32_t) materials->length);
|
||||||
arr_push(materials, ((ModelMaterial) {
|
arr_push(materials, ((ModelMaterial) {
|
||||||
.scalars[SCALAR_METALNESS] = 1.f,
|
.scalars[SCALAR_METALNESS] = 1.f,
|
||||||
.scalars[SCALAR_ROUGHNESS] = 1.f,
|
.scalars[SCALAR_ROUGHNESS] = 1.f,
|
||||||
|
@ -95,9 +95,9 @@ ModelData* lovrModelDataInitObj(ModelData* model, Blob* source) {
|
||||||
arr_group_t groups;
|
arr_group_t groups;
|
||||||
arr_texturedata_t textures;
|
arr_texturedata_t textures;
|
||||||
arr_material_t materials;
|
arr_material_t materials;
|
||||||
map_int_t materialNames;
|
|
||||||
arr_t(float, 1) vertexBlob;
|
arr_t(float, 1) vertexBlob;
|
||||||
arr_t(int, 1) indexBlob;
|
arr_t(int, 1) indexBlob;
|
||||||
|
map_u32_t materialMap;
|
||||||
map_int_t vertexMap;
|
map_int_t vertexMap;
|
||||||
arr_t(float, 1) positions;
|
arr_t(float, 1) positions;
|
||||||
arr_t(float, 1) normals;
|
arr_t(float, 1) normals;
|
||||||
|
@ -106,7 +106,7 @@ ModelData* lovrModelDataInitObj(ModelData* model, Blob* source) {
|
||||||
arr_init(&groups);
|
arr_init(&groups);
|
||||||
arr_init(&textures);
|
arr_init(&textures);
|
||||||
arr_init(&materials);
|
arr_init(&materials);
|
||||||
map_init(&materialNames);
|
map_init(&materialMap);
|
||||||
arr_init(&vertexBlob);
|
arr_init(&vertexBlob);
|
||||||
arr_init(&indexBlob);
|
arr_init(&indexBlob);
|
||||||
map_init(&vertexMap);
|
map_init(&vertexMap);
|
||||||
|
@ -189,12 +189,12 @@ ModelData* lovrModelDataInitObj(ModelData* model, Blob* source) {
|
||||||
lovrAssert(hasName, "Bad OBJ: Expected filename after mtllib");
|
lovrAssert(hasName, "Bad OBJ: Expected filename after mtllib");
|
||||||
char path[1024];
|
char path[1024];
|
||||||
snprintf(path, sizeof(path), "%s%s", base, filename);
|
snprintf(path, sizeof(path), "%s%s", base, filename);
|
||||||
parseMtl(path, &textures, &materials, &materialNames, base);
|
parseMtl(path, &textures, &materials, &materialMap, base);
|
||||||
} else if (STARTS_WITH(data, "usemtl ")) {
|
} else if (STARTS_WITH(data, "usemtl ")) {
|
||||||
char name[128];
|
char name[128];
|
||||||
bool hasName = sscanf(data + 7, "%s\n%n", name, &lineLength);
|
bool hasName = sscanf(data + 7, "%s\n%n", name, &lineLength);
|
||||||
int* material = map_get(&materialNames, name);
|
uint32_t* material = map_get(&materialMap, name);
|
||||||
lovrAssert(hasName, "Bad OBJ: Expected a material name");
|
lovrAssert(hasName, "Bad OBJ: Expected a valid material name");
|
||||||
|
|
||||||
// If the last group didn't have any faces, just reuse it, otherwise make a new group
|
// If the last group didn't have any faces, just reuse it, otherwise make a new group
|
||||||
objGroup* group = &groups.data[groups.length - 1];
|
objGroup* group = &groups.data[groups.length - 1];
|
||||||
|
@ -251,11 +251,12 @@ ModelData* lovrModelDataInitObj(ModelData* model, Blob* source) {
|
||||||
|
|
||||||
memcpy(model->textures, textures.data, model->textureCount * sizeof(TextureData*));
|
memcpy(model->textures, textures.data, model->textureCount * sizeof(TextureData*));
|
||||||
memcpy(model->materials, materials.data, model->materialCount * sizeof(ModelMaterial));
|
memcpy(model->materials, materials.data, model->materialCount * sizeof(ModelMaterial));
|
||||||
|
model->materialMap = materialMap; // Copy by value, no need to free original, questionable
|
||||||
|
|
||||||
model->attributes[0] = (ModelAttribute) {
|
model->attributes[0] = (ModelAttribute) {
|
||||||
.buffer = 0,
|
.buffer = 0,
|
||||||
.offset = 0,
|
.offset = 0,
|
||||||
.count = (uint32_t)vertexBlob.length / 8,
|
.count = (uint32_t) vertexBlob.length / 8,
|
||||||
.type = F32,
|
.type = F32,
|
||||||
.components = 3
|
.components = 3
|
||||||
};
|
};
|
||||||
|
@ -312,7 +313,6 @@ ModelData* lovrModelDataInitObj(ModelData* model, Blob* source) {
|
||||||
arr_free(&groups);
|
arr_free(&groups);
|
||||||
arr_free(&textures);
|
arr_free(&textures);
|
||||||
arr_free(&materials);
|
arr_free(&materials);
|
||||||
map_deinit(&materialNames);
|
|
||||||
map_deinit(&vertexMap);
|
map_deinit(&vertexMap);
|
||||||
arr_free(&positions);
|
arr_free(&positions);
|
||||||
arr_free(&normals);
|
arr_free(&normals);
|
||||||
|
|
Loading…
Reference in New Issue