void Matrix2DClient::Draw()
{
mRenderTarget->Clear(D2D1::ColorF(0.34f, 0.39f, 0.4f, 1.0f));
makeGrid(100);
mHandle->draw();
//constant rotation between 0 and 360°
static float constRotation;
constRotation += constRotation + 1.7 < 360 ? 1.7 : -constRotation;
float angle = mHandle->getHandleAngle(); //Wheel Handle angle in degree
//cube points
const unsigned points = 8;
std::array<std::array<float, 3>, points> cubePoint = {
-70.82, -112.90, -40.75,//0
-0.34 , -170.82, 0.2,//1
0.11, -112.90, 81.71,//2
-70.59, -54.97, 40.75,//3
0.34, 2.38, -0.2,//4
0.11, -55.54, -81.71,//5
70.59, -113.46, -40.75,//6
70.82, -55.54, 40.75 //7
};
//rotation matrix on axis X
float RX[3][3] = { 1 , 0 , 0 ,
0 ,cos(rad(0)) ,sin(rad(0)),
0 ,-sin(rad(0)),cos(rad(0))
};
//rotation matrix on axis Y
float RY[3][3] = { cos(rad(constRotation)), 0 ,-sin(rad(constRotation)),
0 , 1 , 0 ,
sin(rad(constRotation)), 0, cos(rad(constRotation))
};
//rotation matrix on axis Z
float RZ[3][3] = { cos(rad(angle)), sin(rad(angle)), 0,
-sin(rad(angle)), cos(rad(angle)), 0,
0 , 0 , 1
};
std::array<std::array<float, 3>, 3> finalMatrix = mBasisVectors;
std::array<std::array<float, 3>, 3> intermediateMatrix1 = mBasisVectors;
std::array<std::array<float, 3>, 3> intermediateMatrix2 = mBasisVectors;
//rotate on axis X (basisVectors x RotationX) = intermediateMatrix1
for (size_t i = 0; i < 3; i++)
{
for (size_t j = 0; j < 3; j++)
{
float sum = 0;
for (size_t k = 0; k < 3; k++)
{
sum += mBasisVectors[i][k] * RX[k][j];
}
intermediateMatrix1[i][j] = sum;
}
}
//rotate on axis Y (intermediateMatrix1 x RotationY) = intermediateMatrix2
for (size_t i = 0; i < 3; i++)
{
for (size_t j = 0; j < 3; j++)
{
float sum = 0;
for (size_t k = 0; k < 3; k++)
{
sum += intermediateMatrix1[i][k] * RY[k][j];
}
intermediateMatrix2[i][j] = sum;
}
}
//rotate on axis Z (intermediateMatrix2 x RotationZ) = finalMatrix
for (size_t i = 0; i < 3; i++)
{
for (size_t j = 0; j < 3; j++)
{
float sum = 0;
for (size_t k = 0; k < 3; k++)
{
sum += intermediateMatrix2[i][k] * RZ[k][j];
}
finalMatrix[i][j] = sum;
}
}
//for every point, apply the rotation finalMatrix
std::array<std::array<float, 3>, 8> temporaryPoints = cubePoint;
for (size_t i = 0; i < points; i++)
{
cubePoint[i][0] = temporaryPoints[i][0] * finalMatrix[0][0] + temporaryPoints[i][1] * finalMatrix[0][1] + temporaryPoints[i][2] * finalMatrix[0][2];//X
cubePoint[i][1] = temporaryPoints[i][0] * finalMatrix[1][0] + temporaryPoints[i][1] * finalMatrix[1][1] + temporaryPoints[i][2] * finalMatrix[1][2];//Y
cubePoint[i][2] = temporaryPoints[i][0] * finalMatrix[2][0] + temporaryPoints[i][1] * finalMatrix[2][1] + temporaryPoints[i][2] * finalMatrix[2][2];//Z
}
//draw cube. The points are calculated using x,y,z and the lines are drawn on screen using only x and y
mBrush->SetColor(D2D1::ColorF(0.17f, 0.83f, 0.96f, 1.f));
float strokeSize = 2.f;
mRenderTarget->DrawLine(D2D1::Point2F(mOriginX + cubePoint[0][0], mOriginY + cubePoint[0][1]), D2D1::Point2F(mOriginX + cubePoint[1][0], mOriginY + cubePoint[1][1]), mBrush, strokeSize);
mRenderTarget->DrawLine(D2D1::Point2F(mOriginX + cubePoint[1][0], mOriginY + cubePoint[1][1]), D2D1::Point2F(mOriginX + cubePoint[6][0], mOriginY + cubePoint[6][1]), mBrush, strokeSize);
mRenderTarget->DrawLine(D2D1::Point2F(mOriginX + cubePoint[6][0], mOriginY + cubePoint[6][1]), D2D1::Point2F(mOriginX + cubePoint[5][0], mOriginY + cubePoint[5][1]), mBrush, strokeSize);
mRenderTarget->DrawLine(D2D1::Point2F(mOriginX + cubePoint[5][0], mOriginY + cubePoint[5][1]), D2D1::Point2F(mOriginX + cubePoint[0][0], mOriginY + cubePoint[0][1]), mBrush, strokeSize);
mRenderTarget->DrawLine(D2D1::Point2F(mOriginX + cubePoint[1][0], mOriginY + cubePoint[1][1]), D2D1::Point2F(mOriginX + cubePoint[2][0], mOriginY + cubePoint[2][1]), mBrush, strokeSize);
mRenderTarget->DrawLine(D2D1::Point2F(mOriginX + cubePoint[6][0], mOriginY + cubePoint[6][1]), D2D1::Point2F(mOriginX + cubePoint[7][0], mOriginY + cubePoint[7][1]), mBrush, strokeSize);
mRenderTarget->DrawLine(D2D1::Point2F(mOriginX + cubePoint[5][0], mOriginY + cubePoint[5][1]), D2D1::Point2F(mOriginX + cubePoint[4][0], mOriginY + cubePoint[4][1]), mBrush, strokeSize);
mRenderTarget->DrawLine(D2D1::Point2F(mOriginX + cubePoint[0][0], mOriginY + cubePoint[0][1]), D2D1::Point2F(mOriginX + cubePoint[3][0], mOriginY + cubePoint[3][1]), mBrush, strokeSize);
mRenderTarget->DrawLine(D2D1::Point2F(mOriginX + cubePoint[3][0], mOriginY + cubePoint[3][1]), D2D1::Point2F(mOriginX + cubePoint[2][0], mOriginY + cubePoint[2][1]), mBrush, strokeSize);
mRenderTarget->DrawLine(D2D1::Point2F(mOriginX + cubePoint[2][0], mOriginY + cubePoint[2][1]), D2D1::Point2F(mOriginX + cubePoint[7][0], mOriginY + cubePoint[7][1]), mBrush, strokeSize);
mRenderTarget->DrawLine(D2D1::Point2F(mOriginX + cubePoint[7][0], mOriginY + cubePoint[7][1]), D2D1::Point2F(mOriginX + cubePoint[4][0], mOriginY + cubePoint[4][1]), mBrush, strokeSize);
mRenderTarget->DrawLine(D2D1::Point2F(mOriginX + cubePoint[4][0], mOriginY + cubePoint[4][1]), D2D1::Point2F(mOriginX + cubePoint[3][0], mOriginY + cubePoint[3][1]), mBrush, strokeSize);
}