22 #ifndef RENDER_AFFINE_HPP_ 23 #define RENDER_AFFINE_HPP_ 25 #include "eos/render/detail/render_detail.hpp" 26 #include "eos/render/detail/render_affine_detail.hpp" 27 #include "eos/render/Mesh.hpp" 29 #include "opencv2/core/core.hpp" 52 std::pair<cv::Mat, cv::Mat>
render_affine(
Mesh mesh, cv::Mat affine_camera_matrix,
int viewport_width,
int viewport_height,
bool do_backface_culling =
true)
60 Mat colourbuffer = Mat::zeros(viewport_height, viewport_width, CV_8UC4);
61 Mat depthbuffer = std::numeric_limits<float>::max() * Mat::ones(viewport_height, viewport_width, CV_64FC1);
63 Mat affine_with_z = detail::calculate_affine_z_direction(affine_camera_matrix);
65 vector<detail::Vertex> projected_vertices;
66 projected_vertices.reserve(mesh.
vertices.size());
67 for (
int i = 0; i < mesh.
vertices.size(); ++i) {
68 Mat vertex_screen_coords = affine_with_z * Mat(mesh.
vertices[i]);
69 cv::Vec3f vertex_colour;
71 vertex_colour = cv::Vec3f(0.5f, 0.5f, 0.5f);
74 vertex_colour = mesh.
colors[i];
76 projected_vertices.push_back(detail::Vertex(vertex_screen_coords, vertex_colour, mesh.
texcoords[i]));
80 vector<detail::TriangleToRasterize> triangles_to_raster;
81 for (
const auto& tri_indices : mesh.
tvi) {
82 if (do_backface_culling) {
83 if (!detail::are_vertices_ccw_in_screen_space(projected_vertices[tri_indices[0]].position, projected_vertices[tri_indices[1]].position, projected_vertices[tri_indices[2]].position))
89 cv::Rect bounding_box = detail::calculate_clipped_bounding_box(projected_vertices[tri_indices[0]].position, projected_vertices[tri_indices[1]].position, projected_vertices[tri_indices[2]].position, viewport_width, viewport_height);
90 auto min_x = bounding_box.x;
91 auto max_x = bounding_box.x + bounding_box.width;
92 auto min_y = bounding_box.y;
93 auto max_y = bounding_box.y + bounding_box.height;
95 if (max_x <= min_x || max_y <= min_y)
98 detail::TriangleToRasterize t;
103 t.v0 = projected_vertices[tri_indices[0]];
104 t.v1 = projected_vertices[tri_indices[1]];
105 t.v2 = projected_vertices[tri_indices[2]];
107 triangles_to_raster.push_back(t);
111 for (
auto&& triangle : triangles_to_raster) {
112 detail::raster_triangle_affine(triangle, colourbuffer, depthbuffer);
114 return std::make_pair(colourbuffer, depthbuffer);
std::pair< cv::Mat, cv::Mat > render(Mesh mesh, cv::Mat model_view_matrix, cv::Mat projection_matrix, int viewport_width, int viewport_height, const boost::optional< Texture > &texture=boost::none, bool enable_backface_culling=false, bool enable_near_clipping=true, bool enable_far_clipping=true)
Definition: render.hpp:125
std::pair< cv::Mat, cv::Mat > render_affine(Mesh mesh, cv::Mat affine_camera_matrix, int viewport_width, int viewport_height, bool do_backface_culling=true)
Definition: render_affine.hpp:52
Namespace containing all of eos's 3D model fitting functionality.
std::vector< cv::Vec4f > vertices
3D vertex positions.
Definition: Mesh.hpp:47
std::vector< cv::Vec3f > colors
Colour information for each vertex. Expected to be in RGB order.
Definition: Mesh.hpp:48
This class represents a 3D mesh consisting of vertices, vertex colour information and texture coordin...
Definition: Mesh.hpp:45
std::vector< std::array< int, 3 > > tvi
Triangle vertex indices.
Definition: Mesh.hpp:51
std::vector< cv::Vec2f > texcoords
Texture coordinates for each vertex.
Definition: Mesh.hpp:49