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

drawAttrib.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_DRAWATTRIB_DOT_H
00021 #define __GLIFT_DRAWATTRIB_DOT_H
00022 
00023 #include "../core/gliftObject.h"
00024 #include <GL/glew.h>
00025 #include <GL/gl.h>
00026 #include <utilGlift.h>
00027 #include <arrayGutz.h>
00028 #include <mathGutz.h>
00029 #include <smartptr.h>
00030 #include <iostream>
00031 
00032 namespace glift {
00033 
00034 /////////////////////////////////////////////////////////////////////////
00035 ///
00036 /// drawAttrib.h
00037 ///
00038 /// drawable Attributes, basic classes defining things like:
00039 ///  vertex arrays
00040 ///  texture coordinate arrays
00041 ///  etc...
00042 /// Base Attribute
00043 ///////////////////////////////////////////////////////////////////////
00044 
00045 class DrawAttrib : public GliftObject {
00046 public:
00047    DrawAttrib(bool active = true):_active(active){}
00048    virtual ~DrawAttrib(){}
00049 
00050    void enable(){enableDef();}
00051    void disable(){enableDef();}
00052 
00053    ///////////////////////////////////////////
00054    /// turn an attribute on and off
00055    void activate(){_active = true;}
00056    void deactivate() {_active = false;}
00057    void setActive(bool onoff) {_active = onoff;}
00058    bool isActive(){return _active;}
00059 
00060 protected:
00061    virtual void enableDef(){}
00062    virtual void disableDef(){}
00063    bool _active;
00064 };
00065 
00066 ///////////////////////////////////////////////////////////////////////
00067 /// Generic Attributes, usefull for vertex program attribute arrays
00068 /// you can pick exactly what type of vector data you want for
00069 /// an attribute (array of vectors for GL) by selecting the
00070 /// appropriate type from "gutzAttrib.h", this will be the
00071 /// template parameter for the attribute class.
00072 ///  For example, I want float 4 vectors, I create an attribute:
00073 ///  GenDrawAttrib<FloatV4A> my4vectorAttrib;
00074 ///////////////////////////////////////////////////////////////////////
00075 
00076 template < class AT  > /// type of this attribute: see gutzAttrib.h
00077 class GenDrawAttrib : public DrawAttrib, public gutz::Counted
00078 {
00079 public:
00080    GenDrawAttrib(unsigned int size,              // how big of an array
00081       unsigned int attribNum  = 0,    // attribute number (GL)
00082       GLboolean    normalized = false)// normalize fixed point types?
00083       :_v(size, typename AT::STORTYPE(0)),
00084       _size(size),
00085       _attribNum(attribNum), 
00086       _norm(normalized)
00087    {}
00088    GenDrawAttrib(const GenDrawAttrib &gda)
00089       :_v(gda._v),_size(gda._size),_attribNum(gda._attribNum),_norm(gda._norm)
00090    {}
00091 
00092    virtual ~GenDrawAttrib(){}
00093 
00094    /////////////////////////////////////////////////
00095    /// attribute vector storage type, float, vec2f, etc..
00096    typedef typename AT::STORTYPE STORTYPE;
00097    typedef typename AT::SYSTYPE SYSTYPE;
00098    typedef gutz::arrayWrap1<STORTYPE> ARRAY_TYPE;
00099    enum{
00100       DIM    = AT::DIM,
00101       GLTYPE = AT::GLTYPE
00102    };
00103 
00104    /////////////////////////////////////////////////
00105    /// Get the Data Array
00106    gutz::arrayWrap1<STORTYPE> 
00107       getArray() const {return gutz::arrayWrap1<STORTYPE>(_v);}
00108 
00109       /////////////////////////////////////////////////
00110       /// Set the Data Array
00111       void      setArray(gutz::arrayWrap1<STORTYPE> v){_v = v;}
00112 
00113       /////////////////////////////////////////////////
00114       /// The array should be bigger than what you need,
00115       /// if it is a dynamic array, set the size to
00116       /// indicate how much of the array is valid,
00117       /// should be -1 if none of it is.
00118       void         setSize(unsigned int size){_size = size;}
00119       unsigned int getSize() {return _size;}
00120 protected:
00121    virtual void enableDef()
00122    {
00123       glEnableVertexAttribArrayARB(_attribNum);
00124       glVertexAttribPointerARB(_attribNum, DIM, GLTYPE, _norm, 0,_v.data());
00125       glerr("enableDef()");
00126    }
00127    virtual void disableDef()
00128    {
00129       glDisableVertexAttribArrayARB(_attribNum);
00130       glerr("disableDef()");
00131    }
00132    gutz::arrayOwn1<STORTYPE> _v;
00133    int        _attribNum;
00134    int        _size;
00135    GLboolean  _norm;
00136 };
00137 
00138 ///////////////////////////////////////////////////////////////////////
00139 /// Specific Attributes
00140 /// The correspond to the standard openGL primitive vectors:
00141 /// Vertex, Color, Normal, Texture etc...
00142 /// Here are my mappings to generic Attributes:
00143 /// Vertex = 0, Color1 = 1, Color2 = 2, Norm = 3, TEX = 4-20 
00144 /// There may be a standard OGL mapping, but it isn't published ;)
00145 ///////////////////////////////////////////////////////////////////////
00146 
00147 ///////////////////////////////////////////////////////////////////////
00148 /// Vertex array attribute
00149 ///////////////////////////////////////////////////////////////////////
00150 template <class VAT> /// attribute type for a vertex array
00151 class VertAttrib : public GenDrawAttrib<VAT>{
00152 public:
00153    VertAttrib(int size)
00154       : GenDrawAttrib<VAT>(size, 0){}
00155       virtual ~VertAttrib(){}
00156 protected:
00157    virtual void enableDef()
00158    { 
00159       glEnableClientState(GL_VERTEX_ARRAY);
00160       glVertexPointer(VAT::DIM, VAT::GLTYPE, 0, _v.data());
00161       glerr("enableDef()");
00162    }
00163    virtual void disableDef()
00164    {
00165       glDisableClientState(GL_VERTEX_ARRAY);
00166       glerr("disbleDef()");
00167    }
00168 };
00169 /// usefull typedefs, try to use these, no templates! hurray
00170 typedef VertAttrib<FloatV2A> VertAttribV2F;
00171 typedef VertAttrib<FloatV3A> VertAttribV3F;
00172 typedef VertAttrib<FloatV4A> VertAttribV4F;
00173 
00174 typedef VertAttrib<DoubleV2A> VertAttribV2D;
00175 typedef VertAttrib<DoubleV3A> VertAttribV3D;
00176 typedef VertAttrib<DoubleV4A> VertAttribV4D;
00177 
00178 typedef VertAttrib<IntV2A> VertAttribV2I;
00179 typedef VertAttrib<IntV3A> VertAttribV3I;
00180 typedef VertAttrib<IntV4A> VertAttribV4I;
00181 
00182 typedef VertAttrib<ShortV2A> VertAttribV2S;
00183 typedef VertAttrib<ShortV3A> VertAttribV3S;
00184 typedef VertAttrib<ShortV4A> VertAttribV4S;
00185 
00186 ///////////////////////////////////////////////////////////////////////
00187 /// TexCoord array attribute
00188 ///////////////////////////////////////////////////////////////////////
00189 template <class VAT> /// attribute type for a vertex array
00190 class TexCoordAttrib : public GenDrawAttrib<VAT>{
00191 public:
00192    TexCoordAttrib(int size, unsigned int texCoordNum=0)
00193       : GenDrawAttrib<VAT>(size, 4+texCoordNum ),_texNum(texCoordNum)
00194    {}
00195    virtual ~TexCoordAttrib(){}
00196 
00197 protected:
00198    virtual void enableDef()
00199    { 
00200       glClientActiveTextureARB(GL_TEXTURE0_ARB + _texNum);
00201       glEnableClientState(GL_TEXTURE_COORD_ARRAY);
00202       glTexCoordPointer(VAT::DIM, VAT::GLTYPE, 0, _v.data());
00203       glClientActiveTextureARB(GL_TEXTURE0_ARB);
00204       glerr("enableDef()");
00205    }
00206    virtual void disableDef()
00207    {
00208       glClientActiveTextureARB(GL_TEXTURE0_ARB + _texNum);
00209       glDisableClientState(GL_TEXTURE_COORD_ARRAY);
00210       glClientActiveTextureARB(GL_TEXTURE0_ARB);
00211       glerr("disableDef()");
00212    }
00213    unsigned int _texNum;
00214 };
00215 /// usefull typedefs, try to use these, no templates! hurray
00216 typedef TexCoordAttrib<FloatV1A> TexCoordAttribV1F;
00217 typedef TexCoordAttrib<FloatV2A> TexCoordAttribV2F;
00218 typedef TexCoordAttrib<FloatV3A> TexCoordAttribV3F;
00219 typedef TexCoordAttrib<FloatV4A> TexCoordAttribV4F;
00220 
00221 typedef TexCoordAttrib<DoubleV1A> TexCoordAttribV1D;
00222 typedef TexCoordAttrib<DoubleV2A> TexCoordAttribV2D;
00223 typedef TexCoordAttrib<DoubleV3A> TexCoordAttribV3D;
00224 typedef TexCoordAttrib<DoubleV4A> TexCoordAttribV4D;
00225 
00226 ///////////////////////////////////////////////////////////////////////
00227 /// Index array attribute
00228 ///////////////////////////////////////////////////////////////////////
00229 template <class VAT> /// attribute type for a vertex array
00230 class IndexAttrib : public GenDrawAttrib<VAT>
00231 {
00232 public:
00233    IndexAttrib(int size, unsigned int texCoordNum=0)
00234       : GenDrawAttrib<VAT>(size, 16 ){}
00235       virtual ~IndexAttrib(){}
00236 protected:
00237    virtual void enableDef()
00238    { 
00239       glEnableClientState(GL_INDEX_ARRAY);
00240       glIndexPointer(VAT::GLTYPE, 0, _v.data());
00241       glerr("enableDef()");
00242    }
00243    virtual void disableDef()
00244    {
00245       glDisableClientState(GL_INDEX_ARRAY);
00246       glerr("disableDef()");
00247    }
00248 };
00249 typedef IndexAttrib<UIntV1A> IndexAttribV1UI;
00250 typedef IndexAttrib<UIntV2A> IndexAttribV2UI;
00251 typedef IndexAttrib<UIntV3A> IndexAttribV3UI;
00252 typedef IndexAttrib<UIntV4A> IndexAttribV4UI;
00253 
00254 typedef IndexAttrib<UShortV1A> IndexAttribV1US;
00255 typedef IndexAttrib<UShortV2A> IndexAttribV2US;
00256 typedef IndexAttrib<UShortV3A> IndexAttribV3US;
00257 typedef IndexAttrib<UShortV4A> IndexAttribV4US;
00258 
00259 typedef IndexAttrib<UByteV1A> IndexAttribV1UB;
00260 typedef IndexAttrib<UByteV2A> IndexAttribV2UB;
00261 typedef IndexAttrib<UByteV3A> IndexAttribV3UB;
00262 typedef IndexAttrib<UByteV4A> IndexAttribV4UB;
00263 
00264 ///////////////////////////////////////////////////////////////////////
00265 /// Normal array attribute
00266 ///////////////////////////////////////////////////////////////////////
00267 template <class VAT> /// attribute type for a vertex array
00268 class NormalAttrib : public GenDrawAttrib<VAT>
00269 {
00270 public:
00271    NormalAttrib(int size, unsigned int texCoordNum=0)
00272       : GenDrawAttrib<VAT>(size, 16 ){}
00273       virtual ~NormalAttrib(){}
00274 protected:
00275    virtual void enableDef()
00276    { 
00277       glEnableClientState(GL_NORMAL_ARRAY);
00278       glNormalPointer(VAT::GLTYPE, 0, _v.data());
00279       glerr("enableDef()");
00280    }
00281    virtual void disableDef()
00282    {
00283       glDisableClientState(GL_NORMAL_ARRAY);
00284       glerr("disbleDef()");
00285    }
00286 };
00287 typedef NormalAttrib<FloatV3A>  NormalAttribV3F;
00288 typedef NormalAttrib<DoubleV3A> NormalAttribV3D;
00289 typedef NormalAttrib<IntV3A>    NormalAttribV3I;
00290 typedef NormalAttrib<ShortV3A>  NormalAttribV3S;
00291 typedef NormalAttrib<ByteV3A>   NormalAttribV3B;
00292 
00293 
00294 
00295 }//end namespace glift
00296 
00297 #endif
00298 

Send questions, comments, and bug reports to:
jmk