Browse Source

add path for binary texture data in util_image

testing
cinnaboot 6 years ago
parent
commit
fbeb8dbaa5
  1. 4
      include/util_image.h
  2. 16
      src/mesh.cpp
  3. 43
      src/util_image.cpp

4
include/util_image.h

@ -24,7 +24,9 @@ struct util_RGBA
real32 A;
};
util_image utilLoadImage(const char* full_path);
util_image utilLoadImagePath(const char* full_path);
util_image utilLoadImageBytes(const unsigned char* bytes, uint length);
void utilFreeImage(util_image image);

16
src/mesh.cpp

@ -152,18 +152,20 @@ loadDiffuseTexture(const aiScene* scene, aiMesh* mesh, meMeshInfo* mi)
LOG(Error) << "No diffuse texture from assimp\n";
return false;
} else {
mi->use_texture = true;
const aiTexture* tex = scene->GetEmbeddedTexture(file_name.C_Str());
if (tex != nullptr) {
LOG(Info) << "has embedded texture\n";
// TODO: load binary texture data with stb_image
LOG(Info) << "format hint: " << tex->achFormatHint << "\n";
mi->diffuse_texture = utilLoadImageBytes((const uint8*) tex->pcData, tex->mWidth);
} else {
LOG(Info) << "Loading texture file: " << file_name.C_Str() << "\n";
mi->diffuse_texture = utilLoadImage(file_name.C_Str());
mi->use_texture = true;
mi->diffuse_texture = utilLoadImagePath(file_name.C_Str());
}
if (mi->diffuse_texture.pixels == nullptr) {
LOG(Error) << "Error loading texture\n";
return false;
}
}

43
src/util_image.cpp

@ -8,14 +8,42 @@
#include "util_image.h"
util_image parseSTBResult(util_image img);
util_image
utilLoadImage(const char* full_path)
utilLoadImagePath(const char* full_path)
{
LOG(Info) << "Loading Image: " << full_path << "\n";
util_image image;
stbi_set_flip_vertically_on_load(1);
image.pixels = stbi_load(full_path, &image.w, &image.h, &image.num_channels, 0);
return parseSTBResult(image);
}
util_image
utilLoadImageBytes(const uint8* bytes, uint length)
{
LOG(Info) << "Loading binary image data\n";
util_image image;
stbi_set_flip_vertically_on_load(1);
image.pixels = stbi_load_from_memory(bytes, length, &image.w, &image.h, &image.num_channels, 0);
return parseSTBResult(image);
}
void
utilFreeImage(util_image image)
{
image.w = image.h = image.data_len = 0;
image.bits_per_channel = 8;
image.num_channels = 4;
std::memset(image.file_path, 0, std::strlen(image.file_path) + 1);
utilSafeFree(image.pixels);
}
util_image
parseSTBResult(util_image image)
{
image.bits_per_channel = 8;
image.data_len = image.w * image.h * image.num_channels; // NOTE: assumes 8 bits per channel
@ -30,14 +58,3 @@ utilLoadImage(const char* full_path)
return image;
}
void
utilFreeImage(util_image image)
{
image.w = image.h = image.data_len = 0;
image.bits_per_channel = 8;
image.num_channels = 4;
std::memset(image.file_path, 0, std::strlen(image.file_path) + 1);
utilSafeFree(image.pixels);
}

Loading…
Cancel
Save