📄 primitives.cpp
字号:
std::complex<float> n = -std::polar(1.0f, theta); std::complex<float> e = n * radius; osg::Vec3 normal(n.imag() * height, radius, n.real() * height); normal.normalize(); osg_normals->push_back(normal); osg_normals->push_back(normal); osg_texcoords->push_back(osg::Vec2(texCoord, 1.0f)); osg_texcoords->push_back(osg::Vec2(texCoord, 0.0f)); osg_vertices->push_back(osg::Vec3(0.0f, topY, 0.0f)); osg_vertices->push_back(osg::Vec3(e.imag(), bottomY, e.real())); } // do last point by hand to ensure no round off errors. osg::Vec3 normal(0.0f, radius, -height); normal.normalize(); osg_normals->push_back(normal); osg_normals->push_back(normal); osg_texcoords->push_back(osg::Vec2(1.0f, 1.0f)); osg_texcoords->push_back(osg::Vec2(1.0f, 0.0f)); osg_vertices->push_back(osg::Vec3(0.0f, topY, 0.0f)); osg_vertices->push_back(osg::Vec3(0.0f, bottomY, -radius)); side->setCount(osg_vertices->size()); osg_geom->addPrimitiveSet(side.get()); } if (bottom) { osg::ref_ptr<osg::DrawArrays> bottom = new osg::DrawArrays(osg::PrimitiveSet::TRIANGLE_FAN); size_t first = osg_vertices->size(); bottom->setFirst(first); float theta = 0.0f; for (unsigned int i = 0; i < numSegments; ++i, theta += thetaDelta) { std::complex<float> n = -std::polar(1.0f, theta); std::complex<float> e = n * radius; osg_normals->push_back(osg::Vec3(0.0f, -1.0f, 0.0f)); osg_texcoords->push_back(osg::Vec2(0.5f - 0.5f * n.imag(), 0.5f + 0.5f * n.real())); osg_vertices->push_back(osg::Vec3(-e.imag(), bottomY, e.real())); } // do last point by hand to ensure no round off errors. osg_normals->push_back(osg::Vec3(0.0f, -1.0f, 0.0f)); osg_texcoords->push_back(osg::Vec2(0.5f, 0.0f)); osg_vertices->push_back(osg::Vec3(0.0f, bottomY, -radius)); bottom->setCount(osg_vertices->size() - first); osg_geom->addPrimitiveSet(bottom.get()); } osg_geom->setVertexArray(osg_vertices.get()); osg_geom->setTexCoordArray(0, osg_texcoords.get()); osg_geom->setNormalArray(osg_normals.get()); osg_geom->setNormalBinding(osg::Geometry::BIND_PER_VERTEX); osg_geom->getOrCreateStateSet()->setAttributeAndModes(new osg::CullFace(osg::CullFace::BACK)); m_coneLibrary[key] = osg_geom; return osg_geom.get();}osg::ref_ptr<osg::Geometry> ReaderWriterVRML2::convertVRML97Cylinder(openvrml::vrml97_node::cylinder_node* vrml_cylinder) const{ float height = static_cast<const openvrml::sffloat&>(vrml_cylinder->field("height")).value; float radius = static_cast<const openvrml::sffloat&>(vrml_cylinder->field("radius")).value; bool bottom = static_cast<const openvrml::sfbool&>(vrml_cylinder->field("bottom")).value; bool side = static_cast<const openvrml::sfbool&>(vrml_cylinder->field("side")).value; bool top = static_cast<const openvrml::sfbool&>(vrml_cylinder->field("top")).value; QuadricKey key(height, radius, bottom, side, top); CylinderLibrary::const_iterator it = m_cylinderLibrary.find(key); if (it != m_cylinderLibrary.end()) { return (*it).second.get(); } osg::ref_ptr<osg::Geometry> osg_geom = new osg::Geometry(); osg::ref_ptr<osg::Vec3Array> osg_vertices = new osg::Vec3Array(); osg::ref_ptr<osg::Vec2Array> osg_texcoords = new osg::Vec2Array(); osg::ref_ptr<osg::Vec3Array> osg_normals = new osg::Vec3Array(); unsigned int numSegments = 40; const float thetaDelta = 2.0f * float(osg::PI) / float(numSegments); float topY = height * 0.5f; float bottomY = height * -0.5f; if (side) { osg::ref_ptr<osg::DrawArrays> side = new osg::DrawArrays(osg::PrimitiveSet::QUAD_STRIP); const float texCoordDelta = 1.0f / float(numSegments); float theta = 0.0f; float texCoord = 0.0f; for (unsigned int i = 0; i < numSegments; ++i, theta += thetaDelta, texCoord += texCoordDelta) { std::complex<float> n = -std::polar(1.0f, theta); std::complex<float> e = n * radius; osg::Vec3 normal(n.imag(), 0.0f, n.real()); osg_normals->push_back(normal); osg_normals->push_back(normal); osg_texcoords->push_back(osg::Vec2(texCoord, 1.0f)); osg_texcoords->push_back(osg::Vec2(texCoord, 0.0f)); osg_vertices->push_back(osg::Vec3(e.imag(), topY, e.real())); osg_vertices->push_back(osg::Vec3(e.imag(), bottomY, e.real())); } // do last point by hand to ensure no round off errors. osg::Vec3 normal(0.0f, 0.0f, -1.0f); osg_normals->push_back(normal); osg_normals->push_back(normal); osg_texcoords->push_back(osg::Vec2(1.0f, 1.0f)); osg_texcoords->push_back(osg::Vec2(1.0f, 0.0f)); osg_vertices->push_back(osg::Vec3(0.0f, topY, -radius)); osg_vertices->push_back(osg::Vec3(0.0f, bottomY, -radius)); side->setCount(osg_vertices->size()); osg_geom->addPrimitiveSet(side.get()); } if (bottom) { osg::ref_ptr<osg::DrawArrays> bottom = new osg::DrawArrays(osg::PrimitiveSet::TRIANGLE_FAN); size_t first = osg_vertices->size(); bottom->setFirst(first); float theta = 0.0f; for (unsigned int i = 0; i < numSegments; ++i, theta += thetaDelta) { std::complex<float> n = -std::polar(1.0f, theta); std::complex<float> e = n * radius; osg_normals->push_back(osg::Vec3(0.0f, -1.0f, 0.0f)); osg_texcoords->push_back(osg::Vec2(0.5f - 0.5f * n.imag(), 0.5f + 0.5f * n.real())); osg_vertices->push_back(osg::Vec3(-e.imag(), bottomY, e.real())); } // do last point by hand to ensure no round off errors. osg_normals->push_back(osg::Vec3(0.0f, -1.0f, 0.0f)); osg_texcoords->push_back(osg::Vec2(0.5f, 0.0f)); osg_vertices->push_back(osg::Vec3(0.0f, bottomY, -radius)); bottom->setCount(osg_vertices->size() - first); osg_geom->addPrimitiveSet(bottom.get()); } if (top) { osg::ref_ptr<osg::DrawArrays> top = new osg::DrawArrays(osg::PrimitiveSet::TRIANGLE_FAN); size_t first = osg_vertices->size(); top->setFirst(first); float theta = 0.0f; for (unsigned int i = 0; i < numSegments; ++i, theta += thetaDelta) { std::complex<float> n = -std::polar(1.0f, theta); std::complex<float> e = n * radius; osg_normals->push_back(osg::Vec3(0.0f, 1.0f, 0.0f)); osg_texcoords->push_back(osg::Vec2(0.5f + 0.5f * n.imag(), 0.5f - 0.5f * n.real())); osg_vertices->push_back(osg::Vec3(e.imag(), topY, e.real())); } // do last point by hand to ensure no round off errors. osg_normals->push_back(osg::Vec3(0.0f, 1.0f, 0.0f)); osg_texcoords->push_back(osg::Vec2(0.5f, 1.0f)); osg_vertices->push_back(osg::Vec3(0.0f, topY, -radius)); top->setCount(osg_vertices->size() - first); osg_geom->addPrimitiveSet(top.get()); } osg_geom->setVertexArray(osg_vertices.get()); osg_geom->setTexCoordArray(0, osg_texcoords.get()); osg_geom->setNormalArray(osg_normals.get()); osg_geom->setNormalBinding(osg::Geometry::BIND_PER_VERTEX); osg_geom->getOrCreateStateSet()->setAttributeAndModes(new osg::CullFace(osg::CullFace::BACK)); m_cylinderLibrary[key] = osg_geom; return osg_geom.get();}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -