Debian Codegraph @

apps/examples/Mentor/CXX/09.2.Texture.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 9, example 2.
40 *
41 * Using the offscreen renderer to generate a texture map.
42 * Generate simple scene and grab the image to use as
43 * a texture map.
44 *----------------------------------------------------------*/
45
46 #include <cstdlib>
47 #include <Inventor/SoDB.h>
48 #include <Inventor/SoInput.h>
49 #include <Inventor/SoOffscreenRenderer.h>
50 #include <Inventor/SbViewportRegion.h>
51 #include <Inventor/nodes/SoCube.h>
52 #include <Inventor/nodes/SoDirectionalLight.h>
53 #include <Inventor/nodes/SoPerspectiveCamera.h>
54 #include <Inventor/nodes/SoRotationXYZ.h>
55 #include <Inventor/nodes/SoSeparator.h>
56 #include <Inventor/nodes/SoTexture2.h>
57 #include <Inventor/Xt/SoXt.h>
58 #include <Inventor/Xt/viewers/SoXtExaminerViewer.h>
59
60
61 SbBool
62 generateTextureMap (SoNode *root, SoTexture2 *texture,
63 short textureWidth, short textureHeight)
64 {
65 SbViewportRegion myViewport(textureWidth, textureHeight);
66
67 // Render the scene
68 SoOffscreenRenderer *myRenderer =
69 new SoOffscreenRenderer(myViewport);
70 myRenderer->setBackgroundColor(SbColor(0.3, 0.3, 0.3));
71 if (!myRenderer->render(root)) {
72 delete myRenderer;
73 return FALSE;
74 }
75
76 // Generate the texture
77 texture->image.setValue(SbVec2s(textureWidth, textureHeight),
78 SoOffscreenRenderer::RGB, myRenderer->getBuffer());
79
80 delete myRenderer;
81 return TRUE;
82 }
83
84 int
85 main(int, char **argv)
86 {
87 // Initialize Inventor and Xt
88 Widget appWindow = SoXt::init(argv[0]);
89 if (appWindow == NULL)
90 exit(1);
91
92 // Make a scene from reading in a file
93 SoSeparator *texRoot = new SoSeparator;
94 SoInput in;
95 SoNode *result;
96
97 texRoot->ref();
98 in.openFile("/usr/share/src/Inventor/examples/data/jumpyMan.iv");
99 SoDB::read(&in, result);
100
101 SoPerspectiveCamera *myCamera = new SoPerspectiveCamera;
102 SoRotationXYZ *rot = new SoRotationXYZ;
103 rot->axis = SoRotationXYZ::X;
104 rot->angle = M_PI_2;
105 myCamera->position.setValue(SbVec3f(-0.2, -0.2, 2.0));
106 myCamera->scaleHeight(0.4);
107 texRoot->addChild(myCamera);
108 texRoot->addChild(new SoDirectionalLight);
109 texRoot->addChild(rot);
110 texRoot->addChild(result);
111
112 // Generate the texture map
113 SoTexture2 *texture = new SoTexture2;
114 texture->ref();
115 if (generateTextureMap(texRoot, texture, 64, 64))
116 printf ("Successfully generated texture map\n");
117 else
118 printf ("Could not generate texture map\n");
119 texRoot->unref();
120
121 // Make a scene with a cube and apply the texture to it
122 SoSeparator *root = new SoSeparator;
123 root->ref();
124 root->addChild(texture);
125 root->addChild(new SoCube);
126
127 // Initialize an Examiner Viewer
128 SoXtExaminerViewer *viewer =
129 new SoXtExaminerViewer(appWindow);
130 viewer->setSceneGraph(root);
131 viewer->setTitle("Offscreen Rendered Texture");
132
133 // In Inventor 2.1, if the machine does not have hardware texture
134 // mapping, we must override the default drawStyle to display textures.
135 viewer->setDrawStyle(SoXtViewer::STILL, SoXtViewer::VIEW_AS_IS);
136
137 viewer->show();
138
139 SoXt::show(appWindow);
140 SoXt::mainLoop();
141 }