This tutorial is based upon the previous one and explain how to add a background image to the rendering process. Our good ol' fiend 👾 from Space Invaders will finally have Space behind him.
👾's Space
One of the main reason to why this is important, and to why actually this library exists, is that in computer vision and augmented-reality applications you want your mesh to be in a specific pose relative to a camera viewpoint. As a consequence, you usually have an image grabbed from a camera and the pose of an object relative to it.
Here is another commented, ready-to-go, code snippet (using default shaders)!
View it online.
7 #include <glm/gtc/matrix_transform.hpp> 8 #include <opencv2/core/core.hpp> 9 #include <opencv2/imgcodecs/imgcodecs.hpp> 10 #include <opencv2/imgproc/imgproc.hpp> 16 const unsigned int cam_width = 320;
17 const unsigned int cam_height = 240;
18 const float cam_fx = 257.34;
19 const float cam_cx = 160;
20 const float cam_fy = 257.34;
21 const float cam_cy = 120;
24 obj.emplace(
"alien",
"./spaceinvader.obj");
26 SICAD si_cad(obj, cam_width, cam_height, cam_fx, cam_fy, cam_cx, cam_cy);
38 objpose_map.emplace(
"alien", obj_pose);
40 double cam_x[] = { 0, 0, 0};
41 double cam_o[] = {1.0, 0, 0, 0};
50 cv::Mat img = cv::imread(
"./space.png");
51 si_cad.setBackgroundOpt(
true);
52 si_cad.superimpose(objpose_map, cam_x, cam_o, img);
53 cv::imwrite(
"./spaceinvader.jpg", img);
std::unordered_map< std::string, std::string > ModelPathContainer
std::vector< double > ModelPose
A Superimpose derived class to superimpose mesh models on images.
std::multimap< std::string, ModelPose > ModelPoseContainer
You should get something like this:
👾 in the Space