| 1 |
/*************************************************************************** |
| 2 |
* Copyright (c) 2010 Werner Mayer <wmayer[at]users.sourceforge.net> * |
| 3 |
* * |
| 4 |
* This file is part of the FreeCAD CAx development system. * |
| 5 |
* * |
| 6 |
* This library is free software; you can redistribute it and/or * |
| 7 |
* modify it under the terms of the GNU Library General Public * |
| 8 |
* License as published by the Free Software Foundation; either * |
| 9 |
* version 2 of the License, or (at your option) any later version. * |
| 10 |
* * |
| 11 |
* This library is distributed in the hope that it will be useful, * |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 14 |
* GNU Library General Public License for more details. * |
| 15 |
* * |
| 16 |
* You should have received a copy of the GNU Library General Public * |
| 17 |
* License along with this library; see the file COPYING.LIB. If not, * |
| 18 |
* write to the Free Software Foundation, Inc., 59 Temple Place, * |
| 19 |
* Suite 330, Boston, MA 02111-1307, USA * |
| 20 |
* * |
| 21 |
***************************************************************************/ |
| 22 |
|
| 23 |
#include <FCConfig.h> |
| 24 |
|
| 25 |
#include <Inventor/actions/SoGLRenderAction.h> |
| 26 |
#include <Inventor/elements/SoCacheElement.h> |
| 27 |
#include <Inventor/elements/SoLazyElement.h> |
| 28 |
#include <Inventor/elements/SoModelMatrixElement.h> |
| 29 |
#include <Inventor/elements/SoProjectionMatrixElement.h> |
| 30 |
#include <Inventor/elements/SoViewingMatrixElement.h> |
| 31 |
#include <Inventor/elements/SoViewportRegionElement.h> |
| 32 |
|
| 33 |
#if defined(FC_OS_WIN32) |
| 34 |
# include <windows.h> |
| 35 |
#endif |
| 36 |
|
| 37 |
#ifdef FC_OS_MACOSX |
| 38 |
# include <OpenGL/gl.h> |
| 39 |
#else |
| 40 |
# include <GL/gl.h> |
| 41 |
#endif |
| 42 |
|
| 43 |
#include "SoDrawingGrid.h" |
| 44 |
|
| 45 |
|
| 46 |
using namespace Gui::Inventor; |
| 47 |
|
| 48 |
/* |
| 49 |
from pivy import coin |
| 50 |
grid=coin.SoType.fromName("SoDrawingGrid").createInstance() |
| 51 |
Gui.ActiveDocument.ActiveView.getSceneGraph().addChild(grid) |
| 52 |
*/ |
| 53 |
|
| 54 |
SO_NODE_SOURCE(SoDrawingGrid) |
| 55 |
|
| 56 |
void SoDrawingGrid::initClass() |
| 57 |
{ |
| 58 |
SO_NODE_INIT_CLASS(SoDrawingGrid, SoShape, "Shape"); |
| 59 |
} |
| 60 |
|
| 61 |
SoDrawingGrid::SoDrawingGrid() |
| 62 |
{ |
| 63 |
SO_NODE_CONSTRUCTOR(SoDrawingGrid); |
| 64 |
} |
| 65 |
|
| 66 |
void SoDrawingGrid::renderGrid(SoGLRenderAction* action) |
| 67 |
{ |
| 68 |
if (!shouldGLRender(action)) { |
| 69 |
return; |
| 70 |
} |
| 71 |
|
| 72 |
SoState* state = action->getState(); |
| 73 |
state->push(); |
| 74 |
SoLazyElement::setLightModel(state, SoLazyElement::BASE_COLOR); |
| 75 |
|
| 76 |
const SbMatrix& mat = SoModelMatrixElement::get(state); |
| 77 |
// const SbViewVolume & vv = SoViewVolumeElement::get(state); |
| 78 |
// const SbMatrix & projmatrix = (mat * SoViewingMatrixElement::get(state) * |
| 79 |
// SoProjectionMatrixElement::get(state)); |
| 80 |
const SbViewportRegion& vp = SoViewportRegionElement::get(state); |
| 81 |
// SbVec2s vpsize = vp.getViewportSizePixels(); |
| 82 |
float fRatio = vp.getViewportAspectRatio(); |
| 83 |
|
| 84 |
// float width = vv.getWidth(); |
| 85 |
// float height = vv.getHeight(); |
| 86 |
SbVec3f worldcenter(0, 0, 0); |
| 87 |
mat.multVecMatrix(worldcenter, worldcenter); |
| 88 |
|
| 89 |
// float dist = (vv.getProjectionPoint() - worldcenter).length(); |
| 90 |
|
| 91 |
SoModelMatrixElement::set(state, this, SbMatrix::identity()); |
| 92 |
SoViewingMatrixElement::set(state, this, SbMatrix::identity()); |
| 93 |
SoProjectionMatrixElement::set(state, this, SbMatrix::identity()); |
| 94 |
|
| 95 |
glColor3f(1.0f, 0.0f, 0.0f); |
| 96 |
glBegin(GL_LINES); |
| 97 |
float p[3]; |
| 98 |
p[2] = 0.0f; |
| 99 |
|
| 100 |
int numX = 20; |
| 101 |
for (int i = -numX; i < numX; i++) { |
| 102 |
p[0] = (float)i / numX; |
| 103 |
p[1] = -1.0f; |
| 104 |
glVertex3fv(p); |
| 105 |
p[1] = 1.0f; |
| 106 |
glVertex3fv(p); |
| 107 |
} |
| 108 |
int numY = numX / fRatio; |
| 109 |
for (int i = -numY; i < numY; i++) { |
| 110 |
p[0] = -1.0f; |
| 111 |
p[1] = (float)i / numY; |
| 112 |
glVertex3fv(p); |
| 113 |
p[0] = 1.0; |
| 114 |
glVertex3fv(p); |
| 115 |
} |
| 116 |
glEnd(); |
| 117 |
|
| 118 |
state->pop(); |
| 119 |
} |
| 120 |
|
| 121 |
void SoDrawingGrid::GLRender(SoGLRenderAction* action) |
| 122 |
{ |
| 123 |
// renderGrid(action); |
| 124 |
// return; |
| 125 |
switch (action->getCurPathCode()) { |
| 126 |
case SoAction::NO_PATH: |
| 127 |
case SoAction::BELOW_PATH: |
| 128 |
this->GLRenderBelowPath(action); |
| 129 |
break; |
| 130 |
case SoAction::OFF_PATH: |
| 131 |
// do nothing. Separator will reset state. |
| 132 |
break; |
| 133 |
case SoAction::IN_PATH: |
| 134 |
this->GLRenderInPath(action); |
| 135 |
break; |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
void SoDrawingGrid::GLRenderBelowPath(SoGLRenderAction* action) |
| 140 |
{ |
| 141 |
// inherited::GLRenderBelowPath(action); |
| 142 |
// return; |
| 143 |
if (action->isRenderingDelayedPaths()) { |
| 144 |
SbBool zbenabled = glIsEnabled(GL_DEPTH_TEST); |
| 145 |
if (zbenabled) { |
| 146 |
glDisable(GL_DEPTH_TEST); |
| 147 |
} |
| 148 |
renderGrid(action); |
| 149 |
if (zbenabled) { |
| 150 |
glEnable(GL_DEPTH_TEST); |
| 151 |
} |
| 152 |
} |
| 153 |
else { |
| 154 |
SoCacheElement::invalidate(action->getState()); |
| 155 |
action->addDelayedPath(action->getCurPath()->copy()); |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
void SoDrawingGrid::GLRenderInPath(SoGLRenderAction* action) |
| 160 |
{ |
| 161 |
// inherited::GLRenderInPath(action); |
| 162 |
// return; |
| 163 |
if (action->isRenderingDelayedPaths()) { |
| 164 |
SbBool zbenabled = glIsEnabled(GL_DEPTH_TEST); |
| 165 |
if (zbenabled) { |
| 166 |
glDisable(GL_DEPTH_TEST); |
| 167 |
} |
| 168 |
renderGrid(action); |
| 169 |
if (zbenabled) { |
| 170 |
glEnable(GL_DEPTH_TEST); |
| 171 |
} |
| 172 |
} |
| 173 |
else { |
| 174 |
SoCacheElement::invalidate(action->getState()); |
| 175 |
action->addDelayedPath(action->getCurPath()->copy()); |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
void SoDrawingGrid::GLRenderOffPath(SoGLRenderAction*) |
| 180 |
{} |
| 181 |
|
| 182 |
void SoDrawingGrid::generatePrimitives(SoAction* action) |
| 183 |
{ |
| 184 |
(void)action; |
| 185 |
} |
| 186 |
|
| 187 |
void SoDrawingGrid::computeBBox(SoAction* action, SbBox3f& box, SbVec3f& center) |
| 188 |
{ |
| 189 |
(void)action; |
| 190 |
(void)box; |
| 191 |
(void)center; |
| 192 |
// SoState* state = action->getState(); |
| 193 |
} |