Refactored unnecessary Normalize, DotProduct, CrossProduct and Clamp functions

dev
Tomasz Kapuściński 2022-01-04 02:10:36 +01:00
parent 50fe905455
commit db3b1d74d1
13 changed files with 35 additions and 68 deletions

View File

@ -197,8 +197,8 @@ void CInput::EventProcess(Event& event)
} }
} }
event.motionInput = Math::Clamp(m_joyMotion + m_keyMotion, glm::vec3(-1.0f, -1.0f, -1.0f), glm::vec3(1.0f, 1.0f, 1.0f)); event.motionInput = glm::clamp(m_joyMotion + m_keyMotion, glm::vec3(-1.0f, -1.0f, -1.0f), glm::vec3(1.0f, 1.0f, 1.0f));
event.cameraInput = Math::Clamp(m_joyMotionCam + m_cameraKeyMotion, glm::vec3(-1.0f, -1.0f, -1.0f), glm::vec3(1.0f, 1.0f, 1.0f)); event.cameraInput = glm::clamp(m_joyMotionCam + m_cameraKeyMotion, glm::vec3(-1.0f, -1.0f, -1.0f), glm::vec3(1.0f, 1.0f, 1.0f));
} }
void CInput::MouseMove(const glm::ivec2& pos) void CInput::MouseMove(const glm::ivec2& pos)

View File

@ -1096,7 +1096,7 @@ bool CCamera::EventFrameFree(const Event &event, bool keysAllowed)
else else
m_heightEye -= cameraMove.z; m_heightEye -= cameraMove.z;
m_heightEye = Math::Clamp(m_heightEye, -2.0f, 500.0f); m_heightEye = glm::clamp(m_heightEye, -2.0f, 500.0f);
m_terrain->AdjustToBounds(m_eyePt, 10.0f); m_terrain->AdjustToBounds(m_eyePt, 10.0f);
@ -1134,7 +1134,7 @@ bool CCamera::EventFrameBack(const Event &event)
m_addDirectionH = Math::NormAngle(m_addDirectionH); m_addDirectionH = Math::NormAngle(m_addDirectionH);
m_addDirectionV = Math::NormAngle(m_addDirectionV); m_addDirectionV = Math::NormAngle(m_addDirectionV);
m_backDist = Math::Clamp(m_backDist, m_backMin, 200.0f); m_backDist = glm::clamp(m_backDist, m_backMin, 200.0f);
// Increase the special framework // Increase the special framework
float centeringH = 0.0f; float centeringH = 0.0f;
@ -1264,8 +1264,8 @@ bool CCamera::EventFrameFix(const Event &event)
m_fixDirectionH = Math::NormAngle(m_fixDirectionH); m_fixDirectionH = Math::NormAngle(m_fixDirectionH);
m_fixDirectionV = Math::Clamp(m_fixDirectionV, -0.5f*Math::PI, 0.25f*Math::PI); m_fixDirectionV = glm::clamp(m_fixDirectionV, -0.5f*Math::PI, 0.25f*Math::PI);
m_fixDist = Math::Clamp(m_fixDist, 10.0f, 200.0f); m_fixDist = glm::clamp(m_fixDist, 10.0f, 200.0f);
if (m_cameraObj != nullptr) if (m_cameraObj != nullptr)
{ {
@ -1346,11 +1346,11 @@ bool CCamera::EventFrameVisit(const Event &event)
// ZoomIn/ZoomOut // ZoomIn/ZoomOut
m_visitDist += cameraMove.z; m_visitDist += cameraMove.z;
m_visitDist = Math::Clamp(m_visitDist, 20.0f, 200.0f); m_visitDist = glm::clamp(m_visitDist, 20.0f, 200.0f);
// Up/Down // Up/Down
m_visitDirectionV += cameraMove.y; m_visitDirectionV += cameraMove.y;
m_visitDirectionV = Math::Clamp(m_visitDirectionV, -Math::PI * 0.40f, 0.0f); m_visitDirectionV = glm::clamp(m_visitDirectionV, -Math::PI * 0.40f, 0.0f);
float angleH = (m_visitTime / 10.0f) * (Math::PI * 2.0f); float angleH = (m_visitTime / 10.0f) * (Math::PI * 2.0f);
float angleV = m_visitDirectionV; float angleV = m_visitDirectionV;

View File

@ -2019,7 +2019,7 @@ int CEngine::ComputeSphereVisibility(const Math::Matrix& m, const glm::vec3& cen
bool CEngine::InPlane(glm::vec3 normal, float originPlane, glm::vec3 center, float radius) bool CEngine::InPlane(glm::vec3 normal, float originPlane, glm::vec3 center, float radius)
{ {
float distance = originPlane + Math::DotProduct(normal, center); float distance = originPlane + glm::dot(normal, center);
if (distance < -radius) if (distance < -radius)
return false; return false;
@ -3764,8 +3764,8 @@ void CEngine::Capture3DScene()
{ {
for (int j = -3; j <= 3; j++) for (int j = -3; j <= 3; j++)
{ {
int xp = Math::Clamp(x + i, 0, newWidth - 1); int xp = glm::clamp(x + i, 0, newWidth - 1);
int yp = Math::Clamp(y + j, 0, newHeight - 1); int yp = glm::clamp(y + j, 0, newHeight - 1);
float weight = matrix[i + 3][j + 3]; float weight = matrix[i + 3][j + 3];
@ -3780,7 +3780,7 @@ void CEngine::Capture3DScene()
for (int k = 0; k < 4; k++) for (int k = 0; k < 4; k++)
{ {
float value = Math::Clamp(color[k], 0.0f, 255.0f); float value = glm::clamp(color[k], 0.0f, 255.0f);
blured[index + k] = static_cast<unsigned char>(value); blured[index + k] = static_cast<unsigned char>(value);
} }
} }
@ -4868,10 +4868,10 @@ void CEngine::DrawShadowSpots()
} }
} }
corner[0] = Math::CrossProduct(corner[0], m_shadowSpots[i].normal); corner[0] = glm::cross(corner[0], m_shadowSpots[i].normal);
corner[1] = Math::CrossProduct(corner[1], m_shadowSpots[i].normal); corner[1] = glm::cross(corner[1], m_shadowSpots[i].normal);
corner[2] = Math::CrossProduct(corner[2], m_shadowSpots[i].normal); corner[2] = glm::cross(corner[2], m_shadowSpots[i].normal);
corner[3] = Math::CrossProduct(corner[3], m_shadowSpots[i].normal); corner[3] = glm::cross(corner[3], m_shadowSpots[i].normal);
corner[0] += pos; corner[0] += pos;
corner[1] += pos; corner[1] += pos;

View File

@ -239,7 +239,7 @@ void CLightning::Draw()
glm::vec3 p1 = m_pos; glm::vec3 p1 = m_pos;
glm::vec3 eye = m_engine->GetEyePt(); glm::vec3 eye = m_engine->GetEyePt();
float a = Math::RotateAngle(eye.x-p1.x, eye.z-p1.z); float a = Math::RotateAngle(eye.x-p1.x, eye.z-p1.z);
glm::vec3 n = Math::Normalize(p1-eye); glm::vec3 n = glm::normalize(p1-eye);
glm::vec3 corner[4]; glm::vec3 corner[4];
Vertex vertex[4]; Vertex vertex[4];

View File

@ -131,9 +131,9 @@ void CGL33Device::DebugLights()
if (l.type == LIGHT_DIRECTIONAL) if (l.type == LIGHT_DIRECTIONAL)
{ {
Gfx::VertexCol v[2]; Gfx::VertexCol v[2];
v[0].coord = -Math::Normalize(l.direction) * 100.0f + glm::vec3(0.0f, 0.0f, 1.0f) * float(i); v[0].coord = -glm::normalize(l.direction) * 100.0f + glm::vec3(0.0f, 0.0f, 1.0f) * float(i);
v[0].color = HSV2RGB(color); v[0].color = HSV2RGB(color);
v[1].coord = Math::Normalize(l.direction) * 100.0f + glm::vec3(0.0f, 0.0f, 1.0f) * float(i); v[1].coord = glm::normalize(l.direction) * 100.0f + glm::vec3(0.0f, 0.0f, 1.0f) * float(i);
v[1].color = HSV2RGB(color); v[1].color = HSV2RGB(color);
while (v[0].coord.y < 60.0f && v[0].coord.y < 60.0f) while (v[0].coord.y < 60.0f && v[0].coord.y < 60.0f)
{ {
@ -142,7 +142,7 @@ void CGL33Device::DebugLights()
} }
DrawPrimitive(PrimitiveType::LINES, v, 2); DrawPrimitive(PrimitiveType::LINES, v, 2);
v[0].coord = v[1].coord + Math::Normalize(v[0].coord - v[1].coord) * 50.0f; v[0].coord = v[1].coord + glm::normalize(v[0].coord - v[1].coord) * 50.0f;
glLineWidth(10.0f); glLineWidth(10.0f);
DrawPrimitive(PrimitiveType::LINES, v, 2); DrawPrimitive(PrimitiveType::LINES, v, 2);
@ -192,7 +192,7 @@ void CGL33Device::DebugLights()
DrawPrimitive(PrimitiveType::LINE_STRIP, v, 5); DrawPrimitive(PrimitiveType::LINE_STRIP, v, 5);
v[0].coord = l.position; v[0].coord = l.position;
v[1].coord = l.position + Math::Normalize(l.direction) * 100.0f; v[1].coord = l.position + glm::normalize(l.direction) * 100.0f;
glEnable(GL_LINE_STIPPLE); glEnable(GL_LINE_STIPPLE);
glLineStipple(3.0, 0xFF); glLineStipple(3.0, 0xFF);
DrawPrimitive(PrimitiveType::LINES, v, 2); DrawPrimitive(PrimitiveType::LINES, v, 2);

View File

@ -415,7 +415,7 @@ GLenum TranslateGfxBlendFunc(BlendFunc func)
bool InPlane(glm::vec3 normal, float originPlane, glm::vec3 center, float radius) bool InPlane(glm::vec3 normal, float originPlane, glm::vec3 center, float radius)
{ {
float distance = originPlane + Math::DotProduct(normal, center); float distance = originPlane + glm::dot(normal, center);
if (distance < -radius) if (distance < -radius)
return false; return false;

View File

@ -5526,7 +5526,7 @@ void CRobotMain::UpdateChapterPassed()
//! Changes game speed //! Changes game speed
void CRobotMain::SetSpeed(float speed) void CRobotMain::SetSpeed(float speed)
{ {
speed = Math::Clamp(speed, MIN_SPEED, MAX_SPEED); speed = glm::clamp(speed, MIN_SPEED, MAX_SPEED);
m_app->SetSimulationSpeed(speed); m_app->SetSimulationSpeed(speed);
UpdateSpeedLabel(); UpdateSpeedLabel();

View File

@ -93,15 +93,6 @@ inline float Max(float a, float b, float c, float d, float e)
return Math::Max( Math::Max(a, b), Math::Max(c, d), e ); return Math::Max( Math::Max(a, b), Math::Max(c, d), e );
} }
//! Clamps the value to a range specified by min and max
template<typename T>
inline T Clamp(T value, T min, T max)
{
if (value < min) return min;
else if (value > max) return max;
else return value;
}
//! Returns the normalized value (0 .. 1) //! Returns the normalized value (0 .. 1)
inline float Norm(float a) inline float Norm(float a)
{ {

View File

@ -279,7 +279,7 @@ inline void LoadViewMatrix(Math::Matrix &mat, const glm::vec3 &from,
// Get the dot product, and calculate the projection of the z basis // Get the dot product, and calculate the projection of the z basis
// vector onto the up vector. The projection is the y basis vector. // vector onto the up vector. The projection is the y basis vector.
float dotProduct = DotProduct(worldUp, view); float dotProduct = glm::dot(worldUp, view);
glm::vec3 up = worldUp - dotProduct * view; glm::vec3 up = worldUp - dotProduct * view;
@ -303,7 +303,7 @@ inline void LoadViewMatrix(Math::Matrix &mat, const glm::vec3 &from,
// The x basis vector is found simply with the cross product of the y // The x basis vector is found simply with the cross product of the y
// and z basis vectors // and z basis vectors
glm::vec3 right = CrossProduct(up, view); glm::vec3 right = glm::cross(up, view);
// Start building the matrix. The first three rows contains the basis // Start building the matrix. The first three rows contains the basis
// vectors used to rotate the view to point at the lookat point // vectors used to rotate the view to point at the lookat point
@ -320,9 +320,9 @@ inline void LoadViewMatrix(Math::Matrix &mat, const glm::vec3 &from,
/* (3,3) */ mat.m[10] = view.z; /* (3,3) */ mat.m[10] = view.z;
// Do the translation values (rotations are still about the eyepoint) // Do the translation values (rotations are still about the eyepoint)
/* (1,4) */ mat.m[12] = -DotProduct(from, right); /* (1,4) */ mat.m[12] = -glm::dot(from, right);
/* (2,4) */ mat.m[13] = -DotProduct(from, up); /* (2,4) */ mat.m[13] = -glm::dot(from, up);
/* (3,4) */ mat.m[14] = -DotProduct(from, view); /* (3,4) */ mat.m[14] = -glm::dot(from, view);
} }
//! Loads a perspective projection matrix //! Loads a perspective projection matrix
@ -449,7 +449,7 @@ inline void LoadRotationMatrix(Math::Matrix &mat, const glm::vec3 &dir, float an
{ {
float cos = cosf(angle); float cos = cosf(angle);
float sin = sinf(angle); float sin = sinf(angle);
glm::vec3 v = Normalize(dir); glm::vec3 v = glm::normalize(dir);
mat.LoadIdentity(); mat.LoadIdentity();
@ -508,7 +508,7 @@ inline glm::vec3 NormalToPlane(const glm::vec3 &p1, const glm::vec3 &p2, const g
glm::vec3 u = p3 - p1; glm::vec3 u = p3 - p1;
glm::vec3 v = p2 - p1; glm::vec3 v = p2 - p1;
return Normalize(CrossProduct(u, v)); return glm::normalize(glm::cross(u, v));
} }
//! Returns a point on the line \a p1 - \a p2, in \a dist distance from \a p1 //! Returns a point on the line \a p1 - \a p2, in \a dist distance from \a p1
@ -622,8 +622,8 @@ inline glm::vec3 Transform(const Math::Matrix &m, const glm::vec3 &p)
*/ */
inline glm::vec3 Projection(const glm::vec3 &a, const glm::vec3 &b, const glm::vec3 &p) inline glm::vec3 Projection(const glm::vec3 &a, const glm::vec3 &b, const glm::vec3 &p)
{ {
float k = DotProduct(b - a, p - a); float k = glm::dot(b - a, p - a);
k /= DotProduct(b - a, b - a); k /= glm::dot(b - a, b - a);
return a + k*(b-a); return a + k*(b-a);
} }

View File

@ -45,24 +45,6 @@ inline bool VectorsEqual(const glm::vec3 &a, const glm::vec3 &b, float tolerance
&& IsEqual(a.z, b.z, tolerance); && IsEqual(a.z, b.z, tolerance);
} }
//! Convenience function for getting normalized vector
inline glm::vec3 Normalize(const glm::vec3 &v)
{
return glm::normalize(v);
}
//! Convenience function for calculating dot product
inline float DotProduct(const glm::vec3 &left, const glm::vec3 &right)
{
return glm::dot(left, right);
}
//! Convenience function for calculating cross product
inline glm::vec3 CrossProduct(const glm::vec3 &left, const glm::vec3 &right)
{
return glm::cross(left, right);
}
//! Convenience function for calculating angle (in radians) between two vectors //! Convenience function for calculating angle (in radians) between two vectors
inline float Angle(const glm::vec3 &a, const glm::vec3 &b) inline float Angle(const glm::vec3 &a, const glm::vec3 &b)
{ {
@ -75,12 +57,6 @@ inline float Distance(const glm::vec3 &a, const glm::vec3 &b)
return glm::distance(a, b); return glm::distance(a, b);
} }
//! Clamps the vector \a vec to range between \a min and \a max
inline glm::vec3 Clamp(const glm::vec3&vec, const glm::vec3&min, const glm::vec3&max)
{
return glm::clamp(vec, min, max);
}
inline std::string ToString(const glm::vec3& vector) inline std::string ToString(const glm::vec3& vector)
{ {
std::stringstream s; std::stringstream s;

View File

@ -176,7 +176,7 @@ float CObjectManager::ClampPower(ObjectType type, float power)
{ {
max = 1; max = 1;
} }
return Math::Clamp(power, min, max); return glm::clamp(power, min, max);
} }
std::vector<CObject*> CObjectManager::GetObjectsOfTeam(int team) std::vector<CObject*> CObjectManager::GetObjectsOfTeam(int team)

View File

@ -332,7 +332,7 @@ void CScreenModList::FindMods()
m_modManager->FindMods(); m_modManager->FindMods();
if (m_modManager->CountMods() != 0) if (m_modManager->CountMods() != 0)
{ {
m_modSelectedIndex = Math::Clamp(m_modSelectedIndex, static_cast<size_t>(0), m_modManager->CountMods() - 1); m_modSelectedIndex = glm::clamp(m_modSelectedIndex, static_cast<size_t>(0), m_modManager->CountMods() - 1);
} }
} }

View File

@ -58,7 +58,7 @@ TEST(VectorTest, DotTest)
float expectedDot = -0.238988896477326; float expectedDot = -0.238988896477326;
EXPECT_TRUE(Math::IsEqual(Math::DotProduct(vecA, vecB), expectedDot, TEST_TOLERANCE)); EXPECT_TRUE(Math::IsEqual(glm::dot(vecA, vecB), expectedDot, TEST_TOLERANCE));
} }
TEST(VectorTest, CrossTest) TEST(VectorTest, CrossTest)