opengl - 如何为球体的一部分生成几何图形
问题描述
生成渲染球体所需点的算法很少,但我找不到一个好的算法,也找不到一个完整的球体算法来为球体的一部分生成点。
假设我有最小/最大纬度、最小/最大经度和球体半径。如何生成能够渲染球体的这一部分的网格?
解决方案
我制作了半球和半球网格的一部分。我稍微编辑了 songho Ahn 的源代码,您可以参考以下链接:
http://www.songho.ca/opengl/gl_sphere.html
主要方法class Sphere
是buildVertices*
您可以通过编辑此方法获得所需的结果。
我做了这样的半球:
void HemiSphere::buildVerticesSmooth()
{
...
float sectorStep = 2 *PI / sectorCount;
float stackStep = (PI / 2) / stackCount;
float sectorAngle, stackAngle;
for(int i = 0; i <= stackCount; ++i)
{
stackAngle = -PI / 2 + i * stackStep; // starting from -pi/2 to 0
xy = radius * cosf(stackAngle); // r * cos(u)
z = radius *( 1 + sinf(stackAngle) ); // r * sin(u)
...
我还制作了半球,切割了它的地板,这样它的底部就有了均匀的地板。为此,保持 xy 坐标不变,只需将 z 坐标编辑为 0。
void HemiSphere::buildVerticesSmooth()
{
....
float sectorStep = 2 *PI / sectorCount;
float stackStep = (PI / 2) / stackCount;
float sectorAngle, stackAngle;
float d = sqrt(radius*radius - flat_radius*flat_radius)/radius;
for(int i = 0; i <= stackCount; ++i)
{
stackAngle = -PI / 2 + i * stackStep; // starting from -pi/2 to 0
xy = radius * cosf(stackAngle); // r * cos(u)
z = radius *( d + sinf(stackAngle) ); // r * sin(u)
if (z<0.0) z = 0.0;
....
希望这可以帮助。
推荐阅读
- mongodb - 无法连接到mongodb云
- r - 从文本文件中读取特定列:R
- database - MS Access 2013 - 如何在自动更新的文本框上触发事件
- validation - 如何在 Rust 中验证用户输入并循环直到给出有效输入?
- javascript - 在 Javascript 中使用 Ipinfo.io
- android - Android Room 需要约 40 秒来执行第一个查询
- python -
() 接受 1 个位置参数,但给出了 2 个 - sql - 使用 UNION ALL 表达式 TSQL 排序 BY CASE
- python - /user/register 处的 ValueError 无法分配“('土木工程(CE)',)”:“User.department”必须是“Department”实例
- android - 写在从相机捕获的图像上