diff --git a/Editor/ModelImporter_GLTF.cpp b/Editor/ModelImporter_GLTF.cpp index 79fac4bd5..a313b1fab 100644 --- a/Editor/ModelImporter_GLTF.cpp +++ b/Editor/ModelImporter_GLTF.cpp @@ -1,6 +1,7 @@ #include "stdafx.h" #include "wiSceneSystem.h" #include "ModelImporter.h" +#include "wiRandom.h" #include "Utility/stb_image.h" @@ -97,9 +98,8 @@ void RegisterTexture2D(tinygltf::Image *image) int width = image->width; int height = image->height; int channelCount = image->component; - const unsigned char* rgb = image->image.data(); - if (rgb != nullptr) + if (!image->image.empty()) { TextureDesc desc; desc.ArraySize = 1; @@ -116,7 +116,7 @@ void RegisterTexture2D(tinygltf::Image *image) SubresourceData* InitData = new SubresourceData[desc.MipLevels]; for (UINT mip = 0; mip < desc.MipLevels; ++mip) { - InitData[mip].pSysMem = rgb; + InitData[mip].pSysMem = image->image.data(); InitData[mip].SysMemPitch = static_cast(mipwidth * channelCount); mipwidth = max(1, mipwidth / 2); } @@ -131,12 +131,17 @@ void RegisterTexture2D(tinygltf::Image *image) { wiRenderer::AddDeferredMIPGen(tex); - if (image->uri.empty()) + //if (image->uri.empty()) { - static UINT imgcounter = 0; - stringstream ss(""); - ss << "gltfLoader_image" << imgcounter++; + // Always export the images, because they were interleaved to engine layout after importing from gltf: + stringstream ss; + do { + ss.str(""); + ss << "gltfLoader_image_" << wiRandom::getRandom(INT_MAX) << ".png"; + } while (wiHelper::FileExists(ss.str())); // this is to avoid overwriting an existing exported image image->uri = ss.str(); + bool success = wiHelper::saveTextureToFile(image->image, desc, ss.str()); + assert(success); } // We loaded the texture2d, so register to the resource manager to be retrieved later: diff --git a/Editor/WeatherWindow.cpp b/Editor/WeatherWindow.cpp index 6046052ce..c658953d8 100644 --- a/Editor/WeatherWindow.cpp +++ b/Editor/WeatherWindow.cpp @@ -150,7 +150,7 @@ WeatherWindow::WeatherWindow(wiGUI* gui) : GUI(gui) auto& weather = GetWeather(); weather.ambient = XMFLOAT3(0.1f, 0.1f, 0.1f); weather.horizon = XMFLOAT3(0.3f, 0.3f, 0.4f); - weather.zenith = XMFLOAT3(0.05f, 0.05f, 0.5f); + weather.zenith = XMFLOAT3(37.0f / 255.0f, 61.0f / 255.0f, 142.0f / 255.0f); weather.cloudiness = 0.4f; weather.fogStart = 100; weather.fogEnd = 1000; diff --git a/WickedEngine/wiHelper.cpp b/WickedEngine/wiHelper.cpp index 3feaa7490..f83010567 100644 --- a/WickedEngine/wiHelper.cpp +++ b/WickedEngine/wiHelper.cpp @@ -84,56 +84,67 @@ namespace wiHelper else ss << name; + bool result = saveTextureToFile(wiRenderer::GetDevice()->GetBackBuffer(), ss.str()); + assert(result); + } + + bool saveTextureToFile(wiGraphicsTypes::Texture2D& texture, const string& fileName) + { using namespace wiGraphicsTypes; GraphicsDevice* device = wiRenderer::GetDevice(); - + device->WaitForGPU(); - static Texture2D tex = device->GetBackBuffer(); - TextureDesc desc = tex.GetDesc(); + TextureDesc desc = texture.GetDesc(); UINT data_count = desc.Width * desc.Height; UINT data_stride = device->GetFormatStride(desc.Format); UINT data_size = data_count * data_stride; - static unsigned char* data = nullptr; - static Texture2D* stagingTex = nullptr; - if (stagingTex == nullptr) - { - TextureDesc staging_desc = desc; - staging_desc.Usage = USAGE_STAGING; - staging_desc.CPUAccessFlags = CPU_ACCESS_READ; - staging_desc.BindFlags = 0; - staging_desc.MiscFlags = 0; - HRESULT hr = device->CreateTexture2D(&staging_desc, nullptr, &stagingTex); - assert(SUCCEEDED(hr)); + vector data(data_size); - data = new unsigned char[data_size]; - } + Texture2D* stagingTex = nullptr; + TextureDesc staging_desc = desc; + staging_desc.Usage = USAGE_STAGING; + staging_desc.CPUAccessFlags = CPU_ACCESS_READ; + staging_desc.BindFlags = 0; + staging_desc.MiscFlags = 0; + HRESULT hr = device->CreateTexture2D(&staging_desc, nullptr, &stagingTex); + assert(SUCCEEDED(hr)); - bool download_success = device->DownloadResource(&tex, stagingTex, data, GRAPHICSTHREAD_IMMEDIATE); + bool download_success = device->DownloadResource(&texture, stagingTex, data.data(), GRAPHICSTHREAD_IMMEDIATE); assert(download_success); + SAFE_DELETE(stagingTex); + + return saveTextureToFile(data, desc, fileName); + } + + bool saveTextureToFile(const std::vector& textureData, const wiGraphicsTypes::TextureDesc& desc, const std::string& fileName) + { + using namespace wiGraphicsTypes; + + UINT data_count = desc.Width * desc.Height; if (desc.Format == FORMAT_R10G10B10A2_UNORM) { // So this should be converted first to rgba8 before saving to common format... - uint32_t* data32 = (uint32_t*)data; + uint32_t* data32 = (uint32_t*)textureData.data(); for (UINT i = 0; i < data_count; ++i) { uint32_t pixel = data32[i]; - float r = ((pixel >> 0 ) & 1023) / 1023.0f; + float r = ((pixel >> 0) & 1023) / 1023.0f; float g = ((pixel >> 10) & 1023) / 1023.0f; float b = ((pixel >> 20) & 1023) / 1023.0f; - float a = ((pixel >> 30) & 3 ) / 3.0f; - + float a = ((pixel >> 30) & 3) / 3.0f; + uint32_t rgba8 = 0; rgba8 |= (uint32_t)(r * 255.0f) << 0; rgba8 |= (uint32_t)(g * 255.0f) << 8; rgba8 |= (uint32_t)(b * 255.0f) << 16; rgba8 |= (uint32_t)(a * 255.0f) << 24; - + data32[i] = rgba8; } } @@ -142,13 +153,31 @@ namespace wiHelper assert(desc.Format == FORMAT_R8G8B8A8_UNORM); // If you need to save other backbuffer format, convert the data here yourself... } - // TODO: png would be better, but it has some problems now... + int write_result = 0; - //int write_result = stbi_write_png(ss.str().c_str(), (int)desc.Width, (int)desc.Height, 4, data, (int)data_stride); - int write_result = stbi_write_jpg(ss.str().c_str(), (int)desc.Width, (int)desc.Height, 4, data, 100); - //int write_result = stbi_write_bmp(ss.str().c_str(), (int)desc.Width, (int)desc.Height, 4, data); - assert(write_result); + string extension = wiHelper::toUpper(wiHelper::GetExtensionFromFileName(fileName)); + if (!extension.compare("JPG")) + { + write_result = stbi_write_jpg(fileName.c_str(), (int)desc.Width, (int)desc.Height, 4, textureData.data(), 100); + } + else if (!extension.compare("PNG")) + { + write_result = stbi_write_png(fileName.c_str(), (int)desc.Width, (int)desc.Height, 4, textureData.data(), 0); + } + else if (!extension.compare("TGA")) + { + write_result = stbi_write_tga(fileName.c_str(), (int)desc.Width, (int)desc.Height, 4, textureData.data()); + } + else if (!extension.compare("BMP")) + { + write_result = stbi_write_bmp(fileName.c_str(), (int)desc.Width, (int)desc.Height, 4, textureData.data()); + } + else + { + assert(0 && "Unsupported extension"); + } + return write_result != 0; } string getCurrentDateTimeAsString() @@ -176,9 +205,9 @@ namespace wiHelper return __appDir; } - static const string __originalWorkingDir = GetWorkingDirectory(); string GetOriginalWorkingDirectory() { + static const string __originalWorkingDir = GetWorkingDirectory(); return __originalWorkingDir; } @@ -301,6 +330,14 @@ namespace wiHelper filename = filename.substr(0, filename.length() - extension.length() - 1); } } + + bool FileExists(const std::string& fileName) + { + ifstream f(fileName); + bool exists = f.is_open(); + f.close(); + return exists; + } void Sleep(float milliseconds) { diff --git a/WickedEngine/wiHelper.h b/WickedEngine/wiHelper.h index 9fa74739d..fc8b9b6a4 100644 --- a/WickedEngine/wiHelper.h +++ b/WickedEngine/wiHelper.h @@ -2,6 +2,7 @@ #define WHELPER #include "CommonInclude.h" +#include "wiGraphicsDevice.h" #include #include @@ -36,6 +37,10 @@ namespace wiHelper void screenshot(const std::string& name = ""); + bool saveTextureToFile(wiGraphicsTypes::Texture2D& texture, const std::string& fileName); + + bool saveTextureToFile(const std::vector& textureData, const wiGraphicsTypes::TextureDesc& desc, const std::string& fileName); + std::string getCurrentDateTimeAsString(); std::string GetApplicationDirectory(); @@ -58,6 +63,8 @@ namespace wiHelper void RemoveExtensionFromFileName(std::string& filename); + bool FileExists(const std::string& fileName); + // Puts the current thread to sleeping state for a given time (OS can overtake) void Sleep(float milliseconds); diff --git a/WickedEngine/wiVersion.cpp b/WickedEngine/wiVersion.cpp index 080f40b0b..503f68ecc 100644 --- a/WickedEngine/wiVersion.cpp +++ b/WickedEngine/wiVersion.cpp @@ -9,7 +9,7 @@ namespace wiVersion // minor features, major updates const int minor = 24; // minor bug fixes, alterations, refactors, updates - const int revision = 27; + const int revision = 28; long GetVersion()