00001 00002 #include <blort/Tracker/Texture.h> 00003 #include <blort/Tracker/Resources.h> 00004 00005 using namespace Tracking; 00006 00007 Texture::Texture(){ 00008 glGenTextures(1, &m_texture_id); 00009 glBindTexture(GL_TEXTURE_2D, m_texture_id); 00010 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); 00011 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP); 00012 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP); 00013 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 00014 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 00015 } 00016 00017 Texture::~Texture(){ 00018 if(glIsTexture(m_texture_id)) 00019 glDeleteTextures(1, &m_texture_id); 00020 } 00021 00022 bool Texture::load(unsigned char* image_data, unsigned width, unsigned height, GLenum format){ 00023 m_width = width; 00024 m_height = height; 00025 glBindTexture(GL_TEXTURE_2D, m_texture_id); 00026 glTexImage2D(GL_TEXTURE_2D, 0, 3, m_width, m_height, 0, format, GL_UNSIGNED_BYTE, image_data); 00027 return true; 00028 } 00029 00030 bool Texture::load(const char* filename){ 00031 IplImage* img = cvLoadImage(filename, CV_LOAD_IMAGE_COLOR); 00032 return load((unsigned char*)img->imageData, img->width, img->height); 00033 } 00034 00035 bool Texture::save(const char* filename){ 00036 bind(); 00037 IplImage* img = cvCreateImage ( cvSize ( m_width, m_height ), IPL_DEPTH_8U, 3 ); 00038 glGetTexImage(GL_TEXTURE_2D, 0, GL_RGB, GL_UNSIGNED_BYTE, img->imageData); 00039 cvConvertImage(img, img, CV_CVTIMG_SWAP_RB); 00040 cvSaveImage(filename, img); 00041 cvReleaseImage(&img); 00042 return true; 00043 } 00044 00045 bool Texture::getImageData(unsigned char* image_data){ 00046 bind(); 00047 glGetTexImage(GL_TEXTURE_2D, 0, GL_RGB, GL_UNSIGNED_BYTE, image_data); 00048 return true; 00049 } 00050 00051 void Texture::bind(int stage){ 00052 glActiveTexture(GL_TEXTURE0 + stage); 00053 glBindTexture(GL_TEXTURE_2D, m_texture_id); 00054 glActiveTexture(GL_TEXTURE0); 00055 } 00056 00057 void Texture::copyTexImage2D(unsigned width, unsigned height){ 00058 copyTexImage2D(0, 0, width, height); 00059 } 00060 00061 void Texture::copyTexImage2D(int x, int y, unsigned width, unsigned height){ 00062 m_width = width; 00063 m_height = height; 00064 bind(); 00065 glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, x, y, m_width, m_height, 0); 00066 } 00067 00068 void Texture::copyFromTexture(Texture* tex){ 00069 g_Resources->GetImageProcessor()->render(tex); 00070 copyTexImage2D(tex->getWidth(), tex->getHeight()); 00071 } 00072 00073 void Texture::copyFromTexture(Texture* tex, int x, int y, unsigned w, unsigned h){ 00074 g_Resources->GetImageProcessor()->render(tex); 00075 copyTexImage2D(x, y, w, h); 00076 } 00077 00078 cv::Mat Texture::toCvMat() 00079 { 00080 bind(); 00081 IplImage* img = cvCreateImage ( cvSize ( m_width, m_height ), IPL_DEPTH_8U, 3 ); 00082 glGetTexImage(GL_TEXTURE_2D, 0, GL_RGB, GL_UNSIGNED_BYTE, img->imageData); 00083 cvConvertImage(img, img, CV_CVTIMG_FLIP | CV_CVTIMG_SWAP_RB); 00084 cv::Mat result(img, true); 00085 cvReleaseImage(&img); 00086 return result; 00087 }