Rearrange, extract, clean up.

Hopefully without unintentional changes to functionality. This renames header
files to .hpp, imposes a gltf/raw/fbx directory structure, extracts standalone
chunks of Fbx2Raw into distinct files, and undoes some particularly egregious
mistakes from when I knew even less C++ than I do now.

This is in anticipation of implementing 3ds Max's "Physical Material".
This commit is contained in:
Par Winzell 2018-08-27 15:48:33 -07:00 committed by Pär Winzell
parent b475fbead4
commit 7d36e7f4d7
3 changed files with 97 additions and 30 deletions

View File

@ -7,6 +7,8 @@
* of patent rights can be found in the PATENTS file in the same directory.
*/
#include "File_Utils.hpp"
#include <string>
#include <vector>
#include <fstream>
@ -35,7 +37,7 @@
#include <sys/stat.h>
#include "FBX2glTF.h"
#include "String_Utils.h"
#include "String_Utils.hpp"
namespace FileUtils {

View File

@ -7,50 +7,71 @@
* of patent rights can be found in the PATENTS file in the same directory.
*/
#include "Image_Utils.hpp"
#include <string>
#include <algorithm>
#define STB_IMAGE_IMPLEMENTATION
#include <stb_image.h>
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include <stb_image_write.h>
#include "Image_Utils.h"
namespace ImageUtils {
static bool imageHasTransparentPixels(FILE *f) {
int width, height, channels;
// RGBA: we have to load the pixels to figure out if the image is fully opaque
uint8_t *pixels = stbi_load_from_file(f, &width, &height, &channels, 0);
if (pixels != nullptr) {
int pixelCount = width * height;
for (int ix = 0; ix < pixelCount; ix ++) {
// test fourth byte (alpha); 255 is 1.0
if (pixels[4*ix + 3] != 255) {
return true;
static bool imageHasTransparentPixels(FILE *f)
{
int width, height, channels;
// RGBA: we have to load the pixels to figure out if the image is fully opaque
uint8_t *pixels = stbi_load_from_file(f, &width, &height, &channels, 0);
if (pixels != nullptr) {
int pixelCount = width * height;
for (int ix = 0; ix < pixelCount; ix++) {
// test fourth byte (alpha); 255 is 1.0
if (pixels[4 * ix + 3] != 255) {
return true;
}
}
}
return false;
}
return false;
}
ImageProperties GetImageProperties(char const *filePath)
{
ImageProperties result = {
1,
1,
IMAGE_OPAQUE,
};
ImageProperties GetImageProperties(char const *filePath)
{
ImageProperties result = {
1,
1,
IMAGE_OPAQUE,
};
FILE *f = fopen(filePath, "rb");
if (f == nullptr) {
FILE *f = fopen(filePath, "rb");
if (f == nullptr) {
return result;
}
int channels;
int success = stbi_info_from_file(f, &result.width, &result.height, &channels);
if (success && channels == 4 && imageHasTransparentPixels(f)) {
result.occlusion = IMAGE_TRANSPARENT;
}
return result;
}
int channels;
int success = stbi_info_from_file(f, &result.width, &result.height, &channels);
std::string suffixToMimeType(std::string suffix)
{
std::transform(suffix.begin(), suffix.end(), suffix.begin(), ::tolower);
if (success && channels == 4 && imageHasTransparentPixels(f)) {
result.occlusion = IMAGE_TRANSPARENT;
if (suffix == "jpg" || suffix == "jpeg") {
return "image/jpeg";
}
if (suffix == "png") {
return "image/png";
}
return "image/unknown";
}
return result;
}
}

View File

@ -7,7 +7,7 @@
* of patent rights can be found in the PATENTS file in the same directory.
*/
#include "String_Utils.h"
#include "String_Utils.hpp"
namespace StringUtils {
@ -16,4 +16,48 @@ namespace StringUtils {
return (s == PATH_WIN) ? PATH_UNIX : PATH_WIN;
}
const std::string GetFolderString(const std::string &path)
{
size_t s = path.rfind(PATH_WIN);
s = (s != std::string::npos) ? s : path.rfind(PATH_UNIX);
return path.substr(0, s + 1);
}
const std::string GetCleanPathString(const std::string &path, const PathSeparator separator)
{
std::string cleanPath = path;
for (size_t s = cleanPath.find(!separator, 0); s != std::string::npos; s = cleanPath.find(!separator, s)) {
cleanPath[s] = separator;
}
return cleanPath;
}
const std::string GetFileNameString(const std::string &path)
{
size_t s = path.rfind(PATH_WIN);
s = (s != std::string::npos) ? s : path.rfind(PATH_UNIX);
return path.substr(s + 1, std::string::npos);
}
const std::string GetFileBaseString(const std::string &path)
{
const std::string fileName = GetFileNameString(path);
return fileName.substr(0, fileName.rfind('.')).c_str();
}
const std::string GetFileSuffixString(const std::string &path)
{
const std::string fileName = GetFileNameString(path);
size_t pos = fileName.rfind('.');
if (pos == std::string::npos) {
return "";
}
return fileName.substr(++pos);
}
int CompareNoCase(const std::string &s1, const std::string &s2)
{
return strncasecmp(s1.c_str(), s2.c_str(), MAX_PATH_LENGTH);
}
}