0
The Rotatex(float) and Scale(float, float, float) functions of the mat4 class return the this pointer. The purpose was to create a temporary instance of mat4 to store the pre-multiplication between the Scale and Rotate functions. As follows:
mat4 mat4::RotateX(float theta){
matrix[1][1] = cos(DegToRad(theta));
matrix[2][1] = sin(DegToRad(theta));
matrix[1][2] = -sin(DegToRad(theta));
matrix[2][2] = cos(DegToRad(theta));
return *this;
}
mat4 mat4::Scale(float x, float y, float z){
matrix[0][0] = x;
matrix[1][1] = y;
matrix[2][2] = z;
return *this;
}
mat4 transform;
mat4 temp;
transform = temp.RotateX(theta) * temp.Scale(x, y, z);
You have the following attribute of the class:
float matrix[4][4]
The mat4 class has a multiplication operator overload (*) which performs a matrix multiplication operation.
mat4 mat4::operator*(const mat4& m) const{
mat4 mult;
for(uint8_t i = 0; i < 4; i++){
for(uint8_t j = 0; j < 4; j++){
mult[i][j] = matrix[i][0] * m[0][j]
+ matrix[i][1] * m[1][j]
+ matrix[i][2] * m[2][j]
+ matrix[i][3] * m[3][j];
}
}
return mult;
}
Thus, temp is the result of concatenation between a scale operation and rotation.
As it stands, this structure makes it possible to do this:
transform.Scale(x, y, z);
Is this legal for the language considering that Scale() returns the this pointer, which in turn stores the address of the instance that calls the function? That would be the same as copying objects?
A small observation for fromDiagonal function, in mat4.Matrix the correct one should be result.Matrix?
– Renato
Yes, you’re right. Corrected.
– darcamo