* @param[in] mvpMatrix Atm working with a 4x4 (full) affine. But anything would work, just take care with the w-division.
* @param[in] view_port_width TODO
* @param[in] view_port_height TODO
* @param[in] image Where to extract the texture from
* @param[in] depth_buffer TODO note: We could also pass an instance of a Renderer here. Depending on how "stateful" the renderer is, this might make more sense.
* @param[in] mapping_type Which Transformation type to use for mapping
* @return A Mat with the texture as an isomap
* // note: framebuffer should have size of the image (ok not necessarily. What about mobile?) (well it should, to get optimal quality (and everywhere the same quality)?)
Mattexture_map=Mat::zeros(512,512,CV_8UC3);// We don't want an alpha channel. We might want to handle grayscale input images though.
MatvisibilityMask=Mat::zeros(512,512,CV_8UC3);
for(constauto&triangleIndices:mesh.tvi){
// Find out if the current triangle is visible:
// We do a second rendering-pass here. We use the depth-buffer of the final image, and then, here,
// check if each pixel in a triangle is visible. If the whole triangle is visible, we use it to extract
// the texture.
// Possible improvement: - If only part of the triangle is visible, split it
// - Share more code with the renderer?
Vec4fv0_3d=mesh.vertices[triangleIndices[0]];
Vec4fv1_3d=mesh.vertices[triangleIndices[1]];
Vec4fv2_3d=mesh.vertices[triangleIndices[2]];
Vec4fv0,v1,v2;// we don't copy the color and texcoords, we only do the visibility check here.
// This could be optimized in 2 ways though:
// - Use render(), or as in render(...), transfer the vertices once, not in a loop over all triangles (vertices are getting transformed multiple times)
// - We transform them later (below) a second time. Only do it once.
v0=Mat(mvpMatrix*Mat(v0_3d));
v1=Mat(mvpMatrix*Mat(v1_3d));
v2=Mat(mvpMatrix*Mat(v2_3d));
// Well, in in principle, we'd have to do the whole stuff as in render(), like
// clipping against the frustums etc.
// But as long as our model is fully on the screen, we're fine.
// divide by w
// if ortho, we can do the divide as well, it will just be a / 1.0f.
v0=v0/v0[3];
v1=v1/v1[3];
v2=v2/v2[3];
// Todo: This is all very similar to processProspectiveTri(...), except the other function does texturing stuff as well. Remove code duplication!