Skip to content

Commit

Permalink
Add optional .OBJ loading support, fix test code
Browse files Browse the repository at this point in the history
  • Loading branch information
zeux committed Sep 9, 2016
1 parent d81f7e3 commit 35eee07
Showing 1 changed file with 44 additions and 5 deletions.
49 changes: 44 additions & 5 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@
#include "pretloptimizer.hpp"
#include "vcacheanalyzer.hpp"

#define USEOBJ 0

#if USEOBJ
#define TINYOBJLOADER_IMPLEMENTATION
#include "tiny_obj_loader.h"
#endif

const size_t kCacheSize = 24;

struct Vertex
Expand Down Expand Up @@ -52,6 +59,37 @@ Mesh generatePlane(unsigned int N)
return result;
}

#if USEOBJ
Mesh readOBJ(const char* path)
{
using namespace tinyobj;
attrib_t attrib;
std::vector<shape_t> shapes;
std::vector<material_t> materials;
std::string error;
if (!LoadObj(&attrib, &shapes, &materials, &error, path))
return Mesh();

const mesh_t& om = shapes[0].mesh;

Mesh result;
result.vertices.reserve(attrib.vertices.size() / 3);

for (size_t i = 0; i < attrib.vertices.size(); i += 3)
{
Vertex v = { vec3(&attrib.vertices[i]), vec3(0,0,1) };
result.vertices.push_back(v);
}

result.indices.reserve(om.indices.size());

for (size_t i = 0; i < om.indices.size(); ++i)
result.indices.push_back(om.indices[i].vertex_index);

return result;
}
#endif

void optimizeNone(Mesh& mesh)
{
}
Expand Down Expand Up @@ -81,14 +119,11 @@ void optimizeTipsifyOverdraw(Mesh& mesh)
std::vector<unsigned int> clusters;
optimizePostTLTipsify(&result[0], &mesh.indices[0], mesh.indices.size(), mesh.vertices.size(), kCacheSize, &clusters);

const float kThreshold = 1.1f; // allow up to 10% worse ACMR to get more reordering opportunities for overdraw
const float kThreshold = 1.05f; // allow up to 5% worse ACMR to get more reordering opportunities for overdraw

optimizeOverdrawTipsify(&mesh.indices[0], &result[0], mesh.indices.size(), &mesh.vertices[0].position, sizeof(Vertex), mesh.vertices.size(), clusters, kCacheSize, kThreshold);

mesh.indices.swap(result);
}


void optimize(const Mesh& mesh, const char* name, void (*optf)(Mesh& mesh))
{
Mesh copy = mesh;
Expand All @@ -102,9 +137,13 @@ void optimize(const Mesh& mesh, const char* name, void (*optf)(Mesh& mesh))
printf("%s: ACMR %f in %f msec (%d triangles)\n", name, stats.acmr, double(end - start) / CLOCKS_PER_SEC * 1000, int(mesh.indices.size() / 3));
}

int main()
int main(int argc, char** argv)
{
#if USEOBJ
Mesh mesh = argc > 1 ? readOBJ(argv[1]) : generatePlane(1000);
#else
Mesh mesh = generatePlane(1000);
#endif

optimize(mesh, "none", optimizeNone);
optimize(mesh, "TomF", optimizeTomF);
Expand Down

0 comments on commit 35eee07

Please sign in to comment.