0
我相信這是一個簡單的錯誤,因爲每個其他文件都抱怨同樣的錯誤。但是我試圖將這些幫助函數放在一個名爲Tools的名稱空間中,它又如何報告這個錯誤?整個項目可以在這裏找到https://github.com/luming89/MyGameEngine。順便說一句,如果你知道如何用glm來代替這些助手,請告訴我。我會很感激!
// This is the Tools.h file
#ifndef TOOLS_H
#define TOOLS_H
#define MATH_PI 3.1415926535897932384626433832795
#define ToRadians(x) (float)(((x) * MATH_PI/180.0f))
#define ToDegrees(x) (float)(((x) * 180.0f/MATH_PI))
typedef glm::detail::tquat<float, glm::precision::highp> Quaternion;
namespace Tools
{
glm::mat4 initRotationFromVectors(const glm::vec3& n, const glm::vec3& v, const glm::vec3& u) // forward, up, right
{
glm::mat4 res; // Identity?
res[0][0] = u.x; res[1][0] = u.y; res[2][0] = u.z; res[3][0] = 0;
res[0][1] = v.x; res[1][1] = v.y; res[2][1] = v.z; res[3][1] = 0;
res[0][2] = n.x; res[1][2] = n.y; res[2][2] = n.z; res[3][2] = 0;
res[0][3] = 0; res[1][3] = 0; res[2][3] = 0; res[3][3] = 1;
return res;
}
glm::vec3 transformToVec3(const glm::mat4& m, const glm::vec3& r)
{
glm::vec3 ret;
for (unsigned int i = 0; i < 3; i++)
{
ret[i] = 0;
for (unsigned int j = 0; j < 3; j++)
ret[i] += m[j][i] * r[j];
}
return ret;
}
glm::vec3 GetRight(const Quaternion r)
{
return glm::rotate(r, glm::vec3(1, 0, 0));
}
glm::mat4 initTranslation(glm::mat4& m, const glm::vec3& r)
{
for (unsigned int i = 0; i < 4; i++)
{
for (unsigned int j = 0; j < 4; j++)
{
if (i == 3 && j != 3)
m[i][j] = r[j];
else if (i == j)
m[i][j] = 1;
else
m[i][j] = 0;
}
}
m[3][3] = 1;
return m;
}
glm::mat4 initScale(glm::mat4& m, const glm::vec3& r)
{
for (unsigned int i = 0; i < 4; i++)
{
for (unsigned int j = 0; j < 4; j++)
{
if (i == j && i != 3)
m[i][j] = r[i];
else
m[i][j] = 0;
}
}
m[3][3] = 1;
return m;
}
glm::mat4 toRotationMatrix(const Quaternion& rot)
{
glm::vec3 forward = glm::vec3(2.0f * (rot.x * rot.z - rot.w * rot.y), 2.0f * (rot.y * rot.z + rot.w * rot.x), 1.0f - 2.0f * (rot.x * rot.x + rot.y * rot.y));
glm::vec3 up = glm::vec3(2.0f * (rot.x*rot.y + rot.w*rot.z), 1.0f - 2.0f * (rot.x*rot.x + rot.z*rot.z), 2.0f * (rot.y*rot.z - rot.w*rot.x));
glm::vec3 right = glm::vec3(1.0f - 2.0f * (rot.y*rot.y + rot.z*rot.z), 2.0f * (rot.x*rot.y - rot.w*rot.z), 2.0f * (rot.x*rot.z + rot.w*rot.y));
return initRotationFromVectors(forward, up, right);
}
}
#endif
不要發佈錯誤的圖片。發佈實際的錯誤測試。 – NathanOliver
看起來像是在頭文件中實現函數並在多個源文件中包含頭文件。相反,在源文件的頭文件和實現中有聲明。 – Mahesh
有點建議;當使用常量值比如PI時,更多的是個人偏好,儘量不要使用#define宏,而是在頭文件'static const float PI;'中使用這個聲明,並在相應的cpp文件中定義它爲'const float PI = 4.0 f * atan(1.0f); // tan(pi/4)= 1'只要確保包含atan()函數的正確標題即可。 –