Image Component Library (ICL)
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
Primitive.h
Go to the documentation of this file.
00001 /********************************************************************
00002 **                Image Component Library (ICL)                    **
00003 **                                                                 **
00004 ** Copyright (C) 2006-2013 CITEC, University of Bielefeld          **
00005 **                         Neuroinformatics Group                  **
00006 ** Website: www.iclcv.org and                                      **
00007 **          http://opensource.cit-ec.de/projects/icl               **
00008 **                                                                 **
00009 ** File   : ICLGeom/src/ICLGeom/Primitive.h                        **
00010 ** Module : ICLGeom                                                **
00011 ** Authors: Christof Elbrechter                                    **
00012 **                                                                 **
00013 **                                                                 **
00014 ** GNU LESSER GENERAL PUBLIC LICENSE                               **
00015 ** This file may be used under the terms of the GNU Lesser General **
00016 ** Public License version 3.0 as published by the                  **
00017 **                                                                 **
00018 ** Free Software Foundation and appearing in the file LICENSE.GPL  **
00019 ** included in the packaging of this file.  Please review the      **
00020 ** following information to ensure the license requirements will   **
00021 ** be met: http://www.gnu.org/licenses/lgpl-3.0.txt                **
00022 **                                                                 **
00023 ** The development of this software was supported by the           **
00024 ** Excellence Cluster EXC 277 Cognitive Interaction Technology.    **
00025 ** The Excellence Cluster EXC 277 is a grant of the Deutsche       **
00026 ** Forschungsgemeinschaft (DFG) in the context of the German       **
00027 ** Excellence Initiative.                                          **
00028 **                                                                 **
00029 ********************************************************************/
00030 
00031 #pragma once
00032 
00033 #ifndef HAVE_OPENGL
00034 #warning "this header must not be included if HAVE_OPENGL is not defined"
00035 #else
00036 
00037 #include <ICLGeom/GeomDefs.h>
00038 #include <ICLCore/Img.h>
00039 #include <ICLMath/FixedVector.h>
00040 #include <ICLUtils/Array2D.h>
00041 #include <ICLQt/GLImg.h>
00042 
00043 namespace icl{
00044   namespace geom{
00046     class SceneObject;
00049 
00050 
00062     struct Primitive{
00063   
00065       enum Type{
00066         vertex   = 1<<0, //<! vertex
00067         line     = 1<<1, //<! line primitive (adressing two vertices -> start and end position of the line)
00068         triangle = 1<<2, //<! triange primitive (adressing three vertices)
00069         quad     = 1<<3, //<! quad primitve (adressing four vertices)
00070         polygon  = 1<<4, //<! polygon primitive (adressing at least 3 vertices)
00071         texture  = 1<<5, //<! texture primitive (using 4 vertices like a quad as textured rectangle)
00072         text     = 1<<6, //<! text primitive (internally implmented as texture or as billboard)
00073         nothing  = 1<<7, //<! internally used type
00074         custom   = 1<<20, //<! for custom primitives
00075         PRIMITIVE_TYPE_COUNT = 8,                //<! also for internal use only
00076         all      = (1<<PRIMITIVE_TYPE_COUNT)-1,    //<! all types
00077         faces    = triangle | quad | polygon | texture | text
00078       };
00079     
00080       Type type;        
00081       GeomColor color;  
00082       
00084 
00088       struct RenderContext{
00089         const std::vector<Vec> &vertices;            
00090         const std::vector<Vec> &normals;             
00091         const std::vector<GeomColor> &vertexColors;  
00092         const std::vector<utils::SmartPtr<qt::GLImg> > &sharedTextures; 
00093         bool lineColorsFromVertices;                 
00094         bool triangleColorsFromVertices;             
00095         bool quadColorsFromVertices;                 
00096         bool polygonColorsFromVertices;              
00097         SceneObject *object;                         
00098       };
00099   
00101       Primitive(Type type=nothing, const GeomColor &color=GeomColor(255,255,255,255)):type(type),color(color){}
00102   
00104       virtual ~Primitive() {}
00105   
00107       virtual void render(const Primitive::RenderContext &ctx) = 0;
00108       
00110       virtual Primitive *copy() const = 0;
00111     };
00112     
00114     struct LinePrimitive : public math::FixedColVector<int,2>, public Primitive{
00116       typedef math::FixedColVector<int,2> super; 
00117       
00119       LinePrimitive(int a, int b, const GeomColor &color):
00120       math::FixedColVector<int,2>(a,b),Primitive(Primitive::line,color){}
00121         
00123       virtual void render(const Primitive::RenderContext &ctx);
00124   
00126       inline int i(int idx) const { return super::operator[](idx); }
00127       
00129       virtual Primitive *copy() const { return new LinePrimitive(*this); }
00130     };
00131   
00133     struct TrianglePrimitive : public math::FixedColVector<int,6>, public Primitive{
00135       typedef  math::FixedColVector<int,6> super; 
00136       
00138       TrianglePrimitive(int a, int b, int c, const GeomColor &color, int na=-1, int nb=-1, int nc=-1):
00139       super(a,b,c,na,nb,nc),Primitive(Primitive::triangle,color){}
00140       
00142       virtual void render(const Primitive::RenderContext &ctx);
00143       
00145       inline int i(int idx) const { return super::operator[](idx); }
00146       
00148       virtual Primitive *copy() const { return new TrianglePrimitive(*this); }
00149 
00151 
00152       Vec computeNormal(const std::vector<Vec> &vertices) const;
00153 
00154     };
00155   
00157     struct QuadPrimitive : public math::FixedColVector<int,8>, public Primitive{
00159       typedef math::FixedColVector<int,8> super; 
00160       
00162       bool trySurfaceOptimization;
00163       
00165 
00167       int tesselationResolution;
00168   
00170       QuadPrimitive(int a, int b, int c, int d, const GeomColor &color, int na=-1, int nb=-1, int nc=-1, int nd=-1, 
00171                     bool trySurfaceOptimization=false,int tesselationResolution=1):
00172       super(a,b,c,d,na,nb,nc,nd),Primitive(Primitive::quad,color),trySurfaceOptimization(trySurfaceOptimization),
00173       tesselationResolution(tesselationResolution){}
00174       
00176       virtual void render(const Primitive::RenderContext &ctx);
00177   
00179       inline int i(int idx) const { return super::operator[](idx); }
00180   
00182       virtual Primitive *copy() const { return new QuadPrimitive(*this); }
00183       
00185 
00186       Vec computeNormal(const std::vector<Vec> &vertices) const;
00187     };
00188   
00190 
00191     struct PolygonPrimitive : public Primitive{
00192       
00194 
00198       utils::Array2D<int> idx;
00199       
00201       PolygonPrimitive(int n,const int *vidx, const GeomColor &color,const int *nidx=0):
00202       Primitive(Primitive::polygon,color),idx(n,nidx?2:1){
00203         std::copy(vidx,vidx+n,idx.begin());
00204         if(nidx) std::copy(nidx,nidx+n,idx.begin()+n);
00205       }
00206       
00208       virtual void render(const Primitive::RenderContext &ctx);
00209       
00211       virtual Primitive *copy() const { 
00212         PolygonPrimitive *p = new PolygonPrimitive(*this);
00213         p->idx.detach();
00214         return p;
00215       }
00216       
00218       inline int getNumPoints() const { return idx.getWidth(); }
00219       
00221       inline int getVertexIndex(int i) const { return idx(i,0); }
00222   
00224 
00225       inline int getNormalIndex(int i) const { return idx(i,1); }
00226       
00228       inline bool hasNormals() const { return idx.getHeight() == 2; }
00229     };
00230   
00232     struct AlphaFuncProperty{
00234       AlphaFuncProperty();
00235       AlphaFuncProperty(int alphaFunc, float alphaValue):alphaFunc(alphaFunc),alphaValue(alphaValue){}
00236     
00237       int alphaFunc;         
00238       float alphaValue;      
00239       
00241       void setAlphaFunc(int func, float value){
00242         alphaFunc = func;
00243         alphaValue = value;
00244       }
00245       
00246       void restoreAlphaDefaults();
00247     };
00248   
00249     
00251 
00260     struct TexturePrimitive : public QuadPrimitive, public AlphaFuncProperty{
00261       qt::GLImg texture;         
00262       const core::ImgBase *image;  
00263   
00265       TexturePrimitive(int a, int b, int c, int d, 
00266                        const core::ImgBase *image=0, bool createTextureOnce=true, 
00267                        int na=-1, int nb=-1, int nc=-1, int nd=-1, core::scalemode sm=core::interpolateLIN):
00268       QuadPrimitive(a,b,c,d,na,nb,nc,nd), texture(image,sm),
00269         image(createTextureOnce ? 0 : image){
00270         type = Primitive::texture;
00271       }
00272   
00274       TexturePrimitive(int a, int b, int c, int d, 
00275                        const core::Img8u &image,
00276                        int na=-1, int nb=-1, int nc=-1, int nd=-1, core::scalemode sm=core::interpolateLIN):
00277       QuadPrimitive(a,b,c,d,na,nb,nc,nd), texture(&image,sm), 
00278         image(0){
00279         type = Primitive::texture;
00280       }
00281   
00283       virtual void render(const Primitive::RenderContext &ctx);
00284   
00286       virtual Primitive *copy() const { 
00287         return new TexturePrimitive(i(0),i(1),i(2),i(3),
00288                                     image ? image : texture.extractImage(),
00289                                     !image,
00290                                     i(4),i(5),i(6),i(7),
00291                                     texture.getScaleMode());
00292       }
00293       
00294   
00295     };
00296   
00298 
00299     class TextureGridPrimitive : public Primitive, public AlphaFuncProperty{
00300       protected:
00301       friend class SceneObject;
00302       int w,h;
00303       qt::GLImg texture;
00304       const icl32f *px, *py, *pz, *pnx,  *pny, *pnz;
00305       int stride;
00306       const core::ImgBase *image;
00307       
00308       public:
00309       TextureGridPrimitive(int w, int h, const core::ImgBase *image,
00310                            const icl32f *px, const icl32f *py, const icl32f *pz,
00311                            const icl32f *pnx=0, const icl32f *pny=0, const icl32f *pnz=0,
00312                            int stride = 1,bool createTextureOnce=true,core::scalemode sm=core::interpolateLIN):
00313       Primitive(Primitive::texture),w(w),h(h),texture(image,sm),px(px),py(py),pz(pz),
00314       pnx(pnx),pny(pny),pnz(pnz),stride(stride),image(createTextureOnce ? 0 : image){}
00315   
00316       virtual void render(const Primitive::RenderContext &ctx);
00317       
00318       virtual Primitive *copy() const { 
00319         return new TextureGridPrimitive(w,h,image ? image : texture.extractImage(),
00320                                         px,py,pz,pnx,pny,pnz,stride,!image,
00321                                         texture.getScaleMode()); 
00322       }
00323       void getAABB(utils::Range32f aabb[3]);
00324       
00325       inline Vec getPos(int x, int y) const {
00326         const int idx = stride*(x + w*y);
00327         return Vec(px[idx],py[idx],pz[idx],1);
00328       }
00329     };
00330   
00331     class TwoSidedTextureGridPrimitive : public TextureGridPrimitive{
00332       qt::GLImg back;
00333       const core::ImgBase *iback;
00334       public:
00335       TwoSidedTextureGridPrimitive(int w, int h, const core::ImgBase *front, const core::ImgBase *back,
00336                                    const icl32f *px, const icl32f *py, const icl32f *pz,
00337                                    const icl32f *pnx=0, const icl32f *pny=0, const icl32f *pnz=0,
00338                                    int stride = 1,bool createFrontOnce=true,
00339                                    bool createBackOnce=true, core::scalemode sm=core::interpolateLIN):
00340       TextureGridPrimitive(w,h,front,px,py,pz,pnx,pny,pnz,stride,createFrontOnce,sm),back(back,sm),
00341       iback(createBackOnce ? 0 : back){}
00342       
00343       virtual void render(const Primitive::RenderContext &ctx);
00344     };
00345     
00347 
00349     struct SharedTexturePrimitive : public QuadPrimitive, public AlphaFuncProperty{
00350       int sharedTextureIndex;
00351       
00353       SharedTexturePrimitive(int a, int b, int c, int d, 
00354                        int sharedTextureIndex,
00355                        int na=-1, int nb=-1, int nc=-1, int nd=-1):
00356       QuadPrimitive(a,b,c,d,na,nb,nc,nd), sharedTextureIndex(sharedTextureIndex){
00357         type = Primitive::texture;
00358       }
00359   
00361       virtual void render(const Primitive::RenderContext &ctx);
00362   
00364       virtual Primitive *copy() const { 
00365         return new SharedTexturePrimitive(*this);
00366       }
00367     };
00368   
00370     struct GenericTexturePrimitive : public Primitive, public AlphaFuncProperty{
00371       utils::SmartPtr<qt::GLImg> texture;
00372       const core::ImgBase *image;
00373   
00374       std::vector<Vec> ps;
00375       std::vector<utils::Point32f> texCoords;
00376       std::vector<Vec> normals;
00377       
00379       std::vector<int> vertexIndices;
00380       std::vector<int> normalIndices;
00381   
00383       GenericTexturePrimitive(const core::ImgBase *image, int numPoints,
00384                               const float *xs, const float *ys, const float *zs, int xyzStride,
00385                               const utils::Point32f *texCoords, const float *nxs=0, const float *nys=0,
00386                               const float *nzs=0, int nxyzStride=1, bool createTextureOnce=true);
00387       
00389       GenericTexturePrimitive(const core::ImgBase *image, int numPoints, const int *vertexIndices,
00390                               const utils::Point32f *texCoords, const int *normalIndices = 0,
00391                               bool createTextureOnce=true);
00392       
00394       virtual void render(const Primitive::RenderContext &ctx);    
00395   
00397       virtual Primitive *copy() const {
00398         GenericTexturePrimitive *cpy = new GenericTexturePrimitive(*this);
00399         cpy->texture = new qt::GLImg(image ? image : texture->extractImage());
00400         return cpy;
00401       }
00402     };
00403     
00405 
00406     struct TextPrimitive : public TexturePrimitive{
00408       int textSize;
00409   
00411       GeomColor textColor;
00412       
00414       static core::Img8u create_texture(const std::string &text, const GeomColor &color, int textSize);
00415       
00417 
00419       float billboardHeight; 
00420       
00422       TextPrimitive(int a, int b, int c, int d, 
00423                     const std::string &text,
00424                     int textSize=20,
00425                     const GeomColor &textColor=GeomColor(255,255,255,255),
00426                     int na=-1, int nb=-1, int nc=-1, int nd=-1,
00427                     float billboardHeight=0,
00428                     core::scalemode sm=core::interpolateLIN);
00429       ~TextPrimitive();
00430   
00432       virtual void render(const Primitive::RenderContext &ctx);
00433   
00435       virtual Primitive *copy() const {
00436         Primitive *p = TexturePrimitive::copy();
00437         p->type = text;
00438         return p;
00439       }
00440   
00442       inline void updateText(const std::string &newText){
00443         core::Img8u t = create_texture(newText,textColor,textSize);
00444         texture.update(&t);
00445       }
00446   
00447     };
00448   
00449     
00450   } // namespace geom
00451 }
00452 
00453 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines