# Visualization Library

A lightweight C++ OpenGL middleware for 2D/3D graphics

[Home] [Tutorials] [All Classes] [Grouped Classes]

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

# D:/VL/BuildSystem/VL_DISTRO/Visualization_Library_SDK/src/vlGraphics/Geometry.hpp

Go to the documentation of this file.

    00001 /**************************************************************************************/
    00002 /*                                                                                    */
    00003 /*  Visualization Library                                                             */
    00004 /*  http://www.visualizationlibrary.com                                               */
    00005 /*                                                                                    */
    00006 /*  Copyright (c) 2005-2010, Michele Bosi                                             */
    00007 /*  All rights reserved.                                                              */
    00008 /*                                                                                    */
    00009 /*  Redistribution and use in source and binary forms, with or without modification,  */
    00010 /*  are permitted provided that the following conditions are met:                     */
    00011 /*                                                                                    */
    00012 /*  - Redistributions of source code must retain the above copyright notice, this     */
    00013 /*  list of conditions and the following disclaimer.                                  */
    00014 /*                                                                                    */
    00015 /*  - Redistributions in binary form must reproduce the above copyright notice, this  */
    00016 /*  list of conditions and the following disclaimer in the documentation and/or       */
    00017 /*  other materials provided with the distribution.                                   */
    00018 /*                                                                                    */
    00019 /*  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND   */
    00020 /*  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED     */
    00021 /*  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE            */
    00022 /*  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR  */
    00023 /*  ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES    */
    00024 /*  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;      */
    00025 /*  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON    */
    00026 /*  ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT           */
    00027 /*  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS     */
    00028 /*  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.                      */
    00029 /*                                                                                    */
    00030 /**************************************************************************************/
    00031
    00032 #ifndef Geometry_INCLUDE_ONCE
    00033 #define Geometry_INCLUDE_ONCE
    00034
    00035 #include <vlGraphics/IVertexAttribSet.hpp>
    00036 #include <vlGraphics/GLBufferObject.hpp>
    00037 #include <vlCore/Vector2.hpp>
    00038 #include <vlCore/Vector4.hpp>
    00039 #include <vlGraphics/Renderable.hpp>
    00040 #include <vlCore/vlnamespace.hpp>
    00041 #include <vlCore/Log.hpp>
    00042 #include <vlCore/Say.hpp>
    00043 #include <vlCore/Colors.hpp>
    00044 #include <vlGraphics/DrawElements.hpp>
    00045 #include <vlGraphics/DrawArrays.hpp>
    00046 #include <vlCore/Collection.hpp>
    00047 #include <vlGraphics/VertexAttribInfo.hpp>
    00048
    00049 namespace vl
    00050 {
    00051   class OpenGLContext;
    00052
    00053   //------------------------------------------------------------------------------
    00054   // Geometry
    00055   //------------------------------------------------------------------------------
    00067   class VLGRAPHICS_EXPORT Geometry: public Renderable, public IVertexAttribSet
    00068   {
    00069   private:
    00070     class TextureArray: public Object
    00071     {
    00072     public:
    00073       TextureArray(int unit, ArrayAbstract* data): mTextureUnit(unit), mTexCoordArray(data) {}
    00074       int mTextureUnit;
    00075       ref<ArrayAbstract> mTexCoordArray;
    00076       const GLBufferObject* gpuBuffer() const { return mTexCoordArray->gpuBuffer(); }
    00077       GLBufferObject* gpuBuffer() { return mTexCoordArray->gpuBuffer(); }
    00078     };
    00079
    00080   public:
    00081     virtual const char* className() { return "vl::Geometry"; }
    00082
    00084     Geometry();
    00085
    00087     virtual ~Geometry();
    00088
    00092     ref<Geometry> shallowCopy();
    00093
    00097     void shallowCopy(Geometry*);
    00098
    00102     ref<Geometry> deepCopy() const;
    00103
    00107     void deepCopy(Geometry* ) const;
    00108
    00110     Geometry& operator=(const Geometry& other);
    00111
    00113     Collection<DrawCall>* drawCalls() { return &mDrawCalls; }
    00114
    00116     const Collection<DrawCall>* drawCalls() const { return &mDrawCalls; }
    00117
    00119     void setNormal(const fvec3& normal) { mNormal = normal; }
    00120
    00122     const fvec3& normal() { return mNormal; }
    00123
    00125     void setColor(const fvec4& color) { mColor = color; }
    00126
    00128     const fvec4& color() { return mColor; }
    00129
    00131     void setSecondaryColor(const fvec4& color) { mSecondaryColor = color; }
    00132
    00134     const fvec4& secondaryColor() { return mSecondaryColor; }
    00135
    00136     // ------------------------------------------------------------------------
    00137     // --- Vertex Array Manipulation ---
    00138     // ------------------------------------------------------------------------
    00139
    00152     void toGenericVertexAttribs();
    00153
    00166     void computeNormals(bool verbose=false);
    00167
    00171     bool flipNormals();
    00172
    00183     void transform(const mat4&matr, bool normalize = true);
    00184
    00186     virtual void clearArrays(bool clear_draw_calls=true);
    00187
    00189     void updateVBOs(bool discard_local_data=false, bool force_update=false);
    00190
    00192     void deleteVBOs();
    00193
    00194     // ------------------------------------------------------------------------
    00195     // --- Geometry Utils ---
    00196     // ------------------------------------------------------------------------
    00197
    00199     void mergeTriangleStrips();
    00200
    00202     void convertDrawCallToDrawArrays();
    00203
    00207     bool sortVertices();
    00208
    00210     void colorizePrimitives();
    00211
    00212     void regenerateVertices(const std::vector<size_t>& map_new_to_old);
    00213
    00214     // mic fixme: move somewhere else?
    00223     // Based on:
    00224     // Lengyel, Eric. “Computing Tangent Space Basis Vectors for an Arbitrary Mesh”. Terathon Software 3D Graphics Library, 2001.
    00225     // http://www.terathon.com/code/tangent.html
    00226     static void computeTangentSpace(
    00227       size_t vert_count,
    00228       const vl::fvec3 *vertex,
    00229       const vl::fvec3* normal,
    00230       const vl::fvec2 *texcoord,
    00231       const vl::DrawCall* primitives,
    00232       vl::fvec3 *tangent,
    00233       vl::fvec3 *bitangent );
    00234
    00235     // ------------------------------------------------------------------------
    00236     // --- IVertexAttribSet Interface Implementation ---
    00237     // ------------------------------------------------------------------------
    00238
    00239     void setVertexArray(ArrayAbstract* data);
    00240
    00241     const ArrayAbstract* vertexArray() const { return mVertexArray.get(); }
    00242
    00243     ArrayAbstract* vertexArray() { return mVertexArray.get(); }
    00244
    00245     void setNormalArray(ArrayAbstract* data);
    00246
    00247     const ArrayAbstract* normalArray() const { return mNormalArray.get(); }
    00248
    00249     ArrayAbstract* normalArray() { return mNormalArray.get(); }
    00250
    00251     void setColorArray(ArrayAbstract* data);
    00252
    00253     const ArrayAbstract* colorArray() const { return mColorArray.get(); }
    00254
    00255     ArrayAbstract* colorArray() { return mColorArray.get(); }
    00256
    00257     void setSecondaryColorArray(ArrayAbstract* data);
    00258
    00259     const ArrayAbstract* secondaryColorArray() const { return mSecondaryColorArray.get(); }
    00260
    00261     ArrayAbstract* secondaryColorArray() { return mSecondaryColorArray.get(); }
    00262
    00263     void setFogCoordArray(ArrayAbstract* data);
    00264
    00265     const ArrayAbstract* fogCoordArray() const { return mFogCoordArray.get(); }
    00266
    00267     ArrayAbstract* fogCoordArray() { return mFogCoordArray.get(); }
    00268
    00269     void setTexCoordArray(int tex_unit, ArrayAbstract* data);
    00270
    00271     const ArrayAbstract* texCoordArray(int tex_unit) const
    00272     {
    00273       for(int i=0; i<mTexCoordArrays.size(); ++i)
    00274         if (mTexCoordArrays.at(i)->mTextureUnit == tex_unit)
    00275           return mTexCoordArrays.at(i)->mTexCoordArray.get();
    00276       return NULL;
    00277     }
    00278
    00279     ArrayAbstract* texCoordArray(int tex_unit)
    00280     {
    00281       for(int i=0; i<mTexCoordArrays.size(); ++i)
    00282         if (mTexCoordArrays.at(i)->mTextureUnit == tex_unit)
    00283           return mTexCoordArrays.at(i)->mTexCoordArray.get();
    00284       return NULL;
    00285     }
    00286
    00287     int texCoordArrayCount() const { return mTexCoordArrays.size(); }
    00288
    00289     void getTexCoordArrayAt(int i, int& out_tex_unit, const ArrayAbstract* &tex_array) const
    00290     {
    00291       VL_CHECK(i<mTexCoordArrays.size());
    00292       out_tex_unit = mTexCoordArrays[i]->mTextureUnit;
    00293       tex_array = mTexCoordArrays[i]->mTexCoordArray.get();
    00294     }
    00295
    00296     void setVertexAttribArray(unsigned int attrib_idx, ArrayAbstract* data, bool normalize=true, EVertexAttribBehavior data_behav=VAB_NORMAL) { setVertexAttribArray(VertexAttribInfo(attrib_idx, data, normalize, data_behav)); }
    00297
    00298     void setVertexAttribArray(const VertexAttribInfo& info);
    00299
    00300     const ArrayAbstract* vertexAttrib(unsigned int attrib_idx) const;
    00301
    00302     ArrayAbstract* vertexAttrib(unsigned int attrib_idx);
    00303
    00304     const VertexAttribInfo* vertexAttribInfo(unsigned int attrib_idx) const;
    00305
    00306     VertexAttribInfo* vertexAttribInfo(unsigned int attrib_idx);
    00307
    00308     ref<VertexAttribInfo> eraseVertexAttrib(unsigned int attrib_idx);
    00309
    00310     int vertexAttribInfoCount() const { return mVertexAttribArrays.size(); }
    00311
    00312     const VertexAttribInfo* getVertexAttribInfoAt(int i) const { return mVertexAttribArrays[i].get(); }
    00313
    00315     Collection<VertexAttribInfo>* vertexAttribArrays() { return &mVertexAttribArrays; }
    00316
    00318     const Collection<VertexAttribInfo>* vertexAttribArrays() const { return &mVertexAttribArrays; }
    00319
    00320   protected:
    00321     virtual void computeBounds_Implementation();
    00322
    00323     virtual void render_Implementation(const Actor* actor, const Shader* shader, const Camera* camera, OpenGLContext* gl_context) const;
    00324
    00325     // render calls
    00326     Collection<DrawCall> mDrawCalls;
    00327
    00328     // geometry color when no color array is defined
    00329     fvec4 mColor;
    00330
    00331     // geometry color when no color array is defined
    00332     fvec4 mSecondaryColor;
    00333
    00334     // normal when no normal array is defined
    00335     fvec3 mNormal;
    00336
    00337     //  --- IVertexAttribSet interface concrete implementation ---
    00338
    00339     // conventional vertex attributes
    00340     ref<ArrayAbstract> mVertexArray;
    00341     ref<ArrayAbstract> mNormalArray;
    00342     ref<ArrayAbstract> mColorArray;
    00343     ref<ArrayAbstract> mSecondaryColorArray;
    00344     ref<ArrayAbstract> mFogCoordArray;
    00345     Collection<TextureArray> mTexCoordArrays;
    00346     // generic vertex attributes
    00347     Collection<VertexAttribInfo> mVertexAttribArrays;
    00348   };
    00349   //------------------------------------------------------------------------------
    00350 }
    00351
    00352 #endif

Visualization Library v2011.05.1142 Reference Documentation
Copyright 2005-2011 Michele Bosi. All rights reserved.
Updated on Thu May 26 2011 22:51:23.
Permission is granted to use this page to write and publish articles regarding Visualization Library.