22 #ifndef RENDER_UTILS_HPP_ 23 #define RENDER_UTILS_HPP_ 25 #include "eos/render/Mesh.hpp" 27 #include "opencv2/core/core.hpp" 28 #include "opencv2/imgproc/imgproc.hpp" 50 inline cv::Vec2f
clip_to_screen_space(
const cv::Vec2f& clip_coordinates,
int screen_width,
int screen_height)
53 const float x_ss = (clip_coordinates[0] + 1.0f) * (screen_width / 2.0f);
54 const float y_ss = screen_height - (clip_coordinates[1] + 1.0f) * (screen_height / 2.0f);
55 return cv::Vec2f(x_ss, y_ss);
76 inline cv::Vec2f
screen_to_clip_space(
const cv::Vec2f& screen_coordinates,
int screen_width,
int screen_height)
78 const float x_cs = screen_coordinates[0] / (screen_width / 2.0f) - 1.0f;
79 float y_cs = screen_coordinates[1] / (screen_height / 2.0f) - 1.0f;
81 return cv::Vec2f(x_cs, y_cs);
97 cv::Vec3f n = (v1 - v0).cross(v2 - v0);
117 image = cv::Mat(512, 512, CV_8UC4, Scalar(0.0f, 0.0f, 0.0f, 255.0f));
120 for (
const auto& triIdx : mesh.
tvi) {
121 cv::line(image, Point2f(mesh.
texcoords[triIdx[0]][0] * image.cols, mesh.
texcoords[triIdx[0]][1] * image.rows), Point2f(mesh.
texcoords[triIdx[1]][0] * image.cols, mesh.
texcoords[triIdx[1]][1] * image.rows), Scalar(255.0f, 0.0f, 0.0f));
122 cv::line(image, Point2f(mesh.
texcoords[triIdx[1]][0] * image.cols, mesh.
texcoords[triIdx[1]][1] * image.rows), Point2f(mesh.
texcoords[triIdx[2]][0] * image.cols, mesh.
texcoords[triIdx[2]][1] * image.rows), Scalar(255.0f, 0.0f, 0.0f));
123 cv::line(image, Point2f(mesh.
texcoords[triIdx[2]][0] * image.cols, mesh.
texcoords[triIdx[2]][1] * image.rows), Point2f(mesh.
texcoords[triIdx[0]][0] * image.cols, mesh.
texcoords[triIdx[0]][1] * image.rows), Scalar(255.0f, 0.0f, 0.0f));
129 unsigned int get_max_possible_mipmaps_num(
unsigned int width,
unsigned int height)
131 unsigned int mipmapsNum = 1;
132 unsigned int size = std::max(width, height);
145 inline bool is_power_of_two(
int x)
147 return !(x & (x - 1));
159 std::vector<cv::Mat> mipmaps;
160 unsigned char widthLog, heightLog;
164 unsigned int mipmaps_num;
168 Texture create_mipmapped_texture(cv::Mat image,
unsigned int mipmapsNum = 0) {
169 assert(image.type() == CV_8UC3 || image.type() == CV_8UC4);
173 texture.mipmaps_num = (mipmapsNum == 0 ? get_max_possible_mipmaps_num(image.cols, image.rows) : mipmapsNum);
183 if (texture.mipmaps_num > 1)
185 if (!is_power_of_two(image.cols) || !is_power_of_two(image.rows))
187 throw std::runtime_error(
"Error: Couldn't generate mipmaps, width or height not power of two.");
190 if (image.type() == CV_8UC3)
192 image.convertTo(image, CV_8UC4);
193 cv::cvtColor(image, image, CV_BGR2BGRA);
196 int currWidth = image.cols;
197 int currHeight = image.rows;
198 std::vector<cv::Mat> mipmaps;
199 for (
int i = 0; i < texture.mipmaps_num; i++)
202 mipmaps.push_back(image);
205 cv::Mat currMipMap(currHeight, currWidth, CV_8UC4);
206 cv::resize(mipmaps[i - 1], currMipMap, currMipMap.size());
207 mipmaps.push_back(currMipMap);
215 texture.mipmaps = mipmaps;
216 texture.widthLog = (uchar)(std::log(mipmaps[0].cols) / CV_LOG2 + 0.0001f);
217 texture.heightLog = (uchar)(std::log(mipmaps[0].rows) / CV_LOG2 + 0.0001f);
cv::Vec3f calculate_face_normal(const cv::Vec3f &v0, const cv::Vec3f &v1, const cv::Vec3f &v2)
Definition: utils.hpp:95
Represents a texture for rendering.
Definition: utils.hpp:156
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
cv::Mat draw_texcoords(Mesh mesh, cv::Mat image=cv::Mat())
Definition: utils.hpp:111
cv::Vec2f screen_to_clip_space(const cv::Vec2f &screen_coordinates, int screen_width, int screen_height)
Definition: utils.hpp:76
Namespace containing all of eos's 3D model fitting functionality.
cv::Vec2f clip_to_screen_space(const cv::Vec2f &clip_coordinates, int screen_width, int screen_height)
Definition: utils.hpp:50
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