Align AVA2 Windows behavior and improve mobile touch interactions
This commit is contained in:
+67
-114
@@ -1,127 +1,80 @@
|
||||
#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 > 0.01f ? tailLength : 0.25f;
|
||||
m_tailSize = tailSize > 0.1f ? tailSize : 10.0f;
|
||||
m_rectShape = rectShape;
|
||||
m_history.clear();
|
||||
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::Clear() {
|
||||
m_history.clear();
|
||||
|
||||
}
|
||||
|
||||
void FlyTrace::UpdateNewPosition(float x, float y, float z, float currentTime) {
|
||||
TraceNode node;
|
||||
node.time = currentTime;
|
||||
node.pos[0] = x;
|
||||
node.pos[1] = y;
|
||||
node.pos[2] = z;
|
||||
|
||||
m_history.push_front(node);
|
||||
|
||||
while (!m_history.empty() && (currentTime - m_history.back().time) > m_tailLength) {
|
||||
m_history.pop_back();
|
||||
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));
|
||||
}
|
||||
|
||||
void FlyTrace::BuildDrawCommands(const float* viewMatrix, const float* projMatrix, float currentTime, std::vector<Render3DDraw>& outDraws) {
|
||||
if (m_history.size() < 2) return;
|
||||
|
||||
Render3DDraw draw;
|
||||
draw.lighting = false;
|
||||
draw.z_enable = true;
|
||||
draw.z_write = false;
|
||||
draw.cull_mode = 1; // D3DCULL_NONE
|
||||
draw.alpha_blend = true;
|
||||
draw.blend_mode = 1; // Additive (D3DBLEND_ONE)
|
||||
|
||||
for (int m = 0; m < 16; ++m) {
|
||||
draw.view[m] = viewMatrix[m];
|
||||
draw.proj[m] = projMatrix[m];
|
||||
draw.world[m] = (m % 5 == 0) ? 1.0f : 0.0f;
|
||||
}
|
||||
|
||||
float camDir[3] = {viewMatrix[2], viewMatrix[6], viewMatrix[10]};
|
||||
float camLen = std::sqrt(camDir[0]*camDir[0] + camDir[1]*camDir[1] + camDir[2]*camDir[2]);
|
||||
if (camLen > 1e-5f) { camDir[0] /= camLen; camDir[1] /= camLen; camDir[2] /= camLen; }
|
||||
|
||||
uint32_t vertexOffset = 0;
|
||||
|
||||
for (size_t i = 0; i + 1 < m_history.size(); ++i) {
|
||||
const auto& n0 = m_history[i];
|
||||
const auto& n1 = m_history[i + 1];
|
||||
|
||||
float age0 = currentTime - n0.time;
|
||||
float age1 = currentTime - n1.time;
|
||||
float rate0 = std::clamp(1.0f - age0 / m_tailLength, 0.0f, 1.0f);
|
||||
float rate1 = std::clamp(1.0f - age1 / m_tailLength, 0.0f, 1.0f);
|
||||
|
||||
float s0 = m_rectShape ? m_tailSize : m_tailSize * rate0;
|
||||
float s1 = m_rectShape ? m_tailSize : m_tailSize * rate1;
|
||||
|
||||
// Ribbon segment direction
|
||||
float seg[3] = {n1.pos[0] - n0.pos[0], n1.pos[1] - n0.pos[1], n1.pos[2] - n0.pos[2]};
|
||||
float segLen = std::sqrt(seg[0]*seg[0] + seg[1]*seg[1] + seg[2]*seg[2]);
|
||||
if (segLen < 1e-4f) continue;
|
||||
|
||||
// Normal perpendicular to segment and camera look
|
||||
float norm[3] = {
|
||||
seg[1] * camDir[2] - seg[2] * camDir[1],
|
||||
seg[2] * camDir[0] - seg[0] * camDir[2],
|
||||
seg[0] * camDir[1] - seg[1] * camDir[0]
|
||||
};
|
||||
float normLen = std::sqrt(norm[0]*norm[0] + norm[1]*norm[1] + norm[2]*norm[2]);
|
||||
if (normLen > 1e-5f) {
|
||||
norm[0] /= normLen; norm[1] /= normLen; norm[2] /= normLen;
|
||||
}
|
||||
|
||||
// Vertices for this quad segment
|
||||
float v0[3] = {n0.pos[0] + norm[0] * s0, n0.pos[1] + norm[1] * s0, n0.pos[2] + norm[2] * s0};
|
||||
float v1[3] = {n0.pos[0] - norm[0] * s0, n0.pos[1] - norm[1] * s0, n0.pos[2] - norm[2] * s0};
|
||||
float v2[3] = {n1.pos[0] - norm[0] * s1, n1.pos[1] - norm[1] * s1, n1.pos[2] - norm[2] * s1};
|
||||
float v3[3] = {n1.pos[0] + norm[0] * s1, n1.pos[1] + norm[1] * s1, n1.pos[2] + norm[2] * s1};
|
||||
|
||||
uint8_t baseA = (m_color >> 24) & 0xFF;
|
||||
uint8_t baseR = (m_color >> 16) & 0xFF;
|
||||
uint8_t baseG = (m_color >> 8) & 0xFF;
|
||||
uint8_t baseB = m_color & 0xFF;
|
||||
|
||||
uint8_t a0 = static_cast<uint8_t>(baseA * rate0);
|
||||
uint8_t a1 = static_cast<uint8_t>(baseA * rate1);
|
||||
|
||||
uint32_t diff0 = (static_cast<uint32_t>(a0) << 24) | (static_cast<uint32_t>(baseR) << 16) | (static_cast<uint32_t>(baseG) << 8) | baseB;
|
||||
uint32_t diff1 = (static_cast<uint32_t>(a1) << 24) | (static_cast<uint32_t>(baseR) << 16) | (static_cast<uint32_t>(baseG) << 8) | baseB;
|
||||
|
||||
draw.positions.insert(draw.positions.end(), {v0[0], v0[1], v0[2]});
|
||||
draw.positions.insert(draw.positions.end(), {v1[0], v1[1], v1[2]});
|
||||
draw.positions.insert(draw.positions.end(), {v2[0], v2[1], v2[2]});
|
||||
draw.positions.insert(draw.positions.end(), {v3[0], v3[1], v3[2]});
|
||||
|
||||
draw.diffuse.insert(draw.diffuse.end(), {diff0, diff0, diff1, diff1});
|
||||
draw.uv0.insert(draw.uv0.end(), {0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f});
|
||||
|
||||
draw.indices.push_back(vertexOffset + 0);
|
||||
draw.indices.push_back(vertexOffset + 1);
|
||||
draw.indices.push_back(vertexOffset + 2);
|
||||
draw.indices.push_back(vertexOffset + 0);
|
||||
draw.indices.push_back(vertexOffset + 2);
|
||||
draw.indices.push_back(vertexOffset + 3);
|
||||
|
||||
vertexOffset += 4;
|
||||
}
|
||||
|
||||
if (!draw.indices.empty()) {
|
||||
outDraws.push_back(std::move(draw));
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace ava2
|
||||
|
||||
Reference in New Issue
Block a user