16
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-07-17 23:39:18 +02:00

Use glm::length2 instead of glm::length where possible

This commit is contained in:
docentYT
2026-04-27 23:27:08 +02:00
parent 7d561f8443
commit cba106e22e
9 changed files with 34 additions and 21 deletions

View File

@@ -569,7 +569,7 @@ void driver_mode::update_camera(double const Deltatime)
Camera.Reset(); // likwidacja obrotów - patrzy horyzontalnie na południe
if (Camera.m_owner == nullptr)
{
if (controlled && glm::length(controlled->GetPosition() - Camera.Pos) < 1500)
if (controlled && glm::length2(controlled->GetPosition() - Camera.Pos) < 1500 * 1500) // length2 is better than length for comparing because it does not require sqrt function
{
// gdy bliżej niż 1.5km
Camera.LookAt = controlled->GetPosition() + 0.4 * controlled->VectorUp() * controlled->MoverParameters->Dim.H;
@@ -583,7 +583,7 @@ void driver_mode::update_camera(double const Deltatime)
if (d && pDynamicNearest)
{
// jeśli jakiś jest znaleziony wcześniej
if (100.0 * glm::length(d->GetPosition() - Camera.Pos) > glm::length(pDynamicNearest->GetPosition() - Camera.Pos))
if (100.0 * glm::length2(d->GetPosition() - Camera.Pos) > glm::length2(pDynamicNearest->GetPosition() - Camera.Pos)) // length2 is better than length for comparing because it does not require sqrt function
{
d = pDynamicNearest; // jeśli najbliższy nie jest 10 razy bliżej niż
}

View File

@@ -406,7 +406,7 @@ openal_renderer::update( double const Deltatime ) {
cameramove = glm::dvec3{ 0.0 };
}
// ... from camera jump to another location
if( glm::length( cameramove ) > 100.0 ) {
if( glm::length2( cameramove ) > 100.0 * 100.0) { // length2 is better than length for comparing because it does not require sqrt function
cameramove = glm::dvec3{ 0.0 };
}
m_listenervelocity = limit_velocity( cameramove / Deltatime );

View File

@@ -630,8 +630,9 @@ std::pair<int, int> TSubModel::Load(cParser &parser, bool dynamic)
if (idx > 0)
{
// jeśli pierwszy trójkąt będzie zdegenerowany, to zostanie usunięty i nie ma co sprawdzać
if ((glm::length((vertex)->position - (vertex - 1)->position) > 1000.0) || (glm::length((vertex - 1)->position - (vertex - 2)->position) > 1000.0) ||
(glm::length((vertex - 2)->position - (vertex)->position) > 1000.0))
// length2 is better than length for comparing because it does not require sqrt function
if ((glm::length2((vertex)->position - (vertex - 1)->position) > 1000.0 * 1000.0) || (glm::length2((vertex - 1)->position - (vertex - 2)->position) > 1000.0 * 1000.0) ||
(glm::length2((vertex - 2)->position - (vertex)->position) > 1000.0 * 1000.0))
{
// jeżeli są dalej niż 2km od siebie //Ra 15-01:
// obiekt wstawiany nie powinien być większy niż 300m (trójkąty terenu w E3D mogą mieć 1.5km)

View File

@@ -1293,7 +1293,8 @@ bool opengl33_renderer::Render_reflections(viewport_config &vp)
auto const timestamp{ Timer::GetRenderTime() };
if( ( timestamp - m_environmentupdatetime < Global.reflectiontune.update_interval )
&& ( glm::length( m_renderpass.pass_camera.position() - m_environmentupdatelocation ) < 1000.0 ) ) {
&& ( glm::length2( m_renderpass.pass_camera.position() - m_environmentupdatelocation ) < 1000.0 * 1000.0 ) ) // length2 is better than length for comparing because it does not require sqrt function
{
// run update every 5+ mins of simulation time, or at least 1km from the last location
return false;
}
@@ -4620,7 +4621,7 @@ void opengl33_renderer::Update_Lights(light_array &Lights)
break;
}
auto const lightoffset = glm::vec3{scenelight.position - camera};
if (glm::length(lightoffset) > 1000.f) {
if (glm::length2(lightoffset) > 1000.f * 1000.f) {
// we don't care about lights past arbitrary limit of 1 km.
// but there could still be weaker lights which are closer, so keep looking
continue;

View File

@@ -892,7 +892,7 @@ opengl_renderer::Render_reflections() {
auto const timestamp { Timer::GetRenderTime() };
if( ( timestamp - m_environmentupdatetime < Global.reflectiontune.update_interval )
&& ( glm::length( m_renderpass.camera.position() - m_environmentupdatelocation ) < 1000.0 ) ) {
&& ( glm::length2( m_renderpass.camera.position() - m_environmentupdatelocation ) < 1000.0 * 1000.0 ) ) {
// run update every 5+ mins of simulation time, or at least 1km from the last location
return false;
}
@@ -4341,7 +4341,8 @@ opengl_renderer::Update_Lights( light_array &Lights ) {
break;
}
auto const lightoffset = glm::vec3{ scenelight.position - camera };
if( glm::length( lightoffset ) > 1000.f ) {
// length2 is better than length for comparing because it does not require sqrt function
if( glm::length2( lightoffset ) > 1000.f * 1000.f ) {
// we don't care about lights past arbitrary limit of 1 km.
// but there could still be weaker lights which are closer, so keep looking
continue;

View File

@@ -70,7 +70,9 @@ basic_precipitation::update() {
cameramove = glm::dvec3{ 0.0 };
}
// ... from camera jump to another location
if( glm::length( cameramove ) > 100.0 ) {
// length2 is better than length for comparing because it does not require sqrt function
if( glm::length2( cameramove ) > 100.0 * 100.0 )
{
cameramove = glm::dvec3{ 0.0 };
}

View File

@@ -285,7 +285,8 @@ basic_cell::insert( shape_node Shape ) {
if( ( shapedata.rangesquared_min == targetshapedata.rangesquared_min )
&& ( shapedata.rangesquared_max == targetshapedata.rangesquared_max )
// ...and located close to each other (within arbitrary limit of 25m)
&& ( glm::length( shapedata.area.center - targetshapedata.area.center ) < 25.0 ) ) {
// length2 is better than length for comparing because it does not require sqrt function
&& ( glm::length2( shapedata.area.center - targetshapedata.area.center ) < 25.0 * 25.0 ) ) {
if( true == targetshape.merge( Shape ) ) {
// if the shape was merged there's nothing left to do
@@ -311,7 +312,8 @@ basic_cell::insert( lines_node Lines ) {
if( ( linesdata.rangesquared_min == targetlinesdata.rangesquared_min )
&& ( linesdata.rangesquared_max == targetlinesdata.rangesquared_max )
// ...and located close to each other (within arbitrary limit of 10m)
&& ( glm::length( linesdata.area.center - targetlinesdata.area.center ) < 10.0 ) ) {
// length2 is better than length for comparing because it does not require sqrt function
&& ( glm::length2( linesdata.area.center - targetlinesdata.area.center ) < 10.0 * 10.0) ) {
if( true == targetlines.merge( Lines ) ) {
// if the shape was merged there's nothing left to do

View File

@@ -1412,7 +1412,8 @@ TDynamicObject * TDynamicObject::ABuFindNearestObject(glm::vec3 pos, TTrack *Tra
if( CouplNr == -2 ) {
// wektor [kamera-obiekt] - poszukiwanie obiektu
if (glm::length(glm::dvec3(pos) - dynamic->vPosition) < 10.0)
// length2 is better than length for comparing because it does not require sqrt function
if (glm::length2(glm::dvec3(pos) - dynamic->vPosition) < 10.0 * 10.0)
{
// 10 metrów
return dynamic;
@@ -1420,12 +1421,14 @@ TDynamicObject * TDynamicObject::ABuFindNearestObject(glm::vec3 pos, TTrack *Tra
}
else {
// jeśli (CouplNr) inne niz -2, szukamy sprzęgu
if (glm::length(glm::dvec3(pos) - dynamic->vCoulpler[0]) < 5.0) {
// length2 is better than length for comparing because it does not require sqrt function
if (glm::length2(glm::dvec3(pos) - dynamic->vCoulpler[0]) < 5.0 * 5.0) {
// 5 metrów
CouplNr = 0;
return dynamic;
}
if (glm::length(glm::dvec3(pos) - dynamic->vCoulpler[1]) < 5.0) {
// length2 is better than length for comparing because it does not require sqrt function
if (glm::length2(glm::dvec3(pos) - dynamic->vCoulpler[1]) < 5.0 * 5.0) {
// 5 metrów
CouplNr = 1;
return dynamic;

View File

@@ -559,8 +559,9 @@ void TTrack::Load(cParser *parser, glm::dvec3 const &pOrigin)
// na przechyłce doliczyć jeszcze pół przechyłki
}
if( (glm::length(( p1 + p1 + p2 ) / 3.0 - p1 - cp1) < 0.02 )
|| ( glm::length(( p1 + p2 + p2 ) / 3.0 - p2 + cp1) < 0.02 ) ) {
// length2 is better than length for comparing because it does not require sqrt function
if( (glm::length2(( p1 + p1 + p2 ) / 3.0 - p1 - cp1) < 0.02 * 0.02)
|| (glm::length2(( p1 + p2 + p2 ) / 3.0 - p2 + cp1) < 0.02 * 0.02) ) {
// "prostowanie" prostych z kontrolnymi, dokładność 2cm
cp1 = cp2 = glm::dvec3(0, 0, 0);
}
@@ -655,8 +656,9 @@ void TTrack::Load(cParser *parser, glm::dvec3 const &pOrigin)
if( eType != tt_Cross ) {
// dla skrzyżowań muszą być podane kontrolne
if( ( glm::length(( p1 + p1 + p2 ) / 3.0 - p1 - cp1 ) < 0.02 )
|| ( glm::length(( p1 + p2 + p2 ) / 3.0 - p2 + cp1 ) < 0.02 ) ) {
// length2 is better than length for comparing because it does not require sqrt function
if( (glm::length2(( p1 + p1 + p2 ) / 3.0 - p1 - cp1 ) < 0.02 * 0.02)
|| (glm::length2(( p1 + p2 + p2 ) / 3.0 - p2 + cp1 ) < 0.02 * 0.02) ) {
// "prostowanie" prostych z kontrolnymi, dokładność 2cm
cp1 = cp2 = glm::dvec3(0, 0, 0);
}
@@ -719,8 +721,9 @@ void TTrack::Load(cParser *parser, glm::dvec3 const &pOrigin)
if( eType != tt_Cross ) {
// dla skrzyżowań muszą być podane kontrolne
if( ( glm::length(( p3 + p3 + p4 ) / 3.0 - p3 - cp3) < 0.02 )
|| ( glm::length(( p3 + p4 + p4 ) / 3.0 - p4 + cp3) < 0.02 ) ) {
// length2 is better than length for comparing because it does not require sqrt function
if( (glm::length2(( p3 + p3 + p4 ) / 3.0 - p3 - cp3) < 0.02 * 0.02)
|| (glm::length2(( p3 + p4 + p4 ) / 3.0 - p4 + cp3) < 0.02 * 0.02) ) {
// "prostowanie" prostych z kontrolnymi, dokładność 2cm
cp3 = cp4 = glm::dvec3(0, 0, 0);
}