Debian Codegraph @

apps/examples/Mentor/CXX/17.3.GLFloor.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 17, example 3.
40 *
41 * This example draws the same scene as Example 17.2,
42 * but using a GLX window.
43 *---------------------------------------------------------*/
44
45 #include <GL/glx.h>
46 #include <GL/gl.h>
47 #include <GL/glu.h>
48 #include <cstdio>
49 #include <unistd.h>
50
51 #include <Inventor/SoDB.h>
52 #include <Inventor/actions/SoGLRenderAction.h>
53 #include <Inventor/nodes/SoCube.h>
54 #include <Inventor/nodes/SoDirectionalLight.h>
55 #include <Inventor/nodes/SoLightModel.h>
56 #include <Inventor/nodes/SoMaterial.h>
57 #include <Inventor/nodes/SoTransform.h>
58 #include <Inventor/nodes/SoSeparator.h>
59 #include <Inventor/nodes/SoSphere.h>
60 #include <Inventor/elements/SoGLLazyElement.h>
61
62 #define WINWIDTH 400
63 #define WINHEIGHT 400
64
65 float floorObj[81][3];
66
67 // Build an Inventor scene with two objects and some light
68 void
69 buildScene(SoGroup *root)
70 {
71 // Some light
72 root->addChild(new SoLightModel);
73 root->addChild(new SoDirectionalLight);
74
75 // A red cube translated to the left and down
76 SoTransform *myTrans = new SoTransform;
77 myTrans->translation.setValue(-2.0, -2.0, 0.0);
78 root->addChild(myTrans);
79
80 SoMaterial *myMtl = new SoMaterial;
81 myMtl->diffuseColor.setValue(1.0, 0.0, 0.0);
82 root->addChild(myMtl);
83
84 root->addChild(new SoCube);
85
86 // A blue sphere translated right
87 myTrans = new SoTransform;
88 myTrans->translation.setValue(4.0, 0.0, 0.0);
89 root->addChild(myTrans);
90
91 myMtl = new SoMaterial;
92 myMtl->diffuseColor.setValue(0.0, 0.0, 1.0);
93 root->addChild(myMtl);
94
95 root->addChild(new SoSphere);
96 }
97
98
99 // Build a floor that will be rendered using OpenGL.
100 void
101 buildFloor()
102 {
103 int a = 0;
104
105 for (float i = -5.0; i <= 5.0; i += 1.25) {
106 for (float j = -5.0; j <= 5.0; j += 1.25, a++) {
107 floorObj[a][0] = j;
108 floorObj[a][1] = 0.0;
109 floorObj[a][2] = i;
110 }
111 }
112 }
113
114 // Callback used by GLX window
115 static Bool
116 waitForNotify(Display *, XEvent *e, char *arg)
117 {
118 return (e->type == MapNotify) &&
119 (e->xmap.window == (Window) arg);
120 }
121
122 // Create and initialize GLX window.
123 void
124 openWindow(Display *&display, Window &window)
125 {
126 XVisualInfo *vi;
127 Colormap cmap;
128 XSetWindowAttributes swa;
129 GLXContext cx;
130 XEvent event;
131 static int attributeList[] = {
132 GLX_RGBA,
133 GLX_RED_SIZE, 1,
134 GLX_GREEN_SIZE, 1,
135 GLX_BLUE_SIZE, 1,
136 GLX_DEPTH_SIZE, 1,
137 GLX_DOUBLEBUFFER,
138 None,
139 };
140
141 // Open the X display
142 display = XOpenDisplay(0);
143
144 // Initialize the GLX visual and context
145 vi = glXChooseVisual(display,
146 DefaultScreen(display), attributeList);
147 cx = glXCreateContext(display, vi, 0, GL_TRUE);
148
149 // Create the X color map
150 cmap = XCreateColormap(display,
151 RootWindow(display, vi->screen),
152 vi->visual, AllocNone);
153
154 // Create and map the X window
155 swa.colormap = cmap;
156 swa.border_pixel = 0;
157 swa.event_mask = StructureNotifyMask;
158 window = XCreateWindow(display,
159 RootWindow(display, vi->screen), 100, 100, WINWIDTH,
160 WINHEIGHT, 0, vi->depth, InputOutput, vi->visual,
161 (CWBorderPixel | CWColormap | CWEventMask), &swa);
162 XMapWindow(display, window);
163 XIfEvent(display, &event, waitForNotify, (char *) window);
164
165 // Attach the GLX context to the window
166 glXMakeCurrent(display, window, cx);
167 }
168
169
170 // Draw the lines that make up the floor, using OpenGL
171 void
172 drawFloor()
173 {
174 int i;
175
176 glBegin(GL_LINES);
177 for (i=0; i<4; i++) {
178 glVertex3fv(floorObj[i*18]);
179 glVertex3fv(floorObj[(i*18)+8]);
180 glVertex3fv(floorObj[(i*18)+17]);
181 glVertex3fv(floorObj[(i*18)+9]);
182 }
183
184 glVertex3fv(floorObj[i*18]);
185 glVertex3fv(floorObj[(i*18)+8]);
186 glEnd();
187
188 glBegin(GL_LINES);
189 for (i=0; i<4; i++) {
190 glVertex3fv(floorObj[i*2]);
191 glVertex3fv(floorObj[(i*2)+72]);
192 glVertex3fv(floorObj[(i*2)+73]);
193 glVertex3fv(floorObj[(i*2)+1]);
194 }
195 glVertex3fv(floorObj[i*2]);
196 glVertex3fv(floorObj[(i*2)+72]);
197 glEnd();
198 }
199
200
201 int
202 main(int, char **)
203 {
204 // Initialize Inventor
205 SoDB::init();
206
207 // Build a simple scene graph
208 SoSeparator *root = new SoSeparator;
209 root->ref();
210 buildScene(root);
211
212 // Build the floor geometry
213 buildFloor();
214
215 // Create and initialize window
216 Display *display;
217 Window window;
218 openWindow(display, window);
219 glEnable(GL_DEPTH_TEST);
220 glClearColor(0.8, 0.8, 0.8, 1.0);
221 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
222
223 // Set up the camera using OpenGL.
224 glMatrixMode(GL_PROJECTION);
225 glLoadIdentity();
226 gluPerspective(90.0, 1.0, 2.0, 12.0);
227
228 glMatrixMode(GL_MODELVIEW);
229 glLoadIdentity();
230 glTranslatef(0.0, 0.0, -5.0);
231
232 // Render the floor using OpenGL
233 glPushMatrix();
234 glTranslatef(0.0, -3.0, 0.0);
235 glColor3f(0.7, 0.0, 0.0);
236 glLineWidth(2.0);
237 glDisable(GL_LIGHTING);
238 drawFloor();
239 glEnable(GL_LIGHTING);
240 glPopMatrix();
241
242 // Render the scene
243 SbViewportRegion myViewport(WINWIDTH, WINHEIGHT);
244 SoGLRenderAction myRenderAction(myViewport);
245
246 myRenderAction.apply(root);
247 glXSwapBuffers(display, window);
248
249 //With inventor 2.1, it's necessary to reset the lazy element
250 //any time GL calls are made outside of inventor. In this example,
251 //between the first and second rendering, the inventor state must
252 //have both diffuse color and light model reset, since these are
253 //modified by the GLX rendering code. For more information about
254 //the lazy element, see the publication,
255 // "Open Inventor 2.1 Porting and Performance Tips"
256
257 //To reset the lazy element, first we obtain the state
258 //from the action, then obtain the lazy element from the state,
259 //and finally apply a reset to that lazy element.
260
261 SoState* state = myRenderAction.getState();
262 SoGLLazyElement* lazyElt =
263 (SoGLLazyElement*)SoLazyElement::getInstance(state);
264 lazyElt->reset(state,
265 (SoLazyElement::DIFFUSE_MASK)|(SoLazyElement::LIGHT_MODEL_MASK));
266
267 sleep(5);
268
269 //Rerender the floor using OpenGL again:
270 glClearColor(0.8, 0.8, 0.8, 1.0);
271 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
272
273 glPushMatrix();
274 glTranslatef(0.0, -3.0, 0.0);
275 glColor3f(0.0, 0.7, 0.0);
276 glLineWidth(2.0);
277 glDisable(GL_LIGHTING);
278 drawFloor();
279 glEnable(GL_LIGHTING);
280 glPopMatrix();
281
282 //Redraw the rest of the scene:
283 myRenderAction.apply(root);
284 glXSwapBuffers(display, window);
285
286 sleep (10);
287 root->unref();
288 return 0;
289 }