Skip to content

Commit

Permalink
Merge pull request #317 from patriciogonzalezvivo/sdf_lod
Browse files Browse the repository at this point in the history
faster SDF generation from model using incremental LOD (with mesh color support)
  • Loading branch information
patriciogonzalezvivo authored Dec 26, 2022
2 parents 407851c + d052219 commit 4ee93c1
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 22 deletions.
1 change: 1 addition & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
[submodule "deps/vera"]
path = deps/vera
url = https://github.com/patriciogonzalezvivo/vera.git
# url = [email protected]:patriciogonzalezvivo/vera.git
Binary file added assets/glslViewer_inv.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
94 changes: 76 additions & 18 deletions src/sandbox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@

#include "vera/window.h"


#include "tools/job.h"
#include "tools/text.h"
#include "tools/record.h"
#include "tools/console.h"

#include "vera/ops/fs.h"
#include "vera/ops/draw.h"
#include "vera/ops/geom.h"
#include "vera/ops/math.h"
#include "vera/ops/image.h"
#include "vera/ops/pixel.h"
Expand Down Expand Up @@ -956,28 +958,83 @@ void Sandbox::commandsInit(CommandList &_commands ) {
if (geom_index != -1) {
std::vector<std::string> values = vera::split(_line,',');
float padding = 0.01f;
int resolution = 6;

if (values.size() > 1)
padding = vera::toFloat(values[1]);

if (values.size() > 2)
resolution = vera::toInt(values[2]);

int cells_per_side = std::sqrt(std::pow(2, resolution));

for (vera::ModelsMap::iterator it = uniforms.models.begin(); it != uniforms.models.end(); ++it) {
std::string name = "u_" + it->first + "Sdf";
uniforms.loadQueue[ name ] = vera::toSdf( it->second->mesh, padding, resolution);
vera::BoundingBox bbox = it->second->getBoundingBox();
float area = bbox.getArea();
glm::vec3 bdiagonal = bbox.getDiagonal();
float max_dist = glm::length(bdiagonal);
bbox.expand( (max_dist*max_dist) * padding );
it->second->addDefine("MODEL_SDF_TEXTURE", name );
it->second->addDefine("MODEL_SDF_SCALE", vera::toString(2.0f-bbox.getArea()/area) );
addDefine("MODEL_SDF_TEXTURE", name );

clock_t start, end;
start = clock();

vera::Mesh mesh = it->second->mesh;
mesh.setMaterial(it->second->mesh.getMaterial());
vera::center(mesh);

std::vector<vera::Triangle> tris = mesh.getTriangles();
vera::BVH acc(tris, vera::SPLIT_MIDPOINT );
acc.square();

// // Calculate scale difference between model and SDF
// vera::BoundingBox bbox = it->second->getBoundingBox();
// float area = bbox.getArea();
// glm::vec3 bdiagonal = bbox.getDiagonal();
// float max_dist = glm::length(bdiagonal);
// bbox.expand( (max_dist*max_dist) * padding );
// it->second->addDefine("MODEL_SDF_SCALE", vera::toString(2.0f-bbox.getArea()/area) );

int voxel_resolution_lod_0 = std::pow(2, 2);
glm::vec3 bdiagonal = acc.getDiagonal();
float max_dist = glm::length(bdiagonal);
acc.expand( (max_dist*max_dist) * padding );

std::vector<vera::Image> current_lod;
for (int z = 0; z < voxel_resolution_lod_0; z++)
current_lod.push_back( vera::toSdfLayer( &acc, voxel_resolution_lod_0, z) );

uniforms.loadMutex.lock();
uniforms.loadQueue[ name ] = packSprite(current_lod);
uniforms.loadMutex.unlock();
addDefine("MODEL_SDF_VOXEL_RESOLUTION", vera::toString(voxel_resolution_lod_0) + ".0" );
addDefine("MODEL_SDF_TEXTURE_RESOLUTION", "vec2(" + vera::toString( uniforms.loadQueue[ name ].getWidth() ) + ".0)" );

size_t lod_total = 4;
for (int l = 1; l < lod_total; l++) {
std::vector<vera::Image> new_lod = vera::scaleSprite(current_lod, 4);
uniforms.loadMutex.lock();
uniforms.loadQueue[ name ] = vera::packSprite(new_lod);
uniforms.loadMutex.unlock();
addDefine("MODEL_SDF_VOXEL_RESOLUTION", vera::toString(new_lod.size()) + ".0" );
addDefine("MODEL_SDF_TEXTURE_RESOLUTION", "vec2(" + vera::toString( uniforms.loadQueue[ name ].getWidth() ) + ".0)" );

if (l < (lod_total-1)) {
for (int z = 0; z < new_lod.size(); z++) {
new_lod[z] = vera::toSdfLayer( &acc, new_lod.size(), z);
console_draw_pct( float(z) / float(new_lod.size() - 1) );
uniforms.loadMutex.lock();
uniforms.loadQueue[ name ] = vera::packSprite(new_lod);
uniforms.loadMutex.unlock();
}
}

current_lod = new_lod;
}

// float pct = 1.0;
// for (size_t i = 0; i < 500; i++) {
// refineSdfLayers(&acc, current_lod, pct);
// uniforms.loadMutex.lock();
// uniforms.loadQueue[ name ] = vera::packSprite(current_lod);
// uniforms.loadMutex.unlock();
// std::cout << "Refine " << i << " at " << pct <<std::endl;
// pct *= 0.95;
// }

end = clock();
double duration_sec = double(end-start)/CLOCKS_PER_SEC;
std::cout << "Took " << duration_sec << "secs" << std::endl;
}
addDefine("SDF_CELLS_PER_SIDE", vera::toString(cells_per_side) );

return true;
}
Expand Down Expand Up @@ -1529,10 +1586,11 @@ void Sandbox::renderPrep() {
if (m_initialized) {
uniforms.update();
if (uniforms.loadQueue.size() > 0) {
for (ImagesMap::iterator it = uniforms.loadQueue.begin(); it != uniforms.loadQueue.end(); ++it) {
uniforms.loadMutex.lock();
for (ImagesMap::iterator it = uniforms.loadQueue.begin(); it != uniforms.loadQueue.end(); ++it)
uniforms.addTexture( it->first, it->second );
}
uniforms.loadQueue.clear();
uniforms.loadMutex.unlock();
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/sceneRender.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,15 @@ void SceneRender::commandsInit(CommandList& _commands, Uniforms& _uniforms) {
_commands.push_back(Command("material", [&](const std::string& _line){
if (_line == "materials") {
for (vera::MaterialsMap::iterator it = _uniforms.materials.begin(); it != _uniforms.materials.end(); it++)
std::cout << it->second.name << std::endl;
std::cout << it->second->name << std::endl;
return true;
}
else {
std::vector<std::string> values = vera::split(_line,',');
if (values.size() == 2 && values[0] == "material") {
for (vera::MaterialsMap::iterator it = _uniforms.materials.begin(); it != _uniforms.materials.end(); it++) {
if (it->second.name == values[1]) {
it->second.printDefines();
if (it->second->name == values[1]) {
it->second->printDefines();
return true;
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/uniforms.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <map>
#include <queue>
#include <mutex>
#include <array>
#include <vector>
#include <string>
Expand Down Expand Up @@ -86,6 +87,7 @@ class Uniforms : public vera::Scene {
virtual void printBuffers();
virtual void clearBuffers();

std::mutex loadMutex;
ImagesMap loadQueue;

// Ingest new uniforms
Expand Down

0 comments on commit 4ee93c1

Please sign in to comment.