Update utf82utf16.

pull/4986/head
Kim Kulling 2023-03-06 19:56:11 +01:00
parent 7916f87404
commit c089f11768
3 changed files with 64 additions and 111 deletions

View File

@ -57,6 +57,12 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <string.h> #include <string.h>
#include <sys/types.h> #include <sys/types.h>
#ifdef ASSIMP_USE_HUNTER
#include <utf8.h>
#else
#include "../contrib/utf8cpp/source/utf8.h"
#endif
// Our compile configuration // Our compile configuration
#include <assimp/defs.h> #include <assimp/defs.h>

View File

@ -13,6 +13,8 @@
// Written by IAS. :) // Written by IAS. :)
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
#include <assimp/types.h>
#include <Windows.h> #include <Windows.h>
#include <shellapi.h> #include <shellapi.h>
#include <stdexcept> #include <stdexcept>
@ -21,11 +23,7 @@
#include <dxgi1_2.h> #include <dxgi1_2.h>
#include <DirectXMath.h> #include <DirectXMath.h>
#include <d3dcompiler.h> #include <d3dcompiler.h>
#ifdef ASSIMP_USE_HUNTER
#include <utf8.h>
#else
#include "../contrib/utf8cpp/source/utf8.h"
#endif
#include "ModelLoader.h" #include "ModelLoader.h"
#include "SafeRelease.hpp" #include "SafeRelease.hpp"
@ -53,10 +51,10 @@ struct ConstantBuffer {
// ------------------------------------------------------------ // ------------------------------------------------------------
// Window Variables // Window Variables
// ------------------------------------------------------------ // ------------------------------------------------------------
#define SCREEN_WIDTH 800 static constexpr uint32_t SCREEN_WIDTH = 800;
#define SCREEN_HEIGHT 600 static constexpr uint32_t SCREEN_HEIGHT = 600;
const char g_szClassName[] = "directxWindowClass"; constexpr char g_szClassName[] = "directxWindowClass";
static std::string g_ModelPath; static std::string g_ModelPath;

View File

@ -41,15 +41,14 @@
#include <assimp/scene.h> #include <assimp/scene.h>
#include <assimp/DefaultLogger.hpp> #include <assimp/DefaultLogger.hpp>
#include <assimp/LogStream.hpp> #include <assimp/LogStream.hpp>
#include "UTFConverter.h"
// The default hard-coded path. Can be overridden by supplying a path through the command line. // The default hard-coded path. Can be overridden by supplying a path through the command line.
static std::string modelpath = "../../test/models/OBJ/spider.obj"; static std::string modelpath = "../../test/models/OBJ/spider.obj";
HGLRC hRC=nullptr; // Permanent Rendering Context HGLRC hRC = nullptr; // Permanent Rendering Context
HDC hDC=nullptr; // Private GDI Device Context HDC hDC = nullptr; // Private GDI Device Context
HWND g_hWnd=nullptr; // Holds Window Handle HWND g_hWnd = nullptr; // Holds Window Handle
HINSTANCE g_hInstance=nullptr; // Holds The Instance Of The Application HINSTANCE g_hInstance = nullptr; // Holds The Instance Of The Application
bool keys[256]; // Array used for Keyboard Routine; bool keys[256]; // Array used for Keyboard Routine;
bool active=TRUE; // Window Active Flag Set To TRUE by Default bool active=TRUE; // Window Active Flag Set To TRUE by Default
@ -69,8 +68,6 @@ GLfloat LightAmbient[]= { 0.5f, 0.5f, 0.5f, 1.0f };
GLfloat LightDiffuse[]= { 1.0f, 1.0f, 1.0f, 1.0f }; GLfloat LightDiffuse[]= { 1.0f, 1.0f, 1.0f, 1.0f };
GLfloat LightPosition[]= { 0.0f, 0.0f, 15.0f, 1.0f }; GLfloat LightPosition[]= { 0.0f, 0.0f, 15.0f, 1.0f };
// the global Assimp scene object // the global Assimp scene object
const aiScene* g_scene = nullptr; const aiScene* g_scene = nullptr;
GLuint scene_list = 0; GLuint scene_list = 0;
@ -83,12 +80,8 @@ GLuint* textureIds; // pointer to texture Array
// Create an instance of the Importer class // Create an instance of the Importer class
Assimp::Importer importer; Assimp::Importer importer;
using namespace AssimpSamples::SharedCode; void createAILogger() {
// Change this line to normal if you not want to analyze the import process
void createAILogger()
{
// Change this line to normal if you not want to analyse the import process
//Assimp::Logger::LogSeverity severity = Assimp::Logger::NORMAL;
Assimp::Logger::LogSeverity severity = Assimp::Logger::VERBOSE; Assimp::Logger::LogSeverity severity = Assimp::Logger::VERBOSE;
// Create a logger instance for Console Output // Create a logger instance for Console Output
@ -101,62 +94,52 @@ void createAILogger()
Assimp::DefaultLogger::get()->info("this is my info-call"); Assimp::DefaultLogger::get()->info("this is my info-call");
} }
void destroyAILogger() void destroyAILogger() {
{
// Kill it after the work is done
Assimp::DefaultLogger::kill(); Assimp::DefaultLogger::kill();
} }
void logInfo(std::string logString) void logInfo(const std::string &logString) {
{
// Will add message to File with "info" Tag
Assimp::DefaultLogger::get()->info(logString.c_str()); Assimp::DefaultLogger::get()->info(logString.c_str());
} }
void logDebug(const char* logString) void logDebug(const char* logString) {
{
// Will add message to File with "debug" Tag
Assimp::DefaultLogger::get()->debug(logString); Assimp::DefaultLogger::get()->debug(logString);
} }
bool Import3DFromFile( const std::string& pFile) bool Import3DFromFile( const std::string &filename) {
{
// Check if file exists // Check if file exists
std::ifstream fin(pFile.c_str()); std::ifstream fin(filename.c_str());
if(!fin.fail()) if(fin.fail()) {
{ std::string message = "Couldn't open file: " + filename;
fin.close(); std::wstring targetMessage;
} //utf8::utf8to16(message.c_str(), message.c_str() + message.size(), targetMessage);
else ::MessageBox(nullptr, targetMessage.c_str(), L"Error", MB_OK | MB_ICONEXCLAMATION);
{ logInfo(importer.GetErrorString());
MessageBox(nullptr, UTFConverter("Couldn't open file: " + pFile).c_wstr() , TEXT("ERROR"), MB_OK | MB_ICONEXCLAMATION);
logInfo( importer.GetErrorString());
return false; return false;
} }
g_scene = importer.ReadFile(pFile, aiProcessPreset_TargetRealtime_Quality); fin.close();
g_scene = importer.ReadFile(filename, aiProcessPreset_TargetRealtime_Quality);
// If the import failed, report it // If the import failed, report it
if(!g_scene) if (g_scene == nullptr) {
{
logInfo( importer.GetErrorString()); logInfo( importer.GetErrorString());
return false; return false;
} }
// Now we can access the file's contents. // Now we can access the file's contents.
logInfo("Import of scene " + pFile + " succeeded."); logInfo("Import of scene " + filename + " succeeded.");
// We're done. Everything will be cleaned up by the importer destructor // We're done. Everything will be cleaned up by the importer destructor
return true; return true;
} }
// Resize And Initialize The GL Window // Resize And Initialize The GL Window
void ReSizeGLScene(GLsizei width, GLsizei height) void ReSizeGLScene(GLsizei width, GLsizei height) {
{
// Prevent A Divide By Zero By // Prevent A Divide By Zero By
if (height==0) if (height == 0) {
{
// Making Height Equal One // Making Height Equal One
height=1; height=1;
} }
@ -174,43 +157,26 @@ void ReSizeGLScene(GLsizei width, GLsizei height)
} }
std::string getBasePath(const std::string& path) std::string getBasePath(const std::string& path) {
{
size_t pos = path.find_last_of("\\/"); size_t pos = path.find_last_of("\\/");
return (std::string::npos == pos) ? "" : path.substr(0, pos + 1); return (std::string::npos == pos) ? "" : path.substr(0, pos + 1);
} }
void freeTextureIds() void freeTextureIds() {
{ // no need to delete pointers in it manually here. (Pointers point to textureIds deleted in next step)
textureIdMap.clear(); //no need to delete pointers in it manually here. (Pointers point to textureIds deleted in next step) textureIdMap.clear();
if (textureIds) if (textureIds) {
{
delete[] textureIds; delete[] textureIds;
textureIds = nullptr; textureIds = nullptr;
} }
} }
int LoadGLTextures(const aiScene* scene) int LoadGLTextures(const aiScene* scene) {
{
freeTextureIds(); freeTextureIds();
//ILboolean success; if (scene->HasTextures())
return 1;
/* Before calling ilInit() version should be checked. */
/*if (ilGetInteger(IL_VERSION_NUM) < IL_VERSION)
{
/// wrong DevIL version ///
std::string err_msg = "Wrong DevIL version. Old devil.dll in system32/SysWow64?";
char* cErr_msg = (char *) err_msg.c_str();
abortGLInit(cErr_msg);
return -1;
}*/
//ilInit(); /* Initialization of DevIL */
if (scene->HasTextures()) return 1;
//abortGLInit("Support for meshes with embedded textures is not implemented");
/* getTexture Filenames and Numb of Textures */ /* getTexture Filenames and Numb of Textures */
for (unsigned int m=0; m<scene->mNumMaterials; m++) for (unsigned int m=0; m<scene->mNumMaterials; m++)
@ -230,14 +196,6 @@ int LoadGLTextures(const aiScene* scene)
const size_t numTextures = textureIdMap.size(); const size_t numTextures = textureIdMap.size();
/* array with DevIL image IDs */
//ILuint* imageIds = NULL;
// imageIds = new ILuint[numTextures];
/* generate DevIL Image IDs */
// ilGenImages(numTextures, imageIds); /* Generation of numTextures image names */
/* create and fill array with GL texture ids */ /* create and fill array with GL texture ids */
textureIds = new GLuint[numTextures]; textureIds = new GLuint[numTextures];
glGenTextures(static_cast<GLsizei>(numTextures), textureIds); /* Texture name generation */ glGenTextures(static_cast<GLsizei>(numTextures), textureIds); /* Texture name generation */
@ -248,29 +206,17 @@ int LoadGLTextures(const aiScene* scene)
std::string basepath = getBasePath(modelpath); std::string basepath = getBasePath(modelpath);
for (size_t i=0; i<numTextures; i++) for (size_t i=0; i<numTextures; i++)
{ {
//save IL image ID
std::string filename = (*itr).first; // get filename std::string filename = (*itr).first; // get filename
(*itr).second = &textureIds[i]; // save texture id for filename in map (*itr).second = &textureIds[i]; // save texture id for filename in map
itr++; // next texture itr++; // next texture
//ilBindImage(imageIds[i]); /* Binding of DevIL image name */
std::string fileloc = basepath + filename; /* Loading of image */ std::string fileloc = basepath + filename; /* Loading of image */
//success = ilLoadImage(fileloc.c_str());
int x, y, n; int x, y, n;
unsigned char *data = stbi_load(fileloc.c_str(), &x, &y, &n, STBI_rgb_alpha); unsigned char *data = stbi_load(fileloc.c_str(), &x, &y, &n, STBI_rgb_alpha);
if (nullptr != data ) if (nullptr != data )
{ {
// Convert every colour component into unsigned byte.If your image contains
// alpha channel you can replace IL_RGB with IL_RGBA
//success = ilConvertImage(IL_RGB, IL_UNSIGNED_BYTE);
/*if (!success)
{
abortGLInit("Couldn't convert image");
return -1;
}*/
// Binding of texture name // Binding of texture name
glBindTexture(GL_TEXTURE_2D, textureIds[i]); glBindTexture(GL_TEXTURE_2D, textureIds[i]);
// redefine standard texture values // redefine standard texture values
@ -291,15 +237,12 @@ int LoadGLTextures(const aiScene* scene)
else else
{ {
/* Error occurred */ /* Error occurred */
MessageBox(nullptr, UTFConverter("Couldn't load Image: " + fileloc).c_wstr(), TEXT("ERROR"), MB_OK | MB_ICONEXCLAMATION); const std::string message = "Couldn't load Image: " + fileloc;
std::wstring targetMessage;
utf8::utf8to16(message.c_str(), message.c_str() + message.size(), back_inserter(targetMessage));
MessageBox(nullptr, targetMessage.c_str(), TEXT("ERROR"), MB_OK | MB_ICONEXCLAMATION);
} }
} }
// Because we have already copied image data into texture data we can release memory used by image.
// ilDeleteImages(numTextures, imageIds);
// Cleanup
//delete [] imageIds;
//imageIds = NULL;
return TRUE; return TRUE;
} }
@ -312,7 +255,6 @@ int InitGL()
return FALSE; return FALSE;
} }
glEnable(GL_TEXTURE_2D); glEnable(GL_TEXTURE_2D);
glShadeModel(GL_SMOOTH); // Enables Smooth Shading glShadeModel(GL_SMOOTH); // Enables Smooth Shading
glClearColor(1.0f, 1.0f, 1.0f, 0.0f); glClearColor(1.0f, 1.0f, 1.0f, 0.0f);
@ -537,9 +479,7 @@ int DrawGLScene() //Here's where we do all the drawing
drawAiScene(g_scene); drawAiScene(g_scene);
//xrot+=0.3f; yrot += 0.2f;
yrot+=0.2f;
//zrot+=0.4f;
return TRUE; // okay return TRUE; // okay
} }
@ -591,8 +531,13 @@ void KillGLWindow() // Properly Kill The Window
GLboolean abortGLInit(const char* abortMessage) GLboolean abortGLInit(const char* abortMessage)
{ {
KillGLWindow(); // Reset Display // Reset Display
MessageBox(nullptr, UTFConverter(abortMessage).c_wstr(), TEXT("ERROR"), MB_OK|MB_ICONEXCLAMATION); KillGLWindow();
const std::string message = abortMessage;
std::wstring targetMessage;
utf8::utf8to16(message.c_str(), message.c_str() + message.size(), back_inserter(targetMessage));
MessageBox(nullptr, targetMessage.c_str(), TEXT("ERROR"), MB_OK|MB_ICONEXCLAMATION);
return FALSE; // quit and return False return FALSE; // quit and return False
} }
@ -669,9 +614,13 @@ BOOL CreateGLWindow(const char* title, int width, int height, int bits, bool ful
AdjustWindowRectEx(&WindowRect, dwStyle, FALSE, dwExStyle); // Adjust Window To True Requested Size AdjustWindowRectEx(&WindowRect, dwStyle, FALSE, dwExStyle); // Adjust Window To True Requested Size
const std::string message = title;
std::wstring targetMessage;
utf8::utf8to16(message.c_str(), message.c_str() + message.size(), back_inserter(targetMessage));
if (nullptr == (g_hWnd=CreateWindowEx(dwExStyle, // Extended Style For The Window if (nullptr == (g_hWnd=CreateWindowEx(dwExStyle, // Extended Style For The Window
TEXT("OpenGL"), // Class Name TEXT("OpenGL"), // Class Name
UTFConverter(title).c_wstr(), // Window Title targetMessage.c_str(), // Window Title
WS_CLIPSIBLINGS | // Required Window Style WS_CLIPSIBLINGS | // Required Window Style
WS_CLIPCHILDREN | // Required Window Style WS_CLIPCHILDREN | // Required Window Style
dwStyle, // Selected WIndow Style dwStyle, // Selected WIndow Style
@ -843,7 +792,7 @@ int WINAPI WinMain( HINSTANCE /*hInstance*/, // The instance
if (argv != nullptr && argc > 1) if (argv != nullptr && argc > 1)
{ {
std::wstring modelpathW(argv[1]); std::wstring modelpathW(argv[1]);
modelpath = UTFConverter(modelpathW).str(); utf8::utf16to8(modelpathW.c_str(), modelpathW.c_str() + modelpathW.size(), back_inserter(modelpath));
} }
if (!Import3DFromFile(modelpath)) if (!Import3DFromFile(modelpath))