OSG实现正八面体剖分成球
#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实现正八面体剖分成球的更多相关文章
- 通过CGAL将一个多边形剖分成Delaunay三角网
目录 1. 概述 2. 实现 3. 结果 4. 参考 1. 概述 对于平面上的点集,通过Delaunay三角剖分算法能够构建一个具有空圆特性和最大化最小角特性的三角网.空圆特性其实就是对于两个共边的三 ...
- osg 中鼠标拾取线段的端点和中点
//NeartestPointNodeVisitor.h #pragma once #include <osg\Matrix> #include <vector> #inclu ...
- Learning OSG programing---osgClip
OSG Clip例程剖析 首先是创建剪切节点的函数代码: osg::ref_ptr<osg::Node> decorate_with_clip_node(const osg::ref_pt ...
- Codeforces 191 C Fools and Roads (树链拆分)
主题链接~~> 做题情绪:做了HDU 5044后就感觉非常easy了. 解题思路: 先树链剖分一下,把树剖分成链,由于最后全是询问,so~能够线性操作.经过树链剖分后,就会形成很多链,可是每条边 ...
- bzoj 1036
1036: [ZJOI2008]树的统计Count Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 11858 Solved: 4803[Submit ...
- <知识整理>2019清北学堂提高储备D1
一.枚举: 枚举是最简单最基础的算法,核心思想是将可能的结果都列举出来并判断是否是解. 优点:思维简单,帮助理解问题.找规律.没头绪时 缺点:时空复杂度较高,会有很多冗余的非解(简单的枚举几乎没有利用 ...
- 蒟蒻浅谈树链剖分之一——两个dfs操作
树链剖分,顾名思义就是将树形的结构剖分成链,我们以此便于在链上操作 首先我们需要明白在树链剖分中的一些概念 重儿子:某节点所有儿子中子树最多的儿子 重链:有重儿子构成的链 dfs序:按重儿子优先遍历时 ...
- [凸包]Triangles
https://nanti.jisuanke.com/t/15429 题目大意:给出平面内$n$个整数坐标点,保证无三点共线.可以进行若干次连线,每次选择一个点对连接线段,但是任意两条线段都不得在给定 ...
- POJ1385 Lifting the Stone
There are many secret openings in the floor which are covered by a big heavy stone. When the stone i ...
随机推荐
- 关于Modelsim安装闪退
在盗版Windows系统上,安装Modelsim时可能出现闪退. 现象表现为,在任务管理器中仍然有Modelsim的进程,但是看不到安装界面. 碰到这种情况可以尝试如下方法:退到安全模式下安装. 一般 ...
- thinkphp 字段定义
通常每个模型类是操作某个数据表,在大多数情况下,系统会自动获取当前数据表的字段信息. 系统会在模型首次实例化的时候自动获取数据表的字段信息(而且只需要一次,以后会永久缓存字段信息,除非设置不缓存或者删 ...
- Vue+Iview+Node 项目结构和配置
1.项目调整后的目录 api:数据接口定义 assets:静态文件 components:组件 config:项目相关配置 driective:指令 router:路由 store:状态管 ...
- vue中axios使用封装
一.在main.js导入 // 引入axios,并加到原型链中 import axios from 'axios'; Vue.prototype.$axios = axios; import QS f ...
- SQLServer日期格式化及创建相关日期
DECLARE @FirstDay_M DATETIME --本月初日期 ,); DECLARE @LastDay_M DATETIME --本月末日期 SET @LastDay_M = DATEAD ...
- 19.SimLogin_case06
# 使用自造的cookies登录GitHub import requests from lxml import etree str = '_octo=GH1.1.518803230.153726461 ...
- Web开发之Tomcat&Servlet
<!doctype html>01 - JavaEE - Tomcat&Servlet figure:first-child { margin-top: -20px; } #wri ...
- scrapy爬虫框架爬取招聘网站
目录结构 BossFace.py文件中代码: # -*- coding: utf-8 -*-import scrapyfrom ..items import BossfaceItemimport js ...
- Python中else的用法
Python中else除了可以与if组成条件语句外,还可以和while .for .try一起串联使用. else和while配合使用: count=0 while count>12: if ( ...
- OpenGL 鼠标交互响应事件
OpenGL 鼠标.键盘交互响应事件 先来一个样例: uses gl,glu,glut; procedure InitEnvironment;cdecl; begin glClearColor();/ ...