rtcTexture.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2008
00003  * Robert Bosch LLC
00004  * Research and Technology Center North America
00005  * Palo Alto, California
00006  *
00007  * All rights reserved.
00008  *
00009  *------------------------------------------------------------------------------
00010  * project ....: PUMA: Probablistic Unsupervised Model Acquisition
00011  * file .......: Texture.cpp
00012  * authors ....: Benjamin Pitzer
00013  * organization: Robert Bosch LLC
00014  * creation ...: 10/13/2006
00015  * modified ...: $Date: 2009-02-02 12:17:10 -0800 (Mon, 02 Feb 2009) $
00016  * changed by .: $Author: wg75pal $
00017  * revision ...: $Revision: 772 $
00018  */
00019 
00020 //== INCLUDES ==================================================================
00021 #ifdef _MSC_VER
00022 #       include <windows.h>
00023 #endif
00024 #include <GL/glew.h>
00025 #include <GL/gl.h>
00026 #include <GL/glu.h>
00027 #include "rtc/rtcTexture.h"
00028 #include <rtc/rtcBase.h>
00029 
00030 //== NAMESPACES ================================================================
00031 namespace rtc {
00032 
00033 //== IMPLEMENTATION ============================================================
00034 
00035 Texture::Texture(bool _multi_use, int _max_texture_size)
00036 : multi_use(_multi_use),max_texture_size(_max_texture_size),texture_id(0),
00037   texture_width(0),texture_height(0),image_width(0),image_height(0),max_u(0),
00038   max_v(0),texture_data(NULL)
00039 {
00040 }
00041 
00042 Texture::~Texture()
00043 {
00044   unloadTexture();
00045 }
00046 
00047 void Texture::create(int width, int height)
00048 {
00049   texture_width = 1;
00050   texture_height = 1;
00051   while(texture_width < width)
00052     texture_width *= 2;
00053   while(texture_height < height)
00054     texture_height *= 2;
00055   image_width = width;
00056   image_height = height;
00057   texture_data = (unsigned char *)calloc(1, 4 * texture_width * texture_height);
00058   rtc_test_alloc(texture_data);
00059 
00060   max_u = width / (float)texture_width;
00061   max_v = height / (float)texture_height;
00062 
00063   if (texture_id==0)
00064     glGenTextures(1, &texture_id);
00065   glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
00066   glBindTexture(GL_TEXTURE_2D, texture_id);
00067 
00068   glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_CLAMP);
00069   glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP);
00070   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
00071   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
00072 }
00073 
00074 void Texture::update()
00075 {
00076   if(texture_data == NULL)
00077     rtc_die("Error: cannont update a single use texture.\n");
00078   glBindTexture(GL_TEXTURE_2D, texture_id);
00079   glTexImage2D(GL_TEXTURE_2D, 0, 4, texture_width,
00080                texture_height, 0, GL_RGBA, GL_UNSIGNED_BYTE,
00081                texture_data);
00082 }
00083 
00084 void Texture::updateRGBA()
00085 {
00086   if(texture_data == NULL)
00087     rtc_die("Error: cannont update a single use texture.\n");
00088   glBindTexture(GL_TEXTURE_2D, texture_id);
00089   glTexImage2D(GL_TEXTURE_2D, 0, 4, texture_width,
00090                texture_height, 0, GL_RGBA, GL_UNSIGNED_BYTE,
00091                texture_data);
00092 }
00093 
00094 void Texture::createRGBA(int width, int height)
00095 {
00096   texture_width = 1;
00097   texture_height = 1;
00098   while(texture_width < width)
00099     texture_width *= 2;
00100   while(texture_height < height)
00101     texture_height *= 2;
00102   image_width = width;
00103   image_height = height;
00104   texture_data = (unsigned char *)calloc(1, 4 * texture_width * texture_height);
00105   rtc_test_alloc(texture_data);
00106 
00107   max_u = width / (float)texture_width;
00108   max_v = height / (float)texture_height;
00109 
00110   glGenTextures(1, &texture_id);
00111   glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
00112   glBindTexture(GL_TEXTURE_2D, texture_id);
00113 
00114   glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_CLAMP);
00115   glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP);
00116   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
00117   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
00118 }
00119 
00120 void Texture::draw()
00121 {
00122   glEnable(GL_TEXTURE_2D);
00123   glBindTexture(GL_TEXTURE_2D, texture_id);
00124   glColor3f(1, 1, 1);
00125   glBegin(GL_POLYGON);
00126   glTexCoord2f(0, 0);
00127   glVertex2f(-1, 1);
00128   glTexCoord2f(0, 1);
00129   glVertex2f(-1, -1);
00130   glTexCoord2f(1, 1);
00131   glVertex2f(1, -1);
00132   glTexCoord2f(1, 0);
00133   glVertex2f(1, 1);
00134   glEnd();
00135   glDisable(GL_TEXTURE_2D);
00136 }
00137 
00138 void Texture::draw2(double x1, double y1,
00139                     double x2, double y2,
00140                     double x3, double y3,
00141                     double x4, double y4)
00142 {
00143   glEnable(GL_TEXTURE_2D);
00144   glBindTexture(GL_TEXTURE_2D, texture_id);
00145   glColor3f(1, 1, 1);
00146   glBegin(GL_POLYGON);
00147   glTexCoord2f(0, max_v);
00148   glVertex2f(x1, y1);
00149   glTexCoord2f(max_u, max_v);
00150   glVertex2f(x2, y2);
00151   glTexCoord2f(max_u, 0);
00152   glVertex2f(x3, y3);
00153   glTexCoord2f(0, 0);
00154   glVertex2f(x4, y4);
00155   glEnd();
00156   glDisable(GL_TEXTURE_2D);
00157 }
00158 
00159 void Texture::fromImage(const Image3uc& image)
00160 {
00161   texture_width = 1;
00162   texture_height = 1;
00163   while(texture_width < image.columns())
00164     texture_width *= 2;
00165   while(texture_height < image.rows())
00166     texture_height *= 2;
00167   if(texture_height > max_texture_size)
00168     texture_height = max_texture_size;
00169   if(texture_width > max_texture_size)
00170     texture_width = max_texture_size;
00171 
00172   Image3uc newimage(texture_height,texture_width);
00173   image.resized(newimage);
00174   unsigned char *data=new unsigned char[texture_height*texture_width*3];
00175   for (int i=0;i<texture_height*texture_width;i++)
00176   {
00177     data[i*3]=newimage.x[i][0];
00178     data[i*3+1]=newimage.x[i][1];
00179     data[i*3+2]=newimage.x[i][2];
00180   }
00181 
00182   if (texture_id==0) glGenTextures(1, &texture_id);
00183   glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
00184   glBindTexture(GL_TEXTURE_2D, texture_id);
00185   glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_CLAMP);
00186   glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP);
00187   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
00188   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
00189   glTexImage2D(GL_TEXTURE_2D,0,GL_RGB,newimage.columns(),newimage.rows(),0,GL_RGB,GL_UNSIGNED_BYTE,data);
00190   
00191   delete data;
00192 
00193 
00194   if(!multi_use) {
00195 //     free(texture_data);
00196 //     texture_data = NULL;
00197   }
00198 }
00199 
00200 void Texture::fromImage(const Image4uc& image)
00201 {
00202   texture_width = 1;
00203   texture_height = 1;
00204   while(texture_width < image.columns())
00205     texture_width *= 2;
00206   while(texture_height < image.rows())
00207     texture_height *= 2;
00208   if(texture_height > max_texture_size)
00209     texture_height = max_texture_size;
00210   if(texture_width > max_texture_size)
00211     texture_width = max_texture_size;
00212 
00213   Image4uc newimage(texture_height,texture_width);
00214   image.resized(newimage);
00215   texture_data=new unsigned char[texture_height*texture_width*4];
00216   for (int i=0;i<texture_height*texture_width;i++)
00217   {
00218       texture_data[i*4]=newimage.x[i][0];
00219       texture_data[i*4+1]=newimage.x[i][1];
00220       texture_data[i*4+2]=newimage.x[i][2];
00221       texture_data[i*4+3]=newimage.x[i][3];
00222   }
00223 
00224   if (texture_id==0) glGenTextures(1, &texture_id);
00225   glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
00226   glBindTexture(GL_TEXTURE_2D, texture_id);
00227   glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_CLAMP);
00228   glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP);
00229   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
00230   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
00231   glTexImage2D(GL_TEXTURE_2D,0,GL_RGB,newimage.columns(),newimage.rows(),0,GL_RGBA,GL_UNSIGNED_BYTE,texture_data);
00232   glDisable(GL_TEXTURE_2D);
00233 
00234   if(!multi_use) {
00235     //     free(texture_data);
00236     //     texture_data = NULL;
00237   }
00238 }
00239 
00240 void Texture::toImage(Image4uc& image)
00241 {
00242     image.setSize(texture_height, texture_width);
00243     int size = texture_height*texture_width;
00244     for (int i=0;i<size;i++)
00245     {
00246         image.x[i][0] = texture_data[i*4];
00247         image.x[i][1] = texture_data[i*4+1];
00248         image.x[i][2] = texture_data[i*4+2];
00249         image.x[i][3] = texture_data[i*4+3];
00250     }
00251 }
00252 
00253 bool Texture::loadFromImage(const char *filename)
00254 {
00255   // read the image from file
00256   Image4uc image;
00257   bool res = image.readFromFile(filename);
00258   if(!res) return false;
00259   // load from image
00260   fromImage(image);
00261   return true;
00262 }
00263 
00264 bool Texture::saveToImage(const char* filename)
00265 {
00266     // convert texture to image
00267     Image4uc image;
00268     toImage(image);
00269     return image.writeToFile(filename);
00270 }
00271 
00272 bool Texture::write(std::ostream& os, bool ascii) const
00273 {
00274   return true;
00275 }
00276 
00277 bool Texture::read(std::istream& is, bool ascii)
00278 {
00279   return true;
00280 }
00281 
00282 void Texture::bind( GLenum target/*=GL_TEXTURE0*/ )
00283 {
00284   glEnable(GL_TEXTURE_2D);
00285   glActiveTexture(target);
00286   glBindTexture(GL_TEXTURE_2D, texture_id);
00287   bind_target=target;
00288 }
00289 
00290 void Texture::unbind()
00291 {
00292   glActiveTexture(bind_target);
00293   glBindTexture(GL_TEXTURE_2D,0);
00294   glDisable(GL_TEXTURE_2D);
00295 }
00296 
00297 void Texture::unloadTexture()
00298 {
00299   if (texture_id!=0) glDeleteTextures(1, &texture_id);
00300   texture_id=0;
00301   if(texture_data != NULL)
00302     free(texture_data);
00303 }
00304 //==============================================================================
00305 } // namespace rtc
00306 //==============================================================================


rtc
Author(s): Benjamin Pitzer
autogenerated on Mon Oct 6 2014 10:07:35