Demo entry 6363919

lc1234

   

Submitted by anonymous on May 15, 2017 at 05:36
Language: C++. Code size: 3.2 kB.

/**********************************************************
*Write by FlySky
*zzuxp@163.com  http://www.OsgChina.org   
**********************************************************/

#include <osgViewer/Viewer>

#include <osg/Node>
#include <osg/Geode>
#include <osg/Group>

#include <osgDB/ReadFile>
#include <osgDB/WriteFile>

#include <osgUtil/Optimizer>

//´´½¨Ò»¸öËıßÐνڵã
osg::ref_ptr<osg::Node> createQuad()
{
	//´´½¨Ò»¸öÒ¶½Úµã¶ÔÏó
	osg::ref_ptr<osg::Geode> geode = new osg::Geode();

	//´´½¨Ò»¸ö¼¸ºÎÌå¶ÔÏó
	osg::ref_ptr<osg::Geometry> geom = new osg::Geometry();

	//´´½¨¶¥µãÊý×é
	osg::ref_ptr<osg::Vec3Array> v= new osg::Vec3Array();
	//Ìí¼ÓÊý¾Ý
	v->push_back(osg::Vec3(0.0f,0.0f,0.0f));
	v->push_back(osg::Vec3(1.0f,0.0f,0.0f));
	v->push_back(osg::Vec3(1.0f,0.0f,1.0f));
	v->push_back(osg::Vec3(0.0f,0.0f,1.0f));
	v->push_back(osg::Vec3(0.0f,-1.0f,0.0f));

	//ÉèÖö¥µãÊý¾Ý
	geom->setVertexArray(v.get());

	//´´½¨ËıßÐζ¥µãË÷ÒýÊý×飬ָ¶¨»æͼ»ùԪΪËıßÐΣ¬×¢ÒâÌí¼Ó˳Ðò
	osg::ref_ptr<osg::DrawElementsUInt> quad= new osg::DrawElementsUInt(osg::PrimitiveSet::QUADS,0);
	//Ìí¼ÓÊý¾Ý
	quad->push_back(0);
	quad->push_back(1);
	quad->push_back(2);
	quad->push_back(3);

	//Ìí¼Óµ½¼¸ºÎÌå
	geom->addPrimitiveSet(quad.get());

	//´´½¨Èý½ÇÐζ¥µãË÷ÒýÊý×飬ָ¶¨»æͼ»ùԪΪÈý½ÇÐΣ¬×¢ÒâÌí¼Ó˳Ðò
	osg::ref_ptr<osg::DrawElementsUInt> triangle = new osg::DrawElementsUInt(osg::PrimitiveSet::TRIANGLES,0);
	//Ìí¼ÓÊý¾Ý
	triangle->push_back(4);
	triangle->push_back(0);
	triangle->push_back(3);

	//Ìí¼Óµ½¼¸ºÎÌå
	geom->addPrimitiveSet(triangle.get());

	//´´½¨ÑÕÉ«Êý×é
	osg::ref_ptr<osg::Vec4Array> vc = new osg::Vec4Array();
	//Ìí¼ÓÊý¾Ý
	vc->push_back(osg::Vec4(1.0f,0.0f,0.0f,1.0f));
	vc->push_back(osg::Vec4(0.0f,1.0f,0.0f,1.0f));
	vc->push_back(osg::Vec4(0.0f,0.0f,1.0f,1.0f));
	vc->push_back(osg::Vec4(1.0f,1.0f,0.0f,1.0f));

	////´´½¨ÑÕÉ«Ë÷ÒýÊý×é
	//osg::TemplateIndexArray<unsigned int ,osg::Array::UIntArrayType,4,4>* colorIndex = 
	//	new osg::TemplateIndexArray<unsigned int ,osg::Array::UIntArrayType,4,4>();
	////Ìí¼ÓÊý¾Ý£¬×¢ÒâÌí¼ÓÊý¾Ý˳ÐòÓ붥µãÒ»Ò»¶ÔÓ¦
	//colorIndex->push_back(0);
	//colorIndex->push_back(1);
	//colorIndex->push_back(2);
	//colorIndex->push_back(3);
	//colorIndex->push_back(2);

	////ÉèÖÃÑÕÉ«Êý×é
	//geom->setColorArray(vc.get());
	////ÉèÖÃÑÕÉ«Ë÷ÒýÊý×é
	//geom->setColorIndices(colorIndex);
	////ÉèÖÃÑÕÉ«µÄ°ó¶¨·½Ê½Îªµ¥¸ö¶¥µã
	//geom->setColorBinding(osg::Geometry::BIND_PER_VERTEX);

	//´´½¨·¨ÏßÊý×é
	osg::ref_ptr<osg::Vec3Array> nc = new osg::Vec3Array();
	//Ìí¼Ó·¨Ïß
	nc->push_back(osg::Vec3(0.0f,-1.0f,0.0f));

	//ÉèÖ÷¨ÏßÊý×é
	geom->setNormalArray(nc.get());
	//ÉèÖ÷¨Ïߵİ󶨷½Ê½ÎªÈ«²¿¶¥µã
	geom->setNormalBinding(osg::Geometry::BIND_OVERALL);

	//Ìí¼Óµ½Ò¶½Úµã
	geode->addDrawable(geom.get());

	return geode.get();
}

int main()
{
	//´´½¨Viewer¶ÔÏ󣬳¡¾°ä¯ÀÀÆ÷
	osg::ref_ptr<osgViewer::Viewer> viewer = new osgViewer::Viewer();

	osg::ref_ptr<osg::Group> root = new osg::Group();

	//Ìí¼Óµ½³¡¾°
	root->addChild(createQuad());

	//ÓÅ»¯³¡¾°Êý¾Ý
	osgUtil::Optimizer optimizer ;
	optimizer.optimize(root.get()) ;

	viewer->setSceneData(root.get());

	viewer->realize();

	viewer->run();

	return 0 ;
}

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).