123 lines
3.9 KiB
C++
123 lines
3.9 KiB
C++
#ifdef _WIN32
|
|
#include <windows.h>
|
|
#endif
|
|
|
|
#include "Renderer.h"
|
|
#include "core/OctreeBuilder/OctreeLoader.h"
|
|
#include "core/OctreeBuilder/OctreeBuilder.h"
|
|
#include "core/OctreeBuilder/OctreeConverter.h"
|
|
#include <regex>
|
|
#include "scene/Octree/Node.h"
|
|
#include <set>
|
|
#include <random>
|
|
|
|
// For normal error calculation
|
|
#include "inc\glm\gtx\vector_angle.hpp"
|
|
#include "scene\Material\MaterialQuantizer\NormalQuantizer.h"
|
|
|
|
// Stuff for direct file loading:
|
|
#include "core/PathHelper.h"
|
|
#include "PropertyLoader.h"
|
|
#include "scene/TextureCompressor/PaletteBlockTexture.h"
|
|
#include "scene/Material/MaterialLibrary.h"
|
|
#include "core/CollectionHelper.h"
|
|
|
|
//#include <vld.h>
|
|
|
|
#ifdef _WIN32
|
|
// Make sure NVidia GPU is used on laptops with Nvidia optimus
|
|
extern "C" {
|
|
_declspec(dllexport) DWORD NvOptimusEnablement = 0x00000001;
|
|
}
|
|
#endif
|
|
|
|
// Calculates and prints the current mean and max normal error
|
|
void calcNormalError()
|
|
{
|
|
// Settings
|
|
const unsigned samples = 100000;
|
|
const NormalQuantizer quantizer(12);
|
|
|
|
std::default_random_engine generator;
|
|
std::normal_distribution<float> distribution(0.0, 1.0);
|
|
|
|
float maxAngle = 0;
|
|
float angleSum = 0;
|
|
for (unsigned i = 0; i < samples; i++)
|
|
{
|
|
double x = distribution(generator);
|
|
float y = distribution(generator);
|
|
float z = distribution(generator);
|
|
glm::vec3 normal = glm::normalize(glm::vec3(x, y, z));
|
|
SmallNormal encoded(normal);
|
|
SmallNormal quantized = quantizer.Quantize(encoded);
|
|
glm::vec3 decoded = quantized.Get();
|
|
float angle = glm::angle(normal, decoded);
|
|
angleSum += angle;
|
|
if (angle > maxAngle) maxAngle = angle;
|
|
}
|
|
float meanAngle = angleSum / float(samples);
|
|
printf("Mean error: %f, Max error: %f\n", meanAngle * (360.0 / (2.0 * mPi)), maxAngle * (360.0 / (2.0 * mPi)));
|
|
}
|
|
|
|
void directPaletteCompression(std::string datfilename)
|
|
{
|
|
// Read the data from the file
|
|
std::ifstream file(datfilename, std::ios::binary);
|
|
unsigned16 width;
|
|
unsigned16 height;
|
|
unsigned16 depth;
|
|
Serializer<unsigned16>::Deserialize(width, file);
|
|
Serializer<unsigned16>::Deserialize(height, file);
|
|
Serializer<unsigned16>::Deserialize(depth, file);
|
|
std::vector<unsigned16> data(width * height * depth);
|
|
Serializer<unsigned16*>::Deserialize(&data[0], width * height * depth, file);
|
|
file.close();
|
|
|
|
printf("Found data of size %u, with max: %u, min: %u, and %u unique values.\n", (unsigned32)data.size(), CollectionHelper::Max(data), CollectionHelper::Max(data, std::less<unsigned16>()), (unsigned32)CollectionHelper::CountUnique(data));
|
|
|
|
std::vector<unsigned16> quantizedData;
|
|
// Quantize the values to 12 bits, remove the zeros
|
|
for (size_t i = 0; i < data.size(); i++)
|
|
//if (data[i] != 0)
|
|
quantizedData.push_back(data[i] >> 4);
|
|
printf("Quantized data has size %u, with max: %u, min: %u, and %u unique values.\n", (unsigned32)quantizedData.size(), CollectionHelper::Max(quantizedData), CollectionHelper::Max(quantizedData, std::less<unsigned16>()), (unsigned32)CollectionHelper::CountUnique(quantizedData));
|
|
data.clear();
|
|
|
|
PaletteBlockTexture<unsigned16> compressed;
|
|
compressed.SetTexture(quantizedData);
|
|
|
|
size_t size1 = compressed.GetAdditionalTexturePoolSize();
|
|
size_t size2 = compressed.GetTexturePoolSize();
|
|
printf("Requires %u + %u = %u bytes (%f MB)", size1, size2, size1 + size2, (float)(size1 + size2) / (1024.f * 1024.f));
|
|
}
|
|
|
|
int main() {
|
|
//printf("Size of node: %llu bytes\n", (unsigned64)sizeof(Node));
|
|
//calcNormalError();
|
|
|
|
//PropertyLoader::Create();
|
|
//std::string fileToDag = PropertyLoader::Instance()->GetProperty("obj_to_dag");
|
|
//if (PathHelper::GetExtension(fileToDag) == "dat")
|
|
//{
|
|
// directPaletteCompression(fileToDag);
|
|
//}
|
|
|
|
if (!OctreeLoader::VerifyPool() && !OctreeLoader::VerifyCache() && !OctreeConverter::Convert())
|
|
{
|
|
OctreeBuilder::Create();
|
|
OctreeBuilder::BuildOctree(); // Files and depth specified in shader
|
|
OctreeBuilder::Destroy();
|
|
}
|
|
|
|
Renderer::Create();
|
|
Renderer* renderer = Renderer::Instance();
|
|
|
|
if (!renderer->Initialize())
|
|
return -1;
|
|
|
|
renderer->Render();
|
|
|
|
renderer->Destroy();
|
|
return 0;
|
|
} |