#include "FlyTrace.h" #include #include #include #include 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 Cross(const std::array& a,const std::array& 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& 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& 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 forward={view[2],view[6],view[10]};Normalize(forward); // Rigid camera inverse: eye=-R^T * translation. const std::array 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 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 positions;}; std::vector segments; for(size_t index=0;index+1 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 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