From 2188c2efdf2b226cb1b97f469cc8ec5fd11ac018 Mon Sep 17 00:00:00 2001 From: Chris Date: Wed, 16 Oct 2019 21:30:23 +0100 Subject: [PATCH 1/4] fixed seg faults and leaks --- src/models.c | 32 ++++++++++++++++++++++---------- 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/src/models.c b/src/models.c index 9981c4e19..7360b4fc0 100644 --- a/src/models.c +++ b/src/models.c @@ -3341,10 +3341,11 @@ static unsigned char *DecodeBase64(char *input, int *size) return buf; } -static Texture LoadTextureFromCGLTFTextureView(cgltf_texture_view* view, Color tint, char* texPath) +//static Texture LoadTextureFromCGLTFTextureView(cgltf_texture_view* view, Color tint, char* texPath) +static Texture LoadTextureFromCGLTFTextureView(cgltf_image* image, Color tint, const char* texPath) { Texture texture = {0}; - cgltf_image *image = view->texture->image; + //cgltf_image *image = view->texture->image; if (image->uri) { @@ -3413,6 +3414,8 @@ static Texture LoadTextureFromCGLTFTextureView(cgltf_texture_view* view, Color t ImageColorTint(&rimage, tint); texture = LoadTextureFromImage(rimage); UnloadImage(rimage); + free(raw); + free(data); } else { @@ -3503,35 +3506,44 @@ static Model LoadGLTF(const char *fileName) for (int i = 0; i < model.meshCount; i++) model.meshes[i].vboId = (unsigned int *)RL_CALLOC(MAX_MESH_VBO, sizeof(unsigned int)); - //For each material + //For each material for (int i = 0; i < model.materialCount - 1; i++) { model.materials[i] = LoadMaterialDefault(); Color tint = (Color){ 1.0f, 1.0f, 1.0f, 1.0f }; const char *texPath = GetDirectoryPath(fileName); - + //Ensure material follows raylib support for PBR (metallic/roughness flow) if (data->materials[i].has_pbr_metallic_roughness) { float roughness = data->materials[i].pbr_metallic_roughness.roughness_factor; float metallic = data->materials[i].pbr_metallic_roughness.metallic_factor; - strcpy(model.materials[i].name, data->materials[i].name); + if (model.materials[i].name && data->materials[i].name) { + strcpy(model.materials[i].name, data->materials[i].name); + } + // shouldn't these be *255 ??? tint.r = (unsigned char)(data->materials[i].pbr_metallic_roughness.base_color_factor[0]*255.99f); tint.g = (unsigned char)(data->materials[i].pbr_metallic_roughness.base_color_factor[1]*255.99f); tint.b = (unsigned char)(data->materials[i].pbr_metallic_roughness.base_color_factor[2]*255.99f); tint.a = (unsigned char)(data->materials[i].pbr_metallic_roughness.base_color_factor[3]*255.99f); model.materials[i].maps[MAP_ALBEDO].texture = LoadTextureFromCGLTFTextureView(data->materials[i].pbr_metallic_roughness.base_color_texture.texture->image, tint, texPath); - + //tint isn't need for other textures.. pass null or clear? tint = (Color){ 0.0f, 0.0f, 0.0f, 0.0f }; - model.materials[i].maps[MAP_ROUGHNESS].texture = LoadTextureFromCGLTFTextureView(data->materials[i].pbr_metallic_roughness.metallic_roughness_texture.texture->image, tint, texPath); + if (data->materials[i].pbr_metallic_roughness.metallic_roughness_texture.texture) { + model.materials[i].maps[MAP_ROUGHNESS].texture = LoadTextureFromCGLTFTextureView(data->materials[i].pbr_metallic_roughness.metallic_roughness_texture.texture->image, tint, texPath); + } model.materials[i].maps[MAP_ROUGHNESS].value = roughness; model.materials[i].maps[MAP_METALNESS].value = metallic; - - model.materials[i].maps[MAP_NORMAL].texture = LoadTextureFromCGLTFTextureView(data->materials[i].normal_texture.texture->image, tint, texPath); - model.materials[i].maps[MAP_OCCLUSION].texture = LoadTextureFromCGLTFTextureView(data->materials[i].occlusion_texture.texture->image, tint, texPath); + + if (data->materials[i].normal_texture.texture) { + model.materials[i].maps[MAP_NORMAL].texture = LoadTextureFromCGLTFTextureView(data->materials[i].normal_texture.texture->image, tint, texPath); + } + if (data->materials[i].occlusion_texture.texture) { + model.materials[i].maps[MAP_OCCLUSION].texture = LoadTextureFromCGLTFTextureView(data->materials[i].occlusion_texture.texture->image, tint, texPath); + } } } From d594cbda9cf077d7d1f3edd18179cdb6138a7205 Mon Sep 17 00:00:00 2001 From: Chris Date: Thu, 17 Oct 2019 17:12:53 +0100 Subject: [PATCH 2/4] temp don't overwrite defuse colour when rendering --- src/models.c | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/src/models.c b/src/models.c index 7360b4fc0..3e51216e4 100644 --- a/src/models.c +++ b/src/models.c @@ -2382,7 +2382,7 @@ void DrawModelEx(Model model, Vector3 position, Vector3 rotationAxis, float rota for (int i = 0; i < model.meshCount; i++) { - model.materials[model.meshMaterial[i]].maps[MAP_DIFFUSE].color = tint; + //model.materials[model.meshMaterial[i]].maps[MAP_DIFFUSE].color = tint; rlDrawMesh(model.meshes[i], model.materials[model.meshMaterial[i]], model.transform); } } @@ -3523,15 +3523,20 @@ static Model LoadGLTF(const char *fileName) } // shouldn't these be *255 ??? - tint.r = (unsigned char)(data->materials[i].pbr_metallic_roughness.base_color_factor[0]*255.99f); - tint.g = (unsigned char)(data->materials[i].pbr_metallic_roughness.base_color_factor[1]*255.99f); - tint.b = (unsigned char)(data->materials[i].pbr_metallic_roughness.base_color_factor[2]*255.99f); - tint.a = (unsigned char)(data->materials[i].pbr_metallic_roughness.base_color_factor[3]*255.99f); + tint.r = (data->materials[i].pbr_metallic_roughness.base_color_factor[0]*255); + tint.g = (data->materials[i].pbr_metallic_roughness.base_color_factor[1]*255); + tint.b = (data->materials[i].pbr_metallic_roughness.base_color_factor[2]*255); + tint.a = (data->materials[i].pbr_metallic_roughness.base_color_factor[3]*255); - model.materials[i].maps[MAP_ALBEDO].texture = LoadTextureFromCGLTFTextureView(data->materials[i].pbr_metallic_roughness.base_color_texture.texture->image, tint, texPath); + model.materials[i].maps[MAP_ROUGHNESS].color = tint; + + if (data->materials[i].pbr_metallic_roughness.base_color_texture.texture) { + model.materials[i].maps[MAP_ALBEDO].texture = LoadTextureFromCGLTFTextureView(data->materials[i].pbr_metallic_roughness.base_color_texture.texture->image, tint, texPath); + } //tint isn't need for other textures.. pass null or clear? - tint = (Color){ 0.0f, 0.0f, 0.0f, 0.0f }; + //tint = (Color){ 0.0f, 0.0f, 0.0f, 0.0f }; + if (data->materials[i].pbr_metallic_roughness.metallic_roughness_texture.texture) { model.materials[i].maps[MAP_ROUGHNESS].texture = LoadTextureFromCGLTFTextureView(data->materials[i].pbr_metallic_roughness.metallic_roughness_texture.texture->image, tint, texPath); } From c79942b348920a4b129c3547105a51f9aed8f9e1 Mon Sep 17 00:00:00 2001 From: Chris Date: Thu, 17 Oct 2019 17:40:17 +0100 Subject: [PATCH 3/4] undid something dumb! --- src/models.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/models.c b/src/models.c index 3e51216e4..1c1a82b73 100644 --- a/src/models.c +++ b/src/models.c @@ -3510,7 +3510,7 @@ static Model LoadGLTF(const char *fileName) for (int i = 0; i < model.materialCount - 1; i++) { model.materials[i] = LoadMaterialDefault(); - Color tint = (Color){ 1.0f, 1.0f, 1.0f, 1.0f }; + Color tint = (Color){ 255, 255, 255, 255 }; const char *texPath = GetDirectoryPath(fileName); //Ensure material follows raylib support for PBR (metallic/roughness flow) @@ -3523,10 +3523,10 @@ static Model LoadGLTF(const char *fileName) } // shouldn't these be *255 ??? - tint.r = (data->materials[i].pbr_metallic_roughness.base_color_factor[0]*255); - tint.g = (data->materials[i].pbr_metallic_roughness.base_color_factor[1]*255); - tint.b = (data->materials[i].pbr_metallic_roughness.base_color_factor[2]*255); - tint.a = (data->materials[i].pbr_metallic_roughness.base_color_factor[3]*255); + tint.r = (unsigned char)(data->materials[i].pbr_metallic_roughness.base_color_factor[0]*255); + tint.g = (unsigned char)(data->materials[i].pbr_metallic_roughness.base_color_factor[1]*255); + tint.b = (unsigned char)(data->materials[i].pbr_metallic_roughness.base_color_factor[2]*255); + tint.a = (unsigned char)(data->materials[i].pbr_metallic_roughness.base_color_factor[3]*255); model.materials[i].maps[MAP_ROUGHNESS].color = tint; From c8f43d4f453f3c6daa8ef02cc284c3d1da001a50 Mon Sep 17 00:00:00 2001 From: Chris Date: Fri, 18 Oct 2019 07:57:05 +0100 Subject: [PATCH 4/4] correctly mixed diffuse map color when rendering to preserve not overwrite it --- src/models.c | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/models.c b/src/models.c index 1c1a82b73..9a8404c38 100644 --- a/src/models.c +++ b/src/models.c @@ -2382,8 +2382,15 @@ void DrawModelEx(Model model, Vector3 position, Vector3 rotationAxis, float rota for (int i = 0; i < model.meshCount; i++) { - //model.materials[model.meshMaterial[i]].maps[MAP_DIFFUSE].color = tint; + Color c = model.materials[model.meshMaterial[i]].maps[MAP_DIFFUSE].color; + Color s = c; + c.r = ((c.r/255) * (tint.r/255)) * 255; + c.g = ((c.g/255) * (tint.g/255)) * 255; + c.b = ((c.b/255) * (tint.b/255)) * 255; + c.a = ((c.a/255) * (tint.a/255)) * 255; + model.materials[model.meshMaterial[i]].maps[MAP_DIFFUSE].color = c; rlDrawMesh(model.meshes[i], model.materials[model.meshMaterial[i]], model.transform); + model.materials[model.meshMaterial[i]].maps[MAP_DIFFUSE].color = s; } } @@ -3534,8 +3541,8 @@ static Model LoadGLTF(const char *fileName) model.materials[i].maps[MAP_ALBEDO].texture = LoadTextureFromCGLTFTextureView(data->materials[i].pbr_metallic_roughness.base_color_texture.texture->image, tint, texPath); } - //tint isn't need for other textures.. pass null or clear? - //tint = (Color){ 0.0f, 0.0f, 0.0f, 0.0f }; + //tint isn't need for other textures.. pass null or clear? (try full white because of mixing (multiplying * white has no effect)) + tint = (Color){ 255, 255, 255, 255 }; if (data->materials[i].pbr_metallic_roughness.metallic_roughness_texture.texture) { model.materials[i].maps[MAP_ROUGHNESS].texture = LoadTextureFromCGLTFTextureView(data->materials[i].pbr_metallic_roughness.metallic_roughness_texture.texture->image, tint, texPath);