Reputation: 2596
I have the following code in C that works just fine
typedef struct { float m[16]; } matrix;
matrix getProjectionMatrix(int w, int h)
{
float fov_y = 1;
float tanFov = tanf( fov_y * 0.5f );
float aspect = (float)w / (float)h;
float near = 1.0f;
float far = 1000.0f;
return (matrix) { .m = {
[0] = 1.0f / (aspect * tanFov ),
[5] = 1.0f / tanFov,
[10] = -1.f,
[11] = -1.0f,
[14] = -(2.0f * near)
}};
}
When I try to use it in C++ I get this compiler error:
error C2143: syntax error: missing ']' before 'constant'
Why so and what's the easiest way to port the code to C++?
Upvotes: 3
Views: 118
Reputation: 223699
You're attempting to use a designated initializer which is allowed in C but not C++.
You'll need to explicitly initialize the individual members:
return (matrix) { {
1.0f / (aspect * tanFov ),
0, 0, 0, 0,
1.0f / tanFov,
0, 0, 0, 0,
-1.f,
-1.0f,
0, 0,
-(2.0f * near),
0
}};
Upvotes: 6