Visualization Library v1.0.3A lightweight C++ OpenGL middleware for 2D/3D graphics |
[Download] [Tutorials] [All Classes] [Grouped Classes] |
00001 /**************************************************************************************/ 00002 /* */ 00003 /* Visualization Library */ 00004 /* http://visualizationlibrary.org */ 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/BufferObject.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 VL_INSTRUMENT_CLASS(vl::Geometry, Renderable) 00070 00071 // use deepCopy() and shallowCopy() instead 00072 // Geometry(const Geometry& other): Renderable(other) { } 00073 Geometry& operator=(const Geometry&) { return *this; } 00074 00075 private: 00076 class TextureArray: public Object 00077 { 00078 public: 00079 TextureArray(int unit, ArrayAbstract* data): mTextureSampler(unit), mTexCoordArray(data) { } 00080 const BufferObject* bufferObject() const { return mTexCoordArray->bufferObject(); } 00081 BufferObject* bufferObject() { return mTexCoordArray->bufferObject(); } 00082 int mTextureSampler; 00083 ref<ArrayAbstract> mTexCoordArray; 00084 }; 00085 00086 public: 00088 Geometry(); 00089 00091 virtual ~Geometry(); 00092 00096 ref<Geometry> shallowCopy() const; 00097 00101 Geometry& shallowCopyFrom(const Geometry&); 00102 00106 ref<Geometry> deepCopy() const; 00107 00111 Geometry& deepCopyFrom(const Geometry&); 00112 00114 Collection<DrawCall>* drawCalls() { return &mDrawCalls; } 00115 00117 const Collection<DrawCall>* drawCalls() const { return &mDrawCalls; } 00118 00120 void setColorArray(const fvec4& color) 00121 { 00122 u32 vert_count = (u32)(vertexArray() ? vertexArray()->size() : vertexAttribArray(VA_Position) ? vertexAttribArray(VA_Position)->data()->size() : 0); 00123 VL_CHECK( vert_count ) 00124 ref<ArrayFloat4> color_array = new ArrayFloat4; 00125 color_array->resize(vert_count); 00126 for(u32 i=0; i<color_array->size(); ++i) 00127 color_array->at(i) = color; 00128 #if defined(VL_OPENGL_ES2) 00129 setVertexAttribArray(VA_Color, color_array.get()); 00130 #else 00131 setColorArray(color_array.get()); 00132 #endif 00133 } 00134 00136 virtual void clearArrays(bool clear_draw_calls=true); 00137 00138 // --- Renderable interface implementation --- 00139 00141 virtual void updateDirtyBufferObject(EBufferObjectUpdateMode mode); 00142 00144 virtual void deleteBufferObject(); 00145 00146 // ------------------------------------------------------------------------ 00147 // Geometry Tools 00148 // ------------------------------------------------------------------------ 00149 00163 void convertToVertexAttribs(); 00164 00177 void computeNormals(bool verbose=false); 00178 00182 bool flipNormals(); 00183 00185 void fixTriangleWinding(); 00186 00197 void transform(const mat4&matr, bool normalize = true); 00198 00200 void convertDrawCallToDrawArrays(); 00201 00204 DrawCall* mergeTriangleStrips(); 00205 00207 void mergeDrawCallsWithPrimitiveRestart(EPrimitiveType primitive_type); 00208 00210 void mergeDrawCallsWithMultiDrawElements(EPrimitiveType primitive_type); 00211 00214 void mergeDrawCallsWithTriangles(EPrimitiveType primitive_type); 00215 00218 void triangulateDrawCalls(); 00219 00223 void shrinkDrawCalls(); 00224 00227 void makeGLESFriendly(); 00228 00233 bool sortVertices(); 00234 00237 void regenerateVertices(const std::vector<u32>& map_new_to_old); 00238 00240 void colorizePrimitives(); 00241 00250 // Based on: 00251 // Lengyel, Eric. “Computing Tangent Space Basis Vectors for an Arbitrary Mesh”. Terathon Software 3D Graphics Library, 2001. 00252 // http://www.terathon.com/code/tangent.html 00253 static void computeTangentSpace( 00254 u32 vert_count, 00255 const vl::fvec3 *vertex, 00256 const vl::fvec3* normal, 00257 const vl::fvec2 *texcoord, 00258 const vl::DrawCall* primitives, 00259 vl::fvec3 *tangent, 00260 vl::fvec3 *bitangent ); 00261 00262 // ------------------------------------------------------------------------ 00263 // IVertexAttribSet Interface Implementation 00264 // ------------------------------------------------------------------------ 00265 00266 void setVertexArray(ArrayAbstract* data); 00267 00268 const ArrayAbstract* vertexArray() const { return mVertexArray.get(); } 00269 00270 ArrayAbstract* vertexArray() { return mVertexArray.get(); } 00271 00272 void setNormalArray(ArrayAbstract* data); 00273 00274 const ArrayAbstract* normalArray() const { return mNormalArray.get(); } 00275 00276 ArrayAbstract* normalArray() { return mNormalArray.get(); } 00277 00278 void setColorArray(ArrayAbstract* data); 00279 00280 const ArrayAbstract* colorArray() const { return mColorArray.get(); } 00281 00282 ArrayAbstract* colorArray() { return mColorArray.get(); } 00283 00284 void setSecondaryColorArray(ArrayAbstract* data); 00285 00286 const ArrayAbstract* secondaryColorArray() const { return mSecondaryColorArray.get(); } 00287 00288 ArrayAbstract* secondaryColorArray() { return mSecondaryColorArray.get(); } 00289 00290 void setFogCoordArray(ArrayAbstract* data); 00291 00292 const ArrayAbstract* fogCoordArray() const { return mFogCoordArray.get(); } 00293 00294 ArrayAbstract* fogCoordArray() { return mFogCoordArray.get(); } 00295 00296 void setTexCoordArray(int tex_unit, ArrayAbstract* data); 00297 00298 const ArrayAbstract* texCoordArray(int tex_unit) const 00299 { 00300 for(int i=0; i<mTexCoordArrays.size(); ++i) 00301 if (mTexCoordArrays.at(i)->mTextureSampler == tex_unit) 00302 return mTexCoordArrays.at(i)->mTexCoordArray.get(); 00303 return NULL; 00304 } 00305 00306 ArrayAbstract* texCoordArray(int tex_unit) 00307 { 00308 for(int i=0; i<mTexCoordArrays.size(); ++i) 00309 if (mTexCoordArrays.at(i)->mTextureSampler == tex_unit) 00310 return mTexCoordArrays.at(i)->mTexCoordArray.get(); 00311 return NULL; 00312 } 00313 00314 int texCoordArrayCount() const { return mTexCoordArrays.size(); } 00315 00316 void getTexCoordArrayAt(int i, int& out_tex_unit, const ArrayAbstract* &tex_array) const 00317 { 00318 VL_CHECK(i<mTexCoordArrays.size()); 00319 out_tex_unit = mTexCoordArrays[i]->mTextureSampler; 00320 tex_array = mTexCoordArrays[i]->mTexCoordArray.get(); 00321 } 00322 00323 void setVertexAttribArray(unsigned int attrib_location, ArrayAbstract* data, bool normalize=true, EVertexAttribInterpretation data_behav=VAI_NORMAL) { setVertexAttribArray(VertexAttribInfo(attrib_location, data, normalize, data_behav)); } 00324 00325 void setVertexAttribArray(const VertexAttribInfo& info); 00326 00327 const VertexAttribInfo* vertexAttribArray(unsigned int attrib_location) const; 00328 00329 VertexAttribInfo* vertexAttribArray(unsigned int attrib_location); 00330 00331 Collection<VertexAttribInfo>* vertexAttribArrays() { return &mVertexAttribArrays; } 00332 00333 const Collection<VertexAttribInfo>* vertexAttribArrays() const { return &mVertexAttribArrays; } 00334 00335 protected: 00336 virtual void computeBounds_Implementation(); 00337 00338 virtual void render_Implementation(const Actor* actor, const Shader* shader, const Camera* camera, OpenGLContext* gl_context) const; 00339 00340 // render calls 00341 Collection<DrawCall> mDrawCalls; 00342 00343 // --- IVertexAttribSet interface concrete implementation --- 00344 00345 // conventional vertex attributes 00346 ref<ArrayAbstract> mVertexArray; 00347 ref<ArrayAbstract> mNormalArray; 00348 ref<ArrayAbstract> mColorArray; 00349 ref<ArrayAbstract> mSecondaryColorArray; 00350 ref<ArrayAbstract> mFogCoordArray; 00351 Collection<TextureArray> mTexCoordArrays; 00352 // generic vertex attributes 00353 Collection<VertexAttribInfo> mVertexAttribArrays; 00354 }; 00355 //------------------------------------------------------------------------------ 00356 } 00357 00358 #endif