1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
|
/* Copyright (c) Mark J. Kilgard, 1994. */
/* This program is freely distributable without licensing fees
and is provided without guarantee or warrantee expressed or
implied. This program is -not- in the public domain. */
/*
* Copyright (c) 1991-94 Silicon Graphics, Inc.
*
* Permission to use, copy, modify, distribute, and sell this software and
* its documentation for any purpose is hereby granted without fee, provided
* that the name of Silicon Graphics may not be used in any advertising or
* publicity relating to the software without the specific, prior written
* permission of Silicon Graphics.
*
* THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
* EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
* WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
*
* IN NO EVENT SHALL SILICON GRAPHICS BE LIABLE FOR ANY SPECIAL, INCIDENTAL,
* INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY DAMAGES WHATSOEVER
* RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER OR NOT ADVISED OF THE
* POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF LIABILITY, ARISING OUT OF OR IN
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
/* Based on an example from the Inventor Mentor chapter 13, example 5. */
#include <stdio.h>
#include <unistd.h>
#include <math.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
#include <Inventor/SoDB.h>
#include <Inventor/SoInput.h>
#include <Inventor/SbViewportRegion.h>
#include <Inventor/nodes/SoSeparator.h>
#include <Inventor/actions/SoGLRenderAction.h>
#include <Inventor/nodes/SoCylinder.h>
#include <Inventor/nodes/SoDirectionalLight.h>
#include <Inventor/nodes/SoEventCallback.h>
#include <Inventor/nodes/SoMaterial.h>
#include <Inventor/nodes/SoPerspectiveCamera.h>
#include <Inventor/nodes/SoRotationXYZ.h>
#include <Inventor/nodes/SoTransform.h>
#include <Inventor/nodes/SoTranslation.h>
/* Some <math.h> files do not define M_PI... */
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
int W = 300, H = 300;
int spinning = 0;
GLubyte *image = NULL;
SoSeparator *root;
SoRotationXYZ *duckRotXYZ;
float angle = 0.0;
int moving = 0;
int begin;
void
reshape(int w, int h)
{
glViewport(0, 0, w, h);
W = w;
H = h;
if (image)
free(image);
image = (GLubyte *) malloc(W * H * 3);
}
void
renderScene(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
SbViewportRegion myViewport(W, H);
SoGLRenderAction myRenderAction(myViewport);
myRenderAction.apply(root);
}
void
redraw(void)
{
renderScene();
glutSwapBuffers();
}
int
duckScene(void)
{
root = new SoSeparator;
root->ref();
// Add a camera and light
SoPerspectiveCamera *myCamera = new SoPerspectiveCamera;
myCamera->position.setValue(0., -4., 8.0);
myCamera->heightAngle = M_PI/2.5;
myCamera->nearDistance = 1.0;
myCamera->farDistance = 15.0;
root->addChild(myCamera);
root->addChild(new SoDirectionalLight);
// Rotate scene slightly to get better view
SoRotationXYZ *globalRotXYZ = new SoRotationXYZ;
globalRotXYZ->axis = SoRotationXYZ::X;
globalRotXYZ->angle = M_PI/9;
root->addChild(globalRotXYZ);
// Pond group
SoSeparator *pond = new SoSeparator;
root->addChild(pond);
SoMaterial *cylMaterial = new SoMaterial;
cylMaterial->diffuseColor.setValue(0., 0.3, 0.8);
pond->addChild(cylMaterial);
SoTranslation *cylTranslation = new SoTranslation;
cylTranslation->translation.setValue(0., -6.725, 0.);
pond->addChild(cylTranslation);
SoCylinder *myCylinder = new SoCylinder;
myCylinder->radius.setValue(4.0);
myCylinder->height.setValue(0.5);
pond->addChild(myCylinder);
// Duck group
SoSeparator *duck = new SoSeparator;
root->addChild(duck);
// Read the duck object from a file and add to the group
SoInput myInput;
if (!myInput.openFile("duck.iv")) {
if (!myInput.openFile("/usr/share/src/Inventor/examples/data/duck.iv")) {
return 1;
}
}
SoSeparator *duckObject = SoDB::readAll(&myInput);
if (duckObject == NULL) return 1;
// Set up the duck transformations
duckRotXYZ = new SoRotationXYZ;
duck->addChild(duckRotXYZ);
duckRotXYZ->angle = angle;
duckRotXYZ->axis = SoRotationXYZ::Y; // rotate about Y axis
SoTransform *initialTransform = new SoTransform;
initialTransform->translation.setValue(0., 0., 3.);
initialTransform->scaleFactor.setValue(6., 6., 6.);
duck->addChild(initialTransform);
duck->addChild(duckObject);
return 0;
}
void
updateModels(void)
{
duckRotXYZ->angle = angle;
glutPostRedisplay();
}
void
animate(void)
{
angle += 0.1;
updateModels();
}
void
setAnimation(int enable)
{
if(enable) {
spinning = 1;
glutIdleFunc(animate);
} else {
spinning = 0;
glutIdleFunc(NULL);
glutPostRedisplay();
}
}
/* ARGSUSED */
void
keyboard(unsigned char ch, int x, int y)
{
if(ch == ' ') {
setAnimation(0);
animate();
}
}
void
menuSelect(int item)
{
switch(item) {
case 1:
animate();
break;
case 2:
if(!spinning) {
setAnimation(1);
} else {
setAnimation(0);
}
break;
#ifdef GL_MULTISAMPLE_SGIS
case 3:
if(glIsEnabled(GL_MULTISAMPLE_SGIS)) {
glDisable(GL_MULTISAMPLE_SGIS);
} else {
glEnable(GL_MULTISAMPLE_SGIS);
}
glutPostRedisplay();
break;
#endif
}
}
void
vis(int visible)
{
if (visible == GLUT_VISIBLE) {
if (spinning)
glutIdleFunc(animate);
} else {
if (spinning)
glutIdleFunc(NULL);
}
}
/* ARGSUSED */
void
mouse(int button, int state, int x, int y)
{
if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) {
setAnimation(0);
moving = 1;
begin = x;
}
if (button == GLUT_LEFT_BUTTON && state == GLUT_UP) {
moving = 0;
glutPostRedisplay();
}
}
/* ARGSUSED */
void
motion(int x, int y)
{
if (moving) {
angle = angle + .01 * (x - begin);
begin = x;
updateModels();
}
}
int
main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH | GLUT_MULTISAMPLE);
SoDB::init();
if(duckScene()) {
fprintf(stderr, "couldn't read IV file\n");
exit(1);
}
glutInitWindowSize(W, H);
glutCreateWindow("GLUT Inventor Duck Pond");
glutDisplayFunc(redraw);
glutReshapeFunc(reshape);
glutCreateMenu(menuSelect);
glutAddMenuEntry("Step", 1);
glutAddMenuEntry("Toggle animation", 2);
if(glutGet(GLUT_WINDOW_NUM_SAMPLES) > 0) {
glutAddMenuEntry("Toggle multisampling", 3);
}
glutAttachMenu(GLUT_RIGHT_BUTTON);
glutKeyboardFunc(keyboard);
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutVisibilityFunc(vis);
glEnable(GL_DEPTH_TEST);
glClearColor(0.132, 0.542, 0.132, 1.0);
glutMainLoop();
return 0; /* ANSI C requires main to return int. */
}
|