# 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/EdgeRenderer.cpp

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 #include <vlGraphics/EdgeRenderer.hpp>
    00033 #include <vlGraphics/RenderQueue.hpp>
    00034 #include <vlGraphics/OpenGLContext.hpp>
    00035
    00036 using namespace vl;
    00037
    00038 //-----------------------------------------------------------------------------
    00039 const RenderQueue* EdgeRenderer::render(const RenderQueue* render_queue, Camera* camera, Real frame_clock)
    00040 {
    00041   // skip if renderer is disabled
    00042
    00043   if (enableMask() == 0)
    00044     return render_queue;
    00045
    00046   // enter/exit behavior contract
    00047
    00048   class InOutContract
    00049   {
    00050     RendererAbstract* mRenderer;
    00051   public:
    00052     InOutContract(RendererAbstract* renderer, Camera* camera): mRenderer(renderer)
    00053     {
    00054       // increment the render tick.
    00055       mRenderer->incrementRenderTick();
    00056
    00057       // render-target activation.
    00058       // note: an OpenGL context can have multiple rendering targets!
    00059       mRenderer->renderTarget()->activate();
    00060
    00061       // viewport setup.
    00062       camera->viewport()->setClearFlags( mRenderer->clearFlags() );
    00063       camera->viewport()->activate();
    00064
    00065       // dispatch the renderer-started event.
    00066       mRenderer->dispatchOnRendererStarted();
    00067
    00068       // check user-generated errors.
    00069       VL_CHECK_OGL()
    00070     }
    00071
    00072     ~InOutContract()
    00073     {
    00074       // dispatch the renderer-finished event
    00075       mRenderer->dispatchOnRendererFinished();
    00076
    00077       // check user-generated errors.
    00078       VL_CHECK_OGL()
    00079     }
    00080   } contract(this, camera);
    00081
    00082   // --------------- rendering ---------------
    00083
    00084   // update actor cache
    00085
    00086   mVisibleActors.clear();
    00087   for(int i=0; i<render_queue->size(); ++i)
    00088   {
    00089     if ( !isEnabled(render_queue->at(i)->mActor->enableMask()) )
    00090       continue;
    00091     // fails for non-Geometry renderables
    00092     WFInfo* wfinfo = declareActor(render_queue->at(i)->mActor);
    00093     if (wfinfo)
    00094     {
    00095       mVisibleActors[render_queue->at(i)->mActor] = wfinfo;
    00096     }
    00097   }
    00098
    00099   camera->applyProjMatrix();
    00100
    00101   // solid
    00102   glEnable(GL_DEPTH_TEST);
    00103   glDepthFunc(GL_LEQUAL);
    00104   glEnable(GL_POLYGON_OFFSET_FILL);
    00105   glPolygonOffset( polygonOffsetFactor(), polygonOffsetUnits() );
    00106   glColorMask(0,0,0,0);
    00107     renderSolids(camera, frame_clock);
    00108   glDisable(GL_POLYGON_OFFSET_FILL);
    00109   glPolygonOffset( 0.0f, 0.0f );
    00110   glPolygonOffset(0,0);
    00111   glColorMask(1,1,1,1);
    00112
    00113   // front wireframe
    00114   glLineWidth(mLineWidth);
    00115   if (smoothLines())
    00116   {
    00117     glEnable(GL_BLEND);
    00118     glEnable(GL_LINE_SMOOTH);
    00119   }
    00120   renderLines(camera);
    00121
    00122   // back wireframe
    00123   glDisable(GL_DEPTH_TEST);
    00124   glLineWidth(mLineWidth > 2.0f ? mLineWidth / 2.0f : 1.0f);
    00125   glLineStipple(1,0xF0F0);
    00126   glEnable(GL_LINE_STIPPLE);
    00127     if (showHiddenLines()) renderLines(camera);
    00128   glDisable(GL_LINE_SMOOTH);
    00129   glDisable(GL_LINE_STIPPLE);
    00130   glDisable(GL_BLEND);
    00131   glLineWidth(1.0f);
    00132
    00133   // disable all vertex arrays
    00134   renderTarget()->openglContext()->bindVAS(NULL, false, true);
    00135   VL_CHECK( renderTarget()->openglContext()->isCleanState(true) );
    00136
    00137   return render_queue;
    00138 }
    00139 //-----------------------------------------------------------------------------
    00140 void EdgeRenderer::renderSolids(Camera* camera, Real frame_clock)
    00141 {
    00142   // transform
    00143   const Transform* cur_transform = NULL;
    00144   camera->applyViewMatrix();
    00145
    00146   const mat4& view_matrix = camera->viewMatrix();
    00147
    00148   for( std::map< ref<Actor>, ref<WFInfo> >::iterator it = mVisibleActors.begin(); it != mVisibleActors.end(); ++it)
    00149   {
    00150     Actor* actor = it->first.get();
    00151     VL_CHECK(actor);
    00152     VL_CHECK(actor->lod(0));
    00153     const WFInfo* wfinfo = it->second.get();
    00154     VL_CHECK(wfinfo);
    00155
    00156     // --------------- transform ---------------
    00157
    00158     // delta-setup for modelview matrix for the object
    00159     if ( actor->transform() != cur_transform )
    00160     {
    00161       cur_transform = actor->transform();
    00162
    00163       if ( cur_transform )
    00164       {
    00165         #if 0
    00166           glMatrixMode(GL_MODELVIEW);
    00167           VL_glLoadMatrix( view_matrix.ptr() );
    00168           VL_glMultMatrix( cur_transform->worldMatrix().ptr() );
    00169         #else
    00170           // should guarantee better precision
    00171           glMatrixMode(GL_MODELVIEW);
    00172           VL_glLoadMatrix( (view_matrix * cur_transform->worldMatrix() ).ptr() );
    00173         #endif
    00174       }
    00175       else
    00176       {
    00177         glMatrixMode(GL_MODELVIEW);
    00178         VL_glLoadMatrix( view_matrix.ptr() );
    00179       }
    00180     }
    00181
    00182     wfinfo->mEdgeCallback->setShowCreases(showCreases());
    00183     wfinfo->mEdgeCallback->onActorRenderStarted( actor, frame_clock, camera, wfinfo->mGeometry.get(), NULL, 0 );
    00184     actor->lod(0)->render( actor, NULL, camera, renderTarget()->openglContext() );
    00185   }
    00186 }
    00187 //-----------------------------------------------------------------------------
    00188 void EdgeRenderer::renderLines(Camera* camera)
    00189 {
    00190   // transform
    00191   const Transform* cur_transform = NULL;
    00192   camera->applyViewMatrix();
    00193
    00194   const mat4& view_matrix = camera->viewMatrix();
    00195
    00196   for( std::map< ref<Actor>, ref<WFInfo> >::iterator it = mVisibleActors.begin(); it != mVisibleActors.end(); ++it)
    00197   {
    00198     Actor* actor = it->first.get();
    00199     const WFInfo* wfinfo = it->second.get();
    00200
    00201     // --------------- transform ---------------
    00202
    00203     // delta-setup for modelview matrix for the object
    00204     if ( actor->transform() != cur_transform )
    00205     {
    00206       cur_transform = actor->transform();
    00207
    00208       if ( cur_transform )
    00209       {
    00210         #if 0
    00211           glMatrixMode(GL_MODELVIEW);
    00212           VL_glLoadMatrix( view_matrix.ptr() );
    00213           VL_glMultMatrix( cur_transform->worldMatrix().ptr() );
    00214         #else
    00215           // should guarantee better precision
    00216           glMatrixMode(GL_MODELVIEW);
    00217           VL_glLoadMatrix( (view_matrix * cur_transform->worldMatrix() ).ptr() );
    00218         #endif
    00219       }
    00220       else
    00221       {
    00222         glMatrixMode(GL_MODELVIEW);
    00223         VL_glLoadMatrix( view_matrix.ptr() );
    00224       }
    00225     }
    00226
    00227     // note: no rendering callbacks here
    00228     wfinfo->mGeometry->render( actor, NULL, camera, renderTarget()->openglContext() );
    00229   }
    00230 }
    00231 //-----------------------------------------------------------------------------
    00232 EdgeRenderer::WFInfo* EdgeRenderer::declareActor(Actor* act, const fvec4& color)
    00233 {
    00234   std::map< ref<Actor>, ref<WFInfo> >::iterator it = mActorCache.find( act );
    00235   if (it!=mActorCache.end())
    00236   {
    00237     it->second->mGeometry->setColor(color);
    00238     return it->second.get();
    00239   }
    00240   else
    00241   {
    00242     ref<WFInfo> info = new WFInfo;
    00243     EdgeExtractor ee;
    00244     ee.setCreaseAngle( creaseAngle() );
    00245     if (ee.extractEdges(act))
    00246     {
    00247       info->mGeometry = ee.generateEdgeGeometry();
    00248       info->mEdgeCallback = new EdgeUpdateCallback(ee.edges());
    00249       if (info->mGeometry)
    00250       {
    00251         info->mGeometry->setColor(color);
    00252         mActorCache[act] = info;
    00253         return info.get();
    00254       }
    00255     }
    00256   }
    00257   return NULL;
    00258 }
    00259 //-----------------------------------------------------------------------------
    00260 EdgeRenderer::WFInfo* EdgeRenderer::declareActor(Actor* act)
    00261 {
    00262   std::map< ref<Actor>, ref<WFInfo> >::iterator it = mActorCache.find( act );
    00263   if (it!=mActorCache.end())
    00264     return it->second.get();
    00265   else
    00266   {
    00267     ref<WFInfo> info = new WFInfo;
    00268     EdgeExtractor ee;
    00269     ee.setCreaseAngle( creaseAngle() );
    00270     if (ee.extractEdges(act))
    00271     {
    00272       info->mGeometry = ee.generateEdgeGeometry();
    00273       info->mEdgeCallback = new EdgeUpdateCallback(ee.edges());
    00274       if (info->mGeometry)
    00275       {
    00276         info->mGeometry->setColor(mDefaultLineColor);
    00277         mActorCache[act] = info;
    00278         return info.get();
    00279       }
    00280     }
    00281   }
    00282   return NULL;
    00283 }
    00284 //-----------------------------------------------------------------------------
    00285

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.