0

enter image description here

I believe this is a simple error as every other file is complaining about the same error. But I tried to put those helper functions in a namespace called Tools, how can it still report this error? The whole project can be found here https://github.com/luming89/MyGameEngine. BTW, if you know how to replace any of these helpers by a glm one, please tell me. I will really appreciate that!

// 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
Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
Asgard
  • 103
  • 1
  • 2
  • 9
  • 2
    Do not post a picture of the error. post the actual error test. – NathanOliver Oct 08 '15 at 20:20
  • Looks like you are implementing functions in headers and including the header in multiple source files. Instead, have declarations in headers and implementations in source file. – Mahesh Oct 08 '15 at 20:22
  • A little advise; and more of a personal preference when using constant values such as PI try not to use #define macros, instead use this declaration in your header `static const float PI;` and in the corresponding cpp file define it as `const float PI = 4.0f * atan(1.0f); // tan(pi/4) = 1` just make sure to include the correct header for the atan() function. – Francis Cugler Oct 09 '15 at 00:10
  • @Francis Cugler Does this provide any advantages? I mean if this is better, I'd like to adopt it. Currently I'm thinking macros will not consume memory as it is removed after preprocessing. But static const float PI will? Am I correct? – Asgard Oct 16 '15 at 05:29
  • @Asgard if you are using a const value that is specific to a class then static const type is the way to go; if it is file global you can drop the static qualifier. #define works good for string types. Here is a good read http://stackoverflow.com/questions/1637332/static-const-vs-define – Francis Cugler Oct 17 '15 at 18:07

1 Answers1

7

You are putting the definitions in a header file, which means that each translation unit (.cpp file) is getting a copy. Place the definitions in a .cpp file instead and put declarations in the .h file. (Or you could make the functions inline.)

rlbond
  • 65,341
  • 56
  • 178
  • 228