arbeit
Main Page | Namespace List | Class Hierarchy | Alphabetical List | Compound List | File List | Namespace Members | Compound Members | File Members

genPrim.h

Go to the documentation of this file.
00001 //////////////////////////////////////////////////////////////////////
00002 // 6/19/03      Joe Kniss       Scientific Computing and Imaging Institute
00003 // School of Computing          University of Utah
00004 //
00005 //  This library is free software; you can redistribute it and/or
00006 //  modify it under the terms of the GNU Lesser General Public
00007 //  License as published by the Free Software Foundation; either
00008 //  version 2.1 of the License, or (at your option) any later version.
00009 //
00010 //  This library is distributed in the hope that it will be useful,
00011 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013 //  Lesser General Public License for more details.
00014 //
00015 //  You should have received a copy of the GNU Lesser General Public
00016 //  License along with this library; if not, write to the Free Software
00017 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00018 
00019 
00020 #ifndef __GLIFT_GENPRIM_DOT_H
00021 #define __GLIFT_GENPRIM_DOT_H
00022 
00023 #include "drawAttrib.h"
00024 #include "drawableGLI.h"
00025 #include <stateGlift.h>
00026 #include <vector>
00027 
00028 
00029 namespace glift {
00030 
00031 //////////////////////////////////////////////////////////////////////////
00032 ///
00033 /// GenPrimGL, A (very) generic geometry primitive
00034 ///
00035 /// Yes this is a lot of template parameters, but you can typedef this
00036 /// class to be a specific type of primitive
00037 ///  - VERTA  = Vertex attribute type
00038 ///  - TEXCA  = Texture Coordinate attribute type
00039 ///  - IDXA   = Index attribute type
00040 ///  - RANGEA = Range elements attribute type
00041 ///////////////////////////////////////////////////////////////////////
00042 template <
00043    class VERTA  = VertAttribV3F,          
00044    class TEXCA  = TexCoordAttribV3F,      
00045    class IDXA   = IndexAttribV3UI,        
00046    class RANGEA = GenDrawAttrib<UIntV2A>  
00047 >
00048 class GenPrimGL : 
00049    public DrawableGLI, 
00050    public StateGLI
00051 {
00052 public:
00053    GenPrimGL(GLenum   primType,
00054       // number of verticies, also number of Norms, tcorrds, colors if enabled
00055       unsigned int nVerts,         
00056       unsigned int nIdx,           // number of indicies
00057       unsigned int nTextures = 0,  // number of textures (0-16 currently)
00058       unsigned int nRanges   = 0,  // index ranges 2 vectors, start & finish index numbers
00059       bool norms  = false,
00060       bool pColor = false,
00061       bool sColor = false,
00062       bool fog    = false);
00063    GenPrimGL(const GenPrimGL &gpgl);
00064 
00065    virtual ~GenPrimGL(){}
00066 
00067    ///////////////////////////////////////
00068    /// Draw range
00069    ///  ranges from Range Array
00070    void drawRange(unsigned int rIdx);
00071    ///  index start and finish ranges
00072    void drawRange(unsigned int sIdx, unsigned int fIdx);
00073 
00074    ///////////////////////////////////////
00075    /// Get the attributes
00076    /// array, size, types, etc...
00077    VERTA*  getVertAttrib()                {return &_verts;}  
00078    IDXA*   getIdxAttrib()                 {return &_idx;}
00079    TEXCA*  getTCoordAttrib(int tcNum = 0) {return &_tcoords[tcNum];}
00080    RANGEA* getRangeAttrib()               {return &_range;}
00081 
00082 protected:
00083    virtual void drawDef();
00084    virtual void bindDef();
00085    virtual void releaseDef();
00086 
00087    VERTA  _verts;
00088    IDXA   _idx;
00089    RANGEA _range;
00090    std::vector<TEXCA> _tcoords;
00091    GLenum _primType;
00092    bool _release;
00093 
00094 };
00095 
00096 ///////////////////////////////////////////////////////////////////////////
00097 ///////////////////////////////////////////////////////////////////////////
00098 /// Implementation
00099 ///////////////////////////////////////////////////////////////////////////
00100 ///////////////////////////////////////////////////////////////////////////
00101 
00102 //////////////////////////////////////////////////////////////////////
00103 /// Construction
00104 ///////////////////////////////////////////////////////////////////////
00105 
00106 template< class VERTA, class TEXCA, class IDXA, class RANGEA >
00107 GenPrimGL<VERTA,TEXCA,IDXA,RANGEA>::
00108 GenPrimGL(GLenum primType,
00109           // number of verticies, also number of Norms, tcorrds, colors if enabled
00110           unsigned int nVerts,         
00111           unsigned int nIdx,           // number of indicies
00112           unsigned int nTextures,      // number of textures (0-16 currently)
00113           unsigned int nRange,         // number of range indicies
00114           bool norms,
00115           bool pColor,
00116           bool sColor,
00117           bool fog)
00118           : _release(true),
00119           _verts(nVerts),
00120           _idx(nIdx),
00121           _primType(primType),
00122           _range(nRange)
00123 {
00124    for(unsigned int i=0; i<nTextures; ++i)
00125    {
00126       _tcoords.push_back(TEXCA(nVerts,i));
00127    }
00128 
00129 }
00130 
00131 template< class VERTA, class TEXCA, class IDXA, class RANGEA >
00132 GenPrimGL<VERTA,TEXCA,IDXA,RANGEA>::
00133 GenPrimGL(const GenPrimGL &gpgl)
00134 :_verts(gpgl._verts), _idx(gpgl._idx), _range(gpgl._range),
00135  _tcoords(gpgl._tcoords), _primType(gpgl._primType),
00136  _release(true)
00137 {
00138 }
00139 
00140 
00141 ///////////////////////////////////////////////////////////////////////
00142 /// Draw
00143 ///////////////////////////////////////////////////////////////////////
00144 template< class VERTA, class TEXCA, class IDXA, class RANGEA >
00145 void GenPrimGL<VERTA,TEXCA,IDXA,RANGEA>::
00146 drawDef()
00147 {
00148    if(!isBound())
00149    {
00150       bindDef();
00151       _release = true;
00152    }
00153    else /// bound externally, release should be external.
00154       _release = false;
00155 
00156    ///////////////////////////
00157    /// draw!!!!!
00158    //if(_idx.getSize() == 0)
00159    //{
00160    //   glDrawArrays( _primType, 0, _verts.getArray().dim(0) );
00161    //   glErr("GenPrimGL::drawDef::glDrawArrays");
00162    //}
00163    //else
00164    //{
00165    glDrawElements(_primType, 
00166       _idx.getSize()*IDXA::DIM, 
00167       IDXA::GLTYPE, 
00168       _idx.getArray().data());
00169    DrawableGLI::glerr("drawDef::glDrawElements");
00170    //}
00171 
00172    if(_release)
00173       releaseDef();
00174 
00175    DrawableGLI::glerr("drawDef()");
00176 }
00177 
00178 ///////////////////////////////////////////////////////////////////////
00179 /// Bind Attributes
00180 ///////////////////////////////////////////////////////////////////////
00181 template< class VERTA, class TEXCA, class IDXA, class RANGEA >
00182 void GenPrimGL<VERTA,TEXCA,IDXA,RANGEA>::
00183 bindDef()
00184 {
00185    _verts.enable();
00186    for(unsigned int i = 0; i < _tcoords.size(); ++i)
00187    {    
00188       if(_tcoords[i].isActive())
00189          _tcoords[i].enable();
00190    }
00191 }
00192 
00193 ///////////////////////////////////////////////////////////////////////
00194 /// Release Attributes
00195 ///////////////////////////////////////////////////////////////////////
00196 template< class VERTA, class TEXCA, class IDXA, class RANGEA >
00197 void GenPrimGL<VERTA,TEXCA,IDXA,RANGEA>::
00198 releaseDef()
00199 {
00200    _verts.disable();
00201    for(unsigned int i = 0; i < _tcoords.size(); ++i)
00202    {
00203       if(_tcoords[i].isActive())
00204          _tcoords[i].disable();
00205    }
00206    _release = true;
00207 }
00208 
00209 }//end namespace glift
00210 
00211 #endif
00212 

Send questions, comments, and bug reports to:
jmk