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"
30
# include <OpenGL/gl.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/errors/SoReadError.h>
42
# include <Inventor/misc/SoState.h>
45
#include "SoFCShapeObject.h"
48
using namespace PartGui;
50
SO_NODE_SOURCE(SoFCControlPoints)
52
void SoFCControlPoints::initClass()
54
SO_NODE_INIT_CLASS(SoFCControlPoints, SoShape, "Shape");
57
SoFCControlPoints::SoFCControlPoints()
59
SO_NODE_CONSTRUCTOR(SoFCControlPoints);
61
SbVec3f c(1.0f, 0.447059f, 0.337255f);
62
SO_NODE_ADD_FIELD(numPolesU, (0));
63
SO_NODE_ADD_FIELD(numPolesV, (0));
64
SO_NODE_ADD_FIELD(numKnotsU, (0));
65
SO_NODE_ADD_FIELD(numKnotsV, (0));
66
SO_NODE_ADD_FIELD(lineColor, (c));
70
* Renders the control points.
72
void SoFCControlPoints::GLRender(SoGLRenderAction *action)
74
if (shouldGLRender(action))
76
SoState* state = action->getState();
77
const SoCoordinateElement * coords = SoCoordinateElement::getInstance(state);
80
const SbVec3f * points = coords->getArrayPtr3();
84
SoMaterialBundle mb(action);
85
SoTextureCoordinateBundle tb(action, true, false);
86
SoLazyElement::setLightModel(state, SoLazyElement::BASE_COLOR);
87
mb.sendFirst(); // make sure we have the correct material
89
int32_t len = coords->getNum();
90
drawControlPoints(points, len);
95
* Renders the control points and knots.
97
void SoFCControlPoints::drawControlPoints(const SbVec3f * points,int32_t len) const
100
glColor3fv(lineColor.getValue().getValue());
102
uint32_t nCtU=numPolesU.getValue();
103
uint32_t nCtV=numPolesV.getValue();
104
uint32_t poles = nCtU * nCtV;
105
if (poles > (uint32_t)len)
106
return; // wrong setup, too few points
109
for (uint32_t u = 0; u < nCtU-1; ++u) {
110
for (uint32_t v = 0; v < nCtV -1; ++v) {
111
glVertex3fv(points[u*nCtV+v].getValue());
112
glVertex3fv(points[u*nCtV+v+1].getValue());
113
glVertex3fv(points[u*nCtV+v].getValue());
114
glVertex3fv(points[(u+1)*nCtV+v].getValue());
116
glVertex3fv(points[(u+1)*nCtV-1].getValue());
117
glVertex3fv(points[(u+2)*nCtV-1].getValue());
119
for (uint32_t v = 0; v < nCtV -1; ++v) {
120
glVertex3fv(points[(nCtU-1)*nCtV+v].getValue());
121
glVertex3fv(points[(nCtU-1)*nCtV+v+1].getValue());
128
for (uint32_t p=0; p<poles; p++) {
129
glVertex3fv(points[p].getValue());
133
// draw knots if available
134
uint32_t knots=numKnotsU.getValue() * numKnotsV.getValue();
135
uint32_t index = poles + knots;
136
if (index > (uint32_t)len)
137
return; // wrong setup, too few points
138
glColor3f(1.0f, 1.0f, 0.0f);
141
for (uint32_t p=poles; p<index; p++) {
142
glVertex3fv(points[p].getValue());
147
void SoFCControlPoints::generatePrimitives(SoAction* /*action*/)
152
* Sets the bounding box of the mesh to \a box and its center to \a center.
154
void SoFCControlPoints::computeBBox(SoAction *action, SbBox3f &box, SbVec3f ¢er)
156
SoState* state = action->getState();
157
const SoCoordinateElement * coords = SoCoordinateElement::getInstance(state);
160
const SbVec3f * points = coords->getArrayPtr3();
163
float maxX=-FLT_MAX, minX=FLT_MAX,
164
maxY=-FLT_MAX, minY=FLT_MAX,
165
maxZ=-FLT_MAX, minZ=FLT_MAX;
166
int32_t len = coords->getNum();
168
for (int32_t i=0; i<len; i++) {
169
maxX = std::max<float>(maxX,points[i][0]);
170
minX = std::min<float>(minX,points[i][0]);
171
maxY = std::max<float>(maxY,points[i][1]);
172
minY = std::min<float>(minY,points[i][1]);
173
maxZ = std::max<float>(maxZ,points[i][2]);
174
minZ = std::min<float>(minZ,points[i][2]);
177
box.setBounds(minX,minY,minZ,maxX,maxY,maxZ);
178
center.setValue(0.5f*(minX+maxX),0.5f*(minY+maxY),0.5f*(minZ+maxZ));
181
box.setBounds(SbVec3f(0,0,0), SbVec3f(0,0,0));
182
center.setValue(0.0f,0.0f,0.0f);