22#ifndef EOS_TEXTURE_EXTRACTION_HPP
23#define EOS_TEXTURE_EXTRACTION_HPP
25#include "eos/core/Image.hpp"
26#include "eos/core/Mesh.hpp"
27#include "eos/render/ProjectionType.hpp"
28#include "eos/render/detail/RayDirection.hpp"
29#include "eos/render/vertex_visibility.hpp"
30#include "eos/render/transforms.hpp"
31#include "eos/render/Rasterizer.hpp"
32#include "eos/render/FragmentShader.hpp"
33#include "eos/render/ray_triangle_intersect.hpp"
34#include "eos/render/detail/utils.hpp"
79 assert(mesh.
tti.empty() || mesh.
tti.size() == mesh.
tvi.size());
81 using detail::divide_by_w;
82 using detail::RayDirection;
83 using Eigen::Vector2f;
84 using Eigen::Vector2d;
85 using Eigen::Vector3f;
86 using Eigen::Vector3d;
87 using Eigen::Vector4f;
88 using Eigen::Vector4d;
92 Texture image_to_extract_from_as_tex = create_mipmapped_texture(image, 1);
93 extraction_rasterizer.enable_depth_test =
false;
94 extraction_rasterizer.perspective_correct_barycentric_weights =
false;
98 RayDirection ray_direction_type;
99 if (projection_type == ProjectionType::Orthographic)
101 ray_direction_type = RayDirection::Parallel;
104 ray_direction_type = RayDirection::TowardsOrigin;
110 mesh.
vertices, mesh.
tvi, view_model_matrix, ray_direction_type);
112 vector<Vector4f> wnd_coords;
115 Vector4f clip_coords = projection_matrix * view_model_matrix * vtx.homogeneous();
116 clip_coords = divide_by_w(clip_coords);
118 const Vector2f screen_coords =
120 clip_coords.x() = screen_coords.x();
121 clip_coords.y() = screen_coords.y();
122 wnd_coords.push_back(clip_coords);
126 const int tex_width = texturemap_resolution;
127 const int tex_height =
128 texturemap_resolution;
131 const auto& mesh_tti = mesh.
tti.empty() ? mesh.
tvi : mesh.
tti;
133 for (std::size_t triangle_index = 0; triangle_index < mesh.
tvi.size(); ++triangle_index)
136 const auto& tvi = mesh.
tvi[triangle_index];
137 const auto& tti = mesh_tti[triangle_index];
140 if (per_vertex_visibility[tvi[0]] && per_vertex_visibility[tvi[1]] &&
141 per_vertex_visibility[tvi[2]])
151 detail::Vertex<double> pa{
152 Vector4d(mesh.
texcoords[tti[0]][0] * tex_width,
154 wnd_coords[tvi[0]].z(),
155 wnd_coords[tvi[0]].w()),
157 Vector2d(wnd_coords[tvi[0]].x() / image.width(),
158 wnd_coords[tvi[0]].y() /
162 detail::Vertex<double> pb{
163 Vector4d(mesh.
texcoords[tti[1]][0] * tex_width,
165 wnd_coords[tvi[1]].z(),
166 wnd_coords[tvi[1]].w()),
168 Vector2d(wnd_coords[tvi[1]].x() / image.width(),
169 wnd_coords[tvi[1]].y() /
173 detail::Vertex<double> pc{
174 Vector4d(mesh.
texcoords[tti[2]][0] * tex_width,
176 wnd_coords[tvi[2]].z(),
177 wnd_coords[tvi[2]].w()),
179 Vector2d(wnd_coords[tvi[2]].x() / image.width(),
180 wnd_coords[tvi[2]].y() /
186 if (pa.texcoords.x() < 0 || pa.texcoords.x() > 1 || pa.texcoords.y() < 0 || pa.texcoords.y() > 1 ||
187 pb.texcoords.x() < 0 || pb.texcoords.x() > 1 || pb.texcoords.y() < 0 || pb.texcoords.y() > 1 ||
188 pc.texcoords.x() < 0 || pc.texcoords.x() > 1 || pc.texcoords.y() < 0 || pc.texcoords.y() > 1)
192 extraction_rasterizer.
raster_triangle(pa, pb, pc, image_to_extract_from_as_tex);
196 return extraction_rasterizer.colorbuffer;
Class to represent images.
Definition: Image.hpp:44
Todo.
Definition: Rasterizer.hpp:50
void raster_triangle(const detail::Vertex< T > &point_a, const detail::Vertex< T > &point_b, const detail::Vertex< T > &point_c, const cpp17::optional< Texture > &texture)
Todo.
Definition: Rasterizer.hpp:67
Represents a texture for rendering.
Definition: Texture.hpp:64
eos::core::Image4u extract_texture(const core::Mesh &mesh, Eigen::Matrix4f view_model_matrix, Eigen::Matrix4f projection_matrix, ProjectionType projection_type, const eos::core::Image4u &image, int texturemap_resolution=512)
Extracts the texture from the given image and returns a texture map.
Definition: texture_extraction.hpp:70
Eigen::Vector2f clip_to_screen_space(const Eigen::Vector2f &clip_coordinates, int screen_width, int screen_height)
Definition: transforms.hpp:47
std::vector< bool > compute_per_vertex_self_occlusion(const std::vector< Eigen::Vector3f > &viewspace_vertices, const std::vector< std::array< int, 3 > > &triangle_vertex_indices, detail::RayDirection ray_direction_type)
Definition: vertex_visibility.hpp:119
ProjectionType
Definition: ProjectionType.hpp:31
core::Image4u render(const core::Mesh &mesh, const Eigen::Matrix4f &model_view_matrix, const Eigen::Matrix4f &projection_matrix, int viewport_width, int viewport_height, bool enable_backface_culling=false, bool enable_near_clipping=true, bool enable_far_clipping=true)
Definition: render.hpp:52
Namespace containing all of eos's 3D model fitting functionality.
This class represents a 3D mesh consisting of vertices, vertex colour information and texture coordin...
Definition: Mesh.hpp:45
std::vector< Eigen::Vector3f > vertices
3D vertex positions.
Definition: Mesh.hpp:46
std::vector< std::array< int, 3 > > tvi
Triangle vertex indices.
Definition: Mesh.hpp:50
std::vector< std::array< int, 3 > > tti
Triangle texture indices.
Definition: Mesh.hpp:52
std::vector< Eigen::Vector2f > texcoords
Texture coordinates.
Definition: Mesh.hpp:48