1
/***************************************************************************
2
* Copyright (c) 2008 Werner Mayer <wmayer[at]users.sourceforge.net> *
4
* This file is part of the FreeCAD CAx development system. *
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. *
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. *
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 *
21
***************************************************************************/
23
#include "PreCompiled.h"
36
#include <Inventor/actions/SoGLRenderAction.h>
37
#include <Inventor/bundles/SoMaterialBundle.h>
38
#include <Inventor/bundles/SoTextureCoordinateBundle.h>
39
#include <Inventor/elements/SoCoordinateElement.h>
40
#include <Inventor/elements/SoLazyElement.h>
41
#include <Inventor/misc/SoState.h>
47
using namespace MeshGui;
49
SO_NODE_SOURCE(SoPolygon)
51
void SoPolygon::initClass()
53
SO_NODE_INIT_CLASS(SoPolygon, SoShape, "Shape");
58
SO_NODE_CONSTRUCTOR(SoPolygon);
60
SO_NODE_ADD_FIELD(startIndex, (0));
61
SO_NODE_ADD_FIELD(numVertices, (0));
62
SO_NODE_ADD_FIELD(highlight, (false));
63
SO_NODE_ADD_FIELD(render, (true));
67
* Renders the polygon.
69
void SoPolygon::GLRender(SoGLRenderAction* action)
71
if (shouldGLRender(action) && render.getValue()) {
72
SoState* state = action->getState();
73
const SoCoordinateElement* coords = SoCoordinateElement::getInstance(state);
77
const SbVec3f* points = coords->getArrayPtr3();
82
SoMaterialBundle mb(action);
83
SoTextureCoordinateBundle tb(action, true, false);
84
SoLazyElement::setLightModel(state, SoLazyElement::BASE_COLOR);
85
mb.sendFirst(); // make sure we have the correct material
87
int32_t len = coords->getNum();
88
drawPolygon(points, len);
93
* Renders the polygon.
95
void SoPolygon::drawPolygon(const SbVec3f* points, int32_t len) const
98
int32_t beg = startIndex.getValue();
99
int32_t cnt = numVertices.getValue();
100
int32_t end = beg + cnt;
102
return; // wrong setup, too few points
106
for (int32_t i = beg; i < end; ++i) {
107
int32_t j = (i - beg + 1) % cnt + beg;
108
glVertex3fv(points[i].getValue());
109
glVertex3fv(points[j].getValue());
115
* Calculates picked point based on primitives generated by subclasses.
117
void SoPolygon::rayPick(SoRayPickAction* action)
119
// if (this->shouldRayPick(action)) {
120
// this->computeObjectSpaceRay(action);
122
// const SoBoundingBoxCache* bboxcache = getBoundingBoxCache();
123
// if (!bboxcache || !bboxcache->isValid(action->getState()) ||
124
// SoFCMeshObjectShape_ray_intersect(action, bboxcache->getProjectedBox())) {
125
// this->generatePrimitives(action);
128
inherited::rayPick(action);
131
void SoPolygon::generatePrimitives(SoAction* /*action*/)
135
* Sets the bounding box of the mesh to \a box and its center to \a center.
137
void SoPolygon::computeBBox(SoAction* action, SbBox3f& box, SbVec3f& center)
139
SoState* state = action->getState();
140
const SoCoordinateElement* coords = SoCoordinateElement::getInstance(state);
144
const SbVec3f* points = coords->getArrayPtr3();
148
float maxX = -FLT_MAX, minX = FLT_MAX, maxY = -FLT_MAX, minY = FLT_MAX, maxZ = -FLT_MAX,
150
int32_t len = coords->getNum();
151
int32_t beg = startIndex.getValue();
152
int32_t cnt = numVertices.getValue();
153
int32_t end = beg + cnt;
155
for (int32_t i = beg; i < end; i++) {
156
maxX = std::max<float>(maxX, points[i][0]);
157
minX = std::min<float>(minX, points[i][0]);
158
maxY = std::max<float>(maxY, points[i][1]);
159
minY = std::min<float>(minY, points[i][1]);
160
maxZ = std::max<float>(maxZ, points[i][2]);
161
minZ = std::min<float>(minZ, points[i][2]);
164
box.setBounds(minX, minY, minZ, maxX, maxY, maxZ);
165
center.setValue(0.5F * (minX + maxX), 0.5F * (minY + maxY), 0.5F * (minZ + maxZ));
168
box.setBounds(SbVec3f(0, 0, 0), SbVec3f(0, 0, 0));
169
center.setValue(0.0F, 0.0F, 0.0F);