| 1 | /* OpenSceneGraph example, osgteapot. |
| 2 | * |
| 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 4 | * of this software and associated documentation files (the "Software"), to deal |
| 5 | * in the Software without restriction, including without limitation the rights |
| 6 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 7 | * copies of the Software, and to permit persons to whom the Software is |
| 8 | * furnished to do so, subject to the following conditions: |
| 9 | * |
| 10 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 11 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 12 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 13 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 14 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 15 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 16 | * THE SOFTWARE. |
| 17 | */ |
| 18 | |
| 19 | #include <osg/Geode> |
| 20 | #include <osg/TexGen> |
| 21 | #include <osg/Texture2D> |
| 22 | |
| 23 | #include <osgDB/ReadFile> |
| 24 | |
| 25 | #include <osgViewer/Viewer> |
| 26 | |
| 27 | #include <osgXR/osgXR> |
| 28 | |
| 29 | |
| 30 | // The classic OpenGL teapot... taken form glut-3.7/lib/glut/glut_teapot.c |
| 31 | |
| 32 | /* Copyright (c) Mark J. Kilgard, 1994. */ |
| 33 | |
| 34 | /** |
| 35 | (c) Copyright 1993, Silicon Graphics, Inc. |
| 36 | |
| 37 | ALL RIGHTS RESERVED |
| 38 | |
| 39 | Permission to use, copy, modify, and distribute this software |
| 40 | for any purpose and without fee is hereby granted, provided |
| 41 | that the above copyright notice appear in all copies and that |
| 42 | both the copyright notice and this permission notice appear in |
| 43 | supporting documentation, and that the name of Silicon |
| 44 | Graphics, Inc. not be used in advertising or publicity |
| 45 | pertaining to distribution of the software without specific, |
| 46 | written prior permission. |
| 47 | |
| 48 | THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU |
| 49 | "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR |
| 50 | OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF |
| 51 | MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. IN NO |
| 52 | EVENT SHALL SILICON GRAPHICS, INC. BE LIABLE TO YOU OR ANYONE |
| 53 | ELSE FOR ANY DIRECT, SPECIAL, INCIDENTAL, INDIRECT OR |
| 54 | CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY DAMAGES WHATSOEVER, |
| 55 | INCLUDING WITHOUT LIMITATION, LOSS OF PROFIT, LOSS OF USE, |
| 56 | SAVINGS OR REVENUE, OR THE CLAIMS OF THIRD PARTIES, WHETHER OR |
| 57 | NOT SILICON GRAPHICS, INC. HAS BEEN ADVISED OF THE POSSIBILITY |
| 58 | OF SUCH LOSS, HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 59 | ARISING OUT OF OR IN CONNECTION WITH THE POSSESSION, USE OR |
| 60 | PERFORMANCE OF THIS SOFTWARE. |
| 61 | |
| 62 | US Government Users Restricted Rights |
| 63 | |
| 64 | Use, duplication, or disclosure by the Government is subject to |
| 65 | restrictions set forth in FAR 52.227.19(c)(2) or subparagraph |
| 66 | (c)(1)(ii) of the Rights in Technical Data and Computer |
| 67 | Software clause at DFARS 252.227-7013 and/or in similar or |
| 68 | successor clauses in the FAR or the DOD or NASA FAR |
| 69 | Supplement. Unpublished-- rights reserved under the copyright |
| 70 | laws of the United States. Contractor/manufacturer is Silicon |
| 71 | Graphics, Inc., 2011 N. Shoreline Blvd., Mountain View, CA |
| 72 | 94039-7311. |
| 73 | |
| 74 | OpenGL(TM) is a trademark of Silicon Graphics, Inc. |
| 75 | */ |
| 76 | |
| 77 | |
| 78 | /* Rim, body, lid, and bottom data must be reflected in x and |
| 79 | y; handle and spout data across the y axis only. */ |
| 80 | |
| 81 | static int patchdata[][16] = |
| 82 | { |
| 83 | /* rim */ |
| 84 | {102, 103, 104, 105, 4, 5, 6, 7, 8, 9, 10, 11, |
| 85 | 12, 13, 14, 15}, |
| 86 | /* body */ |
| 87 | {12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, |
| 88 | 24, 25, 26, 27}, |
| 89 | {24, 25, 26, 27, 29, 30, 31, 32, 33, 34, 35, 36, |
| 90 | 37, 38, 39, 40}, |
| 91 | /* lid */ |
| 92 | {96, 96, 96, 96, 97, 98, 99, 100, 101, 101, 101, |
| 93 | 101, 0, 1, 2, 3,}, |
| 94 | {0, 1, 2, 3, 106, 107, 108, 109, 110, 111, 112, |
| 95 | 113, 114, 115, 116, 117}, |
| 96 | /* bottom */ |
| 97 | {118, 118, 118, 118, 124, 122, 119, 121, 123, 126, |
| 98 | 125, 120, 40, 39, 38, 37}, |
| 99 | /* handle */ |
| 100 | {41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, |
| 101 | 53, 54, 55, 56}, |
| 102 | {53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, |
| 103 | 28, 65, 66, 67}, |
| 104 | /* spout */ |
| 105 | {68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, |
| 106 | 80, 81, 82, 83}, |
| 107 | {80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, |
| 108 | 92, 93, 94, 95} |
| 109 | }; |
| 110 | /* *INDENT-OFF* */ |
| 111 | |
| 112 | static float cpdata[][3] = |
| 113 | { |
| 114 | {0.2, 0, 2.7}, {0.2, -0.112, 2.7}, {0.112, -0.2, 2.7}, {0, |
| 115 | -0.2, 2.7}, {1.3375, 0, 2.53125}, {1.3375, -0.749, 2.53125}, |
| 116 | {0.749, -1.3375, 2.53125}, {0, -1.3375, 2.53125}, {1.4375, |
| 117 | 0, 2.53125}, {1.4375, -0.805, 2.53125}, {0.805, -1.4375, |
| 118 | 2.53125}, {0, -1.4375, 2.53125}, {1.5, 0, 2.4}, {1.5, -0.84, |
| 119 | 2.4}, {0.84, -1.5, 2.4}, {0, -1.5, 2.4}, {1.75, 0, 1.875}, |
| 120 | {1.75, -0.98, 1.875}, {0.98, -1.75, 1.875}, {0, -1.75, |
| 121 | 1.875}, {2, 0, 1.35}, {2, -1.12, 1.35}, {1.12, -2, 1.35}, |
| 122 | {0, -2, 1.35}, {2, 0, 0.9}, {2, -1.12, 0.9}, {1.12, -2, |
| 123 | 0.9}, {0, -2, 0.9}, {-2, 0, 0.9}, {2, 0, 0.45}, {2, -1.12, |
| 124 | 0.45}, {1.12, -2, 0.45}, {0, -2, 0.45}, {1.5, 0, 0.225}, |
| 125 | {1.5, -0.84, 0.225}, {0.84, -1.5, 0.225}, {0, -1.5, 0.225}, |
| 126 | {1.5, 0, 0.15}, {1.5, -0.84, 0.15}, {0.84, -1.5, 0.15}, {0, |
| 127 | -1.5, 0.15}, {-1.6, 0, 2.025}, {-1.6, -0.3, 2.025}, {-1.5, |
| 128 | -0.3, 2.25}, {-1.5, 0, 2.25}, {-2.3, 0, 2.025}, {-2.3, -0.3, |
| 129 | 2.025}, {-2.5, -0.3, 2.25}, {-2.5, 0, 2.25}, {-2.7, 0, |
| 130 | 2.025}, {-2.7, -0.3, 2.025}, {-3, -0.3, 2.25}, {-3, 0, |
| 131 | 2.25}, {-2.7, 0, 1.8}, {-2.7, -0.3, 1.8}, {-3, -0.3, 1.8}, |
| 132 | {-3, 0, 1.8}, {-2.7, 0, 1.575}, {-2.7, -0.3, 1.575}, {-3, |
| 133 | -0.3, 1.35}, {-3, 0, 1.35}, {-2.5, 0, 1.125}, {-2.5, -0.3, |
| 134 | 1.125}, {-2.65, -0.3, 0.9375}, {-2.65, 0, 0.9375}, {-2, |
| 135 | -0.3, 0.9}, {-1.9, -0.3, 0.6}, {-1.9, 0, 0.6}, {1.7, 0, |
| 136 | 1.425}, {1.7, -0.66, 1.425}, {1.7, -0.66, 0.6}, {1.7, 0, |
| 137 | 0.6}, {2.6, 0, 1.425}, {2.6, -0.66, 1.425}, {3.1, -0.66, |
| 138 | 0.825}, {3.1, 0, 0.825}, {2.3, 0, 2.1}, {2.3, -0.25, 2.1}, |
| 139 | {2.4, -0.25, 2.025}, {2.4, 0, 2.025}, {2.7, 0, 2.4}, {2.7, |
| 140 | -0.25, 2.4}, {3.3, -0.25, 2.4}, {3.3, 0, 2.4}, {2.8, 0, |
| 141 | 2.475}, {2.8, -0.25, 2.475}, {3.525, -0.25, 2.49375}, |
| 142 | {3.525, 0, 2.49375}, {2.9, 0, 2.475}, {2.9, -0.15, 2.475}, |
| 143 | {3.45, -0.15, 2.5125}, {3.45, 0, 2.5125}, {2.8, 0, 2.4}, |
| 144 | {2.8, -0.15, 2.4}, {3.2, -0.15, 2.4}, {3.2, 0, 2.4}, {0, 0, |
| 145 | 3.15}, {0.8, 0, 3.15}, {0.8, -0.45, 3.15}, {0.45, -0.8, |
| 146 | 3.15}, {0, -0.8, 3.15}, {0, 0, 2.85}, {1.4, 0, 2.4}, {1.4, |
| 147 | -0.784, 2.4}, {0.784, -1.4, 2.4}, {0, -1.4, 2.4}, {0.4, 0, |
| 148 | 2.55}, {0.4, -0.224, 2.55}, {0.224, -0.4, 2.55}, {0, -0.4, |
| 149 | 2.55}, {1.3, 0, 2.55}, {1.3, -0.728, 2.55}, {0.728, -1.3, |
| 150 | 2.55}, {0, -1.3, 2.55}, {1.3, 0, 2.4}, {1.3, -0.728, 2.4}, |
| 151 | {0.728, -1.3, 2.4}, {0, -1.3, 2.4}, {0, 0, 0}, {1.425, |
| 152 | -0.798, 0}, {1.5, 0, 0.075}, {1.425, 0, 0}, {0.798, -1.425, |
| 153 | 0}, {0, -1.5, 0.075}, {0, -1.425, 0}, {1.5, -0.84, 0.075}, |
| 154 | {0.84, -1.5, 0.075} |
| 155 | }; |
| 156 | |
| 157 | static float tex[2][2][2] = |
| 158 | { |
| 159 | { {0, 0}, |
| 160 | {1, 0}}, |
| 161 | { {0, 1}, |
| 162 | {1, 1}} |
| 163 | }; |
| 164 | |
| 165 | /* *INDENT-ON* */ |
| 166 | |
| 167 | static void |
| 168 | teapot(GLint grid, GLenum type) |
| 169 | { |
| 170 | float p[4][4][3], q[4][4][3], r[4][4][3], s[4][4][3]; |
| 171 | long i, j, k, l; |
| 172 | |
| 173 | glPushAttrib(GL_ENABLE_BIT | GL_EVAL_BIT); |
| 174 | glEnable(GL_AUTO_NORMAL); |
| 175 | glEnable(GL_NORMALIZE); |
| 176 | glEnable(GL_MAP2_VERTEX_3); |
| 177 | glEnable(GL_MAP2_TEXTURE_COORD_2); |
| 178 | for (i = 0; i < 10; i++) { |
| 179 | for (j = 0; j < 4; j++) { |
| 180 | for (k = 0; k < 4; k++) { |
| 181 | for (l = 0; l < 3; l++) { |
| 182 | p[j][k][l] = cpdata[patchdata[i][j * 4 + k]][l]; |
| 183 | q[j][k][l] = cpdata[patchdata[i][j * 4 + (3 - k)]][l]; |
| 184 | if (l == 1) |
| 185 | q[j][k][l] *= -1.0; |
| 186 | if (i < 6) { |
| 187 | r[j][k][l] = |
| 188 | cpdata[patchdata[i][j * 4 + (3 - k)]][l]; |
| 189 | if (l == 0) |
| 190 | r[j][k][l] *= -1.0; |
| 191 | s[j][k][l] = cpdata[patchdata[i][j * 4 + k]][l]; |
| 192 | if (l == 0) |
| 193 | s[j][k][l] *= -1.0; |
| 194 | if (l == 1) |
| 195 | s[j][k][l] *= -1.0; |
| 196 | } |
| 197 | } |
| 198 | } |
| 199 | } |
| 200 | glMap2f(GL_MAP2_TEXTURE_COORD_2, 0, 1, 2, 2, 0, 1, 4, 2, |
| 201 | &tex[0][0][0]); |
| 202 | glMap2f(GL_MAP2_VERTEX_3, 0, 1, 3, 4, 0, 1, 12, 4, |
| 203 | &p[0][0][0]); |
| 204 | glMapGrid2f(grid, 0.0, 1.0, grid, 0.0, 1.0); |
| 205 | glEvalMesh2(type, 0, grid, 0, grid); |
| 206 | glMap2f(GL_MAP2_VERTEX_3, 0, 1, 3, 4, 0, 1, 12, 4, |
| 207 | &q[0][0][0]); |
| 208 | glEvalMesh2(type, 0, grid, 0, grid); |
| 209 | if (i < 6) { |
| 210 | glMap2f(GL_MAP2_VERTEX_3, 0, 1, 3, 4, 0, 1, 12, 4, |
| 211 | &r[0][0][0]); |
| 212 | glEvalMesh2(type, 0, grid, 0, grid); |
| 213 | glMap2f(GL_MAP2_VERTEX_3, 0, 1, 3, 4, 0, 1, 12, 4, |
| 214 | &s[0][0][0]); |
| 215 | glEvalMesh2(type, 0, grid, 0, grid); |
| 216 | } |
| 217 | } |
| 218 | glPopAttrib(); |
| 219 | } |
| 220 | |
| 221 | |
| 222 | // Now the OSG wrapper for the above OpenGL code, the most complicated bit is computing |
| 223 | // the bounding box for the above example, normally you'll find this the easy bit. |
| 224 | class Teapot : public osg::Drawable |
| 225 | { |
| 226 | public: |
| 227 | Teapot() {} |
| 228 | |
| 229 | /** Copy constructor using CopyOp to manage deep vs shallow copy.*/ |
| 230 | Teapot(const Teapot& teapot,const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY): |
| 231 | osg::Drawable(teapot,copyop) {} |
| 232 | |
| 233 | META_Object(myTeapotApp,Teapot) |
| 234 | |
| 235 | |
| 236 | // the draw immediate mode method is where the OSG wraps up the drawing of |
| 237 | // of OpenGL primitives. |
| 238 | virtual void drawImplementation(osg::RenderInfo&) const |
| 239 | { |
| 240 | // teapot(..) doesn't use vertex arrays at all so we don't need to toggle their state |
| 241 | // if we did we'd need to something like following call |
| 242 | // state.disableAllVertexArrays(), see src/osg/Geometry.cpp for the low down. |
| 243 | |
| 244 | // just call the OpenGL code. |
| 245 | teapot(14,GL_FILL); |
| 246 | } |
| 247 | |
| 248 | |
| 249 | // we need to set up the bounding box of the data too, so that the scene graph knows where this |
| 250 | // objects is, for both positioning the camera at start up, and most importantly for culling. |
| 251 | virtual osg::BoundingBox computeBoundingBox() const |
| 252 | { |
| 253 | osg::BoundingBox bbox; |
| 254 | |
| 255 | // follow is some truly horrible code required to calculate the |
| 256 | // bounding box of the teapot. Have used the original code above to do |
| 257 | // help compute it. |
| 258 | float p[4][4][3], q[4][4][3], r[4][4][3], s[4][4][3]; |
| 259 | long i, j, k, l; |
| 260 | |
| 261 | for (i = 0; i < 10; i++) { |
| 262 | for (j = 0; j < 4; j++) { |
| 263 | for (k = 0; k < 4; k++) { |
| 264 | |
| 265 | for (l = 0; l < 3; l++) { |
| 266 | p[j][k][l] = cpdata[patchdata[i][j * 4 + k]][l]; |
| 267 | q[j][k][l] = cpdata[patchdata[i][j * 4 + (3 - k)]][l]; |
| 268 | if (l == 1) |
| 269 | q[j][k][l] *= -1.0; |
| 270 | |
| 271 | if (i < 6) { |
| 272 | r[j][k][l] = |
| 273 | cpdata[patchdata[i][j * 4 + (3 - k)]][l]; |
| 274 | if (l == 0) |
| 275 | r[j][k][l] *= -1.0; |
| 276 | s[j][k][l] = cpdata[patchdata[i][j * 4 + k]][l]; |
| 277 | if (l == 0) |
| 278 | s[j][k][l] *= -1.0; |
| 279 | if (l == 1) |
| 280 | s[j][k][l] *= -1.0; |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | bbox.expandBy(osg::Vec3(p[j][k][0],p[j][k][1],p[j][k][2])); |
| 285 | bbox.expandBy(osg::Vec3(q[j][k][0],q[j][k][1],q[j][k][2])); |
| 286 | |
| 287 | if (i < 6) |
| 288 | { |
| 289 | bbox.expandBy(osg::Vec3(r[j][k][0],r[j][k][1],r[j][k][2])); |
| 290 | bbox.expandBy(osg::Vec3(s[j][k][0],s[j][k][1],s[j][k][2])); |
| 291 | } |
| 292 | |
| 293 | |
| 294 | } |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | return bbox; |
| 299 | } |
| 300 | |
| 301 | protected: |
| 302 | |
| 303 | virtual ~Teapot() {} |
| 304 | |
| 305 | }; |
| 306 | |
| 307 | |
| 308 | osg::Geode* createTeapot() |
| 309 | { |
| 310 | osg::Geode* geode = new osg::Geode(); |
| 311 | |
| 312 | // add the teapot to the geode. |
| 313 | geode->addDrawable( new Teapot ); |
| 314 | |
| 315 | // add a reflection map to the teapot. |
| 316 | osg::ref_ptr<osg::Image> image = osgDB::readRefImageFile("Images/reflect.rgb"); |
| 317 | if (image) |
| 318 | { |
| 319 | osg::Texture2D* texture = new osg::Texture2D; |
| 320 | texture->setImage(image); |
| 321 | |
| 322 | osg::TexGen* texgen = new osg::TexGen; |
| 323 | texgen->setMode(osg::TexGen::SPHERE_MAP); |
| 324 | |
| 325 | osg::StateSet* stateset = new osg::StateSet; |
| 326 | stateset->setTextureAttributeAndModes(0,texture,osg::StateAttribute::ON); |
| 327 | stateset->setTextureAttributeAndModes(0,texgen,osg::StateAttribute::ON); |
| 328 | |
| 329 | geode->setStateSet(stateset); |
| 330 | } |
| 331 | |
| 332 | return geode; |
| 333 | } |
| 334 | |
| 335 | int main(int , char **) |
| 336 | { |
| 337 | #if 1 |
| 338 | |
| 339 | // create viewer on heap as a test, this looks to be causing problems |
| 340 | // on init on some platforms, and seg fault on exit when multi-threading on linux. |
| 341 | // Normal stack based version below works fine though... |
| 342 | |
| 343 | // construct the viewer. |
| 344 | osg::ref_ptr<osgViewer::Viewer> viewer = new osgViewer::Viewer; |
| 345 | |
| 346 | // add model to viewer. |
| 347 | viewer->setSceneData( createTeapot() ); |
| 348 | |
| 349 | osgXR::setupViewerDefaults(viewer, "osgteaport", 1); |
| 350 | |
| 351 | return viewer->run(); |
| 352 | |
| 353 | #else |
| 354 | |
| 355 | // construct the viewer. |
| 356 | osgViewer::Viewer viewer; |
| 357 | |
| 358 | // add model to viewer. |
| 359 | viewer.setSceneData( createTeapot() ); |
| 360 | |
| 361 | // create the windows and run the threads. |
| 362 | return viewer.run(); |
| 363 | #endif |
| 364 | |
| 365 | } |