Fixing image leak and computeDiffuse in Wavefront shading

This commit is contained in:
mklefrancois 2020-07-09 11:30:10 +02:00
parent 2eb9b6e522
commit e56201ef1d
25 changed files with 141 additions and 77 deletions

View file

@ -338,16 +338,17 @@ void HelloVulkan::createTextureImages(const vk::CommandBuffer& cmdBuf,
o << "media/textures/" << texture;
std::string txtFile = nvh::findFile(o.str(), defaultSearchPaths);
stbi_uc* pixels =
stbi_load(txtFile.c_str(), &texWidth, &texHeight, &texChannels, STBI_rgb_alpha);
stbi_uc* stbi_pixels = stbi_load(txtFile.c_str(), &texWidth, &texHeight, &texChannels, STBI_rgb_alpha);
std::array<stbi_uc, 4> color{255u, 0u, 255u, 255u};
stbi_uc* pixels = stbi_pixels;
// Handle failure
if(!pixels)
if(!stbi_pixels)
{
texWidth = texHeight = 1;
texChannels = 4;
std::array<uint8_t, 4> color{255u, 0u, 255u, 255u};
pixels = reinterpret_cast<stbi_uc*>(color.data());
pixels = reinterpret_cast<stbi_uc*>(color.data());
}
vk::DeviceSize bufferSize = static_cast<uint64_t>(texWidth) * texHeight * sizeof(uint8_t) * 4;
@ -364,6 +365,8 @@ void HelloVulkan::createTextureImages(const vk::CommandBuffer& cmdBuf,
m_textures.push_back(texture);
}
stbi_image_free(stbi_pixels);
}
}
}

View file

@ -36,7 +36,8 @@ vec3 computeDiffuse(WaveFrontMaterial mat, vec3 lightDir, vec3 normal)
float dotNL = max(dot(normal, lightDir), 0.0);
vec3 c = mat.diffuse * dotNL;
if(mat.illum >= 1)
return c + mat.ambient;
c += mat.ambient;
return c;
}
vec3 computeSpecular(WaveFrontMaterial mat, vec3 viewDir, vec3 lightDir, vec3 normal)