00001 00002 #include <blort/TomGine/tgTexture.h> 00003 #include <blort/TomGine/tgError.h> 00004 #include <opencv2/highgui/highgui.hpp> 00005 #include <stdexcept> 00006 00007 using namespace TomGine; 00008 00009 tgTexture::tgTexture(){ 00010 glGenTextures(1, &m_texture_id); 00011 glBindTexture(GL_TEXTURE_2D, m_texture_id); 00012 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); 00013 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP); 00014 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP); 00015 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); 00016 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 00017 glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE); 00018 tgCheckError("[tgTexture::tgTexture()]"); 00019 } 00020 00021 tgTexture::~tgTexture(){ 00022 if(glIsTexture(m_texture_id)) 00023 glDeleteTextures(1, &m_texture_id); 00024 } 00025 00026 bool tgTexture::Load(void* image_data, int width, int height, GLenum format, GLenum internal){ 00027 m_width = width; 00028 m_height = height; 00029 glBindTexture(GL_TEXTURE_2D, m_texture_id); 00030 glTexImage2D(GL_TEXTURE_2D, 0, 3, (int)m_width, (int)m_height, 0, format, internal, image_data); 00031 return true; 00032 } 00033 00034 bool tgTexture::Load(const char* filename){ 00035 IplImage* img = cvLoadImage(filename, CV_LOAD_IMAGE_COLOR); 00036 if(img==NULL){ 00037 std::string errmsg = std::string("[tgTexture::Load] Error loading file '") + filename + "'."; 00038 throw std::runtime_error(errmsg.c_str()); 00039 } 00040 return Load((unsigned char*)img->imageData, img->width, img->height, GL_BGR); 00041 } 00042 00043 bool tgTexture::Save(const char* filename, bool overwrite){ 00044 Bind(); 00045 IplImage* img = cvCreateImage ( cvSize ( m_width, m_height ), IPL_DEPTH_8U, 3 ); 00046 glGetTexImage(GL_TEXTURE_2D, 0, GL_BGR, GL_UNSIGNED_BYTE, img->imageData); 00047 cvSaveImage(filename, img); 00048 cvReleaseImage(&img); 00049 return true; 00050 } 00051 00052 bool tgTexture::GetImageData(unsigned char* image_data){ 00053 Bind(); 00054 glGetTexImage(GL_TEXTURE_2D, 0, GL_RGB, GL_UNSIGNED_BYTE, image_data); 00055 return true; 00056 } 00057 00058 void tgTexture::Bind(int stage) const{ 00059 glActiveTexture(GL_TEXTURE0 + stage); 00060 glBindTexture(GL_TEXTURE_2D, m_texture_id); 00061 glEnable(GL_TEXTURE_2D); 00062 } 00063 00064 void tgTexture::CopyTexImage2D(int width, int height){ 00065 m_width = width; 00066 m_height = height; 00067 Bind(); 00068 glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 0, 0, m_width, m_height, 0); 00069 } 00070 00071 void tgTexture::CopyTexImage2D(int x, int y, int width, int height){ 00072 m_width = width; 00073 m_height = height; 00074 Bind(); 00075 glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, x, y, m_width, m_height, 0); 00076 } 00077 00078