Skip to content
Snippets Groups Projects
Commit 9b00e3e8 authored by Patrik Huber's avatar Patrik Huber
Browse files

Added Texture class

parent d1b0b8b7
No related branches found
No related tags found
No related merge requests found
......@@ -23,6 +23,7 @@
#define RENDER_UTILS_HPP_
#include "opencv2/core/core.hpp"
#include "opencv2/imgproc/imgproc.hpp"
namespace eos {
namespace render {
......@@ -78,6 +79,85 @@ inline cv::Vec2f screen_to_clip_space(const cv::Vec2f& screen_coordinates, int s
return cv::Vec2f(x_cs, y_cs);
};
// TODO: Should go to detail:: namespace, or texturing/utils or whatever.
unsigned int get_max_possible_mipmaps_num(unsigned int width, unsigned int height)
{
unsigned int mipmapsNum = 1;
unsigned int size = std::max(width, height);
if (size == 1)
return 1;
do {
size >>= 1;
mipmapsNum++;
} while (size != 1);
return mipmapsNum;
};
inline bool is_power_of_two(int x)
{
return !(x & (x - 1));
};
class Texture
{
public:
// Todo: This whole class needs a major overhaul and documentation.
// throws: ocv exc, runtime_ex
void create_mipmapped_texture(cv::Mat image, unsigned int mipmapsNum = 0) {
this->mipmaps_num = (mipmapsNum == 0 ? get_max_possible_mipmaps_num(image.cols, image.rows) : mipmapsNum);
/*if (mipmapsNum == 0)
{
uchar mmn = render::utils::MatrixUtils::getMaxPossibleMipmapsNum(image.cols, image.rows);
this->mipmapsNum = mmn;
} else
{
this->mipmapsNum = mipmapsNum;
}*/
if (this->mipmaps_num > 1)
{
if (!is_power_of_two(image.cols) || !is_power_of_two(image.rows))
{
throw std::runtime_error("Error: Couldn't generate mipmaps, width or height not power of two.");
}
}
image.convertTo(image, CV_8UC4); // Most often, the input img is CV_8UC3. Img is BGR. Add an alpha channel
cv::cvtColor(image, image, CV_BGR2BGRA);
int currWidth = image.cols;
int currHeight = image.rows;
for (int i = 0; i < this->mipmaps_num; i++)
{
if (i == 0) {
mipmaps.push_back(image);
}
else {
cv::Mat currMipMap(currHeight, currWidth, CV_8UC4);
cv::resize(mipmaps[i - 1], currMipMap, currMipMap.size());
mipmaps.push_back(currMipMap);
}
if (currWidth > 1)
currWidth >>= 1;
if (currHeight > 1)
currHeight >>= 1;
}
this->widthLog = (uchar)(std::log(mipmaps[0].cols) / CV_LOG2 + 0.0001f); // std::epsilon or something? or why 0.0001f here?
this->heightLog = (uchar)(std::log(mipmaps[0].rows) / CV_LOG2 + 0.0001f); // Changed std::logf to std::log because it doesnt compile in linux (gcc 4.8). CHECK THAT
};
std::vector<cv::Mat> mipmaps; // make Texture a friend class of renderer, then move this to private?
unsigned char widthLog, heightLog; // log2 of width and height of the base mip-level
private:
//std::string filename;
unsigned int mipmaps_num;
};
} /* namespace render */
} /* namespace eos */
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment