| 111 | } |
| 112 | |
| 113 | static void generateCylinder(osg::Geometry& geom, float radius, float height, int subdiv) |
| 114 | { |
| 115 | osg::ref_ptr<osg::Vec3Array> vertices = new osg::Vec3Array; |
| 116 | osg::ref_ptr<osg::Vec3Array> normals = new osg::Vec3Array; |
| 117 | osg::ref_ptr<osg::DrawElementsUShort> indices = new osg::DrawElementsUShort(osg::DrawElementsUShort::TRIANGLES, 0); |
| 118 | int vertexCount = subdiv * 4 + 2; // 2 discs + top and bottom + 2 center |
| 119 | indices->reserve(vertexCount); |
| 120 | int iVertex = 0; |
| 121 | |
| 122 | int beginTop = iVertex; |
| 123 | auto topNormal = osg::Vec3(0., 0., 1.); |
| 124 | // top disk |
| 125 | for (int i = 0; i < subdiv; i++) |
| 126 | { |
| 127 | float theta = (float(i) / float(subdiv)) * osg::PIf * 2.f; |
| 128 | osg::Vec3 pos = sphereCoordToCartesian(theta, osg::PI_2f, 1.f); |
| 129 | pos *= radius; |
| 130 | pos.z() = height / 2.f; |
| 131 | vertices->push_back(pos); |
| 132 | normals->push_back(topNormal); |
| 133 | iVertex += 1; |
| 134 | } |
| 135 | auto centerTop = static_cast<GLushort>(iVertex); |
| 136 | // centerTop |
| 137 | { |
| 138 | vertices->push_back(osg::Vec3(0.f, 0.f, height / 2.f)); |
| 139 | normals->push_back(topNormal); |
| 140 | iVertex += 1; |
| 141 | } |
| 142 | auto centerBot = static_cast<GLushort>(iVertex); |
| 143 | // centerBot |
| 144 | { |
| 145 | vertices->push_back(osg::Vec3(0.f, 0.f, -height / 2)); |
| 146 | normals->push_back(-topNormal); |
| 147 | iVertex += 1; |
| 148 | } |
| 149 | // bottom disk |
| 150 | auto beginBot = iVertex; |
| 151 | for (int i = 0; i < subdiv; i++) |
| 152 | { |
| 153 | float theta = float(i) / float(subdiv) * osg::PIf * 2.f; |
| 154 | osg::Vec3 pos = sphereCoordToCartesian(theta, osg::PI_2f, 1.f); |
| 155 | pos *= radius; |
| 156 | pos.z() = -height / 2.f; |
| 157 | vertices->push_back(pos); |
| 158 | normals->push_back(-topNormal); |
| 159 | iVertex += 1; |
| 160 | } |
| 161 | // sides |
| 162 | int beginSide = iVertex; |
| 163 | for (int i = 0; i < subdiv; i++) |
| 164 | { |
| 165 | float theta = float(i) / float(subdiv) * osg::PIf * 2.f; |
| 166 | osg::Vec3 normal = sphereCoordToCartesian(theta, osg::PI_2f, 1.f); |
| 167 | auto posTop = normal; |
| 168 | posTop *= radius; |
| 169 | auto posBot = posTop; |
| 170 | posTop.z() = height / 2.f; |
no test coverage detected