Add rubber band rendering for legacy video drivers

This commit is contained in:
Benau 2022-11-13 16:08:54 +08:00
parent 013d604846
commit 58223928d8
2 changed files with 136 additions and 47 deletions

View File

@ -35,6 +35,10 @@
#include "mini_glm.hpp"
#include "utils/string_utils.hpp"
#include <array>
#include <SMesh.h>
#include <SMeshBuffer.h>
/** RubberBand constructor. It creates a simple quad and attaches it to the
* root(!) of the graph. It's easier this way to get the right coordinates
* than attaching it to the plunger or kart, and trying to find the other
@ -44,16 +48,18 @@
* \param kart Reference to the kart.
*/
RubberBand::RubberBand(Plunger *plunger, AbstractKart *kart)
: m_plunger(plunger), m_owner(kart)
: m_plunger(plunger), m_owner(kart), m_node(NULL)
{
m_hit_kart = NULL;
m_attached_state = RB_TO_PLUNGER;
#ifndef SERVER_ONLY
if (GUIEngine::isNoGraphics() || !CVS->isGLSL())
{
if (GUIEngine::isNoGraphics())
return;
}
video::SColor color(255, 179, 0, 0);
if (CVS->isGLSL())
{
if (CVS->isDeferredEnabled())
{
color.setRed(SP::srgb255ToLinear(color.getRed()));
@ -64,13 +70,38 @@ RubberBand::RubberBand(Plunger *plunger, AbstractKart *kart)
(scene::EPT_TRIANGLE_STRIP, SP::SPShaderManager::get()->getSPShader
("unlit"), material_manager->getDefaultSPMaterial("unlit"));
m_dy_dc->getVerticesVector().resize(4);
// Set the vertex colors properly, as the new pipeline doesn't use the old
// light values
// Set the vertex colors properly, as the new pipeline doesn't use the
// old light values
for (unsigned i = 0; i < 4; i++)
{
m_dy_dc->getSPMVertex()[i].m_color = color;
}
SP::addDynamicDrawCall(m_dy_dc);
}
else
{
video::S3DVertex v;
v.Normal = core::vector3df(0, 1, 0);
v.Color = color;
std::array<video::S3DVertex, 4> vertices = {{ v, v, v, v }};
std::array<uint16_t, 6> indices = {{ 0, 1, 2, 0, 2, 3 }};
scene::SMeshBuffer* buffer = new scene::SMeshBuffer();
buffer->append(vertices.data(), vertices.size(), indices.data(),
indices.size());
buffer->getMaterial().AmbientColor = color;
buffer->getMaterial().DiffuseColor = color;
buffer->getMaterial().EmissiveColor = color;
buffer->setHardwareMappingHint(scene::EHM_STREAM);
buffer->recalculateBoundingBox();
scene::SMesh* mesh = new scene::SMesh();
mesh->addMeshBuffer(buffer);
mesh->setBoundingBox(buffer->getBoundingBox());
buffer->drop();
std::string debug_name = m_owner->getIdent() + " (rubber-band)";
m_node = static_cast<scene::IMeshSceneNode*>(
irr_driver->addMesh(mesh, debug_name));
mesh->drop();
}
#endif
} // RubberBand
@ -112,11 +143,53 @@ void RubberBand::updatePosition()
void RubberBand::updateGraphics(float dt)
{
#ifndef SERVER_ONLY
if (!m_dy_dc)
if (m_node)
{
return;
scene::IMesh* mesh = m_node->getMesh();
scene::IMeshBuffer* buffer = mesh->getMeshBuffer(0);
// Update the rubber band positions
// --------------------------------
// Todo: make height dependent on length (i.e. rubber band gets
// thinner). And call explosion if the band is too long.
const Vec3& k = m_owner->getXYZ();
const float hh = .1f; // half height of the band
const Vec3& p = m_end_position; // for shorter typing
core::vector3df& v0 = buffer->getPosition(0);
core::vector3df& v1 = buffer->getPosition(1);
core::vector3df& v2 = buffer->getPosition(2);
core::vector3df& v3 = buffer->getPosition(3);
v0.X = p.getX() - hh; v0.Y = p.getY(); v0.Z = p.getZ() - hh;
v1.X = p.getX() + hh; v1.Y = p.getY(); v1.Z = p.getZ() + hh;
v2.X = k.getX() + hh; v2.Y = k.getY(); v2.Z = k.getZ() + hh;
v3.X = k.getX() - hh; v3.Y = k.getY(); v3.Z = k.getZ() - hh;
core::vector3df normal = (v1 - v0).crossProduct(v2 - v0);
core::vector3df kart_pos = Vec3(m_owner->getTrans()(
Vec3(0, 5.0f, -2.0f))).toIrrVector();
float dot_product = (v0 - kart_pos).dotProduct(normal);
// For avoiding cull face
if (dot_product >= 0.0f)
{
buffer->getIndices()[0] = 2;
buffer->getIndices()[1] = 1;
buffer->getIndices()[2] = 0;
buffer->getIndices()[3] = 3;
buffer->getIndices()[4] = 2;
buffer->getIndices()[5] = 0;
}
else
{
buffer->getIndices()[0] = 0;
buffer->getIndices()[1] = 1;
buffer->getIndices()[2] = 2;
buffer->getIndices()[3] = 0;
buffer->getIndices()[4] = 2;
buffer->getIndices()[5] = 3;
}
buffer->recalculateBoundingBox();
mesh->setBoundingBox(buffer->getBoundingBox());
}
else if (m_dy_dc)
{
// Update the rubber band positions
// --------------------------------
// Todo: make height dependent on length (i.e. rubber band gets
@ -146,6 +219,7 @@ void RubberBand::updateGraphics(float dt)
}
m_dy_dc->setUpdateOffset(0);
m_dy_dc->recalculateBoundingBox();
}
#endif
} // updateGraphics
@ -297,6 +371,11 @@ void RubberBand::remove()
m_dy_dc->removeFromSP();
m_dy_dc = nullptr;
}
else if (m_node)
{
irr_driver->removeNode(m_node);
m_node = NULL;
}
#endif
} // remove

View File

@ -30,6 +30,14 @@ namespace SP
class SPDynamicDrawCall;
}
namespace irr
{
namespace scene
{
class IMeshSceneNode;
}
}
class AbstractKart;
class Plunger;
@ -59,6 +67,8 @@ private:
/** The dynamic draw call of the rubber band. */
std::shared_ptr<SP::SPDynamicDrawCall> m_dy_dc;
irr::scene::IMeshSceneNode* m_node;
/** The kart a plunger might have hit. */
AbstractKart *m_hit_kart;
/** Stores the end of the rubber band (i.e. the side attached to the