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 EdgeUpdateCallback_INCLUDE_ONCE 00033 #define EdgeUpdateCallback_INCLUDE_ONCE 00034 00035 #include <vlGraphics/Actor.hpp> 00036 #include <vlGraphics/EdgeExtractor.hpp> 00037 #include <vlGraphics/Geometry.hpp> 00038 #include <vlGraphics/Camera.hpp> 00039 00040 namespace vl 00041 { 00044 class EdgeUpdateCallback: public ActorEventCallback 00045 { 00046 VL_INSTRUMENT_CLASS(vl::EdgeUpdateCallback, ActorEventCallback) 00047 00048 public: 00049 EdgeUpdateCallback(): mShowCreases(true) 00050 { 00051 VL_DEBUG_SET_OBJECT_NAME() 00052 } 00053 00054 EdgeUpdateCallback(const std::vector<EdgeExtractor::Edge>& edge): mEdges(edge), mShowCreases(false) 00055 { 00056 VL_DEBUG_SET_OBJECT_NAME() 00057 } 00058 00060 void setShowCreases(bool sonly) { mShowCreases = sonly; } 00062 bool showCreases() const { return mShowCreases; } 00063 00064 virtual void onActorDelete(Actor*) {} 00065 00066 virtual void onActorRenderStarted(Actor* act, real /*frame_clock*/, const Camera* cam, Renderable* renderable, const Shader*, int pass) 00067 { 00068 if (pass != 0) 00069 return; 00070 00071 fmat4 vmat = (fmat4)cam->viewMatrix(); 00072 if (act->transform()) 00073 vmat = vmat * (fmat4)act->transform()->worldMatrix(); 00074 fmat4 nmat = vmat.as3x3(); 00075 nmat = nmat.getInverse().transpose(); 00076 00077 ref<Geometry> geom = cast<Geometry>(renderable); 00078 ref<ArrayFloat3> vert_array = cast<ArrayFloat3>(geom->vertexArray()); 00079 // VL_CHECK(vert_array->size() == edges().size()*2); 00080 for(unsigned i=0; i<edges().size(); ++i) 00081 { 00082 bool insert_edge = edges()[i].isCrease() && showCreases(); 00083 if (!insert_edge) 00084 { 00085 fvec3 v1 = vmat * edges()[i].vertex1(); 00086 fvec3 v2 = vmat * edges()[i].vertex2(); 00087 fvec3 v = ((v1+v2) * 0.5f).normalize(); 00088 fvec3 n1 = nmat * edges()[i].normal1(); 00089 fvec3 n2 = nmat * edges()[i].normal2(); 00090 insert_edge = dot(n1, v) * dot(n2, v) < 0; 00091 } 00092 if ( insert_edge ) 00093 { 00094 vert_array->at(i*2+0) = edges()[i].vertex1(); 00095 vert_array->at(i*2+1) = edges()[i].vertex2(); 00096 } 00097 else 00098 { 00099 // degenerate 00100 vert_array->at(i*2+0) = vert_array->at(i*2+1); 00101 } 00102 } 00103 } 00104 00105 const std::vector<EdgeExtractor::Edge>& edges() const { return mEdges; } 00106 std::vector<EdgeExtractor::Edge>& edges() { return mEdges; } 00107 00108 private: 00109 std::vector<EdgeExtractor::Edge> mEdges; 00110 bool mShowCreases; 00111 }; 00112 00113 } 00114 00115 #endif