I am working on developing a library to render 3d graphics on low resource systems. I am trying to understand the coordinate system pipeline in graphics. I know I have to follow these steps,
Local space
World space
View space
Clip space
Screen space
i apply the model matrix to the vertices and then i apply the view space matrix and then the projection matrix, and the result is the normalized vertices in the coordinates from -1 to 1, but i am not getting the normalized coordinates. i don’t understand what to do to get the normalized coordinates. please help me.
//vertices to create a cube from 12 triangles
std::vector<linear::vector3> vertices{
vector3(-1, -1, -1), vector3(1, -1, -1), vector3(1, 1, -1), vector3(-1, 1, -1),
vector3(-1, -1, 1), vector3(1, -1, 1), vector3(1, 1, 1), vector3(-1, 1, 1)
};
float aspec = 1080 / 720;
linear::Matrix4x4 modelMat = linear::modelMat(vector3(30, 30, 30), vector3(100, 100, 0 ), 0.0, 0.0, linear::toRadians(45.0));
linear::Matrix4x4 viewMat = linear::lookAt(vector3(0, 0, 5), vector3(0, 0, 0), vector3(0, 1, 0));
linear::Matrix4x4 projectionMat = linear::perspectiveMat(linear::toRadians(60.0f), aspec, 1.0f, 1000.0);
vector3 transformAndNormalizeVertex(const vector3& vertex, const Matrix4x4& modelMat, const Matrix4x4& viewMat, const Matrix4x4& projectionMat) {
vector4 vec1(vertex.x, vertex.y, vertex.z, 1.0);
vec1 = matrixVector(modelMat, vec1);
vec1 = matrixVector(viewMat, vec1);
vec1 = matrixVector(projectionMat, vec1);
if (vec1.w != 0.0) {
vec1.x /= vec1.w;
vec1.y /= vec1.w;
vec1.z /= vec1.w;
}
return vector3(vec1.x, vec1.y, vec1.z);
}
//code block to apply the transformations.
for (auto &triangle : triangles){
cout<< "triangle " << i << endl;
for (auto &vertex : triangle)
{
vertex = transformAndNormalizeVertex(vertex, modelMat, viewMat, projectionMat);
cout<< vertex.x << " " << vertex.y << " " << vertex.z << endl;
}
drawtriangle(linear::vertex2{round(triangle[0].x), round(triangle[0].y)}, linear::vertex2{round(triangle[1].x), round(triangle[1].y)}, linear::vertex2{round(triangle[2].x), round(triangle[2].y)});
}