没有参数的构造函数不起作用,但一个工作

时间:2015-05-12 15:02:11

标签: c++

我正在尝试用C ++编写矩阵结构,我是新手。 mat4.h文件:

#include "math.h"
namespace engine {
    namespace math {
        struct mat4 {
            float elements[4 * 4]; // column major ordering, index = row + col * 4

            mat4();
            mat4(float diagonal);


            mat4& mul(const mat4& other);
            static mat4 identity(); // construct and return an identity matrix
            static mat4 orthographic(float left, float right, float bottom, float top, float near, float far); // boundaries (clipping planes)
            static mat4 perspective(float fov, float aspectRatio, float near, float far);
            static mat4 translation(const vec3& translation);
            static mat4 rotation(float angle, const vec3 & axis);
            static mat4 scale(const vec3 & scale);
            friend mat4 operator*(mat4 left, const mat4 & right);
            mat4& operator*=(const mat4 &other); 

        };
    }
}

这是mat4.cpp:

#include "mat4.h"
namespace engine {
    namespace math {

        mat4::mat4() {
            for (int i = 0; i < 4 * 4; i++)
                elements[i] = 0;
        }
        mat4::mat4(float diagonal) {
            for (int i = 0; i < 4 * 4; ++i) {
                elements[i] = 0;
            }
            for(int i = 0; i < 4; i += 1)
                elements[i + i * 4] = diagonal;
        }
        mat4& mat4::mul(const mat4 &other) {
            for (int i = 0; i < 4; ++i)        // col
                for (int j = 0; j < 4; ++j) {  // row
                    float sum = 0;
                    for (int k = 0; k < 4; ++k)
                        sum += elements[j + k * 4] * other.elements[k + i * 4];
                    elements[j + i * 4] = sum;
                }
        }

        mat4 mat4::identity() {
            return mat4(1);
        }

        mat4 operator*(mat4 left, const mat4 &right) {
            return left.mul(right);
        }

        mat4 &mat4::operator*=(const mat4 &other) {
            return mul(other);
        }

        mat4 mat4::orthographic(float left, float right, float bottom, float top, float near, float far) {
            mat4 result(1);
            result.elements[0 + 0 * 4] =  2.0f / (right - left);
            result.elements[1 + 1 * 4] =  2.0f / (top - bottom);
            result.elements[2 + 2 * 4] = -2.0f / (far - near);
            result.elements[0 + 3 * 4] = (left + right) / (left - right);
            result.elements[1 + 3 * 4] = (bottom + top) / (bottom - top);
            result.elements[2 + 3 * 4] = (far + near)   / (far - near);
            //result.elements[3 + 3 * 4] = 1; this is achieved by mat result(1);
            return result;
        }

        mat4 mat4::perspective(float fov, float aspectRatio, float near, float far) {
            mat4 result();

            float q = 1.0f / (float) tan(toRadians(fov) / 2.0f);
            result.elements[0 + 0 * 4] = q / aspectRatio;
            result.elements[1 + 1 * 4] = q;
            result.elements[2 + 2 * 4] = (-near - far)  / (near - far);
            result.elements[3 + 2 * 4] = 1;
            result.elements[2 + 3 * 4] = 2 * far * near / (near - far);
            return result;
        }
    }
}

正如你所看到的,我有2个mat4构造函数,一个获得浮动对角线,一个没有参数。 在正交(...)结果中可以正确访问其元素,但在透视(...)中它不编译(当我做mat4 result();时),说“需要结构类型而不是'mat4()' ”。 但是,如果我将行mat4 result();更改为:

mat4 result = mat4();

然后一切都很好。 这是怎么回事?

1 个答案:

答案 0 :(得分:10)

mat4 result();声明一个函数。将其更改为mat4 result;以声明(并默认初始化)变量。