Drafting the octree, allocating the first cells

This commit is contained in:
samcake 2016-01-26 01:59:00 -08:00
parent a847a16788
commit 903c7dde5a
6 changed files with 50 additions and 32 deletions

View file

@ -25,8 +25,6 @@
using namespace render; using namespace render;
const int DrawSceneOctree_CellsSlot = 0;
const int DrawSceneOctree_OctreeInfoSlot = 1;
const gpu::PipelinePointer DrawSceneOctree::getDrawCellBoundsPipeline() { const gpu::PipelinePointer DrawSceneOctree::getDrawCellBoundsPipeline() {
if (!_drawCellBoundsPipeline) { if (!_drawCellBoundsPipeline) {
@ -35,10 +33,11 @@ const gpu::PipelinePointer DrawSceneOctree::getDrawCellBoundsPipeline() {
gpu::ShaderPointer program = gpu::Shader::createProgram(vs, ps); gpu::ShaderPointer program = gpu::Shader::createProgram(vs, ps);
gpu::Shader::BindingSet slotBindings; gpu::Shader::BindingSet slotBindings;
slotBindings.insert(gpu::Shader::Binding(std::string("cellsBuffer"), DrawSceneOctree_CellsSlot));
slotBindings.insert(gpu::Shader::Binding(std::string("octreeInfoBuffer"), DrawSceneOctree_OctreeInfoSlot));
gpu::Shader::makeProgram(*program, slotBindings); gpu::Shader::makeProgram(*program, slotBindings);
_drawBoundPosLoc = program->getUniforms().findLocation("inBoundPos");
_drawBoundDimLoc = program->getUniforms().findLocation("inBoundDim");
auto state = std::make_shared<gpu::State>(); auto state = std::make_shared<gpu::State>();
state->setDepthTest(true, false, gpu::LESS_EQUAL); state->setDepthTest(true, false, gpu::LESS_EQUAL);
@ -69,9 +68,19 @@ void DrawSceneOctree::run(const SceneContextPointer& sceneContext,
if (!_cells) { if (!_cells) {
_cells = std::make_shared<gpu::Buffer>(); _cells = std::make_shared<gpu::Buffer>();
} }
if (!_octreeInfo) { /* if (!_octreeInfo) {
_octreeInfo = std::make_shared<gpu::Buffer>();; _octreeInfo = std::make_shared<gpu::Buffer>();;
}*/
const auto& inCells = scene->_spatialTree._cells;
_cells->resize(inCells.size() * sizeof(AABox));
AABox* cellAABox = reinterpret_cast<AABox*> (_cells->editData());
for (const auto& cell : inCells) {
(*cellAABox) = scene->_spatialTree.evalBound(cell.cellpos);
nbCells++;
cellAABox++;
} }
/* /*
_cells->resize((inItems.size() * sizeof(AABox))); _cells->resize((inItems.size() * sizeof(AABox)));
_itemStatus->resize((inItems.size() * NUM_STATUS_VEC4_PER_ITEM * sizeof(glm::vec4))); _itemStatus->resize((inItems.size() * NUM_STATUS_VEC4_PER_ITEM * sizeof(glm::vec4)));
@ -108,9 +117,7 @@ void DrawSceneOctree::run(const SceneContextPointer& sceneContext,
itemStatus++; itemStatus++;
} }
nbItems++; }
itemAABox++;
}
}*/ }*/
} }
@ -119,7 +126,7 @@ void DrawSceneOctree::run(const SceneContextPointer& sceneContext,
} }
// Allright, something to render let's do it // Allright, something to render let's do it
/* gpu::doInBatch(args->_context, [&](gpu::Batch& batch) { gpu::doInBatch(args->_context, [&](gpu::Batch& batch) {
glm::mat4 projMat; glm::mat4 projMat;
Transform viewMat; Transform viewMat;
args->_viewFrustum->evalProjectionMatrix(projMat); args->_viewFrustum->evalProjectionMatrix(projMat);
@ -133,18 +140,15 @@ void DrawSceneOctree::run(const SceneContextPointer& sceneContext,
// bind the one gpu::Pipeline we need // bind the one gpu::Pipeline we need
batch.setPipeline(getDrawCellBoundsPipeline()); batch.setPipeline(getDrawCellBoundsPipeline());
AABox* itemAABox = reinterpret_cast<AABox*> (_itemBounds->editData()); AABox* cellAABox = reinterpret_cast<AABox*> (_cells->editData());
glm::ivec4* itemStatus = reinterpret_cast<glm::ivec4*> (_itemStatus->editData());
const unsigned int VEC3_ADRESS_OFFSET = 3; const unsigned int VEC3_ADRESS_OFFSET = 3;
if ((renderContext->getDrawStatus() & showDisplayStatusFlag) > 0) { for (int i = 0; i < nbCells; i++) {
for (int i = 0; i < nbCells; i++) { batch._glUniform3fv(_drawBoundPosLoc, 1, (const float*) (cellAABox + i));
batch._glUniform3fv(_drawItemBoundPosLoc, 1, (const float*) (itemAABox + i)); batch._glUniform3fv(_drawBoundDimLoc, 1, ((const float*) (cellAABox + i)) + VEC3_ADRESS_OFFSET);
batch._glUniform3fv(_drawItemBoundDimLoc, 1, ((const float*) (itemAABox + i)) + VEC3_ADRESS_OFFSET);
batch.draw(gpu::LINES, 24, 0); batch.draw(gpu::LINES, 24, 0);
}
} }
});*/ });
} }

View file

@ -18,9 +18,10 @@
namespace render { namespace render {
class DrawSceneOctree { class DrawSceneOctree {
int _drawBoundPosLoc;
int _drawBoundDimLoc;
gpu::PipelinePointer _drawCellBoundsPipeline; gpu::PipelinePointer _drawCellBoundsPipeline;
gpu::BufferPointer _cells; gpu::BufferPointer _cells;
gpu::BufferPointer _octreeInfo;
public: public:

View file

@ -14,12 +14,6 @@
using namespace render; using namespace render;
Octree::Indices Octree::allocateCellPath(const CellPath& path) { Octree::Indices Octree::allocateCellPath(const CellPath& path) {
CellPoint point{ Coord3{ 2, 4, 3 }, 3 };
auto ppath = CellPoint::rootTo(point);
Indices cellPath; Indices cellPath;
Index currentIndex = 0; Index currentIndex = 0;
@ -27,14 +21,15 @@ Octree::Indices Octree::allocateCellPath(const CellPath& path) {
int d = 0; int d = 0;
cellPath.push_back(currentIndex); cellPath.push_back(currentIndex);
for (; d < path.back().depth; d++) { for (; d <= path.back().depth; d++) {
auto& cellPoint = path[d]; auto& cellPoint = path[d];
auto currentIndex = currentCell->child(cellPoint.octant()); auto currentIndex = currentCell->child(cellPoint.octant());
if (currentIndex == INVALID) { if (currentIndex == INVALID) {
break; currentIndex = _cells.size();
currentCell->links[cellPoint.octant()] = currentIndex;
_cells.push_back(Cell(cellPath.back(), cellPoint));
} }
cellPath.push_back(currentIndex); cellPath.push_back(currentIndex);
currentCell = _cells.data() + currentIndex; currentCell = _cells.data() + currentIndex;
} }

View file

@ -19,6 +19,9 @@
#include <glm/glm.hpp> #include <glm/glm.hpp>
#include <glm/gtx/bit.hpp> #include <glm/gtx/bit.hpp>
#include <AABox.h>
namespace render { namespace render {
class Octree { class Octree {
@ -147,6 +150,11 @@ namespace render {
Cell() : Cell() :
links({ { INVALID, INVALID, INVALID, INVALID, INVALID, INVALID, INVALID, INVALID, INVALID } }) links({ { INVALID, INVALID, INVALID, INVALID, INVALID, INVALID, INVALID, INVALID, INVALID } })
{} {}
Cell(Index parent, CellPoint pos) :
cellpos(pos),
links({ { INVALID, INVALID, INVALID, INVALID, INVALID, INVALID, INVALID, INVALID, parent } })
{}
}; };
using Cells = std::vector< Cell >; using Cells = std::vector< Cell >;
@ -163,11 +171,20 @@ namespace render {
Cells _cells = Cells(1, Cell()); // start with only the Cell root Cells _cells = Cells(1, Cell()); // start with only the Cell root
Bricks _bricks; Bricks _bricks;
float _size = 320.0f;
Octree() {}; Octree() {};
// allocatePath // allocatePath
Indices allocateCellPath(const CellPath& path); Indices allocateCellPath(const CellPath& path);
AABox evalBound(const CellPoint& point) const {
float width = (float) (_size / double(1 << point.depth));
glm::vec3 corner = glm::vec3(-_size * 0.5f) + glm::vec3(point.pos) * width;
return AABox(corner, width);
}
}; };

View file

@ -142,9 +142,9 @@ Scene::Scene() {
_spatialTree; _spatialTree;
Octree::CellPoint point{ Octree::Coord3{ 2, 4, 3 }, 3 }; Octree::CellPoint point{ Octree::Coord3{ 2, 4, 3 }, 15};
auto path = Octree::CellPoint::rootTo(point); auto path = Octree::CellPoint::rootTo(point);
auto indices = _spatialTree.allocateCellPath(path);
} }
ItemID Scene::allocateID() { ItemID Scene::allocateID() {

View file

@ -513,6 +513,9 @@ public:
void processPendingChangesQueue(); void processPendingChangesQueue();
Octree _spatialTree;
protected: protected:
// Thread safe elements that can be accessed from anywhere // Thread safe elements that can be accessed from anywhere
std::atomic<unsigned int> _IDAllocator{ 1 }; // first valid itemID will be One std::atomic<unsigned int> _IDAllocator{ 1 }; // first valid itemID will be One
@ -529,8 +532,6 @@ protected:
void removeItems(const ItemIDs& ids); void removeItems(const ItemIDs& ids);
void updateItems(const ItemIDs& ids, UpdateFunctors& functors); void updateItems(const ItemIDs& ids, UpdateFunctors& functors);
Octree _spatialTree;
friend class Engine; friend class Engine;
}; };