# Visualization Library

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

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

- Main Page
- Related Pages
- 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.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 #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->framebuffer()->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 #if defined(VL_OPENGL)
    00126   glLineStipple(1,0xF0F0);
    00127   glEnable(GL_LINE_STIPPLE);
    00128 #endif
    00129   if (showHiddenLines())
    00130     renderLines(camera);
    00131   glDisable(GL_LINE_SMOOTH);
    00132 #if defined(VL_OPENGL)
    00133   glDisable(GL_LINE_STIPPLE);
    00134 #endif
    00135   glDisable(GL_BLEND);
    00136   glLineWidth(1.0f);
    00137
    00138   // was enabled by viewport
    00139   glDisable(GL_SCISSOR_TEST);
    00140
    00141   // disable all vertex arrays
    00142   framebuffer()->openglContext()->bindVAS(NULL, false, true);
    00143
    00144   VL_CHECK( framebuffer()->openglContext()->isCleanState(true) );
    00145
    00146   return render_queue;
    00147 }
    00148 //-----------------------------------------------------------------------------
    00149 void EdgeRenderer::renderSolids(Camera* camera, real frame_clock)
    00150 {
    00151   // transform
    00152   const Transform* cur_transform = NULL;
    00153   camera->applyViewMatrix();
    00154
    00155   const mat4& view_matrix = camera->viewMatrix();
    00156
    00157   for( std::map< ref<Actor>, ref<WFInfo> >::iterator it = mVisibleActors.begin(); it != mVisibleActors.end(); ++it)
    00158   {
    00159     ref<Actor> actor = it->first;
    00160     VL_CHECK(actor);
    00161     VL_CHECK(actor->lod(0));
    00162     WFInfo* wfinfo = it->second.get();
    00163     VL_CHECK(wfinfo);
    00164
    00165     // --------------- transform ---------------
    00166
    00167     // delta-setup for modelview matrix for the object
    00168     if ( actor->transform() != cur_transform )
    00169     {
    00170       cur_transform = actor->transform();
    00171
    00172       if ( cur_transform )
    00173       {
    00174         #if 0
    00175           glMatrixMode(GL_MODELVIEW);
    00176           VL_glLoadMatrix( view_matrix.ptr() );
    00177           VL_glMultMatrix( cur_transform->worldMatrix().ptr() );
    00178         #else
    00179           // should guarantee better precision
    00180           glMatrixMode(GL_MODELVIEW);
    00181           VL_glLoadMatrix( (view_matrix * cur_transform->worldMatrix() ).ptr() );
    00182         #endif
    00183       }
    00184       else
    00185       {
    00186         glMatrixMode(GL_MODELVIEW);
    00187         VL_glLoadMatrix( view_matrix.ptr() );
    00188       }
    00189     }
    00190
    00191     // note: the color is not important here
    00192     wfinfo->mEdgeCallback->setShowCreases(showCreases());
    00193     wfinfo->mEdgeCallback->onActorRenderStarted( actor.get(), frame_clock, camera, wfinfo->mGeometry.get(), NULL, 0 );
    00194     actor->lod(0)->render( actor.get(), NULL, camera, framebuffer()->openglContext() );
    00195   }
    00196 }
    00197 //-----------------------------------------------------------------------------
    00198 void EdgeRenderer::renderLines(Camera* camera)
    00199 {
    00200   // transform
    00201   const Transform* cur_transform = NULL;
    00202   camera->applyViewMatrix();
    00203
    00204   const mat4& view_matrix = camera->viewMatrix();
    00205
    00206   for( std::map< ref<Actor>, ref<WFInfo> >::iterator it = mVisibleActors.begin(); it != mVisibleActors.end(); ++it)
    00207   {
    00208     ref<Actor> actor = it->first;
    00209     WFInfo* wfinfo = it->second.get();
    00210
    00211     // --------------- transform ---------------
    00212
    00213     // delta-setup for modelview matrix for the object
    00214     if ( actor->transform() != cur_transform )
    00215     {
    00216       cur_transform = actor->transform();
    00217
    00218       if ( cur_transform )
    00219       {
    00220         #if 0
    00221           glMatrixMode(GL_MODELVIEW);
    00222           VL_glLoadMatrix( view_matrix.ptr() );
    00223           VL_glMultMatrix( cur_transform->worldMatrix().ptr() );
    00224         #else
    00225           // should guarantee better precision
    00226           glMatrixMode(GL_MODELVIEW);
    00227           VL_glLoadMatrix( (view_matrix * cur_transform->worldMatrix() ).ptr() );
    00228         #endif
    00229       }
    00230       else
    00231       {
    00232         glMatrixMode(GL_MODELVIEW);
    00233         VL_glLoadMatrix( view_matrix.ptr() );
    00234       }
    00235     }
    00236
    00237     // note: no rendering callbacks here
    00238     glColor4fv( wfinfo->mColor.ptr() );
    00239     wfinfo->mGeometry->render( actor.get(), NULL, camera, framebuffer()->openglContext() );
    00240   }
    00241 }
    00242 //-----------------------------------------------------------------------------
    00243 EdgeRenderer::WFInfo* EdgeRenderer::declareActor(Actor* act, const fvec4& color)
    00244 {
    00245   std::map< ref<Actor>, ref<WFInfo> >::iterator it = mActorCache.find( act );
    00246   if (it!=mActorCache.end())
    00247   {
    00248     it->second->mColor = color;
    00249     return it->second.get();
    00250   }
    00251   else
    00252   {
    00253     ref<WFInfo> info = new WFInfo;
    00254     EdgeExtractor ee;
    00255     ee.setCreaseAngle( creaseAngle() );
    00256     if (ee.extractEdges(act))
    00257     {
    00258       info->mGeometry = ee.generateEdgeGeometry();
    00259       info->mEdgeCallback = new EdgeUpdateCallback(ee.edges());
    00260       if (info->mGeometry)
    00261       {
    00262         info->mColor = color;
    00263         mActorCache[act] = info;
    00264         return info.get();
    00265       }
    00266     }
    00267   }
    00268   return NULL;
    00269 }
    00270 //-----------------------------------------------------------------------------
    00271 EdgeRenderer::WFInfo* EdgeRenderer::declareActor(Actor* act)
    00272 {
    00273   std::map< ref<Actor>, ref<WFInfo> >::iterator it = mActorCache.find( act );
    00274   if (it!=mActorCache.end())
    00275     return it->second.get();
    00276   else
    00277   {
    00278     ref<WFInfo> info = new WFInfo;
    00279     EdgeExtractor ee;
    00280     ee.setCreaseAngle( creaseAngle() );
    00281     if (ee.extractEdges(act))
    00282     {
    00283       info->mGeometry = ee.generateEdgeGeometry();
    00284       info->mEdgeCallback = new EdgeUpdateCallback(ee.edges());
    00285       if (info->mGeometry)
    00286       {
    00287         info->mColor = mDefaultLineColor;
    00288         mActorCache[act] = info;
    00289         return info.get();
    00290       }
    00291     }
    00292   }
    00293   return NULL;
    00294 }
    00295 //-----------------------------------------------------------------------------
    00296

Visualization Library v2011.09.1162 Reference Documentation
Copyright 2005-2011 Michele Bosi. All rights reserved.
Updated on Thu Jan 26 2012 00:43:37.
Permission is granted to use this page to write and publish articles regarding Visualization Library.