OSG实现利用菲波那契网格(Fibonacci lattice 或 Fibonacci grid)均分球面

#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>
#define PI 3.1415926 
osg::Vec3 pointSet[100000];
#define N 301
int main()
{
	float phi = (sqrt(5) + 1) / 2 - 1;
	//float phi = 0.617;
	float xTemp,yTemp,zTemp;
	for (int i=1;i<= N;i++)
	{
		zTemp = (2*(float)i-1)/ N -1;
		xTemp = sqrt(1 - zTemp*zTemp)*cos(2 * PI*(float)i*phi);
		yTemp = sqrt(1 - zTemp*zTemp)*sin(2 * PI*(float)i*phi);
		pointSet[i - 1].set(xTemp, yTemp, zTemp);
		pointSet[i - 1].normalize();
	}
	std::ofstream outf;
	outf.open("shotEye.txt", std::ios::trunc);
	for (int i = 0; i < N; i++)
	{

		outf << pointSet[i]._v[0] << "\t" << pointSet[i]._v[1] << "\t" << pointSet[i]._v[2] << std::endl;

	}

	outf.close();
	
	osg::ref_ptr<osg::Vec3Array> vertex = new osg::Vec3Array;
	osg::ref_ptr<osg::Vec3Array> normal = new osg::Vec3Array;
	for (int i=0;i<N;i++)
	{
		vertex->push_back(pointSet[i]);
		normal->push_back(pointSet[i]);
	}
	osg::ref_ptr<osg::Vec4Array> colorArray = new osg::Vec4Array;
	colorArray->push_back(osg::Vec4(1.0f, 0.0f, 0.0f, 1.0f));
	osg::ref_ptr<osgViewer::Viewer> viewer = new osgViewer::Viewer;
	osg::ref_ptr<osg::Group> node = new osg::Group;
	osg::ref_ptr<osg::Geode> geode = new osg::Geode;
	osg::ref_ptr<osg::Geometry> geometry = new osg::Geometry;

	geometry->setVertexArray(vertex.get());
	geometry->setNormalArray(normal.get());
	geometry->setColorArray(colorArray.get());
	geometry->setColorBinding(osg::Geometry::BIND_OVERALL);
	geometry->setNormalBinding(osg::Geometry::BIND_PER_VERTEX);
	geometry->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::POINTS, 0, N));
	geode->addDrawable(geometry);
	node->addChild(geode);
	osgUtil::Optimizer optimizer;
	optimizer.optimize(node.get());

	viewer->setSceneData(node.get());
	viewer->realize();
	viewer->run();
	return 0;
}

 效果图:

 

 实现原理来自:

https://stackoverflow.com/questions/9600801/evenly-distributing-n-points-on-a-sphere/26127012#26127012

https://zhuanlan.zhihu.com/p/25988652

https://zhuanlan.zhihu.com/p/25998937

转载于:https://www.cnblogs.com/tangmiao/p/7691095.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值