glh_mipmaps.h
Go to the documentation of this file.
00001 /*
00002     glh - is a platform-indepenedent C++ OpenGL helper library 
00003 
00004 
00005     Copyright (c) 2000 Cass Everitt
00006         Copyright (c) 2000, 2001 NVIDIA Corporation
00007     All rights reserved.
00008 
00009     Redistribution and use in source and binary forms, with or
00010         without modification, are permitted provided that the following
00011         conditions are met:
00012 
00013      * Redistributions of source code must retain the above
00014            copyright notice, this list of conditions and the following
00015            disclaimer.
00016 
00017      * Redistributions in binary form must reproduce the above
00018            copyright notice, this list of conditions and the following
00019            disclaimer in the documentation and/or other materials
00020            provided with the distribution.
00021 
00022      * The names of contributors to this software may not be used
00023            to endorse or promote products derived from this software
00024            without specific prior written permission. 
00025 
00026        THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00027            ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00028            LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00029            FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00030            REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00031            INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00032            BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00033            LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00034            CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00035            LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00036            ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
00037            POSSIBILITY OF SUCH DAMAGE. 
00038 
00039 
00040     Cass Everitt - cass@r3.nu
00041 */
00042 
00043 /*
00044    gluBuild2DMipmaps cannot build mipmaps for textures whose
00045    "format" it does not recognize.  This is primarily because
00046    it infers the number of components and how to average them
00047    from the format.  This helper eliminates that problem by
00048    factoring out that functionality.
00049    
00050    Cass Everitt
00051    1-9-01
00052 
00053 */
00054 
00055 #ifndef GLH_MIPMAPS_H
00056 #define GLH_MIPMAPS_H
00057 
00058 #ifdef _WIN32
00059 #  include <windows.h>
00060 #endif
00061 
00062 #ifdef MACOS
00063 #include <OpenGL/gl.h>
00064 #else
00065 #include <GL/gl.h>
00066 #endif
00067 
00068 namespace glh
00069 {
00070         
00071         template <class T> 
00072         class tex_indexer2
00073         {
00074                 public:
00075                         tex_indexer2(int width, int height, int tuple_size, T * data) :
00076                           w(width), h(height), n(tuple_size), d(data) {}
00077 
00078                         T * operator()(int i, int j)
00079                         { return d + n * (w * j + i); }
00080                 private:
00081                         int w, h, n;
00082                         T * d;
00083         };
00084 
00085         template <class T>
00086         struct generic_filter
00087         {
00088                 typedef T element_type;
00089 
00090                 generic_filter(int tuplesize, GLenum gltype) :
00091                         gl_type(gltype), tuple_size(tuplesize) {}
00092 
00093                 void average( T * out,
00094                                           const T * a, const T * b, 
00095                                           const T * c, const T * d)
00096                 {
00097                         for(int i=0; i < tuple_size; i++)
00098                         {
00099                                 double in = double(a[i]) + double(b[i]) + double(c[i]) + double(d[i]);
00100                                 in /= 4;
00101                                 out[i] = T(in);
00102                         }
00103                 }
00104 
00105                 const GLenum gl_type;
00106                 const int tuple_size;
00107         };
00108 
00109         // fixme: supports non-square textures!
00110         template <class F>
00111         void build_2D_mipmaps( GLenum target, GLenum internal_format,
00112                                                    GLsizei w, GLsizei h, GLenum format, 
00113                                                    F filter, const void * vdata)
00114         {
00115 
00116                 typedef typename F::element_type DataType;
00117                 const DataType * in_data = (const DataType *)vdata;
00118                 DataType * data = new DataType [w * h * filter.tuple_size];
00119 
00120                 glTexImage2D(target, 0, internal_format, w, h, 0, format, filter.gl_type, (const DataType *)vdata);
00121 
00122                 int level = 1;
00123                 if( w >= 2 ) w /= 2;
00124                 if( h >= 2 ) h /= 2;
00125                 bool done = false;
00126                 while(! done)
00127                 {
00128                         tex_indexer2<const DataType> bg(w*2, h*2, filter.tuple_size, in_data);
00129                         tex_indexer2<DataType> sm(w  , h  , filter.tuple_size, data);
00130                         for(int j=0; j < h; j++)
00131                         {
00132                                 int J = j * 2;
00133                                 for(int i=0; i < w; i++)
00134                                 {
00135                                         int I = i*2;
00136                                         filter.average( sm(i,j),
00137                                                                         bg(I  , J  ), bg(I+1, J  ),
00138                                                                         bg(I  , J+1), bg(I+1, J+1));
00139                                 }
00140                         }
00141                         
00142                         glTexImage2D(target, level, internal_format, w, h, 0, format, filter.gl_type, data);
00143 
00144                         if(w == 1 && h == 1) done = true;
00145                         
00146                         if( w >= 2 ) w /= 2;
00147                         if( h >= 2 ) h /= 2;
00148                         level++;
00149                         in_data = data;
00150                 }
00151 
00152                 delete [] data;
00153         }
00154 
00155 }
00156 
00157 #endif


nao_openni
Author(s): Bener SUAY
autogenerated on Mon Jan 6 2014 11:27:50