First I...
Code: Select all
#define NUMBER_OF_DAFFODILS 8192
core::array<ISceneNode *> node; /* have nodes representing flower meshes */
core::array<ISceneNode *> particleNode; /* have nodes representing flower particles (this is for my "level of detail") */
core::array<vector3df> origNodeRotations; // original rotations for meshes
core::array<vector3df> origPartNodePositions; // original positions for particles/billboards. cuz you can't rotate them :(
I load the data in for every node. Each flower "mesh" node has a random position which is scattered all over the terrain. Each "particle" flower node shares that same position with the mesh node. Each mesh node also has its own rotation (which gets modified over time) so that it looks like its swaying in the wind. Then, depending on the distance, either the mesh will show, its billboarded/particle version will show, or nothing will show at all.
Code: Select all
if (countUp) // if swaying in one direction already
{
// sway some more
rotAngle += angleInc * irrtime::timeFactor;
if (rotAngle >= maxAngle) // if it gets to that maximum "sway" angle
{ // sway other direction
rotAngle = maxAngle;
countUp = false;
}
}
else
{
rotAngle -= angleInc * irrtime::timeFactor;
if (rotAngle <= minAngle)
{
rotAngle = minAngle;
countUp = true;
}
}
vector3df pos1 = camera->getPosition();
for (int i = 0; i < NUMBER_OF_DAFFODILS; ++i)
{
#define MAX_DISTANCE 5000.0f
#define MAX_SPRITE_DISTANCE MAX_DISTANCE*0.1f
vector3df pos2 = node[i]->getPosition();
vector3df rotation = node[i]->getRotation();
f32 distance = sqrtf((pos2.X - pos1.X)*(pos2.X - pos1.X) +
(pos2.Y - pos1.Y)*(pos2.Y - pos1.Y) +
(pos2.Z - pos1.Z)*(pos2.Z - pos1.Z));
if (distance <= MAX_DISTANCE)
{
if (distance <= MAX_SPRITE_DISTANCE)
{
// show mesh
node[i]->setVisible(true);
particleNode[i]->setVisible(false);
}
else
{
// show particle
node[i]->setVisible(false);
particleNode[i]->setVisible(true);
}
}
else
{
// too far. show nothing
node[i]->setVisible(false);
particleNode[i]->setVisible(false);
}
// rotate/translate entities if they can be seen
if (node[i]->isVisible())
node[i]->setRotation(origNodeRotations[i] + vector3df(0,0,rotAngle));
if (particleNode[i]->isVisible())
particleNode[i]->setPosition(origPartNodePositions[i] + vector3df(-rotAngle,0,0));
}
Is there a better/more optimized way of doing this? Some way to possibly get more frames per second? Instancing or something or possibly shaders? Thanks in advance.