forked from kcat/opendf
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use separate classes to handle object positions and nodes
- Loading branch information
Showing
12 changed files
with
465 additions
and
227 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
|
||
#include "placeable.hpp" | ||
|
||
#include "render/renderer.hpp" | ||
|
||
|
||
namespace DF | ||
{ | ||
|
||
Placeable Placeable::sPlaceables; | ||
|
||
|
||
void Placeable::deallocate(const size_t *ids, size_t count) | ||
{ | ||
for(size_t i = 0;i < count;++i) | ||
mPositions.erase(ids[i]); | ||
} | ||
|
||
|
||
void Placeable::setPos(size_t idx, const osg::Vec3f &pt, const osg::Vec3f &rot) | ||
{ | ||
Position &pos = mPositions[idx]; | ||
pos.mRotation = rot; | ||
pos.mPoint = pt; | ||
Renderer::get().markDirty(idx, pos); | ||
} | ||
|
||
void Placeable::setRotate(size_t idx, const osg::Vec3f &rot) | ||
{ | ||
Position &pos = mPositions[idx]; | ||
pos.mRotation = rot; | ||
Renderer::get().markDirty(idx, pos); | ||
} | ||
|
||
void Placeable::setPoint(size_t idx, const osg::Vec3f &pt) | ||
{ | ||
Position &pos = mPositions[idx]; | ||
pos.mPoint = pt; | ||
Renderer::get().markDirty(idx, pos); | ||
} | ||
|
||
void Placeable::setLocalRot(size_t idx, const osg::Vec3f &rot) | ||
{ | ||
Position &pos = mPositions[idx]; | ||
pos.mLocalRotation = rot; | ||
Renderer::get().markDirty(idx, pos); | ||
} | ||
|
||
} // namespace DF |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
#ifndef CLASS_PLACEABLE_HPP | ||
#define CLASS_PLACEABLE_HPP | ||
|
||
#include <osg/Vec3> | ||
|
||
#include "misc/sparsearray.hpp" | ||
|
||
|
||
namespace DF | ||
{ | ||
|
||
// Should probably be somewhere else... | ||
struct Position { | ||
osg::Vec3f mRotation; | ||
osg::Vec3f mPoint; | ||
osg::Vec3f mLocalRotation; | ||
}; | ||
|
||
class Placeable { | ||
static Placeable sPlaceables; | ||
|
||
Misc::SparseArray<Position> mPositions; | ||
|
||
public: | ||
void deallocate(const size_t *ids, size_t count); | ||
void deallocate(size_t idx) { return deallocate(&idx, 1); } | ||
|
||
void setPos(size_t idx, const osg::Vec3f &pt, const osg::Vec3f &rot); | ||
void setRotate(size_t idx, const osg::Vec3f &rot); | ||
void setPoint(size_t idx, const osg::Vec3f &pt); | ||
void setLocalRot(size_t idx, const osg::Vec3f &rot); | ||
|
||
const Position &getPos(size_t idx) | ||
{ | ||
return mPositions[idx]; | ||
}; | ||
|
||
static Placeable &get() { return sPlaceables; } | ||
}; | ||
|
||
} // namespace DF | ||
|
||
#endif /* CLASS_PLACEABLE_HPP */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
|
||
#include "renderer.hpp" | ||
|
||
#include <osg/MatrixTransform> | ||
|
||
#include "class/placeable.hpp" | ||
|
||
|
||
namespace DF | ||
{ | ||
|
||
Renderer Renderer::sRenderer; | ||
|
||
|
||
void Renderer::setNode(size_t idx, osg::MatrixTransform *node) | ||
{ | ||
mBaseNodes[idx] = node; | ||
} | ||
|
||
void Renderer::remove(const size_t *ids, size_t count) | ||
{ | ||
for(size_t i = 0;i < count;++i) | ||
mBaseNodes.erase(ids[i]); | ||
} | ||
|
||
|
||
void Renderer::markDirty(size_t idx, const Position &pos) | ||
{ | ||
if(mBaseNodes.exists(idx)) | ||
mDirtyNodes.push({mBaseNodes[idx], pos}); | ||
} | ||
|
||
void Renderer::update() | ||
{ | ||
while(!mDirtyNodes.empty()) | ||
{ | ||
const NodePosPair &nodepos = mDirtyNodes.top(); | ||
|
||
const Position &pos = nodepos.mPosition; | ||
osg::Matrix mat; | ||
mat.makeRotate( | ||
pos.mRotation.x()*3.14159f/1024.0f, osg::Vec3f(1.0f, 0.0f, 0.0f), | ||
-pos.mRotation.y()*3.14159f/1024.0f, osg::Vec3f(0.0f, 1.0f, 0.0f), | ||
pos.mRotation.z()*3.14159f/1024.0f, osg::Vec3f(0.0f, 0.0f, 1.0f) | ||
); | ||
mat.postMultTranslate(osg::Vec3(pos.mPoint.x(), pos.mPoint.y(), pos.mPoint.z())); | ||
mat.postMultRotate(osg::Quat( | ||
pos.mLocalRotation.x()*3.14159f/1024.0f, osg::Vec3f(1.0f, 0.0f, 0.0f), | ||
-pos.mLocalRotation.y()*3.14159f/1024.0f, osg::Vec3f(0.0f, 1.0f, 0.0f), | ||
pos.mLocalRotation.z()*3.14159f/1024.0f, osg::Vec3f(0.0f, 0.0f, 1.0f) | ||
)); | ||
|
||
osg::MatrixTransform *node = nodepos.mNode; | ||
//node->setDataVariance(osg::Node::DYNAMIC); | ||
node->setMatrix(mat); | ||
|
||
mDirtyNodes.pop(); | ||
} | ||
} | ||
|
||
|
||
} // namespace DF |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
#ifndef RENDER_RENDERER_HPP | ||
#define RENDER_RENDERER_HPP | ||
|
||
#include <queue> | ||
|
||
#include <osg/ref_ptr> | ||
|
||
#include "misc/sparsearray.hpp" | ||
|
||
#include "class/placeable.hpp" | ||
|
||
|
||
namespace osg | ||
{ | ||
class MatrixTransform; | ||
} | ||
|
||
namespace DF | ||
{ | ||
|
||
struct NodePosPair { | ||
osg::ref_ptr<osg::MatrixTransform> mNode; | ||
Position mPosition; | ||
|
||
bool operator<(const NodePosPair &rhs) const | ||
{ return mNode < rhs.mNode; } | ||
}; | ||
|
||
class Renderer { | ||
static Renderer sRenderer; | ||
|
||
Misc::SparseArray<osg::ref_ptr<osg::MatrixTransform>> mBaseNodes; | ||
std::priority_queue<NodePosPair> mDirtyNodes; | ||
|
||
public: | ||
void setNode(size_t idx, osg::MatrixTransform *node); | ||
|
||
void remove(const size_t *ids, size_t count); | ||
|
||
void markDirty(size_t idx, const Position &pos); | ||
|
||
void update(); | ||
|
||
static Renderer &get() { return sRenderer; } | ||
}; | ||
|
||
} // namespace DF | ||
|
||
#endif /* RENDER_RENDERER_HPP */ |
Oops, something went wrong.