81 lines
4.9 KiB
C++
81 lines
4.9 KiB
C++
#include "FlyTrace.h"
|
|
#include <cmath>
|
|
#include <algorithm>
|
|
#include <array>
|
|
#include <optional>
|
|
|
|
namespace ava2 {
|
|
// Portions Copyright (C) John W. Ratcliff, 2001 (AVA2 SphereLib/frustum.cpp).
|
|
// AVA2 GameLib/FlyTrace.cpp: six-vertex triangle strip, camera-eye basis,
|
|
// constant diffuse color and width decay. Engine coordinates are already converted.
|
|
FlyTrace::FlyTrace() = default;
|
|
FlyTrace::~FlyTrace() = default;
|
|
void FlyTrace::Create(uint32_t color,float tailLength,float tailSize,bool rectShape) {
|
|
m_color=color;m_tailLength=tailLength;m_tailSize=tailSize;m_rectShape=rectShape;m_history.clear();
|
|
}
|
|
void FlyTrace::Clear(){m_history.clear();m_color=0;m_tailLength=0;m_tailSize=0;m_rectShape=false;}
|
|
void FlyTrace::UpdateNewPosition(float x,float y,float z,float currentTime) {
|
|
m_history.push_front({currentTime,{x,y,z}});
|
|
while(!m_history.empty() && m_history.back().time+m_tailLength<currentTime)m_history.pop_back();
|
|
}
|
|
namespace {
|
|
std::array<float,3> Cross(const std::array<float,3>& a,const std::array<float,3>& b) {
|
|
return {a[1]*b[2]-a[2]*b[1],a[2]*b[0]-a[0]*b[2],a[0]*b[1]-a[1]*b[0]};
|
|
}
|
|
void Normalize(std::array<float,3>& v) {
|
|
const float length=std::sqrt(v[0]*v[0]+v[1]*v[1]+v[2]*v[2]);
|
|
if(length>0)for(auto& value:v)value/=length;
|
|
}
|
|
|
|
|
|
}
|
|
void FlyTrace::BuildDrawCommands(const float* view,const float* projection,float now,std::vector<Render3DDraw>& out,const ViewFrustum* cachedFrustum,DepthState depthState) {
|
|
if(m_history.size()<2 || !view || !projection || !std::isfinite(m_tailLength) || m_tailLength<=0 || !std::isfinite(m_tailSize))return;
|
|
std::array<float,3> forward={view[2],view[6],view[10]};Normalize(forward);
|
|
// Rigid camera inverse: eye=-R^T * translation.
|
|
const std::array<float,3> eye={-(view[0]*view[12]+view[1]*view[13]+view[2]*view[14]),
|
|
-(view[4]*view[12]+view[5]*view[13]+view[6]*view[14]),-(view[8]*view[12]+view[9]*view[13]+view[10]*view[14])};
|
|
// Standalone callers have no update phase; production passes the UpdateGame snapshot.
|
|
std::optional<ViewFrustum> fallback;
|
|
if(!cachedFrustum) {
|
|
PerspectiveMetadata inferred;const PerspectiveMetadata* metadata=nullptr;
|
|
if(projection[11]==1 && projection[15]==0 && projection[8]==0 && projection[9]==0 && projection[0]>0 && projection[5]>0 && projection[10]>1) {
|
|
inferred={2*std::atan(1/projection[5])*180/3.14159265358979323846f,projection[5]/projection[0],-projection[14]/projection[10],projection[14]/(1-projection[10])};metadata=&inferred;
|
|
}
|
|
fallback.emplace(view,projection,eye,forward,metadata);
|
|
}
|
|
const ViewFrustum& frustum=cachedFrustum?*cachedFrustum:*fallback;
|
|
struct Segment {float depth;std::array<float,18> positions;};
|
|
std::vector<Segment> segments;
|
|
for(size_t index=0;index+1<m_history.size();++index) {
|
|
const auto& first=m_history[index];const auto& second=m_history[index+1];
|
|
std::array<float,3> b,e;for(int k=0;k<3;++k){b[k]=second.pos[k]-first.pos[k];e[k]=eye[k]-first.pos[k];}
|
|
std::array<float,3> center;for(int k=0;k<3;++k)center[k]=first.pos[k]+b[k]*0.5f;
|
|
const float radius=std::max({std::abs(b[0]),std::abs(b[1]),std::abs(b[2])})*0.5f;
|
|
if(frustum.Outside(center,radius))continue;
|
|
const auto p=Cross(b,e);auto up=Cross(forward,p);Normalize(up);const auto right=Cross(forward,up);
|
|
// Source does not clamp valid aged rates or fade diffuse alpha.
|
|
const float firstSize=m_rectShape?m_tailSize:m_tailSize*(1-(now-first.time)/m_tailLength);
|
|
const float secondSize=m_rectShape?m_tailSize:m_tailSize*(1-(now-second.time)/m_tailLength);
|
|
const float local[6][2]={{0,firstSize},{-firstSize,0},{firstSize,0},{-secondSize,0},{secondSize,0},{0,-secondSize}};
|
|
Segment segment{};for(int k=0;k<3;++k)segment.depth-=e[k]*forward[k];
|
|
for(int vertex=0;vertex<6;++vertex)for(int k=0;k<3;++k)
|
|
segment.positions[vertex*3+k]=local[vertex][0]*right[k]+local[vertex][1]*up[k]+(vertex<3?first.pos[k]:second.pos[k]);
|
|
segments.push_back(segment);
|
|
}
|
|
std::stable_sort(segments.begin(),segments.end(),[](const auto& a,const auto& b){return a.depth<b.depth;});
|
|
Render3DDraw draw;draw.lighting=false;draw.z_enable=depthState.enabled;draw.z_write=depthState.write;draw.depth_less=true;draw.cull_mode=1;
|
|
draw.alpha_blend=true;draw.blend_mode=1;draw.alpha_test=true;draw.alpha_ref=0;
|
|
std::copy(view,view+16,draw.view);std::copy(projection,projection+16,draw.proj);
|
|
for(int k=0;k<16;++k)draw.world[k]=(k%5==0)?1.0f:0.0f;
|
|
for(const auto& segment:segments) {
|
|
const uint32_t base=uint32_t(draw.positions.size()/3);
|
|
draw.positions.insert(draw.positions.end(),segment.positions.begin(),segment.positions.end());
|
|
draw.diffuse.insert(draw.diffuse.end(),6,m_color);
|
|
draw.uv0.insert(draw.uv0.end(),{0,0,0,0.5f,0.5f,0,0.5f,1,1,0.5f,1,1});
|
|
for(uint32_t index:{0u,1u,2u,2u,1u,3u,2u,3u,4u,4u,3u,5u})draw.indices.push_back(base+index);
|
|
}
|
|
if(!draw.indices.empty())out.push_back(std::move(draw));
|
|
}
|
|
} // namespace ava2
|