root/branches/Dirk_CPtr/Tutorials/08materials.cpp

Revision 637, 8.6 kB (checked in by vossg, 2 years ago)

fixed : added statistics key (s)

  • Property svn:eol-style set to native
Line 
1 // OpenSG Tutorial Example: Materials
2 //
3 // This example shows how to create a material. Materials define the surface
4 // properties of the geometry, and change how they look.
5 //
6 // This example shows the usage of SimpleMaterial and SimpleTexturedMaterial.
7 // Call it with an image filename as a parameter.
8 //
9
10 // Headers
11 #include <OpenSG/OSGGLUT.h>
12 #include <OpenSG/OSGConfig.h>
13 #include <OpenSG/OSGSimpleGeometry.h>
14 #include <OpenSG/OSGGLUTWindow.h>
15 #include <OpenSG/OSGSimpleSceneManager.h>
16 #include <OpenSG/OSGBaseFunctions.h>
17 #include <OpenSG/OSGTransform.h>
18 #include <OpenSG/OSGGroup.h>
19
20 // new headers:
21
22 // the headers for the SimpleMaterials
23 #include <OpenSG/OSGSimpleMaterial.h>
24 #include <OpenSG/OSGSimpleTexturedMaterial.h>
25 #include <OpenSG/OSGImage.h>
26
27
28 // Activate the OpenSG namespace
29 OSG_USING_NAMESPACE
30
31 // a separate transformation for every object
32 TransformPtr cyltrans, tortrans;
33
34
35 // The SimpleSceneManager to manage simple applications
36 SimpleSceneManager *mgr;
37
38 // forward declaration so we can have the interesting stuff upfront
39 int setupGLUT(int *argc, char *argv[]);
40
41 // redraw the window
42 void display(void)
43 {
44     // create the matrix
45     Matrix m;
46     Real32 t = glutGet(GLUT_ELAPSED_TIME );
47    
48     // set the transforms' matrices
49     m.setTransform(Vec3f(0, 0, osgSin(t / 1000.f) * 1.5),
50                    Quaternion( Vec3f (1, 0, 0), t / 500.f));
51
52     cyltrans->setMatrix(m);
53    
54     m.setTransform(Vec3f(osgSin(t / 1000.f), 0, 0),
55                    Quaternion( Vec3f (0, 0, 1), t / 1000.f));
56
57     tortrans->setMatrix(m);
58
59     commitChanges();
60    
61     mgr->redraw();
62 }
63
64
65 // Initialize GLUT & OpenSG and set up the scene
66 int main(int argc, char **argv)
67 {
68     // OSG init
69     osgInit(argc,argv);
70
71     // GLUT init
72     int winid = setupGLUT(&argc, argv);
73
74     // the connection between GLUT and OpenSG
75     GLUTWindowPtr gwin= GLUTWindow::create();
76     gwin->setId(winid);
77     gwin->init();
78
79     // create the scene
80     
81     // create a pretty simple graph: a Group with two Transforms as children,
82     // each of which carries a single Geometry.
83     
84     // The scene group
85     
86     NodePtr  scene = Node::create();
87     GroupPtr g     = Group::create();
88    
89     scene->setCore(g);
90    
91     // The cylinder and its transformation
92     NodePtr     cyl    = Node::create();
93     GeometryPtr cylgeo = makeCylinderGeo( 1.4, .3, 8, true, true, true );
94    
95     cyl->setCore(cylgeo);
96
97     cyltrans = Transform::create();
98
99     NodePtr cyltransnode = Node::create();
100     cyltransnode->setCore (cyltrans);
101     cyltransnode->addChild(cyl     );
102    
103     // add it to the scene
104     scene->addChild(cyltransnode);
105    
106     // The torus and its transformation
107     NodePtr     torus    = Node::create();
108     GeometryPtr torusgeo = makeTorusGeo( .2, 1, 8, 12 );
109    
110     torus->setCore(torusgeo);
111        
112     tortrans = Transform::create();
113
114     NodePtr tortransnode = Node::create();
115     tortransnode->setCore (tortrans);
116     tortransnode->addChild(torus   );
117    
118     // add it to the scene
119     scene->addChild(tortransnode);
120
121     // now create and assign the materials
122  
123     /*
124         Materials in OpenSG are associated with the Geometry NodeCores, i.e.
125         every Geometry knows its material.
126         
127         Right now there are two kinds of Materials: SimpleMaterial and
128         SimpleTexturedMaterial.
129         
130         SimpleMaterial is a pretty direct wrapper of the OpenGL material
131         characteristics. It has an ambient, diffuse, emission and specular
132         color together with a shininess. It also defines the transparency of
133         the material. Finally there are two flags. Lit defines if the material
134         is influenced by light sources and the lighting calculation. If lit is
135         false, only the diffuse color is used. ColorMaterial defines if and
136         how the colors given in the object influence the lighting calculation.
137         Possible values are GL_NONE, GL_EMISSION, GL_AMBIENT, GL_DIFFUSE,
138         GL_SPECULAR and  GL_AMBIENT_AND_DIFFUSE.
139     */
140    
141     SimpleMaterialPtr m1 = SimpleMaterial::create();
142    
143     // when everything is changed, not setting the mask is ok
144     m1->setAmbient      (Color3f(0.2,0.2,0.2));
145     m1->setDiffuse      (Color3f(0.8,0.5,0.2));
146     m1->setEmission     (Color3f(0.0,0.0,0.0));
147     m1->setSpecular     (Color3f(1.0,1.0,1.0));
148     m1->setShininess    (10);
149    
150     /*
151         Transparent objects are rendered last and sorted from back to
152         front, but only objectwise. Thus transparecy within an object is
153         not handled correctly.
154     */
155     m1->setTransparency (0);
156    
157     /*
158         The SimpleGeometry objects do not contain colors, turn
159         ColorMaterial off.
160     */
161     m1->setColorMaterial(GL_NONE);
162    
163
164     // assign the material to the geometry
165     cylgeo->setMaterial(m1);
166    
167    
168     /*
169         SimpleTexturedMaterial is a SimpleMaterial with a texture.
170         
171         The texture is specified using an Image, which can be created on the
172         fly or loaded from a file.
173         
174         Additionally the texture filters can be changed via the
175         minFilter/magFilter fields. Possible values are taken from OpenGL's
176         glTexParameter() function. MinFilter can be GL_NEAREST, GL_LINEAR, and
177         the mipmap modes, the most common being GL_LINEAR_MIPMAP_LINEAR.
178         MagFilter can be GL_NEAREST or GL_LINEAR.
179         
180         Textures are independent from the normal lighting calculations. How the
181         texture is combined with the lighting color is define by the envMode.
182         The possible values are taken from glTexEnv(). GL_MODULATE just
183         multiplies the two, GL_REPLACE ignores the lighting color and just uses
184         the texture.
185         
186         The option that the SimpleTexturedMaterial has is the ability to use
187         spherical environment maps. These can be used to simulate reflective
188         environments.
189     */
190      
191     /*
192         Images can be created directly from the data or loaded from disk.
193         
194         The Image can handle a variety of data formats, including 1, 2, 3 or 4
195         component images, and 1, 2 or 3 dimensional data. It can load and save
196         a variety of formats, exactly which depends on the configured
197         libraries.
198     */
199    
200     ImagePtr image = Image::create();
201    
202     if(argc > 1)
203     {
204         image->read(argv[1]);
205     }
206     else
207     {
208         UChar8 data[] = {  0xff, 0xff, 0xff,  0x80, 0x00, 0x00,
209                            0x80, 0x00, 0x00,  0xff, 0xff, 0xff };
210
211         image->set( Image::OSG_RGB_PF, 2, 2, 1, 1, 1, 0, data );
212     }
213    
214     SimpleTexturedMaterialPtr m2 = SimpleTexturedMaterial::create();
215    
216     m2->setAmbient      (Color3f(0.3,0.3,0.3));
217     m2->setDiffuse      (Color3f(0.2,0.8,0.8));
218     m2->setEmission     (Color3f(0.0,0.0,0.0));
219     m2->setSpecular     (Color3f(1.0,1.0,1.0));
220     m2->setShininess    (20);
221     m2->setTransparency (0);
222     m2->setColorMaterial(GL_NONE);
223        
224     m2->setImage        (image);
225     m2->setMinFilter    (GL_LINEAR_MIPMAP_LINEAR);
226     m2->setMagFilter    (GL_NEAREST);
227     m2->setEnvMode      (GL_MODULATE);
228     m2->setEnvMap       (false);
229    
230     // assign the material to the geometry
231     torusgeo->setMaterial(m2);
232
233     commitChanges();
234
235     // create the SimpleSceneManager helper
236     mgr = new SimpleSceneManager;
237
238     // tell the manager what to manage
239     mgr->setWindow(gwin );
240     mgr->setRoot  (scene);
241
242     // show the whole scene
243     mgr->showAll();
244
245     // GLUT main loop
246     glutMainLoop();
247
248     return 0;
249 }
250
251 //
252 // GLUT callback functions
253 //
254
255 // react to size changes
256 void reshape(int w, int h)
257 {
258     mgr->resize(w, h);
259     glutPostRedisplay();
260 }
261
262 // react to mouse button presses
263 void mouse(int button, int state, int x, int y)
264 {
265     if (state)
266         mgr->mouseButtonRelease(button, x, y);
267     else
268         mgr->mouseButtonPress(button, x, y);
269        
270     glutPostRedisplay();
271 }
272
273 // react to mouse motions with pressed buttons
274 void motion(int x, int y)
275 {
276     mgr->mouseMove(x, y);
277     glutPostRedisplay();
278 }
279
280 // react to keys
281 void keyboard(unsigned char k, int x, int y)
282 {
283     switch(k)
284     {
285         case 27:   
286         {
287             OSG::osgExit();
288             exit(0);
289         }
290         break;
291
292         case 's':
293         {
294             mgr->setStatistics(!mgr->getStatistics());
295         }
296         break;
297     }
298 }
299
300 // setup the GLUT library which handles the windows for us
301 int setupGLUT(int *argc, char *argv[])
302 {
303     glutInit(argc, argv);
304     glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
305    
306     int winid = glutCreateWindow("OpenSG");
307    
308     glutReshapeFunc(reshape);
309     glutDisplayFunc(display);
310     glutMouseFunc(mouse);
311     glutMotionFunc(motion);
312     glutKeyboardFunc(keyboard);
313
314     // call the redraw function whenever there's nothing else to do
315     glutIdleFunc(display);
316
317     return winid;
318 }
Note: See TracBrowser for help on using the browser.