2019-12-31 05:05:16 -08:00
|
|
|
// Copyright © 2008-2020 Pioneer Developers. See AUTHORS.txt for details
|
2014-01-18 03:50:57 -08:00
|
|
|
// Licensed under the terms of the GPL v3. See licenses/GPL-3.txt
|
|
|
|
|
2013-11-05 13:55:10 -08:00
|
|
|
#include "HudTrail.h"
|
2019-02-15 15:05:44 -08:00
|
|
|
|
|
|
|
#include "Body.h"
|
2019-10-07 09:12:28 -07:00
|
|
|
#include "Frame.h"
|
2014-01-26 17:26:44 -08:00
|
|
|
#include "Pi.h"
|
|
|
|
#include "graphics/RenderState.h"
|
2019-11-26 00:26:59 -08:00
|
|
|
#include "graphics/Renderer.h"
|
2013-11-05 13:55:10 -08:00
|
|
|
|
|
|
|
const float UPDATE_INTERVAL = 0.1f;
|
|
|
|
const Uint16 MAX_POINTS = 100;
|
|
|
|
|
2019-01-02 08:59:07 -08:00
|
|
|
HudTrail::HudTrail(Body *b, const Color &c) :
|
|
|
|
m_body(b),
|
|
|
|
m_updateTime(0.f),
|
|
|
|
m_color(c)
|
2013-11-05 13:55:10 -08:00
|
|
|
{
|
2013-11-11 09:45:53 -08:00
|
|
|
m_currentFrame = b->GetFrame();
|
2014-01-26 17:26:44 -08:00
|
|
|
|
|
|
|
Graphics::RenderStateDesc rsd;
|
|
|
|
rsd.blendMode = Graphics::BLEND_ALPHA_ONE;
|
|
|
|
rsd.depthWrite = false;
|
|
|
|
m_renderState = Pi::renderer->CreateRenderState(rsd);
|
2013-11-05 13:55:10 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
void HudTrail::Update(float time)
|
|
|
|
{
|
2015-03-21 16:18:39 -07:00
|
|
|
PROFILE_SCOPED();
|
2013-11-05 13:55:10 -08:00
|
|
|
//record position
|
|
|
|
m_updateTime += time;
|
|
|
|
if (m_updateTime > UPDATE_INTERVAL) {
|
|
|
|
m_updateTime = 0.f;
|
2019-10-07 09:12:28 -07:00
|
|
|
FrameId bodyFrameId = m_body->GetFrame();
|
2017-03-05 05:15:55 -08:00
|
|
|
|
2019-11-26 00:26:59 -08:00
|
|
|
if (!m_currentFrame) {
|
2019-10-07 09:12:28 -07:00
|
|
|
m_currentFrame = bodyFrameId;
|
2014-05-10 14:02:27 -07:00
|
|
|
m_trailPoints.clear();
|
|
|
|
}
|
2017-03-05 05:15:55 -08:00
|
|
|
|
2019-10-07 09:12:28 -07:00
|
|
|
if (bodyFrameId == m_currentFrame)
|
2013-11-11 09:45:53 -08:00
|
|
|
m_trailPoints.push_back(m_body->GetInterpPosition());
|
2013-11-05 13:55:10 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
while (m_trailPoints.size() > MAX_POINTS)
|
|
|
|
m_trailPoints.pop_front();
|
|
|
|
}
|
|
|
|
|
|
|
|
void HudTrail::Render(Graphics::Renderer *r)
|
|
|
|
{
|
2015-03-21 16:18:39 -07:00
|
|
|
PROFILE_SCOPED();
|
2013-11-05 13:55:10 -08:00
|
|
|
//render trail
|
|
|
|
if (m_trailPoints.size() > 1) {
|
|
|
|
const vector3d vpos = m_transform * m_body->GetInterpPosition();
|
|
|
|
m_transform[12] = vpos.x;
|
|
|
|
m_transform[13] = vpos.y;
|
|
|
|
m_transform[14] = vpos.z;
|
|
|
|
m_transform[15] = 1.0;
|
|
|
|
|
|
|
|
static std::vector<vector3f> tvts;
|
|
|
|
static std::vector<Color> colors;
|
|
|
|
tvts.clear();
|
|
|
|
colors.clear();
|
|
|
|
const vector3d curpos = m_body->GetInterpPosition();
|
2015-01-02 06:53:33 -08:00
|
|
|
tvts.reserve(MAX_POINTS);
|
|
|
|
colors.reserve(MAX_POINTS);
|
2013-11-05 13:55:10 -08:00
|
|
|
tvts.push_back(vector3f(0.f));
|
2015-11-22 11:02:20 -08:00
|
|
|
colors.push_back(Color::BLANK);
|
2013-11-05 13:55:10 -08:00
|
|
|
float alpha = 1.f;
|
|
|
|
const float decrement = 1.f / m_trailPoints.size();
|
|
|
|
const Color tcolor = m_color;
|
2019-01-02 08:59:07 -08:00
|
|
|
for (size_t i = m_trailPoints.size() - 1; i > 0; i--) {
|
2013-11-05 13:55:10 -08:00
|
|
|
tvts.push_back(-vector3f(curpos - m_trailPoints[i]));
|
|
|
|
alpha -= decrement;
|
|
|
|
colors.push_back(tcolor);
|
2013-12-29 16:06:16 -08:00
|
|
|
colors.back().a = Uint8(alpha * 255);
|
2013-11-05 13:55:10 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
r->SetTransform(m_transform);
|
2015-01-02 06:53:33 -08:00
|
|
|
m_lines.SetData(tvts.size(), &tvts[0], &colors[0]);
|
|
|
|
m_lines.Draw(r, m_renderState, Graphics::LINE_STRIP);
|
2013-11-05 13:55:10 -08:00
|
|
|
}
|
2013-11-05 14:35:37 -08:00
|
|
|
}
|
|
|
|
|
2019-10-07 09:12:28 -07:00
|
|
|
void HudTrail::Reset(FrameId newFrame)
|
2013-11-05 14:35:37 -08:00
|
|
|
{
|
2013-11-11 09:45:53 -08:00
|
|
|
m_currentFrame = newFrame;
|
2013-11-05 14:35:37 -08:00
|
|
|
m_trailPoints.clear();
|
2014-01-18 03:41:19 -08:00
|
|
|
}
|