Visualization LibraryA lightweight C++ OpenGL middleware for 2D/3D graphics |
[Home] [Tutorials] [All Classes] [Grouped Classes] |
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 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 public: 00047 virtual const char* className() { return "vl::EdgeUpdateCallback"; } 00048 EdgeUpdateCallback(): mShowCreases(true) 00049 { 00050 VL_DEBUG_SET_OBJECT_NAME() 00051 } 00052 EdgeUpdateCallback(const std::vector<EdgeExtractor::Edge>& edge): mEdges(edge), mShowCreases(false) 00053 { 00054 VL_DEBUG_SET_OBJECT_NAME() 00055 } 00056 00058 void setShowCreases(bool sonly) { mShowCreases = sonly; } 00060 bool showCreases() const { return mShowCreases; } 00061 00062 virtual void onActorDelete(Actor*) {} 00063 00064 virtual void onActorRenderStarted(Actor* act, Real /*frame_clock*/, const Camera* cam, Renderable* renderable, const Shader*, int pass) 00065 { 00066 if (pass != 0) 00067 return; 00068 00069 fmat4 vmat = (fmat4)cam->viewMatrix(); 00070 if (act->transform()) 00071 vmat = vmat * (fmat4)act->transform()->worldMatrix(); 00072 fmat4 nmat = vmat.as3x3(); 00073 nmat = nmat.getInverse().transpose(); 00074 00075 ref<Geometry> geom = dynamic_cast<Geometry*>(renderable); 00076 ref<ArrayFloat3> vert_array = dynamic_cast<ArrayFloat3*>(geom->vertexArray()); 00077 // VL_CHECK(vert_array->size() == edges().size()*2); 00078 for(unsigned i=0; i<edges().size(); ++i) 00079 { 00080 bool insert_edge = edges()[i].isCrease() && showCreases(); 00081 if (!insert_edge) 00082 { 00083 fvec3 v1 = vmat * edges()[i].vertex1(); 00084 fvec3 v2 = vmat * edges()[i].vertex2(); 00085 fvec3 v = ((v1+v2) * 0.5f).normalize(); 00086 fvec3 n1 = nmat * edges()[i].normal1(); 00087 fvec3 n2 = nmat * edges()[i].normal2(); 00088 insert_edge = dot(n1, v) * dot(n2, v) < 0; 00089 } 00090 if ( insert_edge ) 00091 { 00092 vert_array->at(i*2+0) = edges()[i].vertex1(); 00093 vert_array->at(i*2+1) = edges()[i].vertex2(); 00094 } 00095 else 00096 { 00097 // degenerate 00098 vert_array->at(i*2+0) = vert_array->at(i*2+1); 00099 } 00100 } 00101 } 00102 00103 const std::vector<EdgeExtractor::Edge>& edges() const { return mEdges; } 00104 std::vector<EdgeExtractor::Edge>& edges() { return mEdges; } 00105 00106 private: 00107 std::vector<EdgeExtractor::Edge> mEdges; 00108 bool mShowCreases; 00109 }; 00110 00111 } 00112 00113 #endif