1 /*
2 * NodeDisk2D.cpp
3 *
4 * Copyright (C) 1999 Stephen F. White, 2007 J. "MUFTI" Scheurich
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program 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 General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program (see the file "COPYING" for details); if
18 * not, write to the Free Software Foundation, Inc., 675 Mass Ave,
19 * Cambridge, MA 02139, USA.
20 */
21
22 #include <stdio.h>
23 #include "stdafx.h"
24
25 #include "NodeDisk2D.h"
26 #include "MyMesh.h"
27 #include "Proto.h"
28 #include "Scene.h"
29 #include "SFBool.h"
30 #include "SFFloat.h"
31 #include "RenderState.h"
32 #include "Util.h"
33 #include "stdafx.h"
34
ProtoDisk2D(Scene * scene)35 ProtoDisk2D::ProtoDisk2D(Scene *scene)
36 : Proto(scene, "Disk2D")
37 {
38 innerRadius.set(
39 addField(SFFLOAT, "innerRadius", new SFFloat(0.0F),
40 new SFFloat(0.0f)));
41
42 outerRadius.set(
43 addField(SFFLOAT, "outerRadius", new SFFloat(1.0F),
44 new SFFloat(0.0f)));
45
46 solid.set(
47 addField(SFBOOL, "solid", new SFBool(false)));
48
49 addURLs(URL_X3D);
50
51 x3domGeometryCommonFields()
52
53 ccw.set(
54 addExposedField(SFBOOL, "ccw", new SFBool(true)));
55 setFieldFlags(ccw, FF_X3DOM_ONLY);
56 }
57
58 Node *
create(Scene * scene)59 ProtoDisk2D::create(Scene *scene)
60 {
61 return new NodeDisk2D(scene, this);
62 }
63
NodeDisk2D(Scene * scene,Proto * def)64 NodeDisk2D::NodeDisk2D(Scene *scene, Proto *def)
65 : MeshBasedNode(scene, def)
66 {
67 }
68
69 void
createMesh(bool cleanDoubleVertices,bool triangulate)70 NodeDisk2D::createMesh(bool cleanDoubleVertices, bool triangulate)
71 {
72 float finnerRadius = innerRadius()->getValue();
73 float fouterRadius = outerRadius()->getValue();
74
75 float *fpoint = NULL;
76 int numPoints = 0;
77 MyArray<int> icoordIndex;
78
79 int tess = TheApp->getTessellation() + 1;
80
81 if (finnerRadius == 0) {
82 numPoints = tess * 3;
83 fpoint = new float[numPoints];
84
85 float fstartAngle = 0;
86 float fendAngle = 2 * M_PI;
87 float incAngle = (fendAngle - fstartAngle) / (tess - 1);
88
89 for (int i = 0; i < tess; i++) {
90 float angle = i * incAngle;
91 fpoint[i * 3 + 0] = fouterRadius * cos(angle);
92 fpoint[i * 3 + 1] = fouterRadius * sin(angle);
93 fpoint[i * 3 + 2] = 0;
94 }
95
96 for (int i = 0; i < tess; i++)
97 icoordIndex.append(i);
98 icoordIndex.append(0);
99 icoordIndex.append(-1);
100 } else {
101 numPoints = tess * 3 * 2;
102 fpoint = new float[numPoints];
103
104 float fstartAngle = 0;
105 float fendAngle = 2 * M_PI;
106 float incAngle = (fendAngle - fstartAngle) / (tess - 1);
107
108 for (int i = 0; i < tess; i++) {
109 float angle = i * incAngle;
110 fpoint[i * 3 * 2 + 0] = fouterRadius * cos(angle);
111 fpoint[i * 3 * 2 + 1] = fouterRadius * sin(angle);
112 fpoint[i * 3 * 2 + 2] = 0;
113 fpoint[i * 3 * 2 + 3 + 0] = finnerRadius * cos(angle);
114 fpoint[i * 3 * 2 + 3 + 1] = finnerRadius * sin(angle);
115 fpoint[i * 3 * 2 + 3 + 2] = 0;
116 }
117
118 for (int i = 0; i < (tess - 1); i++) {
119 icoordIndex.append(2 * i + 0);
120 icoordIndex.append(2 * i + 1);
121 icoordIndex.append(2 * i + 3);
122 icoordIndex.append(2 * i + 2);
123 icoordIndex.append(-1);
124 }
125 }
126
127 MFVec3f* mfpoint = new MFVec3f(fpoint, numPoints);
128 MFVec3f *coords = (MFVec3f *)mfpoint->copy();
129
130 MFInt32 *mfcoordIndex = new MFInt32((int *)icoordIndex.getData(),
131 icoordIndex.size());
132 MFInt32 *coordIndex = (MFInt32 *)mfcoordIndex->copy();
133
134 int meshFlags = MESH_NORMAL_PER_VERTEX;
135 if (solid()->getValue())
136 meshFlags |= MESH_SOLID;
137 meshFlags |= MESH_CCW;
138
139 float transparency = 0;
140 if (hasParent())
141 transparency = getParent()->getTransparency();
142
143 MyArray<MFVec2f *>texCoords;
144 m_mesh = new MyMesh(this,
145 coords, coordIndex, NULL, NULL, NULL, NULL, texCoords,
146 NULL, 0, meshFlags, transparency);
147 }
148
149
150