$search
00001 /********************************************************************* 00002 * 00003 * Software License Agreement (BSD License) 00004 * 00005 * Copyright (c) 2011, Robert Bosch LLC. 00006 * All rights reserved. 00007 * 00008 * Redistribution and use in source and binary forms, with or without 00009 * modification, are permitted provided that the following conditions 00010 * are met: 00011 * 00012 * * Redistributions of source code must retain the above copyright 00013 * notice, this list of conditions and the following disclaimer. 00014 * * Redistributions in binary form must reproduce the above 00015 * copyright notice, this list of conditions and the following 00016 * disclaimer in the documentation and/or other materials provided 00017 * with the distribution. 00018 * * Neither the name of the Robert Bosch nor the names of its 00019 * contributors may be used to endorse or promote products derived 00020 * from this software without specific prior written permission. 00021 * 00022 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00023 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00024 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 00025 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 00026 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 00027 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 00028 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 00029 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 00030 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 00031 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 00032 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00033 * POSSIBILITY OF SUCH DAMAGE. 00034 * 00035 *********************************************************************/ 00036 #include <iostream> 00037 00038 #include "vlr/displayGL.h" 00039 00040 namespace vlr { 00041 00042 template<class T> bool DisplayGL::internalPaint2d() { 00043 return false; // TODO: implement conversion for non-standard types 00044 } 00045 00046 template<> bool DisplayGL::internalPaint2d<unsigned char>() { 00047 unsigned char* data = &static_cast<Image<unsigned char>*> (imgBuf)->data()[slice_offset_]; 00048 glTexImage2D(texType, 0, GL_RGBA, imgBuf->width(), imgBuf->height(), 0, bufColorFormat, GL_UNSIGNED_BYTE, data); 00049 // printf("- 1 - %s: EC: %i\n", __FUNCTION__, glGetError()); 00050 return true; 00051 } 00052 00053 template<> bool DisplayGL::internalPaint2d<char>() { 00054 char* data = &static_cast<Image<char>*> (imgBuf)->data()[slice_offset_]; 00055 glTexImage2D(texType, 0, GL_RGBA, imgBuf->width(), imgBuf->height(), 0, bufColorFormat, GL_BYTE, data); 00056 return true; 00057 } 00058 00059 template<> bool DisplayGL::internalPaint2d<unsigned short>() { 00060 unsigned short* data = &static_cast<Image<unsigned short>*> (imgBuf)->data()[slice_offset_]; 00061 glTexImage2D(texType, 0, GL_RGBA, imgBuf->width(), imgBuf->height(), 0, bufColorFormat, GL_UNSIGNED_SHORT, data); 00062 return true; 00063 } 00064 00065 template<> bool DisplayGL::internalPaint2d<short>() { 00066 short* data = &static_cast<Image<short>*> (imgBuf)->data()[slice_offset_]; 00067 glTexImage2D(texType, 0, GL_RGBA, imgBuf->width(), imgBuf->height(), 0, bufColorFormat, GL_SHORT, data); 00068 return true; 00069 } 00070 00071 template<> bool DisplayGL::internalPaint2d<unsigned int>() { 00072 unsigned int* data = &static_cast<Image<unsigned int>*> (imgBuf)->data()[slice_offset_]; 00073 glTexImage2D(texType, 0, GL_RGBA, imgBuf->width(), imgBuf->height(), 0, bufColorFormat, GL_UNSIGNED_INT, data); 00074 return true; 00075 } 00076 00077 template<> bool DisplayGL::internalPaint2d<int>() { 00078 int* data = &static_cast<Image<int>*> (imgBuf)->data()[slice_offset_]; 00079 glTexImage2D(texType, 0, GL_RGBA, imgBuf->width(), imgBuf->height(), 0, bufColorFormat, GL_INT, data); 00080 return true; 00081 } 00082 00083 template<> bool DisplayGL::internalPaint2d<float>() { 00084 float* data = &static_cast<Image<float>*> (imgBuf)->data()[slice_offset_]; 00085 glTexImage2D(texType, 0, GL_RGBA, imgBuf->width(), imgBuf->height(), 0, bufColorFormat, GL_FLOAT, data); 00086 return true; 00087 } 00088 00089 template<> bool DisplayGL::internalPaint2d<double>() { 00090 double* data = &static_cast<Image<double>*> (imgBuf)->data()[slice_offset_]; 00091 glTexImage2D(texType, 0, GL_RGBA, imgBuf->width(), imgBuf->height(), 0, bufColorFormat, GL_DOUBLE, data); 00092 return true; 00093 } 00094 00095 void DisplayGL::internalPaint2d() { 00096 glMatrixMode(GL_PROJECTION); 00097 glLoadIdentity(); 00098 float scaled_width = width()*scale_; 00099 float scaled_height = height()*scale_; 00100 glOrtho(0., scaled_width, 0., scaled_height, -1, 1); 00101 glViewport(0, 0, scaled_width, scaled_height); 00102 00103 glMatrixMode(GL_MODELVIEW); 00104 glLoadIdentity(); 00105 00106 glEnable(GL_TEXTURE_2D); 00107 glEnable(GL_TEXTURE_RECTANGLE_ARB); 00108 00109 glBindTexture(texType, imgTexture); 00110 00111 // glColor4f(1, 0, 0, 1);//0.75); 00112 glColor4f(1.0, 1.0, 1.0, 1.0); 00113 00114 if (imgBuf) { 00115 switch(data_type_) { 00116 case TYPE_UCHAR: 00117 internalPaint2d<unsigned char> (); 00118 break; 00119 00120 case TYPE_CHAR: 00121 internalPaint2d<char> (); 00122 break; 00123 00124 case TYPE_USHORT: 00125 internalPaint2d<unsigned short> (); 00126 break; 00127 00128 case TYPE_SHORT: 00129 internalPaint2d<short> (); 00130 break; 00131 00132 case TYPE_UINT: 00133 internalPaint2d<unsigned int> (); 00134 break; 00135 00136 case TYPE_INT: 00137 internalPaint2d<int> (); 00138 break; 00139 00140 case TYPE_FLOAT: 00141 internalPaint2d<float> (); 00142 break; 00143 00144 case TYPE_DOUBLE: 00145 internalPaint2d<float> (); // TODO: Make double texture work?!? 00146 // internalPaint2d<double> (); 00147 break; 00148 } 00149 // if (dynamic_cast<Image<unsigned char>*> (imgBuf)) { 00150 // internalPaint2d<unsigned char> (); 00151 // } 00152 // else if (dynamic_cast<Image<char>*> (imgBuf)) { 00153 // internalPaint2d<char> (); 00154 // } 00155 // else if (dynamic_cast<Image<unsigned short>*> (imgBuf)) { 00156 // internalPaint2d<unsigned short> (); 00157 // } 00158 // else if (dynamic_cast<Image<short>*> (imgBuf)) { 00159 // internalPaint2d<short> (); 00160 // } 00161 // else if (dynamic_cast<Image<unsigned int>*> (imgBuf)) { 00162 // internalPaint2d<unsigned int> (); 00163 // } 00164 // else if (dynamic_cast<Image<int>*> (imgBuf)) { 00165 // internalPaint2d<int> (); 00166 // } 00167 // else if (dynamic_cast<Image<float>*> (imgBuf)) { 00168 // internalPaint2d<float> (); 00169 // } 00170 // else if (dynamic_cast<Image<double>*> (imgBuf)) { 00171 // internalPaint2d<double> (); 00172 // } 00173 // else if(dynamic_cast<Image<ImageBase::complex>*>(imgBuf)) 00174 // {internalPaint2d<ImageBase::complex>();} 00175 // else if(dynamic_cast<Image<ImageBase::dcomplex>*>(imgBuf)) 00176 // {internalPaint2d<ImageBase::complex>();} 00177 00178 glEnable(GL_BLEND); 00179 glBegin(GL_QUADS); 00180 glTexCoord2f(0, 0); glVertex2f(0, scaled_height); 00181 glTexCoord2f(width(), 0); glVertex2f(scaled_width, scaled_height); 00182 glTexCoord2f(width(), height()); glVertex2f(scaled_width, 0); 00183 glTexCoord2f(0, height()); glVertex2f(0, 0); 00184 glEnd(); 00185 glDisable(GL_BLEND); 00186 00187 glDisable(GL_TEXTURE_2D); 00188 glDisable(GL_TEXTURE_RECTANGLE_ARB); 00189 00190 drawTags2d(); 00191 drawCustomTags2d(); 00192 } 00193 } 00194 00195 void DisplayGL::drawTags2d() { 00196 00197 std::pair<ImageBase::tagCIter, ImageBase::tagCIter> range; 00198 glDisable(GL_DEPTH_TEST); 00199 glEnable(GL_BLEND); 00200 glEnable(GL_POLYGON_SMOOTH); 00201 00202 // draw lines 00203 imgBuf->lines(range); 00204 glBegin(GL_LINES); 00205 for (ImageBase::tagCIter pit = range.first; pit != range.second; ++pit) { 00206 TagLine* ln = static_cast<TagLine*> ((*pit).second); 00207 glColor4f(ln->r(), ln->g(), ln->b(), ln->a()); 00208 glVertex2f(ln->x0()*scale_, height() - 1 - ln->y0()*scale_); 00209 glVertex2f(ln->x1()*scale_, height() - 1 - ln->y1()*scale_); 00210 } 00211 glEnd(); 00212 00213 for (ImageBase::tagCIter pit = range.first; pit != range.second; ++pit) { 00214 TagLine* ln = static_cast<TagLine*> ((*pit).second); 00215 glColor4f(ln->r(), ln->g(), ln->b(), ln->a()); 00216 fr.drawString2D(ln->label(), ln->x1()*scale_ + crossSize, height() - 1 - ln->y1()*scale_ + crossSize); 00217 } 00218 00219 // draw points 00220 imgBuf->points(range); 00221 00222 glBegin(GL_LINES); 00223 for (ImageBase::tagCIter pit = range.first; pit != range.second; ++pit) { 00224 TagPoint* pt = static_cast<TagPoint*> ((*pit).second); 00225 glColor4f(pt->r(), pt->g(), pt->b(), pt->a()); 00226 glVertex2f(pt->x()*scale_ - crossSize, (height() - 1 - pt->y())*scale_); 00227 glVertex2f(pt->x()*scale_ + crossSize, (height() - 1 - pt->y())*scale_); 00228 glVertex2f(pt->x()*scale_, (height() - 1 - pt->y())*scale_ - crossSize); 00229 glVertex2f(pt->x()*scale_, (height() - 1 - pt->y())*scale_ + crossSize); 00230 } 00231 glEnd(); 00232 00233 for (ImageBase::tagCIter pit = range.first; pit != range.second; ++pit) { 00234 TagPoint* pt = static_cast<TagPoint*> ((*pit).second); 00235 glColor4f(pt->r(), pt->g(), pt->b(), pt->a()); 00236 fr.drawString2D(pt->label(), pt->x()*scale_ + crossSize, (height() - 1 - pt->y())*scale_ + crossSize); 00237 } 00238 00239 glDisable(GL_POLYGON_SMOOTH); 00240 glDisable(GL_BLEND); 00241 glEnable(GL_DEPTH_TEST); 00242 } 00243 00244 } // namespace vlr