1 /*
2  * IndexedTrianglesNode
3  *
4  * Copyright (C) 1999 Stephen F. White
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 <string.h>
24 #include "stdafx.h"
25 
26 #include "NodeIndexedTriangleFanSet.h"
27 #include "Proto.h"
28 #include "Scene.h"
29 #include "FieldValue.h"
30 #include "Node.h"
31 #include "MyMesh.h"
32 #include "FaceData.h"
33 #include "Vec3f.h"
34 #include "NodeColor.h"
35 #include "NodeColorRGBA.h"
36 #include "NodeCoordinate.h"
37 #include "NodeGeoCoordinate.h"
38 #include "NodeNormal.h"
39 #include "NodeTextureCoordinate.h"
40 #include "NodeIndexedLineSet.h"
41 
IndexedTriangleSetProto(Scene * scene,const char * name)42 IndexedTriangleSetProto::IndexedTriangleSetProto(Scene *scene, const char *name)
43   : GeometryProto(scene, name)
44 {
45     color.set(
46           addExposedField(SFNODE, "color", new SFNode(NULL), COLOR_NODE));
47     coord.set(
48           addExposedField(SFNODE, "coord", new SFNode(NULL), COORDINATE_NODE));
49     normal.set(
50           addExposedField(SFNODE, "normal", new SFNode(NULL), VRML_NORMAL));
51     texCoord.set(
52           addExposedField(SFNODE, "texCoord", new SFNode(NULL),
53                           TEXTURE_COORDINATE_NODE));
54     ccw.set(
55           addField(SFBOOL, "ccw", new SFBool(true)));
56     colorPerVertex.set(
57         addField(SFBOOL, "colorPerVertex", new SFBool(true)));
58     normalPerVertex.set(
59           addField(SFBOOL, "normalPerVertex", new SFBool(true)));
60     solid.set(
61           addField(SFBOOL, "solid", new SFBool(true)));
62     index.set(
63           addField(MFINT32, "index", new MFInt32(), new SFInt32(-1)));
64     addEventIn(MFINT32, "set_index", 0, index);
65 }
66 
IndexedTriangleSetNode(Scene * scene,Proto * def)67 IndexedTriangleSetNode::IndexedTriangleSetNode(Scene *scene, Proto *def)
68   : MeshBasedNode(scene, def)
69 {
70     m_coordIndex = NULL;
71 }
72 
~IndexedTriangleSetNode()73 IndexedTriangleSetNode::~IndexedTriangleSetNode()
74 {
75     if (m_coordIndex != NULL)
76         m_coordIndex->unref();
77 }
78 
79 void
draw()80 IndexedTriangleSetNode::draw()
81 {
82     Node *ncoord = coord()->getValue();
83     if (ncoord != NULL) {
84         glPushName(coord_Field());       // field coord
85         glPushName(0);                   // index 0
86         if (ncoord->getType() == VRML_COORDINATE)
87             ((NodeCoordinate *)ncoord)->draw(this);
88         else if (ncoord->getType() == VRML_GEO_COORDINATE) {
89             setDoubleMesh(true);
90             ((NodeGeoCoordinate *)ncoord)->draw(this);
91         }
92         glPopName();
93         glPopName();
94     }
95 }
96 
97 void
setField(int index,FieldValue * value,int cf)98 IndexedTriangleSetNode::setField(int index, FieldValue *value, int cf)
99 {
100     m_meshDirty = true;
101     Node::setField(index, value, cf);
102     update();
103 }
104 
105 void
flip(int index)106 IndexedTriangleSetNode::flip(int index)
107 {
108     NodeCoordinate *ncoord = (NodeCoordinate *)coord()->getValue();
109     if (ncoord)
110         if (ncoord->getType() == VRML_COORDINATE)
111             ncoord->flip(index);
112     NodeNormal *nnormal = (NodeNormal *)normal()->getValue();
113     if (nnormal)
114         if (nnormal->getType() == VRML_NORMAL)
115             nnormal->flip(index);
116     SFBool *bccw = new SFBool(!(ccw()->getValue()));
117     ccw(bccw);
118     m_meshDirty = true;
119 }
120 
121 void
swap(int fromTo)122 IndexedTriangleSetNode::swap(int fromTo)
123 {
124     NodeCoordinate *ncoord = (NodeCoordinate *)coord()->getValue();
125     if (ncoord)
126         if (ncoord->getType() == VRML_COORDINATE)
127             ncoord->swap(fromTo);
128     NodeNormal *nnormal = (NodeNormal *)normal()->getValue();
129     if (nnormal)
130         if (nnormal->getType() == VRML_NORMAL)
131             nnormal->swap(fromTo);
132     SFBool *bccw = new SFBool(!(ccw()->getValue()));
133     ccw(bccw);
134     m_meshDirty = true;
135 }
136 
137