| 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 2. |
| 40 |
* |
| 41 |
* Example of combining Inventor and OpenGL rendering. |
| 42 |
* Create an Inventor render area and draw a red cube |
| 43 |
* and a blue sphere. Render the floor with OpenGL |
| 44 |
* through a Callback node. |
| 45 |
*----------------------------------------------------------*/ |
| 46 |
|
| 47 |
#include <GL/gl.h> |
| 48 |
#include <Inventor/SbLinear.h> |
| 49 |
#include <Inventor/Xt/SoXt.h> |
| 50 |
#include <Inventor/Xt/SoXtRenderArea.h> |
| 51 |
#include <Inventor/nodes/SoCallback.h> |
| 52 |
#include <Inventor/nodes/SoCube.h> |
| 53 |
#include <Inventor/nodes/SoDirectionalLight.h> |
| 54 |
#include <Inventor/nodes/SoLightModel.h> |
| 55 |
#include <Inventor/nodes/SoMaterial.h> |
| 56 |
#include <Inventor/nodes/SoPerspectiveCamera.h> |
| 57 |
#include <Inventor/nodes/SoSphere.h> |
| 58 |
#include <Inventor/nodes/SoSeparator.h> |
| 59 |
#include <Inventor/nodes/SoTransform.h> |
| 60 |
#include <Inventor/elements/SoGLLazyElement.h> |
| 61 |
|
| 62 |
float floorObj[81][3]; |
| 63 |
|
| 64 |
// Build a scene with two objects and some light |
| 65 |
void |
| 66 |
buildScene(SoGroup *root) |
| 67 |
{ |
| 68 |
// Some light |
| 69 |
root->addChild(new SoLightModel); |
| 70 |
root->addChild(new SoDirectionalLight); |
| 71 |
|
| 72 |
// A red cube translated to the left and down |
| 73 |
SoTransform *myTrans = new SoTransform; |
| 74 |
myTrans->translation.setValue(-2.0, -2.0, 0.0); |
| 75 |
root->addChild(myTrans); |
| 76 |
|
| 77 |
SoMaterial *myMtl = new SoMaterial; |
| 78 |
myMtl->diffuseColor.setValue(1.0, 0.0, 0.0); |
| 79 |
root->addChild(myMtl); |
| 80 |
|
| 81 |
root->addChild(new SoCube); |
| 82 |
|
| 83 |
// A blue sphere translated right |
| 84 |
myTrans = new SoTransform; |
| 85 |
myTrans->translation.setValue(4.0, 0.0, 0.0); |
| 86 |
root->addChild(myTrans); |
| 87 |
|
| 88 |
myMtl = new SoMaterial; |
| 89 |
myMtl->diffuseColor.setValue(0.0, 0.0, 1.0); |
| 90 |
root->addChild(myMtl); |
| 91 |
|
| 92 |
root->addChild(new SoSphere); |
| 93 |
} |
| 94 |
|
| 95 |
// Build the floor that will be rendered using OpenGL. |
| 96 |
void |
| 97 |
buildFloor() |
| 98 |
{ |
| 99 |
int a = 0; |
| 100 |
|
| 101 |
for (float i = -5.0; i <= 5.0; i += 1.25) { |
| 102 |
for (float j = -5.0; j <= 5.0; j += 1.25, a++) { |
| 103 |
floorObj[a][0] = j; |
| 104 |
floorObj[a][1] = 0.0; |
| 105 |
floorObj[a][2] = i; |
| 106 |
} |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
// Draw the lines that make up the floor, using OpenGL |
| 111 |
void |
| 112 |
drawFloor() |
| 113 |
{ |
| 114 |
int i; |
| 115 |
|
| 116 |
glBegin(GL_LINES); |
| 117 |
for (i=0; i<4; i++) { |
| 118 |
glVertex3fv(floorObj[i*18]); |
| 119 |
glVertex3fv(floorObj[(i*18)+8]); |
| 120 |
glVertex3fv(floorObj[(i*18)+17]); |
| 121 |
glVertex3fv(floorObj[(i*18)+9]); |
| 122 |
} |
| 123 |
|
| 124 |
glVertex3fv(floorObj[i*18]); |
| 125 |
glVertex3fv(floorObj[(i*18)+8]); |
| 126 |
glEnd(); |
| 127 |
|
| 128 |
glBegin(GL_LINES); |
| 129 |
for (i=0; i<4; i++) { |
| 130 |
glVertex3fv(floorObj[i*2]); |
| 131 |
glVertex3fv(floorObj[(i*2)+72]); |
| 132 |
glVertex3fv(floorObj[(i*2)+73]); |
| 133 |
glVertex3fv(floorObj[(i*2)+1]); |
| 134 |
} |
| 135 |
glVertex3fv(floorObj[i*2]); |
| 136 |
glVertex3fv(floorObj[(i*2)+72]); |
| 137 |
glEnd(); |
| 138 |
} |
| 139 |
|
| 140 |
// Callback routine to render the floor using OpenGL |
| 141 |
void |
| 142 |
myCallbackRoutine(void *, SoAction *action) |
| 143 |
{ |
| 144 |
// only render the floor during GLRender actions: |
| 145 |
if(!action->isOfType(SoGLRenderAction::getClassTypeId())) return; |
| 146 |
|
| 147 |
glPushMatrix(); |
| 148 |
glTranslatef(0.0, -3.0, 0.0); |
| 149 |
glColor3f(0.0, 0.7, 0.0); |
| 150 |
glLineWidth(2); |
| 151 |
glDisable(GL_LIGHTING); // so we don't have to set normals |
| 152 |
drawFloor(); |
| 153 |
glEnable(GL_LIGHTING); |
| 154 |
glLineWidth(1); |
| 155 |
glPopMatrix(); |
| 156 |
|
| 157 |
//With Inventor 2.1, it's necessary to reset SoGLLazyElement after |
| 158 |
//making calls (such as glColor3f()) that affect material state. |
| 159 |
//In this case, the diffuse color and light model are being modified, |
| 160 |
//so the logical-or of DIFFUSE_MASK and LIGHT_MODEL_MASK is passed |
| 161 |
//to SoGLLazyElement::reset(). |
| 162 |
//Additional information can be found in the publication |
| 163 |
// "Open Inventor 2.1 Porting and Performance Tips" |
| 164 |
|
| 165 |
SoState *state = action->getState(); |
| 166 |
SoGLLazyElement* lazyElt = |
| 167 |
(SoGLLazyElement*)SoLazyElement::getInstance(state); |
| 168 |
lazyElt->reset(state, |
| 169 |
(SoLazyElement::DIFFUSE_MASK)|(SoLazyElement::LIGHT_MODEL_MASK)); |
| 170 |
|
| 171 |
} |
| 172 |
|
| 173 |
int |
| 174 |
main(int, char **) |
| 175 |
{ |
| 176 |
// Initialize Inventor utilities |
| 177 |
Widget myWindow = SoXt::init("Example 17.1"); |
| 178 |
|
| 179 |
buildFloor(); |
| 180 |
|
| 181 |
// Build a simple scene graph, including a camera and |
| 182 |
// a SoCallback node for performing some GL rendering. |
| 183 |
SoSeparator *root = new SoSeparator; |
| 184 |
root->ref(); |
| 185 |
|
| 186 |
SoPerspectiveCamera *myCamera = new SoPerspectiveCamera; |
| 187 |
myCamera->position.setValue(0.0, 0.0, 5.0); |
| 188 |
myCamera->heightAngle = M_PI/2.0; // 90 degrees |
| 189 |
myCamera->nearDistance = 2.0; |
| 190 |
myCamera->farDistance = 12.0; |
| 191 |
root->addChild(myCamera); |
| 192 |
|
| 193 |
SoCallback *myCallback = new SoCallback; |
| 194 |
myCallback->setCallback(myCallbackRoutine); |
| 195 |
root->addChild(myCallback); |
| 196 |
|
| 197 |
buildScene(root); |
| 198 |
|
| 199 |
// Initialize an Inventor Xt RenderArea and draw the scene. |
| 200 |
SoXtRenderArea *myRenderArea = new SoXtRenderArea(myWindow); |
| 201 |
myRenderArea->setSceneGraph(root); |
| 202 |
myRenderArea->setTitle("OpenGL Callback"); |
| 203 |
myRenderArea->setBackgroundColor(SbColor(.8, .8, .8)); |
| 204 |
myRenderArea->show(); |
| 205 |
|
| 206 |
SoXt::show(myWindow); |
| 207 |
SoXt::mainLoop(); |
| 208 |
} |