SoDrawingGrid.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (c) 2010 Werner Mayer <wmayer[at]users.sourceforge.net>     *
00003  *                                                                         *
00004  *   This file is part of the FreeCAD CAx development system.              *
00005  *                                                                         *
00006  *   This library is free software; you can redistribute it and/or         *
00007  *   modify it under the terms of the GNU Library General Public           *
00008  *   License as published by the Free Software Foundation; either          *
00009  *   version 2 of the License, or (at your option) any later version.      *
00010  *                                                                         *
00011  *   This library  is distributed in the hope that it will be useful,      *
00012  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00013  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00014  *   GNU Library General Public License for more details.                  *
00015  *                                                                         *
00016  *   You should have received a copy of the GNU Library General Public     *
00017  *   License along with this library; see the file COPYING.LIB. If not,    *
00018  *   write to the Free Software Foundation, Inc., 59 Temple Place,         *
00019  *   Suite 330, Boston, MA  02111-1307, USA                                *
00020  *                                                                         *
00021  ***************************************************************************/
00022 
00023 #include "PreCompiled.h"
00024 #ifdef FC_OS_MACOSX
00025 #include <OpenGL/gl.h>
00026 #else
00027 #include <GL/gl.h>
00028 #endif
00029 #include <Inventor/actions/SoGLRenderAction.h>
00030 #include <Inventor/elements/SoCacheElement.h>
00031 #include <Inventor/elements/SoLazyElement.h>
00032 #include <Inventor/elements/SoModelMatrixElement.h>
00033 #include <Inventor/elements/SoProjectionMatrixElement.h>
00034 #include <Inventor/elements/SoViewingMatrixElement.h>
00035 #include <Inventor/elements/SoViewVolumeElement.h>
00036 #include <Inventor/elements/SoViewportRegionElement.h>
00037 
00038 #include "SoDrawingGrid.h"
00039 
00040 using namespace Gui::Inventor;
00041 
00042 /*
00043 from pivy import coin
00044 grid=coin.SoType.fromName("SoDrawingGrid").createInstance()
00045 Gui.ActiveDocument.ActiveView.getSceneGraph().addChild(grid)
00046 */
00047 
00048 SO_NODE_SOURCE(SoDrawingGrid);
00049 
00050 void
00051 SoDrawingGrid::initClass()
00052 {
00053     SO_NODE_INIT_CLASS(SoDrawingGrid, SoShape, "Shape");
00054 }
00055 
00056 SoDrawingGrid::SoDrawingGrid()
00057 {
00058     SO_NODE_CONSTRUCTOR(SoDrawingGrid);
00059 }
00060 
00061 void
00062 SoDrawingGrid::renderGrid(SoGLRenderAction *action)
00063 {
00064     if (!shouldGLRender(action))
00065         return;
00066 
00067     SoState*  state = action->getState();
00068     state->push();
00069     SoLazyElement::setLightModel(state, SoLazyElement::BASE_COLOR);
00070 
00071     const SbMatrix & mat = SoModelMatrixElement::get(state);
00072     //const SbViewVolume & vv = SoViewVolumeElement::get(state);
00073     //const SbMatrix & projmatrix = (mat * SoViewingMatrixElement::get(state) *
00074     //                               SoProjectionMatrixElement::get(state));
00075     const SbViewportRegion & vp = SoViewportRegionElement::get(state);
00076     //SbVec2s vpsize = vp.getViewportSizePixels();
00077     float fRatio = vp.getViewportAspectRatio();
00078 
00079     //float width = vv.getWidth();
00080     //float height = vv.getHeight();
00081     SbVec3f worldcenter(0,0,0);
00082     mat.multVecMatrix(worldcenter, worldcenter);
00083 
00084     //float dist = (vv.getProjectionPoint() - worldcenter).length();
00085 
00086     SoModelMatrixElement::set(state,this,SbMatrix::identity());
00087     SoViewingMatrixElement::set(state,this,SbMatrix::identity());
00088     SoProjectionMatrixElement::set(state,this,SbMatrix::identity());
00089 
00090     glColor3f(1.0f,0.0f,0.0f);
00091     glBegin(GL_LINES);
00092     float p[3];
00093     p[2] = 0.0f;
00094 
00095     int numX = 20;
00096     for (int i=-numX; i<numX; i++) {
00097         p[0] = (float)i/numX;
00098         p[1] = -1.0f;
00099         glVertex3fv(p);
00100         p[1] = 1.0f;
00101         glVertex3fv(p);
00102     }
00103     int numY = numX / fRatio;
00104     for (int i=-numY; i<numY; i++) {
00105         p[0] = -1.0f;
00106         p[1] = (float)i/numY;
00107         glVertex3fv(p);
00108         p[0] = 1.0;
00109         glVertex3fv(p);
00110     }
00111     glEnd();
00112 
00113     state->pop();
00114 }
00115 
00116 void
00117 SoDrawingGrid::GLRender(SoGLRenderAction *action)
00118 {
00119     //renderGrid(action);
00120     //return;
00121     switch (action->getCurPathCode()) {
00122     case SoAction::NO_PATH:
00123     case SoAction::BELOW_PATH:
00124         this->GLRenderBelowPath(action);
00125         break;
00126     case SoAction::OFF_PATH:
00127         // do nothing. Separator will reset state.
00128         break;
00129     case SoAction::IN_PATH:
00130         this->GLRenderInPath(action);
00131         break;
00132     }
00133 }
00134 
00135 void
00136 SoDrawingGrid::GLRenderBelowPath(SoGLRenderAction * action)
00137 {
00138     //inherited::GLRenderBelowPath(action);
00139     //return;
00140     if (action->isRenderingDelayedPaths()) {
00141         SbBool zbenabled = glIsEnabled(GL_DEPTH_TEST);
00142         if (zbenabled) glDisable(GL_DEPTH_TEST);
00143         renderGrid(action);
00144         if (zbenabled) glEnable(GL_DEPTH_TEST);
00145     }
00146     else {
00147         SoCacheElement::invalidate(action->getState());
00148         action->addDelayedPath(action->getCurPath()->copy());
00149     }
00150 }
00151 
00152 void
00153 SoDrawingGrid::GLRenderInPath(SoGLRenderAction * action)
00154 {
00155     //inherited::GLRenderInPath(action);
00156     //return;
00157     if (action->isRenderingDelayedPaths()) {
00158         SbBool zbenabled = glIsEnabled(GL_DEPTH_TEST);
00159         if (zbenabled) glDisable(GL_DEPTH_TEST);
00160         renderGrid(action);
00161         if (zbenabled) glEnable(GL_DEPTH_TEST);
00162     }
00163     else {
00164         SoCacheElement::invalidate(action->getState());
00165         action->addDelayedPath(action->getCurPath()->copy());
00166     }
00167 }
00168 
00169 void
00170 SoDrawingGrid::GLRenderOffPath(SoGLRenderAction *)
00171 {
00172 }
00173 
00174 void
00175 SoDrawingGrid::generatePrimitives(SoAction* action)
00176 {
00177 }
00178 
00179 void
00180 SoDrawingGrid::computeBBox(SoAction *action, SbBox3f &box, SbVec3f &center)
00181 {
00182     //SoState*  state = action->getState();
00183 }

Generated on Wed Nov 23 19:00:39 2011 for FreeCAD by  doxygen 1.6.1