Skip to content

Commit

Permalink
Cab mesh - eliminated distortion at large world distances
Browse files Browse the repository at this point in the history
I took advantage of the fact the mesh is regenerated every frame. Instead of choosing an arbitrary mesh origin and generating the mesh from node absolute positions, I place the mesh origin at physics origin and generate the mesh from node relative positions. This creates an illusion of smooth movement within world space while the mesh is actualy not moving, only vertices within the mesh are moving. The same fix can be applied to flextires, flexbodies and flex airfoil, but this commit only fixes the cab mesh as proof of concept.
  • Loading branch information
ohlidalp committed Sep 4, 2023
1 parent 72d2cd9 commit e202a7c
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 14 deletions.
3 changes: 2 additions & 1 deletion source/main/gfx/GfxActor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1815,7 +1815,8 @@ void RoR::GfxActor::UpdateCabMesh()
{
if ((m_cab_entity != nullptr) && (m_cab_mesh != nullptr))
{
m_cab_scene_node->setPosition(m_cab_mesh->UpdateFlexObj());
m_cab_scene_node->setPosition(m_simbuf.simbuf_origin);
m_cab_mesh->UpdateFlexObj();
}
}

Expand Down
18 changes: 7 additions & 11 deletions source/main/physics/flex/FlexObj.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -183,23 +183,22 @@ int FlexObj::ComputeVertexPos(int tidx, int v, std::vector<CabSubmesh>& submeshe
return 0;
}

Vector3 FlexObj::UpdateMesh()
void FlexObj::UpdateMesh()
{
RoR::NodeSB* all_nodes = m_gfx_actor->GetSimNodeBuffer();
Ogre::Vector3 center=(all_nodes[m_vertex_nodes[0]].AbsPosition+all_nodes[m_vertex_nodes[1]].AbsPosition)/2.0;
for (size_t i=0; i<m_vertex_count; i++)
{
//set position
m_vertices[i].position=all_nodes[m_vertex_nodes[i]].AbsPosition-center;
//set position (the scene node is at physics origin, so use node relative position)
m_vertices[i].position=all_nodes[m_vertex_nodes[i]].RelPosition;
//reset normals
m_vertices[i].normal=Vector3::ZERO;
}
//accumulate normals per triangle
for (size_t i=0; i<m_index_count/3; i++)
{
Vector3 v1, v2;
v1=all_nodes[m_vertex_nodes[m_indices[i*3+1]]].AbsPosition-all_nodes[m_vertex_nodes[m_indices[i*3]]].AbsPosition;
v2=all_nodes[m_vertex_nodes[m_indices[i*3+2]]].AbsPosition-all_nodes[m_vertex_nodes[m_indices[i*3]]].AbsPosition;
v1=all_nodes[m_vertex_nodes[m_indices[i*3+1]]].RelPosition-all_nodes[m_vertex_nodes[m_indices[i*3]]].RelPosition;
v2=all_nodes[m_vertex_nodes[m_indices[i*3+2]]].RelPosition-all_nodes[m_vertex_nodes[m_indices[i*3]]].RelPosition;
v1=v1.crossProduct(v2);
float s=v1.length();

Expand All @@ -223,15 +222,12 @@ Vector3 FlexObj::UpdateMesh()
{
m_vertices[i].normal = approx_normalise(m_vertices[i].normal);
}

return center;
}

Vector3 FlexObj::UpdateFlexObj()
void FlexObj::UpdateFlexObj()
{
Ogre::Vector3 center = this->UpdateMesh();
this->UpdateMesh();
m_hw_vbuf->writeData(0, m_hw_vbuf->getSizeInBytes(), m_vertices_raw, true);
return center;
}

FlexObj::~FlexObj()
Expand Down
4 changes: 2 additions & 2 deletions source/main/physics/flex/FlexObj.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class FlexObj

~FlexObj();

Ogre::Vector3 UpdateFlexObj();
void UpdateFlexObj();
void ScaleFlexObj(float factor);

private:
Expand All @@ -88,7 +88,7 @@ class FlexObj

/// Compute vertex position in the vertexbuffer (0-based offset) for node `v` of triangle `tidx`
int ComputeVertexPos(int tidx, int v, std::vector<CabSubmesh>& submeshes);
Ogre::Vector3 UpdateMesh();
void UpdateMesh();

Ogre::MeshPtr m_mesh;
std::vector<Ogre::SubMesh*> m_submeshes;
Expand Down

0 comments on commit e202a7c

Please sign in to comment.