CMS 3D CMS Logo

IgSoGL2PSAction.cc

Go to the documentation of this file.
00001 //<<<<<< INCLUDES                                                       >>>>>>
00002 
00003 #include "Iguana/Inventor/interface/IgSoGL2PSAction.h"
00004 #include <Inventor/nodes/SoVertexProperty.h>
00005 #include <Inventor/elements/SoCoordinateElement.h>
00006 #include <Inventor/elements/SoFontNameElement.h>
00007 #include <Inventor/elements/SoFontSizeElement.h>
00008 #include <Inventor/elements/SoViewportRegionElement.h>
00009 #include <Inventor/elements/SoModelMatrixElement.h>
00010 #include <Inventor/elements/SoViewingMatrixElement.h>
00011 #include <Inventor/elements/SoProjectionMatrixElement.h>
00012 #include <Inventor/elements/SoViewVolumeElement.h>
00013 #include <Inventor/elements/SoLazyElement.h>
00014 #include <Inventor/nodes/SoIndexedLineSet.h>
00015 #include <Inventor/nodes/SoLineSet.h>
00016 #include <Inventor/nodes/SoPointSet.h>
00017 #include <Inventor/nodes/SoSeparator.h>
00018 #include <Inventor/nodes/SoText2.h>
00019 #include <Inventor/nodes/SoMarkerSet.h>
00020 #include "Iguana/GL2PS/interface/gl2ps.h"
00021 
00022 //<<<<<< PRIVATE DEFINES                                                >>>>>>
00023 //<<<<<< PRIVATE CONSTANTS                                              >>>>>>
00024 //<<<<<< PRIVATE TYPES                                                  >>>>>>
00025 //<<<<<< PRIVATE VARIABLE DEFINITIONS                                   >>>>>>
00026 //<<<<<< PUBLIC VARIABLE DEFINITIONS                                    >>>>>>
00027 //<<<<<< CLASS STRUCTURE INITIALIZATION                                 >>>>>>
00028 
00029 SO_ACTION_SOURCE (IgSoGL2PSAction)
00030 
00031 //<<<<<< PRIVATE FUNCTION DEFINITIONS                                   >>>>>>
00032 //<<<<<< PUBLIC FUNCTION DEFINITIONS                                    >>>>>>
00033 //<<<<<< MEMBER FUNCTION DEFINITIONS                                    >>>>>>
00034 
00035 void IgSoGL2PSAction::initClass (void)
00036 {
00037     SO_ACTION_INIT_CLASS (IgSoGL2PSAction,  SoGLRenderAction);
00038     SO_ACTION_ADD_METHOD (SoIndexedLineSet, indexedLineSet);
00039     SO_ACTION_ADD_METHOD (SoLineSet,        lineSet);
00040     SO_ACTION_ADD_METHOD (SoPointSet,       pointSet);
00041     SO_ACTION_ADD_METHOD (SoText2,          text2);
00042     SO_ACTION_ADD_METHOD (SoMarkerSet,      markerSet);
00043     SO_ACTION_ADD_METHOD (SoSeparator,      separatorSet);
00044 }
00045 
00046 IgSoGL2PSAction::IgSoGL2PSAction (const SbViewportRegion& region)
00047     :SoGLRenderAction (region)
00048 {
00049     SO_ACTION_CONSTRUCTOR (IgSoGL2PSAction);
00050 }
00051 
00052 void
00053 IgSoGL2PSAction::indexedLineSet (SoAction* self, SoNode* node)
00054 {
00055     GLfloat value;
00056     glGetFloatv (GL_LINE_WIDTH, &value);
00057     gl2psLineWidth (value);
00058     GLint pat;
00059     glGetIntegerv (GL_LINE_STIPPLE_PATTERN, &pat);
00060     if (pat == 0xffff)
00061         gl2psDisable (GL2PS_LINE_STIPPLE);
00062     else
00063         gl2psEnable (GL2PS_LINE_STIPPLE);
00064     node->GLRender ((IgSoGL2PSAction *)self);
00065 }
00066 
00067 void 
00068 IgSoGL2PSAction::lineSet (SoAction* self, SoNode* node)
00069 {
00070     GLfloat value;
00071     glGetFloatv (GL_LINE_WIDTH, &value);
00072     gl2psLineWidth (value);
00073     node->GLRender ((IgSoGL2PSAction *)self);
00074 }
00075 
00076 void
00077 IgSoGL2PSAction::pointSet (SoAction* self, SoNode* node)
00078 {
00079     GLfloat value;
00080     glGetFloatv (GL_POINT_SIZE, &value);
00081     gl2psPointSize (value);
00082     node->GLRender ((IgSoGL2PSAction *)self);
00083 }
00084 
00085 void
00086 IgSoGL2PSAction::text2 (SoAction* self, SoNode* node)
00087 {
00088     SoText2 *text = (SoText2 *) node;
00089     SoState * state = self->getState ();
00090     state->push ();
00091       
00092     SbName fontName = SoFontNameElement::get(state);
00093     if (fontName == SoFontNameElement::getDefault ())
00094         fontName = "Courier";
00095 
00096     int fontSize = static_cast<short>(SoFontSizeElement::get (state));
00097     int alignment = GL2PS_TEXT_C;
00098     switch (text->justification.getValue ())
00099     {
00100     case 1:
00101         alignment = GL2PS_TEXT_CL;
00102         break;
00103     case 2:
00104         alignment = GL2PS_TEXT_CR;
00105         break;
00106     case 3:
00107         alignment = GL2PS_TEXT_C;
00108     default:
00109         break;
00110     }
00111  
00112     SbVec3f nilpoint (0.0f, 0.0f, 0.0f);
00113     SbColor color = SoLazyElement::getDiffuse (state, 0);
00114         
00115     const SbMatrix & mat = SoModelMatrixElement::get (state);
00116     const SbMatrix & projmatrix = (mat * SoViewingMatrixElement::get (state) *
00117                                    SoProjectionMatrixElement::get (state));
00118     const SbViewportRegion & vp = SoViewportRegionElement::get (state);
00119     SbVec2s vpsize = vp.getViewportSizePixels ();
00120 
00121     projmatrix.multVecMatrix (nilpoint, nilpoint);
00122     nilpoint [0] = (nilpoint [0] + 1.0f) * 0.5f * vpsize [0];
00123     nilpoint [1] = (nilpoint [1] + 1.0f) * 0.5f * vpsize [1];      
00124  
00125     // Set new state.
00126     glPushAttrib ((GLbitfield)(GL_CURRENT_BIT | GL_ENABLE_BIT));
00127     glDisable (GL_LIGHTING);
00128     glColor3fv (color.getValue ());
00129     glMatrixMode (GL_MODELVIEW);
00130     glPushMatrix ();
00131     glLoadIdentity ();
00132     glMatrixMode (GL_PROJECTION);
00133     glPushMatrix ();
00134     glLoadIdentity ();
00135     glOrtho (0, vpsize [0], 0, vpsize [1], -1.0f, 1.0f);
00136     glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
00137 
00138     const int nrlines = text->string.getNum ();
00139     for (int i = 0; i < nrlines; ++i)
00140     {
00141         SbString str = text->string [i];
00142         glRasterPos3f (nilpoint [0], nilpoint [1], -nilpoint [2]);
00143         
00144         gl2psTextOpt (str.getString (),
00145                      fontName.getString (), fontSize,
00146                      alignment, 0.F);
00147         nilpoint [1] -= (fontSize * text->spacing.getValue ());
00148     }
00149     glPixelStorei (GL_UNPACK_ALIGNMENT, 4);
00150     glMatrixMode (GL_PROJECTION);
00151     glPopMatrix ();
00152     glMatrixMode (GL_MODELVIEW);
00153     glPopMatrix ();
00154     glPopAttrib ();
00155     state->pop ();
00156 }
00157 
00158 void
00159 IgSoGL2PSAction::markerSet (SoAction* self, SoNode* node)
00160 {
00161     SoMarkerSet *markerSet = (SoMarkerSet*) node;
00162     SoState *state = self->getState ();
00163     state->push ();
00164     
00165     SoVertexProperty *vernode = static_cast<SoVertexProperty *>(
00166         markerSet->vertexProperty.getValue ());
00167     int starti = 0;
00168     int pointn = vernode->vertex.getNum ();
00169       
00170     const SbVec3f *position = vernode->vertex.getValues (starti);
00171 
00172     SbColor color;
00173     int totalMarkers = markerSet->markerIndex.getNum ();
00174     int totalColors  = vernode->orderedRGBA.getNum ();
00175       
00176     if (totalColors > 0)
00177     {
00178         float transparency;
00179         color.setPackedValue (*(vernode->orderedRGBA.getValues (starti)),
00180                               transparency);
00181     }
00182     else
00183     {
00184         const SoCoordinateElement *coordinateElement = 
00185             SoCoordinateElement::getInstance (state);
00186         if (coordinateElement == NULL) return;
00187 
00188         color = SoLazyElement::getDiffuse (state, 0);        
00189     }
00190     glPushAttrib ((GLbitfield)(GL_CURRENT_BIT | GL_ENABLE_BIT));
00191     glDisable (GL_LIGHTING);
00192     glColor3fv (color.getValue ());
00193     glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
00194       
00195     while (starti < pointn)
00196     {
00197         SbBool isLSBFirst;
00198         SbVec2s size;
00199         const unsigned char *image;
00200 
00201         if ((totalColors > 0) && (starti < totalColors))
00202         {
00203             SbColor color1;
00204             float transparency;
00205             color1.setPackedValue (*(vernode->orderedRGBA.getValues (starti)),
00206                                    transparency);
00207             if (color1 != color)
00208             {
00209                 color = color1;
00210                 glPixelStorei (GL_UNPACK_ALIGNMENT, 4);
00211                 glColor3fv (color.getValue ());
00212                 glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
00213             }
00214         }
00215         
00216         if (starti < totalMarkers)
00217             markerSet->getMarker (markerSet->markerIndex [starti], size, image, isLSBFirst);
00218         else
00219             markerSet->getMarker (markerSet->markerIndex [totalMarkers - 1], size, image, isLSBFirst);
00220 
00221         gl2psDrawImageMap (size [0], size [1],
00222                            position->getValue (), image);
00223         ++position; ++starti;
00224     }
00225 
00226     glPixelStorei (GL_UNPACK_ALIGNMENT, 4);
00227     glPopAttrib ();
00228     state->pop ();
00229 }
00230 
00231 void
00232 IgSoGL2PSAction::separatorSet (SoAction* self, SoNode* node)
00233 { node->doAction (self); }

Generated on Tue Jun 9 17:38:45 2009 for CMSSW by  doxygen 1.5.4