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

43
src/util_image.cpp

@ -8,27 +8,27 @@
#include "util_image.h" #include "util_image.h"
util_image parseSTBResult(util_image img);
util_image util_image
utilLoadImage(const char* full_path) utilLoadImagePath(const char* full_path)
{ {
LOG(Info) << "Loading Image: " << full_path << "\n"; LOG(Info) << "Loading Image: " << full_path << "\n";
util_image image; util_image image;
stbi_set_flip_vertically_on_load(1); stbi_set_flip_vertically_on_load(1);
image.pixels = stbi_load(full_path, &image.w, &image.h, &image.num_channels, 0); return parseSTBResult(image);
image.bits_per_channel = 8;
image.data_len = image.w * image.h * image.num_channels; // NOTE: assumes 8 bits per channel
if (image.pixels == 0) {
LOG(Error) << stbi_failure_reason() << "\n";
utilFreeImage(image);
return image;
} }
LOG(Info) << "Image properties, data_len: " << image.data_len util_image
<< ", width: " << image.w << ", height: " << image.h << "\n"; utilLoadImageBytes(const uint8* bytes, uint length)
{
return image; 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 void
@ -41,3 +41,20 @@ utilFreeImage(util_image image)
utilSafeFree(image.pixels); 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
if (image.pixels == 0) {
LOG(Error) << stbi_failure_reason() << "\n";
utilFreeImage(image);
return image;
}
LOG(Info) << "Image properties, data_len: " << image.data_len
<< ", width: " << image.w << ", height: " << image.h << "\n";
return image;
}

Loading…
Cancel
Save