| 82 | | osg::Node* createScene() |
|---|
| 83 | | { |
|---|
| 84 | | // create the Geode (Geometry Node) to contain all our osg::Geometry objects. |
|---|
| 85 | | osg::Geode* geode = new osg::Geode(); |
|---|
| 86 | | |
|---|
| 87 | | // follows are separate blocks for creating POINTS, LINES, LINE_STRIP, LINE_LOOP, POLYGON, QUADS, |
|---|
| 88 | | // QUAD_STRIP, TRIANGLES, TRIANGLE_STRIP and TRIANGLE_FAN primitives. A image of these primitives |
|---|
| 89 | | // are provided in the distribution : OpenSceneGraph-Data/Images/primitives.gif. |
|---|
| 90 | | |
|---|
| 91 | | |
|---|
| 92 | | // create POINTS |
|---|
| 93 | | { |
|---|
| 94 | | // create Geometry object to store all the vertices and points primitive. |
|---|
| 95 | | osg::Geometry* pointsGeom = new osg::Geometry(); |
|---|
| 96 | | |
|---|
| 97 | | // create a Vec3Array and add to it all my coordinates. |
|---|
| 98 | | // Like all the *Array variants (see include/osg/Array) , Vec3Array is derived from both osg::Array |
|---|
| 99 | | // and std::vector<>. osg::Array's are reference counted and hence sharable, |
|---|
| 100 | | // which std::vector<> provides all the convenience, flexibility and robustness |
|---|
| 101 | | // of the most popular of all STL containers. |
|---|
| 102 | | osg::Vec3Array* vertices = new osg::Vec3Array; |
|---|
| 103 | | vertices->push_back(osg::Vec3(-1.02168, -2.15188e-09, 0.885735)); |
|---|
| 104 | | vertices->push_back(osg::Vec3(-0.976368, -2.15188e-09, 0.832179)); |
|---|
| 105 | | vertices->push_back(osg::Vec3(-0.873376, 9.18133e-09, 0.832179)); |
|---|
| 106 | | vertices->push_back(osg::Vec3(-0.836299, -2.15188e-09, 0.885735)); |
|---|
| 107 | | vertices->push_back(osg::Vec3(-0.790982, 9.18133e-09, 0.959889)); |
|---|
| 108 | | |
|---|
| 109 | | // pass the created vertex array to the points geometry object. |
|---|
| 110 | | pointsGeom->setVertexArray(vertices); |
|---|
| 111 | | |
|---|
| 112 | | |
|---|
| 113 | | |
|---|
| 114 | | // create the color of the geometry, one single for the whole geometry. |
|---|
| 115 | | // for consistency of design even one single color must added as an element |
|---|
| 116 | | // in a color array. |
|---|
| 117 | | osg::Vec4Array* colors = new osg::Vec4Array; |
|---|
| 118 | | // add a white color, colors take the form r,g,b,a with 0.0 off, 1.0 full on. |
|---|
| 119 | | colors->push_back(osg::Vec4(1.0f,1.0f,0.0f,1.0f)); |
|---|
| 120 | | |
|---|
| 121 | | // pass the color array to points geometry, note the binding to tell the geometry |
|---|
| 122 | | // that only use one color for the whole object. |
|---|
| 123 | | pointsGeom->setColorArray(colors); |
|---|
| 124 | | pointsGeom->setColorBinding(osg::Geometry::BIND_OVERALL); |
|---|
| 125 | | |
|---|
| 126 | | |
|---|
| 127 | | // set the normal in the same way color. |
|---|
| 128 | | osg::Vec3Array* normals = new osg::Vec3Array; |
|---|
| 129 | | normals->push_back(osg::Vec3(0.0f,-1.0f,0.0f)); |
|---|
| 130 | | pointsGeom->setNormalArray(normals); |
|---|
| 131 | | pointsGeom->setNormalBinding(osg::Geometry::BIND_OVERALL); |
|---|
| 132 | | |
|---|
| 133 | | |
|---|
| 134 | | // create and add a DrawArray Primitive (see include/osg/Primitive). The first |
|---|
| 135 | | // parameter passed to the DrawArrays constructor is the Primitive::Mode which |
|---|
| 136 | | // in this case is POINTS (which has the same value GL_POINTS), the second |
|---|
| 137 | | // parameter is the index position into the vertex array of the first point |
|---|
| 138 | | // to draw, and the third parameter is the number of points to draw. |
|---|
| 139 | | pointsGeom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::POINTS,0,vertices->size())); |
|---|
| 140 | | |
|---|
| 141 | | |
|---|
| 142 | | // add the points geometry to the geode. |
|---|
| 143 | | geode->addDrawable(pointsGeom); |
|---|
| 144 | | } |
|---|
| 145 | | |
|---|
| 146 | | // create LINES |
|---|
| 147 | | { |
|---|
| 148 | | // create Geometry object to store all the vertices and lines primitive. |
|---|
| 149 | | osg::Geometry* linesGeom = new osg::Geometry(); |
|---|
| 150 | | |
|---|
| 151 | | // this time we'll preallocate the vertex array to the size we |
|---|
| 152 | | // need and then simple set them as array elements, 8 points |
|---|
| 153 | | // makes 4 line segments. |
|---|
| 154 | | osg::Vec3Array* vertices = new osg::Vec3Array(8); |
|---|
| 155 | | (*vertices)[0].set(-1.13704, -2.15188e-09, 0.40373); |
|---|
| 156 | | (*vertices)[1].set(-0.856897, -2.15188e-09, 0.531441); |
|---|
| 157 | | (*vertices)[2].set(-0.889855, -2.15188e-09, 0.444927); |
|---|
| 158 | | (*vertices)[3].set(-0.568518, -2.15188e-09, 0.40373); |
|---|
| 159 | | (*vertices)[4].set(-1.00933, -2.15188e-09, 0.370773); |
|---|
| 160 | | (*vertices)[5].set(-0.716827, -2.15188e-09, 0.292498); |
|---|
| 161 | | (*vertices)[6].set(-1.07936, 9.18133e-09, 0.317217); |
|---|
| 162 | | (*vertices)[7].set(-0.700348, 9.18133e-09, 0.362533); |
|---|
| 163 | | |
|---|
| 164 | | |
|---|
| 165 | | // pass the created vertex array to the points geometry object. |
|---|
| 166 | | linesGeom->setVertexArray(vertices); |
|---|
| 167 | | |
|---|
| 168 | | // set the colors as before, plus using the above |
|---|
| 169 | | osg::Vec4Array* colors = new osg::Vec4Array; |
|---|
| 170 | | colors->push_back(osg::Vec4(1.0f,1.0f,0.0f,1.0f)); |
|---|
| 171 | | linesGeom->setColorArray(colors); |
|---|
| 172 | | linesGeom->setColorBinding(osg::Geometry::BIND_OVERALL); |
|---|
| 173 | | |
|---|
| 174 | | |
|---|
| 175 | | // set the normal in the same way color. |
|---|
| 176 | | osg::Vec3Array* normals = new osg::Vec3Array; |
|---|
| 177 | | normals->push_back(osg::Vec3(0.0f,-1.0f,0.0f)); |
|---|
| 178 | | linesGeom->setNormalArray(normals); |
|---|
| 179 | | linesGeom->setNormalBinding(osg::Geometry::BIND_OVERALL); |
|---|
| 180 | | |
|---|
| 181 | | |
|---|
| 182 | | // This time we simply use primitive, and hardwire the number of coords to use |
|---|
| 183 | | // since we know up front, |
|---|
| 184 | | linesGeom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::LINES,0,8)); |
|---|
| 185 | | |
|---|
| 186 | | |
|---|
| 187 | | // add the points geometry to the geode. |
|---|
| 188 | | geode->addDrawable(linesGeom); |
|---|
| 189 | | } |
|---|
| 190 | | |
|---|
| 191 | | // create LINE_STRIP |
|---|
| 192 | | { |
|---|
| 193 | | // create Geometry object to store all the vertices and lines primitive. |
|---|
| 194 | | osg::Geometry* linesGeom = new osg::Geometry(); |
|---|
| 195 | | |
|---|
| 196 | | // this time we'll preallocate the vertex array to the size |
|---|
| 197 | | // and then use an iterator to fill in the values, a bit perverse |
|---|
| 198 | | // but does demonstrate that we have just a standard std::vector underneath. |
|---|
| 199 | | osg::Vec3Array* vertices = new osg::Vec3Array(5); |
|---|
| 200 | | osg::Vec3Array::iterator vitr = vertices->begin(); |
|---|
| 201 | | (vitr++)->set(-0.0741545, -2.15188e-09, 0.416089); |
|---|
| 202 | | (vitr++)->set(0.234823, -2.15188e-09, 0.259541); |
|---|
| 203 | | (vitr++)->set(0.164788, -2.15188e-09, 0.366653); |
|---|
| 204 | | (vitr++)->set(-0.0288379, -2.15188e-09, 0.333695); |
|---|
| 205 | | (vitr++)->set(-0.0453167, -2.15188e-09, 0.280139); |
|---|
| 206 | | |
|---|
| 207 | | // pass the created vertex array to the points geometry object. |
|---|
| 208 | | linesGeom->setVertexArray(vertices); |
|---|
| 209 | | |
|---|
| 210 | | // set the colors as before, plus using the above |
|---|
| 211 | | osg::Vec4Array* colors = new osg::Vec4Array; |
|---|
| 212 | | colors->push_back(osg::Vec4(1.0f,1.0f,0.0f,1.0f)); |
|---|
| 213 | | linesGeom->setColorArray(colors); |
|---|
| 214 | | linesGeom->setColorBinding(osg::Geometry::BIND_OVERALL); |
|---|
| 215 | | |
|---|
| 216 | | |
|---|
| 217 | | // set the normal in the same way color. |
|---|
| 218 | | osg::Vec3Array* normals = new osg::Vec3Array; |
|---|
| 219 | | normals->push_back(osg::Vec3(0.0f,-1.0f,0.0f)); |
|---|
| 220 | | linesGeom->setNormalArray(normals); |
|---|
| 221 | | linesGeom->setNormalBinding(osg::Geometry::BIND_OVERALL); |
|---|
| 222 | | |
|---|
| 223 | | |
|---|
| 224 | | // This time we simply use primitive, and hardwire the number of coords to use |
|---|
| 225 | | // since we know up front, |
|---|
| 226 | | linesGeom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::LINE_STRIP,0,5)); |
|---|
| 227 | | |
|---|
| 228 | | |
|---|
| 229 | | // add the points geometry to the geode. |
|---|
| 230 | | geode->addDrawable(linesGeom); |
|---|
| 231 | | } |
|---|
| 232 | | |
|---|
| 233 | | // create LINE_LOOP |
|---|
| 234 | | { |
|---|
| 235 | | // create Geometry object to store all the vertices and lines primitive. |
|---|
| 236 | | osg::Geometry* linesGeom = new osg::Geometry(); |
|---|
| 237 | | |
|---|
| 238 | | // this time we'll a C arrays to initialize the vertices. |
|---|
| 239 | | |
|---|
| 240 | | osg::Vec3 myCoords[] = |
|---|
| 241 | | { |
|---|
| 242 | | osg::Vec3(0.741546, -2.15188e-09, 0.453167), |
|---|
| 243 | | osg::Vec3(0.840418, -2.15188e-09, 0.304858), |
|---|
| 244 | | osg::Vec3(1.12468, -2.15188e-09, 0.300738), |
|---|
| 245 | | osg::Vec3(1.03816, 9.18133e-09, 0.453167), |
|---|
| 246 | | osg::Vec3(0.968129, -2.15188e-09, 0.337815), |
|---|
| 247 | | osg::Vec3(0.869256, -2.15188e-09, 0.531441) |
|---|
| 248 | | }; |
|---|
| 249 | | |
|---|
| 250 | | int numCoords = sizeof(myCoords)/sizeof(osg::Vec3); |
|---|
| 251 | | |
|---|
| 252 | | osg::Vec3Array* vertices = new osg::Vec3Array(numCoords,myCoords); |
|---|
| 253 | | |
|---|
| 254 | | // pass the created vertex array to the points geometry object. |
|---|
| 255 | | linesGeom->setVertexArray(vertices); |
|---|
| 256 | | |
|---|
| 257 | | // set the colors as before, plus using the above |
|---|
| 258 | | osg::Vec4Array* colors = new osg::Vec4Array; |
|---|
| 259 | | colors->push_back(osg::Vec4(1.0f,1.0f,0.0f,1.0f)); |
|---|
| 260 | | linesGeom->setColorArray(colors); |
|---|
| 261 | | linesGeom->setColorBinding(osg::Geometry::BIND_OVERALL); |
|---|
| 262 | | |
|---|
| 263 | | |
|---|
| 264 | | // set the normal in the same way color. |
|---|
| 265 | | osg::Vec3Array* normals = new osg::Vec3Array; |
|---|
| 266 | | normals->push_back(osg::Vec3(0.0f,-1.0f,0.0f)); |
|---|
| 267 | | linesGeom->setNormalArray(normals); |
|---|
| 268 | | linesGeom->setNormalBinding(osg::Geometry::BIND_OVERALL); |
|---|
| 269 | | |
|---|
| 270 | | |
|---|
| 271 | | // This time we simply use primitive, and hardwire the number of coords to use |
|---|
| 272 | | // since we know up front, |
|---|
| 273 | | linesGeom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::LINE_LOOP,0,numCoords)); |
|---|
| 274 | | |
|---|
| 275 | | |
|---|
| 276 | | // add the points geometry to the geode. |
|---|
| 277 | | geode->addDrawable(linesGeom); |
|---|
| 278 | | } |
|---|
| 279 | | |
|---|
| 280 | | |
|---|
| 281 | | |
|---|
| 282 | | |
|---|
| 283 | | // now we'll stop creating separate normal and color arrays |
|---|
| 284 | | // since we are using the same values all the time, we'll just |
|---|
| 285 | | // share the same ColorArray and NormalArrays.. |
|---|
| 286 | | |
|---|
| 287 | | // set the colors as before, use a ref_ptr rather than just |
|---|
| 288 | | // standard C pointer, as that in the case of it not being |
|---|
| 289 | | // assigned it will still be cleaned up automatically. |
|---|
| 290 | | osg::ref_ptr<osg::Vec4Array> shared_colors = new osg::Vec4Array; |
|---|
| 291 | | shared_colors->push_back(osg::Vec4(1.0f,1.0f,0.0f,1.0f)); |
|---|
| 292 | | |
|---|
| 293 | | // same trick for shared normal. |
|---|
| 294 | | osg::ref_ptr<osg::Vec3Array> shared_normals = new osg::Vec3Array; |
|---|
| 295 | | shared_normals->push_back(osg::Vec3(0.0f,-1.0f,0.0f)); |
|---|
| 296 | | |
|---|
| 297 | | |
|---|
| 298 | | |
|---|
| 299 | | // Note on vertex ordering. |
|---|
| 300 | | // While the OpenGL diagram should vertices specified in a clockwise direction |
|---|
| 301 | | // in reality you need to specify coords for polygons into a anticlockwise direction |
|---|
| 302 | | // for their front face to be pointing towards your, get this wrong and you could |
|---|
| 303 | | // find back face culling removing the wrong faces of your models. The OpenGL diagram |
|---|
| 304 | | // is just plain wrong, but its nice diagram so we'll keep it for now! |
|---|
| 305 | | |
|---|
| 306 | | // create POLYGON |
|---|
| 307 | | { |
|---|
| 308 | | // create Geometry object to store all the vertices and lines primitive. |
|---|
| 309 | | osg::Geometry* polyGeom = new osg::Geometry(); |
|---|
| 310 | | |
|---|
| 311 | | // this time we'll a C arrays to initialize the vertices. |
|---|
| 312 | | // note, anticlockwise ordering. |
|---|
| 313 | | // note II, OpenGL polygons must be convex plan polygons, otherwise |
|---|
| 314 | | // undefined results will occur. If you have concave polygons or ones |
|---|
| 315 | | // that cross over themselves then use the osgUtil::Tessellator to fix |
|---|
| 316 | | // the polygons into a set of valid polygons. |
|---|
| 317 | | osg::Vec3 myCoords[] = |
|---|
| 318 | | { |
|---|
| 319 | | osg::Vec3(-1.0464, 0.0f, -0.193626), |
|---|
| 320 | | osg::Vec3(-1.0258, 0.0f, -0.26778), |
|---|
| 321 | | osg::Vec3(-0.807461, 0.0f, -0.181267), |
|---|
| 322 | | osg::Vec3(-0.766264, 0.0f, -0.0576758), |
|---|
| 323 | | osg::Vec3(-0.980488, 0.0f, -0.094753) |
|---|
| 324 | | }; |
|---|
| 325 | | |
|---|
| 326 | | int numCoords = sizeof(myCoords)/sizeof(osg::Vec3); |
|---|
| 327 | | |
|---|
| 328 | | osg::Vec3Array* vertices = new osg::Vec3Array(numCoords,myCoords); |
|---|
| 329 | | |
|---|
| 330 | | // pass the created vertex array to the points geometry object. |
|---|
| 331 | | polyGeom->setVertexArray(vertices); |
|---|
| 332 | | |
|---|
| 333 | | // use the shared color array. |
|---|
| 334 | | polyGeom->setColorArray(shared_colors.get()); |
|---|
| 335 | | polyGeom->setColorBinding(osg::Geometry::BIND_OVERALL); |
|---|
| 336 | | |
|---|
| 337 | | |
|---|
| 338 | | // use the shared normal array. |
|---|
| 339 | | polyGeom->setNormalArray(shared_normals.get()); |
|---|
| 340 | | polyGeom->setNormalBinding(osg::Geometry::BIND_OVERALL); |
|---|
| 341 | | |
|---|
| 342 | | |
|---|
| 343 | | // This time we simply use primitive, and hardwire the number of coords to use |
|---|
| 344 | | // since we know up front, |
|---|
| 345 | | polyGeom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::POLYGON,0,numCoords)); |
|---|
| 346 | | |
|---|
| 347 | | printTriangles("Polygon",*polyGeom); |
|---|
| 348 | | |
|---|
| 349 | | // add the points geometry to the geode. |
|---|
| 350 | | geode->addDrawable(polyGeom); |
|---|
| 351 | | } |
|---|
| 352 | | |
|---|
| 353 | | |
|---|
| 354 | | // create QUADS |
|---|
| 355 | | { |
|---|
| 356 | | // create Geometry object to store all the vertices and lines primitive. |
|---|
| 357 | | osg::Geometry* polyGeom = new osg::Geometry(); |
|---|
| 358 | | |
|---|
| 359 | | // note, anticlockwise ordering. |
|---|
| 360 | | osg::Vec3 myCoords[] = |
|---|
| 361 | | { |
|---|
| 362 | | osg::Vec3(0.0247182, 0.0f, -0.156548), |
|---|
| 363 | | osg::Vec3(0.0247182, 0.0f, -0.00823939), |
|---|
| 364 | | osg::Vec3(-0.160668, 0.0f, -0.0453167), |
|---|
| 365 | | osg::Vec3(-0.222464, 0.0f, -0.13183), |
|---|
| 366 | | |
|---|
| 367 | | osg::Vec3(0.238942, 0.0f, -0.251302), |
|---|
| 368 | | osg::Vec3(0.333696, 0.0f, 0.0329576), |
|---|
| 369 | | osg::Vec3(0.164788, 0.0f, -0.0453167), |
|---|
| 370 | | osg::Vec3(0.13595, 0.0f, -0.255421) |
|---|
| 371 | | }; |
|---|
| 372 | | |
|---|
| 373 | | int numCoords = sizeof(myCoords)/sizeof(osg::Vec3); |
|---|
| 374 | | |
|---|
| 375 | | osg::Vec3Array* vertices = new osg::Vec3Array(numCoords,myCoords); |
|---|
| 376 | | |
|---|
| 377 | | // pass the created vertex array to the points geometry object. |
|---|
| 378 | | polyGeom->setVertexArray(vertices); |
|---|
| 379 | | |
|---|
| 380 | | // use the shared color array. |
|---|
| 381 | | polyGeom->setColorArray(shared_colors.get()); |
|---|
| 382 | | polyGeom->setColorBinding(osg::Geometry::BIND_OVERALL); |
|---|
| 383 | | |
|---|
| 384 | | |
|---|
| 385 | | // use the shared normal array. |
|---|
| 386 | | polyGeom->setNormalArray(shared_normals.get()); |
|---|
| 387 | | polyGeom->setNormalBinding(osg::Geometry::BIND_OVERALL); |
|---|
| 388 | | |
|---|
| 389 | | |
|---|
| 390 | | // This time we simply use primitive, and hardwire the number of coords to use |
|---|
| 391 | | // since we know up front, |
|---|
| 392 | | polyGeom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::QUADS,0,numCoords)); |
|---|
| 393 | | |
|---|
| 394 | | |
|---|
| 395 | | printTriangles("Quads",*polyGeom); |
|---|
| 396 | | |
|---|
| 397 | | // add the points geometry to the geode. |
|---|
| 398 | | geode->addDrawable(polyGeom); |
|---|
| 399 | | } |
|---|
| 400 | | |
|---|
| 401 | | // create QUAD_STRIP |
|---|
| 402 | | { |
|---|
| 403 | | // create Geometry object to store all the vertices and lines primitive. |
|---|
| 404 | | osg::Geometry* polyGeom = new osg::Geometry(); |
|---|
| 405 | | |
|---|
| 406 | | // note, first coord at top, second at bottom, reverse to that buggy OpenGL image.. |
|---|
| 407 | | osg::Vec3 myCoords[] = |
|---|
| 408 | | { |
|---|
| 409 | | osg::Vec3(0.733306, -2.15188e-09, -0.0741545), |
|---|
| 410 | | osg::Vec3(0.758024, -2.15188e-09, -0.205985), |
|---|
| 411 | | |
|---|
| 412 | | osg::Vec3(0.885735, -2.15188e-09, -0.0576757), |
|---|
| 413 | | osg::Vec3(0.885735, -2.15188e-09, -0.214224), |
|---|
| 414 | | |
|---|
| 415 | | osg::Vec3(0.964009, 9.18133e-09, -0.0370773), |
|---|
| 416 | | osg::Vec3(1.0464, 9.18133e-09, -0.173027), |
|---|
| 417 | | |
|---|
| 418 | | osg::Vec3(1.11232, -2.15188e-09, 0.0123591), |
|---|
| 419 | | osg::Vec3(1.12468, 9.18133e-09, -0.164788), |
|---|
| 420 | | }; |
|---|
| 421 | | |
|---|
| 422 | | int numCoords = sizeof(myCoords)/sizeof(osg::Vec3); |
|---|
| 423 | | |
|---|
| 424 | | osg::Vec3Array* vertices = new osg::Vec3Array(numCoords,myCoords); |
|---|
| 425 | | |
|---|
| 426 | | // pass the created vertex array to the points geometry object. |
|---|
| 427 | | polyGeom->setVertexArray(vertices); |
|---|
| 428 | | |
|---|
| 429 | | // use the shared color array. |
|---|
| 430 | | polyGeom->setColorArray(shared_colors.get()); |
|---|
| 431 | | polyGeom->setColorBinding(osg::Geometry::BIND_OVERALL); |
|---|
| 432 | | |
|---|
| 433 | | |
|---|
| 434 | | // use the shared normal array. |
|---|
| 435 | | polyGeom->setNormalArray(shared_normals.get()); |
|---|
| 436 | | polyGeom->setNormalBinding(osg::Geometry::BIND_OVERALL); |
|---|
| 437 | | |
|---|
| 438 | | |
|---|
| 439 | | // This time we simply use primitive, and hardwire the number of coords to use |
|---|
| 440 | | // since we know up front, |
|---|
| 441 | | polyGeom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::QUAD_STRIP,0,numCoords)); |
|---|
| 442 | | |
|---|
| 443 | | |
|---|
| 444 | | printTriangles("Quads strip",*polyGeom); |
|---|
| 445 | | |
|---|
| 446 | | // add the points geometry to the geode. |
|---|
| 447 | | geode->addDrawable(polyGeom); |
|---|
| 448 | | } |
|---|
| 449 | | |
|---|
| 450 | | // create TRIANGLES, TRIANGLE_STRIP and TRIANGLE_FAN all in one Geometry/ |
|---|
| 451 | | { |
|---|
| 452 | | // create Geometry object to store all the vertices and lines primitive. |
|---|
| 453 | | osg::Geometry* polyGeom = new osg::Geometry(); |
|---|
| 454 | | |
|---|
| 455 | | // note, first coord at top, second at bottom, reverse to that buggy OpenGL image.. |
|---|
| 456 | | osg::Vec3 myCoords[] = |
|---|
| 457 | | { |
|---|
| 458 | | // TRIANGLES 6 vertices, v0..v5 |
|---|
| 459 | | // note in anticlockwise order. |
|---|
| 460 | | osg::Vec3(-1.12056, -2.15188e-09, -0.840418), |
|---|
| 461 | | osg::Vec3(-0.95165, -2.15188e-09, -0.840418), |
|---|
| 462 | | osg::Vec3(-1.11644, 9.18133e-09, -0.716827), |
|---|
| 463 | | |
|---|
| 464 | | // note in anticlockwise order. |
|---|
| 465 | | osg::Vec3(-0.840418, 9.18133e-09, -0.778623), |
|---|
| 466 | | osg::Vec3(-0.622074, 9.18133e-09, -0.613835), |
|---|
| 467 | | osg::Vec3(-1.067, 9.18133e-09, -0.609715), |
|---|
| 468 | | |
|---|
| 469 | | // TRIANGLE STRIP 6 vertices, v6..v11 |
|---|
| 470 | | // note defined top point first, |
|---|
| 471 | | // then anticlockwise for the next two points, |
|---|
| 472 | | // then alternating to bottom there after. |
|---|
| 473 | | osg::Vec3(-0.160668, -2.15188e-09, -0.531441), |
|---|
| 474 | | osg::Vec3(-0.160668, -2.15188e-09, -0.749785), |
|---|
| 475 | | osg::Vec3(0.0617955, 9.18133e-09, -0.531441), |
|---|
| 476 | | osg::Vec3(0.168908, -2.15188e-09, -0.753905), |
|---|
| 477 | | osg::Vec3(0.238942, -2.15188e-09, -0.531441), |
|---|
| 478 | | osg::Vec3(0.280139, -2.15188e-09, -0.823939), |
|---|
| 479 | | |
|---|
| 480 | | // TRIANGLE FAN 5 vertices, v12..v16 |
|---|
| 481 | | // note defined in anticlockwise order. |
|---|
| 482 | | osg::Vec3(0.844538, 9.18133e-09, -0.712708), |
|---|
| 483 | | osg::Vec3(1.0258, 9.18133e-09, -0.799221), |
|---|
| 484 | | osg::Vec3(1.03816, -2.15188e-09, -0.692109), |
|---|
| 485 | | osg::Vec3(0.988727, 9.18133e-09, -0.568518), |
|---|
| 486 | | osg::Vec3(0.840418, -2.15188e-09, -0.506723), |
|---|
| 487 | | |
|---|
| 488 | | }; |
|---|
| 489 | | |
|---|
| 490 | | int numCoords = sizeof(myCoords)/sizeof(osg::Vec3); |
|---|
| 491 | | |
|---|
| 492 | | osg::Vec3Array* vertices = new osg::Vec3Array(numCoords,myCoords); |
|---|
| 493 | | |
|---|
| 494 | | // pass the created vertex array to the points geometry object. |
|---|
| 495 | | polyGeom->setVertexArray(vertices); |
|---|
| 496 | | |
|---|
| 497 | | // use the shared color array. |
|---|
| 498 | | polyGeom->setColorArray(shared_colors.get()); |
|---|
| 499 | | polyGeom->setColorBinding(osg::Geometry::BIND_OVERALL); |
|---|
| 500 | | |
|---|
| 501 | | |
|---|
| 502 | | // use the shared normal array. |
|---|
| 503 | | polyGeom->setNormalArray(shared_normals.get()); |
|---|
| 504 | | polyGeom->setNormalBinding(osg::Geometry::BIND_OVERALL); |
|---|
| 505 | | |
|---|
| 506 | | |
|---|
| 507 | | // This time we simply use primitive, and hardwire the number of coords to use |
|---|
| 508 | | // since we know up front, |
|---|
| 509 | | polyGeom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::TRIANGLES,0,6)); |
|---|
| 510 | | polyGeom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::TRIANGLE_STRIP,6,6)); |
|---|
| 511 | | polyGeom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::TRIANGLE_FAN,12,5)); |
|---|
| 512 | | |
|---|
| 513 | | // polygon stipple |
|---|
| 514 | | osg::StateSet* stateSet = new osg::StateSet(); |
|---|
| 515 | | polyGeom->setStateSet(stateSet); |
|---|
| 516 | | osg::PolygonStipple* polygonStipple = new osg::PolygonStipple; |
|---|
| 517 | | stateSet->setAttributeAndModes(polygonStipple,osg::StateAttribute::OVERRIDE|osg::StateAttribute::ON); |
|---|
| 518 | | |
|---|
| 519 | | printTriangles("Triangles/Strip/Fan",*polyGeom); |
|---|
| 520 | | |
|---|
| 521 | | // add the points geometry to the geode. |
|---|
| 522 | | geode->addDrawable(polyGeom); |
|---|
| 523 | | } |
|---|
| 524 | | |
|---|
| 525 | | return geode; |
|---|
| 526 | | } |
|---|
| 527 | | |
|---|
| 528 | | |
|---|
| 529 | | // define a node callback to animation a transform as a cycle along the y axis, between 0 and 2.0. |
|---|
| 530 | | class MyTransformCallback : public osg::NodeCallback |
|---|
| 531 | | { |
|---|
| 532 | | |
|---|
| 533 | | public: |
|---|
| 534 | | |
|---|
| 535 | | MyTransformCallback(float angularVelocity) |
|---|
| 536 | | { |
|---|
| 537 | | _angular_velocity = angularVelocity; |
|---|
| 538 | | } |
|---|
| 539 | | |
|---|
| 540 | | virtual void operator() (osg::Node* node, osg::NodeVisitor* nv) |
|---|
| 541 | | { |
|---|
| 542 | | osg::MatrixTransform* transform = dynamic_cast<osg::MatrixTransform*>(node); |
|---|
| 543 | | if (nv && transform && nv->getFrameStamp()) |
|---|
| 544 | | { |
|---|
| 545 | | double time = nv->getFrameStamp()->getSimulationTime(); |
|---|
| 546 | | transform->setMatrix(osg::Matrix::translate(0.0f,1.0f+cosf(time*_angular_velocity),0.0f)); |
|---|
| 547 | | } |
|---|
| 548 | | |
|---|
| 549 | | // must continue subgraph traversal. |
|---|
| 550 | | traverse(node,nv); |
|---|
| 551 | | |
|---|
| 552 | | } |
|---|
| 553 | | |
|---|
| 554 | | protected: |
|---|
| 555 | | |
|---|
| 556 | | float _angular_velocity; |
|---|
| 557 | | |
|---|
| 558 | | }; |
|---|
| 559 | | |
|---|
| 560 | | |
|---|
| 561 | | osg::Node* createBackground() |
|---|
| 562 | | { |
|---|
| 563 | | |
|---|
| 564 | | // we'll create a texture mapped quad to sit behind the Geometry |
|---|
| 565 | | osg::Image* image = osgDB::readImageFile("Images/primitives.gif"); |
|---|
| 566 | | if (!image) return NULL; |
|---|
| 567 | | |
|---|
| 568 | | |
|---|
| 569 | | // create Geometry object to store all the vertices and lines primitive. |
|---|
| 570 | | osg::Geometry* polyGeom = new osg::Geometry(); |
|---|
| 571 | | |
|---|
| 572 | | // note, anticlockwise ordering. |
|---|
| 573 | | osg::Vec3 myCoords[] = |
|---|
| 574 | | { |
|---|
| 575 | | osg::Vec3(-1.22908f,0.0f,1.0f), |
|---|
| 576 | | osg::Vec3(-1.22908f,0.0f,-1.0f), |
|---|
| 577 | | osg::Vec3(1.22908f,0.0f,-1.0f), |
|---|
| 578 | | osg::Vec3(1.22908f,0.0f,1.0f) |
|---|
| 579 | | }; |
|---|
| 580 | | |
|---|
| 581 | | int numCoords = sizeof(myCoords)/sizeof(osg::Vec3); |
|---|
| 582 | | |
|---|
| 583 | | // pass the created vertex array to the points geometry object. |
|---|
| 584 | | polyGeom->setVertexArray(new osg::Vec3Array(numCoords,myCoords)); |
|---|
| 585 | | |
|---|
| 586 | | osg::Vec4Array* colors = new osg::Vec4Array; |
|---|
| 587 | | colors->push_back(osg::Vec4(1.0f,1.0f,1.0f,1.0f)); |
|---|
| 588 | | polyGeom->setColorArray(colors); |
|---|
| 589 | | polyGeom->setColorBinding(osg::Geometry::BIND_OVERALL); |
|---|
| 590 | | |
|---|
| 591 | | |
|---|
| 592 | | // set the normal in the same way color. |
|---|
| 593 | | osg::Vec3Array* normals = new osg::Vec3Array; |
|---|
| 594 | | normals->push_back(osg::Vec3(0.0f,-1.0f,0.0f)); |
|---|
| 595 | | polyGeom->setNormalArray(normals); |
|---|
| 596 | | polyGeom->setNormalBinding(osg::Geometry::BIND_OVERALL); |
|---|
| 597 | | |
|---|
| 598 | | osg::Vec2 myTexCoords[] = |
|---|
| 599 | | { |
|---|
| 600 | | osg::Vec2(0,1), |
|---|
| 601 | | osg::Vec2(0,0), |
|---|
| 602 | | osg::Vec2(1,0), |
|---|
| 603 | | osg::Vec2(1,1) |
|---|
| 604 | | }; |
|---|
| 605 | | |
|---|
| 606 | | int numTexCoords = sizeof(myTexCoords)/sizeof(osg::Vec2); |
|---|
| 607 | | |
|---|
| 608 | | // pass the created tex coord array to the points geometry object, |
|---|
| 609 | | // and use it to set texture unit 0. |
|---|
| 610 | | polyGeom->setTexCoordArray(0,new osg::Vec2Array(numTexCoords,myTexCoords)); |
|---|
| 611 | | |
|---|
| 612 | | // well use indices and DrawElements to define the primitive this time. |
|---|
| 613 | | unsigned short myIndices[] = |
|---|
| 614 | | { |
|---|
| 615 | | 0, |
|---|
| 616 | | 1, |
|---|
| 617 | | 2, |
|---|
| 618 | | 3 |
|---|
| 619 | | }; |
|---|
| 620 | | |
|---|
| 621 | | int numIndices = sizeof(myIndices)/sizeof(unsigned short); |
|---|
| 622 | | |
|---|
| 623 | | // There are three variants of the DrawElements osg::Primitive, UByteDrawElements which |
|---|
| 624 | | // contains unsigned char indices, UShortDrawElements which contains unsigned short indices, |
|---|
| 625 | | // and UIntDrawElements which contains ... unsigned int indices. |
|---|
| 626 | | // The first parameter to DrawElements is |
|---|
| 627 | | polyGeom->addPrimitiveSet(new osg::DrawElementsUShort(osg::PrimitiveSet::QUADS,numIndices,myIndices)); |
|---|
| 628 | | |
|---|
| 629 | | // new we need to add the texture to the Drawable, we do so by creating a |
|---|
| 630 | | // StateSet to contain the Texture2D StateAttribute. |
|---|
| 631 | | osg::StateSet* stateset = new osg::StateSet; |
|---|
| 632 | | |
|---|
| 633 | | // set up the texture. |
|---|
| 634 | | osg::Texture2D* texture = new osg::Texture2D; |
|---|
| 635 | | texture->setImage(image); |
|---|
| 636 | | |
|---|
| 637 | | stateset->setTextureAttributeAndModes(0, texture,osg::StateAttribute::ON); |
|---|
| 638 | | |
|---|
| 639 | | polyGeom->setStateSet(stateset); |
|---|
| 640 | | |
|---|
| 641 | | |
|---|
| 642 | | // create the Geode (Geometry Node) to contain all our osg::Geometry objects. |
|---|
| 643 | | osg::Geode* geode = new osg::Geode(); |
|---|
| 644 | | |
|---|
| 645 | | // add the points geometry to the geode. |
|---|
| 646 | | geode->addDrawable(polyGeom); |
|---|
| 647 | | |
|---|
| 648 | | //return geode; |
|---|
| 649 | | |
|---|
| 650 | | // create a transform to move the background back and forward with. |
|---|
| 651 | | |
|---|
| 652 | | osg::MatrixTransform* transform = new osg::MatrixTransform(); |
|---|
| 653 | | transform->setUpdateCallback(new MyTransformCallback(1.0f)); |
|---|
| 654 | | transform->addChild(geode); |
|---|
| 655 | | |
|---|
| 656 | | return transform; |
|---|
| 657 | | } |
|---|
| 658 | | |
|---|