diff --git a/src/models.c b/src/models.c index 329d8d4a8..39eee2dee 100644 --- a/src/models.c +++ b/src/models.c @@ -3370,7 +3370,7 @@ static Model LoadOBJ(const char *fileName) if (ret != TINYOBJ_SUCCESS) TRACELOG(LOG_WARNING, "MODEL: [%s] Failed to load OBJ data", fileName); else TRACELOG(LOG_INFO, "MODEL: [%s] OBJ data loaded successfully: %i meshes/%i materials", fileName, meshCount, materialCount); - model.meshCount = materialCount; // TODO: REVIEW!!! + model.meshCount = materialCount; // Init model materials array if (materialCount > 0) @@ -3389,7 +3389,7 @@ static Model LoadOBJ(const char *fileName) model.meshMaterial = (int *)RL_CALLOC(model.meshCount, sizeof(int)); // Count the faces for each material - int *matFaces = RL_CALLOC(meshCount, sizeof(int)); + int *matFaces = RL_CALLOC(materialCount, sizeof(int)); for (unsigned int mi = 0; mi < meshCount; mi++) { @@ -3434,9 +3434,32 @@ static Model LoadOBJ(const char *fileName) tinyobj_vertex_index_t idx2 = attrib.faces[3*af + 2]; // Fill vertices buffer (float) using vertex index of the face - for (int v = 0; v < 3; v++) { model.meshes[mm].vertices[vCount[mm] + v] = attrib.vertices[idx0.v_idx*3 + v]; } vCount[mm] +=3; - for (int v = 0; v < 3; v++) { model.meshes[mm].vertices[vCount[mm] + v] = attrib.vertices[idx1.v_idx*3 + v]; } vCount[mm] +=3; - for (int v = 0; v < 3; v++) { model.meshes[mm].vertices[vCount[mm] + v] = attrib.vertices[idx2.v_idx*3 + v]; } vCount[mm] +=3; + for (int v = 0; v < 3; v++) + { + if (mm > model.meshCount) + { + TRACELOG(LOG_ERROR, "mm > meshCount"); + } + + if ((vCount[mm] + v) > model.meshes[mm].vertexCount * 3) + { + TRACELOG(LOG_ERROR, "vCount > vertexCount"); + } + model.meshes[mm].vertices[vCount[mm] + v] = attrib.vertices[idx0.v_idx*3 + v]; + } + vCount[mm] +=3; + + for (int v = 0; v < 3; v++) + { + model.meshes[mm].vertices[vCount[mm] + v] = attrib.vertices[idx1.v_idx*3 + v]; + } + vCount[mm] +=3; + + for (int v = 0; v < 3; v++) + { + model.meshes[mm].vertices[vCount[mm] + v] = attrib.vertices[idx2.v_idx*3 + v]; + } + vCount[mm] +=3; if (attrib.num_texcoords > 0) { @@ -3467,11 +3490,10 @@ static Model LoadOBJ(const char *fileName) // NOTE: Uses default shader, which only supports MATERIAL_MAP_DIFFUSE model.materials[m] = LoadMaterialDefault(); - // Get default texture, in case no texture is defined - // NOTE: rlgl default texture is a 1x1 pixel UNCOMPRESSED_R8G8B8A8 - model.materials[m].maps[MATERIAL_MAP_DIFFUSE].texture = (Texture2D){ rlGetTextureIdDefault(), 1, 1, 1, PIXELFORMAT_UNCOMPRESSED_R8G8B8A8 }; + model.materials[m].maps[MATERIAL_MAP_DIFFUSE].texture = rlGetTextureDefault(); // Get default texture, in case no texture is defined if (materials[m].diffuse_texname != NULL) model.materials[m].maps[MATERIAL_MAP_DIFFUSE].texture = LoadTexture(materials[m].diffuse_texname); //char *diffuse_texname; // map_Kd + else model.materials[m].maps[MATERIAL_MAP_DIFFUSE].texture = rlGetTextureDefault(); model.materials[m].maps[MATERIAL_MAP_DIFFUSE].color = (Color){ (unsigned char)(materials[m].diffuse[0]*255.0f), (unsigned char)(materials[m].diffuse[1]*255.0f), (unsigned char)(materials[m].diffuse[2]*255.0f), 255 }; //float diffuse[3]; model.materials[m].maps[MATERIAL_MAP_DIFFUSE].value = 0.0f;