Debian Codegraph @

apps/examples/Mentor/CXX/07.2.TextureCoordinates.c++

raw
1 /*
2 *
3 * Copyright (C) 2000 Silicon Graphics, Inc. All Rights Reserved.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * Further, this software is distributed without any warranty that it is
16 * free of the rightful claim of any third person regarding infringement
17 * or the like. Any license provided herein, whether implied or
18 * otherwise, applies only to this software file. Patent licenses, if
19 * any, provided herein do not apply to combinations of this program with
20 * other software, or any other product whatsoever.
21 *
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with this library; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 *
26 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
27 * Mountain View, CA 94043, or:
28 *
29 * http://www.sgi.com
30 *
31 * For further information regarding this notice, see:
32 *
33 * http://oss.sgi.com/projects/GenInfo/NoticeExplan/
34 *
35 */
36
37 /*--------------------------------------------------------------
38 * This is an example from the Inventor Mentor
39 * chapter 7, example 2.
40 *
41 * This example illustrates using texture coordindates on
42 * a Face Set.
43 *------------------------------------------------------------*/
44
45 #include <cstdlib>
46 #include <Inventor/Xt/SoXt.h>
47 #include <Inventor/Xt/viewers/SoXtExaminerViewer.h>
48 #include <Inventor/nodes/SoCoordinate3.h>
49 #include <Inventor/nodes/SoFaceSet.h>
50 #include <Inventor/nodes/SoNormal.h>
51 #include <Inventor/nodes/SoNormalBinding.h>
52 #include <Inventor/nodes/SoSeparator.h>
53 #include <Inventor/nodes/SoTexture2.h>
54 #include <Inventor/nodes/SoTextureCoordinate2.h>
55 #include <Inventor/nodes/SoTextureCoordinateBinding.h>
56
57 int
58 main(int , char **argv)
59 {
60 Widget myWindow = SoXt::init(argv[0]);
61 if(myWindow == NULL) exit(1);
62
63 SoSeparator *root = new SoSeparator;
64 root->ref();
65
66 // Choose a texture
67 SoTexture2 *brick = new SoTexture2;
68 root->addChild(brick);
69 brick->filename.setValue("/usr/share/src/Inventor/examples/data/brick.1.rgb");
70
71 #ifdef IV_STRICT
72 // This is the preferred code for Inventor 2.1
73
74 // Using the new SoVertexProperty node is more efficient
75 SoVertexProperty *myVertexProperty = new SoVertexProperty;
76
77 // Define the square's spatial coordinates
78 myVertexProperty->vertex.set1Value(0, SbVec3f(-3, -3, 0));
79 myVertexProperty->vertex.set1Value(1, SbVec3f( 3, -3, 0));
80 myVertexProperty->vertex.set1Value(2, SbVec3f( 3, 3, 0));
81 myVertexProperty->vertex.set1Value(3, SbVec3f(-3, 3, 0));
82
83 // Define the square's normal
84 myVertexProperty->normal.set1Value(0, SbVec3f(0, 0, 1));
85
86 // Define the square's texture coordinates
87 myVertexProperty->texCoord.set1Value(0, SbVec2f(0, 0));
88 myVertexProperty->texCoord.set1Value(1, SbVec2f(1, 0));
89 myVertexProperty->texCoord.set1Value(2, SbVec2f(1, 1));
90 myVertexProperty->texCoord.set1Value(3, SbVec2f(0, 1));
91
92 // SoTextureCoordinateBinding node is now obsolete--in Inventor 2.1,
93 // texture coordinates will always be generated if none are
94 // provided.
95 /*
96 SoTextureCoordinateBinding *tBind =
97 new SoTextureCoordinateBinding;
98 root->addChild(tBind);
99 tBind->value = SoTextureCoordinateBinding::PER_VERTEX;
100 */
101 // Define normal binding
102 myVertexProperty->normalBinding = SoNormalBinding::OVERALL;
103
104 // Define a FaceSet
105 SoFaceSet *myFaceSet = new SoFaceSet;
106 root->addChild(myFaceSet);
107 myFaceSet->numVertices.set1Value(0, 4);
108
109 myFaceSet->vertexProperty.setValue(myVertexProperty);
110
111 #else
112 // Define the square's spatial coordinates
113 SoCoordinate3 *coord = new SoCoordinate3;
114 root->addChild(coord);
115 coord->point.set1Value(0, SbVec3f(-3, -3, 0));
116 coord->point.set1Value(1, SbVec3f( 3, -3, 0));
117 coord->point.set1Value(2, SbVec3f( 3, 3, 0));
118 coord->point.set1Value(3, SbVec3f(-3, 3, 0));
119
120 // Define the square's normal
121 SoNormal *normal = new SoNormal;
122 root->addChild(normal);
123 normal->vector.set1Value(0, SbVec3f(0, 0, 1));
124
125 // Define the square's texture coordinates
126 SoTextureCoordinate2 *texCoord = new SoTextureCoordinate2;
127 root->addChild(texCoord);
128 texCoord->point.set1Value(0, SbVec2f(0, 0));
129 texCoord->point.set1Value(1, SbVec2f(1, 0));
130 texCoord->point.set1Value(2, SbVec2f(1, 1));
131 texCoord->point.set1Value(3, SbVec2f(0, 1));
132
133 // Define normal binding
134 SoNormalBinding *nBind = new SoNormalBinding;
135 root->addChild(nBind);
136 nBind->value.setValue(SoNormalBinding::OVERALL);
137
138 // SoTextureCoordinateBinding node is now obsolete--in Inventor 2.1,
139 // texture coordinates will always be generated if none are
140 // provided.
141 /*
142 SoTextureCoordinateBinding *tBind =
143 new SoTextureCoordinateBinding;
144 root->addChild(tBind);
145 tBind->value.setValue
146 (SoTextureCoordinateBinding::PER_VERTEX);
147 */
148
149 // Define a FaceSet
150 SoFaceSet *myFaceSet = new SoFaceSet;
151 root->addChild(myFaceSet);
152 myFaceSet->numVertices.set1Value(0, 4);
153 #endif
154
155 SoXtExaminerViewer *myViewer =
156 new SoXtExaminerViewer(myWindow);
157 myViewer->setSceneGraph(root);
158 myViewer->setTitle("Texture Coordinates");
159
160 // In Inventor 2.1, if the machine does not have hardware texture
161 // mapping, we must override the default drawStyle to display textures.
162 myViewer->setDrawStyle(SoXtViewer::STILL, SoXtViewer::VIEW_AS_IS);
163
164 myViewer->show();
165
166 SoXt::show(myWindow);
167 SoXt::mainLoop();
168 }