1 /*
2  * NodeTextureCoordinate.cpp
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 "stdafx.h"
24 
25 #include "NodeTextureCoordinate.h"
26 #include "Proto.h"
27 #include "FieldValue.h"
28 #include "Scene.h"
29 #include "NodeIndexedFaceSet.h"
30 #include "MFVec3f.h"
31 #include "MFVec2f.h"
32 #include "MFInt32.h"
33 #include "MyMesh.h"
34 
ProtoTextureCoordinate(Scene * scene)35 ProtoTextureCoordinate::ProtoTextureCoordinate(Scene *scene)
36   : WonderlandExportProto(scene, "TextureCoordinate")
37 {
38     point.set(addExposedField(MFVEC2F, "point", new MFVec2f()));
39 }
40 
41 Node *
create(Scene * scene)42 ProtoTextureCoordinate::create(Scene *scene)
43 {
44     return new NodeTextureCoordinate(scene, this);
45 }
46 
NodeTextureCoordinate(Scene * scene,Proto * def)47 NodeTextureCoordinate::NodeTextureCoordinate(Scene *scene, Proto *def)
48   : Node(scene, def)
49 {
50 }
51 
52 void
setField(int index,FieldValue * value,int cf)53 NodeTextureCoordinate::setField(int index, FieldValue *value, int cf)
54 {
55     Node::setField(index, value, cf);
56     if (hasParent())
57         getParents().update();
58 }
59 
60 void
setTextureCoordinateFromIndexedFaceSet()61 NodeTextureCoordinate::setTextureCoordinateFromIndexedFaceSet()
62 {
63     Node* parent = getParent();
64     if (parent && parent->getType() == VRML_INDEXED_FACE_SET) {
65         NodeIndexedFaceSet *faceSet = (NodeIndexedFaceSet*) parent;
66         MFVec3f *coords = faceSet->getCoordinates();
67         MFInt32 *coordIndex = faceSet->getCoordIndex();
68         MFInt32 *texCoordIndex = faceSet->getTexCoordIndex();
69         if (texCoordIndex->getSize() != coordIndex->getSize()) {
70             texCoordIndex = coordIndex;
71         }
72     MFVec2f *tc = generateTextureCoordinates(coords, texCoordIndex, 0.0f);
73     if (tc != NULL)
74         setField(point_Field(), tc);
75     }
76 }
77 
78 void
update()79 NodeTextureCoordinate::update()
80 {
81     if (hasParent()) {
82         for (int i = 0; i < getNumParents(); i++) {
83             Node *parent = getParent(i);
84             if (parent->getType() == X3D_MULTI_TEXTURE_COORDINATE) {
85                 parent->update();
86             }
87         }
88     }
89 }
90 
91