zoukankan      html  css  js  c++  java
  • 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] << "	" << pointSet[i]._v[1] << "	" << 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

  • 相关阅读:
    Java数组排序和搜索
    JDBC排序数据实例
    JDBC Like子句实例
    JDBC WHERE子句条件实例
    JDBC删除数据实例
    JDBC更新数据实例
    JDBC查询数据实例
    JDBC插入数据实例
    JDBC删除表实例
    JDBC创建表实例
  • 原文地址:https://www.cnblogs.com/tangmiao/p/7691095.html
Copyright © 2011-2022 走看看