I am trying to implement homogeneous coordinates into my 3D renderer after being able to render an orthographic projection of a 3D cube using only 3×3 matrices. But now, things started going wrong. And there is not much tutorials online about this, but I want to be able to use 3d vectors and homogenous matrices to display a 3d cube in an SFML window using perspective projection.
GitHub repository
What is specifically wrong with my code is that I get large numbers for my vertices (as shown in the console log), and ultimately I don’t see the cube!
Cube.cpp: Screen Projection function, which handles all the 3d vector matrix multiplication. This is where the vertices changes to then be ultimately sent to p_cube_vertbuff, which is an SFML vertex array linestrip that should render a 3d Cube.
void Cube::screen_projection(Camera* camera, Projection* projection, sf::RenderWindow& wind) {
printf("Cuben");
for (int i = 0; i < vertices.size(); i++) {
printf("%f, %f, %fn", vertices[i].x, vertices[i].y, vertices[i].z);
vertices[i] = vec3_mat_mul(vertices[i], camera->camera_matrix());
// printf("%f, %f, %fn", vertices[i].x, vertices[i].y, vertices[i].z);
vertices[i] = vec3_mat_mul(vertices[i], projection->projection_matrix);
// printf("%f, %f, %fn", vertices[i].x, vertices[i].y, vertices[i].z);
vertices[i] = vec3_mat_mul(vertices[i], projection->to_screen_matrix);
// printf("%f, %f, %fn", vertices[i].x, vertices[i].y, vertices[i].z);
modified_vertices[i] = vec3_to_vec2(vertices[i]);
}
for (int i = 0; i < modified_vertices.size(); i++) {
// printf("%f, %fn", modified_vertices[i].x, modified_vertices[i].y);
(*(p_cube_vertbuff))[i].position = sf::Vector2f(modified_vertices[i].x, modified_vertices[i].y);
(*(p_cube_vertbuff))[0].color = sf::Color(255, 0, 0);
}
wind.draw(*p_cube_vertbuff);
}
I have tried to use structs for the vectors and make a matrix vector multiplication function and use the 4×4 matrices for perspective projection, model to view, camera transformations, and cube transformations, that I found online but they don’t seem to be compatible with my code perhaps due to their origins coming from OpenGL or Vulkan, but I don’t want to use any of those, just pure C++ and SFML (I know SFML uses OpenGl)