| 63 | } |
| 64 | |
| 65 | static void generateCube(osg::Geometry& geom, float dim) |
| 66 | { |
| 67 | osg::ref_ptr<osg::Vec3Array> vertices = new osg::Vec3Array; |
| 68 | osg::ref_ptr<osg::Vec3Array> normals = new osg::Vec3Array; |
| 69 | osg::ref_ptr<osg::DrawElementsUShort> indices = new osg::DrawElementsUShort(osg::DrawElementsUShort::TRIANGLES, 0); |
| 70 | |
| 71 | for (GLushort iFace = 0; iFace < 6; iFace++) |
| 72 | { |
| 73 | osg::Vec3f normale(0., 0., 0.); |
| 74 | osg::Vec3f u(0., 0., 0.); |
| 75 | osg::Vec3f v(0., 0., 0.); |
| 76 | GLushort axis = iFace / 2; |
| 77 | float floatDir = iFace % 2 == 0 ? -1.f : 1.f; |
| 78 | normale[axis] = floatDir; |
| 79 | u[(axis + 1) % 3] = 1.0; |
| 80 | v[(axis + 2) % 3] = 1.0; |
| 81 | |
| 82 | for (int iPoint = 0; iPoint < 4; iPoint++) |
| 83 | { |
| 84 | float iu = iPoint % 2 == 1 |
| 85 | ? floatDir |
| 86 | : -floatDir; // This is to get the right triangle orientation when the normal changes* |
| 87 | float iv = iPoint / 2 == 1 ? 1.f : -1.f; |
| 88 | osg::Vec3f point = (u * iu) + (v * iv); |
| 89 | point = (point + normale); |
| 90 | point = point * (dim * 0.5f); |
| 91 | vertices->push_back(point); |
| 92 | normals->push_back(normale); |
| 93 | } |
| 94 | GLushort startVertex(iFace * 4); |
| 95 | GLushort newFace1[] |
| 96 | = { startVertex, static_cast<GLushort>(startVertex + 1u), static_cast<GLushort>(startVertex + 2u) }; |
| 97 | for (int i = 0; i < 3; i++) |
| 98 | { |
| 99 | indices->push_back(newFace1[i]); |
| 100 | } |
| 101 | GLushort newFace2[] = { static_cast<GLushort>(startVertex + 2u), static_cast<GLushort>(startVertex + 1u), |
| 102 | static_cast<GLushort>(startVertex + 3u) }; |
| 103 | for (int i = 0; i < 3; i++) |
| 104 | { |
| 105 | indices->push_back(newFace2[i]); |
| 106 | } |
| 107 | } |
| 108 | geom.setVertexArray(vertices); |
| 109 | geom.setNormalArray(normals, osg::Array::BIND_PER_VERTEX); |
| 110 | geom.addPrimitiveSet(indices); |
| 111 | } |
| 112 | |
| 113 | static void generateCylinder(osg::Geometry& geom, float radius, float height, int subdiv) |
| 114 | { |