#include<Windows.h>
#include<osg/Node>
#include<osg/Geode>
#include<osg/Group>
#include <osg/Geometry>
#include<osgUtil/Optimizer>
#include <cmath>
#include<iostream>
#include<osgViewer/Viewer>
#include<osgDB/ReadFile>
#include<osgDB/WriteFile>
std::set<osg::Vec3> pointSet;
osg::ref_ptr<osg::Geode> geode = new osg::Geode;
void subdivide(float v1x, float v1y, float v1z,
float v2x, float v2y, float v2z,
float v3x, float v3y, float v3z,
int level) {
if (level == 0) {
// Reached desired tessellation level, emit triangle.
osg::Vec3 v1Temp = osg::Vec3(v1x, v1y, v1z);
osg::Vec3 v2Temp = osg::Vec3(v2x, v2y, v2z);
osg::Vec3 v3Temp = osg::Vec3(v3x, v3y, v3z);
v1Temp.normalize();
v2Temp.normalize();
v3Temp.normalize();
pointSet.insert(v1Temp);
pointSet.insert(v2Temp);
pointSet.insert(v3Temp);
osg::ref_ptr<osg::Vec3Array> vertex = new osg::Vec3Array;
vertex->push_back(v1Temp);
vertex->push_back(v2Temp);
vertex->push_back(v3Temp);
osg::ref_ptr < osg::Geometry>geometry = new osg::Geometry;
geometry->setVertexArray(vertex.get());
geometry->setNormalArray(vertex.get());
geometry->setNormalBinding(osg::Geometry::BIND_PER_VERTEX);
geometry->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::TRIANGLES, 0, vertex->size()));
geode->addDrawable(geometry);
/*drawTriangle(v1x, v1y, v1z,
v2x, v2y, v2z,
v3x, v3y, v3z);
*/
}
else {
// Calculate middle of first edge...
float v12x = 0.5f * (v1x + v2x);
float v12y = 0.5f * (v1y + v2y);
float v12z = 0.5f * (v1z + v2z);
// ... and renormalize it to get a point on the sphere.
float s = 1.0f / sqrt(v12x * v12x + v12y * v12y + v12z * v12z);
v12x *= s;
v12y *= s;
v12z *= s; // Same thing for the middle of the other two edges.
float v13x = 0.5f * (v1x + v3x);
float v13y = 0.5f * (v1y + v3y);
float v13z = 0.5f * (v1z + v3z); s = 1.0f / sqrt(v13x * v13x + v13y * v13y + v13z * v13z);
v13x *= s;
v13y *= s;
v13z *= s; float v23x = 0.5f * (v2x + v3x);
float v23y = 0.5f * (v2y + v3y);
float v23z = 0.5f * (v2z + v3z);
s = 1.0f / sqrt(v23x * v23x + v23y * v23y + v23z * v23z);
v23x *= s;
v23y *= s;
v23z *= s; // Make the recursive calls.
subdivide(v1x, v1y, v1z,
v12x, v12y, v12z,
v13x, v13y, v13z,
level - 1);
subdivide(v12x, v12y, v12z,
v2x, v2y, v2z,
v23x, v23y, v23z,
level - 1);
subdivide(v13x, v13y, v13z,
v23x, v23y, v23z,
v3x, v3y, v3z,
level - 1);
subdivide(v12x, v12y, v12z,
v23x, v23y, v23z,
v13x, v13y, v13z,
level - 1);
}
}
int main()
{
osg::ref_ptr<osgViewer::Viewer> viewer = new osgViewer::Viewer; int level = 3;
subdivide(0.000000, 0.000000, 1.000000, 1.000000, 0.000000, 0.000000, 0.000000, 1.000000, 0.000000, level);
subdivide(0.000000, 0.000000, 1.000000, -1.000000,0.000000, 0.000000, 0.000000, 1.000000, 0.000000, level);
subdivide(0.000000, 0.000000, 1.000000, -1.000000, 0.000000, 0.000000, -0.000000 ,- 1.000000,0.000000, level);
subdivide(0.000000, 0.000000, 1.000000, 1.000000, 0.000000, 0.000000, -0.000000, -1.000000, 0.000000, level); subdivide(0.000000, 0.000000, -1.000000, 1.000000, 0.000000, 0.000000, 0.000000, 1.000000, 0.000000, level);
subdivide(0.000000, 0.000000, -1.000000, -1.000000, 0.000000, 0.000000, 0.000000, 1.000000, 0.000000, level);
subdivide(0.000000, 0.000000, -1.000000, -1.000000, 0.000000, 0.000000, 0.000000, -1.000000, 0.000000, level);
subdivide(0.000000, 0.000000, -1.000000, 1.000000, 0.000000, 0.000000, 0.000000, -1.000000, 0.000000, level); osg::ref_ptr<osg::Group> node = new osg::Group;
node->addChild(geode);
osgUtil::Optimizer optimizer;
optimizer.optimize(node.get()); viewer->setSceneData(node.get());
viewer->realize();
viewer->run();
return 0;
}

  

OSG实现正八面体剖分成球的更多相关文章

  1. 通过CGAL将一个多边形剖分成Delaunay三角网

    目录 1. 概述 2. 实现 3. 结果 4. 参考 1. 概述 对于平面上的点集,通过Delaunay三角剖分算法能够构建一个具有空圆特性和最大化最小角特性的三角网.空圆特性其实就是对于两个共边的三 ...

  2. osg 中鼠标拾取线段的端点和中点

    //NeartestPointNodeVisitor.h #pragma once #include <osg\Matrix> #include <vector> #inclu ...

  3. Learning OSG programing---osgClip

    OSG Clip例程剖析 首先是创建剪切节点的函数代码: osg::ref_ptr<osg::Node> decorate_with_clip_node(const osg::ref_pt ...

  4. Codeforces 191 C Fools and Roads (树链拆分)

    主题链接~~> 做题情绪:做了HDU 5044后就感觉非常easy了. 解题思路: 先树链剖分一下,把树剖分成链,由于最后全是询问,so~能够线性操作.经过树链剖分后,就会形成很多链,可是每条边 ...

  5. bzoj 1036

    1036: [ZJOI2008]树的统计Count Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 11858  Solved: 4803[Submit ...

  6. <知识整理>2019清北学堂提高储备D1

    一.枚举: 枚举是最简单最基础的算法,核心思想是将可能的结果都列举出来并判断是否是解. 优点:思维简单,帮助理解问题.找规律.没头绪时 缺点:时空复杂度较高,会有很多冗余的非解(简单的枚举几乎没有利用 ...

  7. 蒟蒻浅谈树链剖分之一——两个dfs操作

    树链剖分,顾名思义就是将树形的结构剖分成链,我们以此便于在链上操作 首先我们需要明白在树链剖分中的一些概念 重儿子:某节点所有儿子中子树最多的儿子 重链:有重儿子构成的链 dfs序:按重儿子优先遍历时 ...

  8. [凸包]Triangles

    https://nanti.jisuanke.com/t/15429 题目大意:给出平面内$n$个整数坐标点,保证无三点共线.可以进行若干次连线,每次选择一个点对连接线段,但是任意两条线段都不得在给定 ...

  9. POJ1385 Lifting the Stone

    There are many secret openings in the floor which are covered by a big heavy stone. When the stone i ...

随机推荐

  1. python库之lightgbm

    一.安装 https://blog.csdn.net/qq_40317897/article/details/81021958 参考文献: [1].LightGBM中文文档 https://light ...

  2. Erlang学习记录:语法和特性

    特性 大下排序:number < atom < reference < fun < port < pid < tuple < list < bit st ...

  3. SPR, subpixel rendering

    参考例子:https://www.grc.com/ctwhat.htm https://en.wikipedia.org/wiki/Subpixel_rendering http://archernz ...

  4. Windows cd

    显示当前目录名或改变当前目录. CHDIR [/D] [drive:][path]CHDIR [..]CD [/D] [drive:][path]CD [..] ..   指定要改成父目录. 键入 C ...

  5. helm安装kubernetes的插件istio

    1.安装istio 要使用Helm自定义Istio安装,请使用--set <key>=<value>Helm命令中的选项覆盖一个或多个值 怎么使用选项配置请查看官网https: ...

  6. mysql查询语句对于为null和为空字符串给出特定值处理

    SELECT if(IFNULL(filedName,"指定字符串")="","指定字符串",filedName) '重命名的字符名' FR ...

  7. Android按钮绑定四种方式

    public class MainActivity extends Activity implements OnClickListener{ @Override protected void onCr ...

  8. 第二篇:怕碰到是因为没掌握,来吧,zTree!

    一直以来看见web项目中的树就头疼.这次又给碰上了,什么也别说,这次自己整理一个版本出来实践一下.zTree v3.2的API界面非常清爽,但是在查看API之前,你需要自己先实践一下,知道基本的概念和 ...

  9. JS之缓冲动画

    原素材 main.html <!DOCTYPE html> <html lang="en"> <head> <link href=&quo ...

  10. 《Practices of an Agile Developer:Woring in the Real World》读书笔记 PB16110698(~3.22)第三周

    <Practices of an Agile Developer:Woring in the Real World>读书笔记  本周我阅读了<高效程序员的45个习惯:敏捷开发修炼之道 ...