osg解析系列-osgShadow::StandardShadowMap

示例代码

// M23.11.W3.Shadow.ShadowMap.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <Windows.h>

//osg
#include <osg/Texture2D>
#include <osg/ShapeDrawable>
#include <osg/MatrixTransform>
#include <osg/Geometry>
#include <osg/Material>

#include <osgViewer/Viewer>
#include <osgDB/ReadFile>
#include <osgGA/TrackballManipulator>

//osgShadow
#include <osgShadow/ShadowedScene>
#include <osgShadow/ShadowTexture>
#include <osgShadow/ShadowMap>
#include <osgShadow/SoftShadowMap>
#include <osgShadow/ParallelSplitShadowMap>
#include <osgShadow/LightSpacePerspectiveShadowMap>
#include <osgShadow/StandardShadowMap>
#include <osgShadow/ViewDependentShadowMap>

//osglib
#pragma comment(lib,"OpenThreads_d.lib")
#pragma comment(lib,"osg_d.lib")
#pragma comment(lib,"osgDB_d.lib")
#pragma comment(lib,"osgFX_d.lib")
#pragma comment(lib,"osgGA_d.lib")
#pragma comment(lib,"osgUtil_d.lib")
#pragma comment(lib,"osgViewer_d.lib")
#pragma comment(lib,"osgShadow_d.lib")

namespace MyShadow
{
	osg::Group* createModel()
	{
		osg::Group* scene = new osg::Group;

		osg::ref_ptr<osg::Geode> geode_1 = new osg::Geode;
		scene->addChild(geode_1.get());

		osg::ref_ptr<osg::Geode> geode_2 = new osg::Geode;
		osg::ref_ptr<osg::MatrixTransform> transform_2 = new osg::MatrixTransform;
		transform_2->addChild(geode_2.get());
		//        transform_2->setUpdateCallback(new osg::AnimationPathCallback(osg::Vec3(0, 0, 0), osg::Z_AXIS, osg::inDegrees(45.0f)));
		scene->addChild(transform_2.get());

		osg::ref_ptr<osg::Geode> geode_3 = new osg::Geode;
		osg::ref_ptr<osg::MatrixTransform> transform_3 = new osg::MatrixTransform;
		transform_3->addChild(geode_3.get());
		//        transform_3->setUpdateCallback(new osg::AnimationPathCallback(osg::Vec3(0, 0, 0), osg::Z_AXIS, osg::inDegrees(-22.5f)));
		scene->addChild(transform_3.get());

		const float radius = 0.8f;
		const float height = 1.0f;
		osg::ref_ptr<osg::TessellationHints> hints = new osg::TessellationHints;
		hints->setDetailRatio(2.0f);
		osg::ref_ptr<osg::ShapeDrawable> shape;
		shape = new osg::ShapeDrawable(new osg::Box(osg::Vec3(0.0f, 0.0f, -2.0f), 10, 10.0f, 0.1f), hints.get());
		shape->setColor(osg::Vec4(0.5f, 0.5f, 0.7f, 1.0f));
		shape->setName("base box");
		geode_1->addDrawable(shape.get());

		shape = new osg::ShapeDrawable(new osg::Sphere(osg::Vec3(0.0f, 0.0f, 0.0f), radius * 2), hints.get());
		shape->setColor(osg::Vec4(0.8f, 0.8f, 0.8f, 1.0f));
		shape->setName("center sphere");
		geode_1->addDrawable(shape.get());

		shape = new osg::ShapeDrawable(new osg::Sphere(osg::Vec3(-3.0f, 0.0f, 0.0f), radius), hints.get());
		shape->setColor(osg::Vec4(0.6f, 0.8f, 0.8f, 1.0f));
		shape->setName("cyan sphere");
		geode_2->addDrawable(shape.get());

		shape = new osg::ShapeDrawable(new osg::Box(osg::Vec3(3.0f, 0.0f, 0.0f), 2 * radius), hints.get());
		shape->setColor(osg::Vec4(0.4f, 0.9f, 0.3f, 1.0f));
		shape->setName("green box");
		geode_2->addDrawable(shape.get());

		shape = new osg::ShapeDrawable(new osg::Cone(osg::Vec3(0.0f, -3.0f, 0.0f), radius, height), hints.get());
		shape->setColor(osg::Vec4(0.2f, 0.5f, 0.7f, 1.0f));
		shape->setName("blue cone");
		geode_2->addDrawable(shape.get());

		shape = new osg::ShapeDrawable(new osg::Cylinder(osg::Vec3(0.0f, 3.0f, 0.0f), radius, height), hints.get());
		shape->setColor(osg::Vec4(1.0f, 0.3f, 0.3f, 1.0f));
		shape->setName("red cylinder");
		geode_2->addDrawable(shape.get());

		shape = new osg::ShapeDrawable(new osg::Box(osg::Vec3(0.0f, 0.0f, 3.0f), 2.0f, 2.0f, 0.1f), hints.get());
		shape->setColor(osg::Vec4(0.8f, 0.8f, 0.4f, 1.0f));
		shape->setName("rotating box");
		geode_3->addDrawable(shape.get());

		// material
		osg::ref_ptr<osg::Material> matirial = new osg::Material;
		matirial->setColorMode(osg::Material::DIFFUSE);
		matirial->setAmbient(osg::Material::FRONT_AND_BACK, osg::Vec4(0, 0, 0, 1));
		matirial->setSpecular(osg::Material::FRONT_AND_BACK, osg::Vec4(1, 1, 1, 1));
		matirial->setShininess(osg::Material::FRONT_AND_BACK, 64.0f);
		scene->getOrCreateStateSet()->setAttributeAndModes(matirial.get(), osg::StateAttribute::ON);

		bool withBaseTexture = true;
		if (withBaseTexture)
		{
			scene->getOrCreateStateSet()->setTextureAttributeAndModes(0, new osg::Texture2D(osgDB::readRefImageFile("D:\\osg365vs2015x64\\Data\\Images\\lz.rgb")), osg::StateAttribute::ON);
		}

		return scene;
	};
}

//不使用阴影
#if 0
int main()
{
	// construct the viewer.
	osgViewer::Viewer viewer;
	// ThreadModel
	viewer.setThreadingModel(osgViewer::Viewer::AutomaticSelection);
	// MasterCamera
	//using default

	// scene data
	viewer.setSceneData(MyShadow::createModel());

	viewer.setCameraManipulator(new osgGA::TrackballManipulator());
	
    return viewer.run();
}
#endif

//使用阴影技术
#if 1
static int ReceivesShadowTraversalMask = 0x1;
static int CastsShadowTraversalMask = 0x2;

int main()
{
	// construct the viewer.
	osgViewer::Viewer viewer;
	// ThreadModel
	viewer.setThreadingModel(osgViewer::Viewer::AutomaticSelection);
	// MasterCamera
	//using default

	// scene data
	osg::ref_ptr<osgShadow::ShadowedScene> shadowedScene = new osgShadow::ShadowedScene;
	osgShadow::ShadowSettings* settings = shadowedScene->getShadowSettings();
	settings->setReceivesShadowTraversalMask(ReceivesShadowTraversalMask);
	settings->setCastsShadowTraversalMask(CastsShadowTraversalMask);

	//阴影技术-StandardShadowMap
	osg::ref_ptr<osgShadow::StandardShadowMap> st = new osgShadow::StandardShadowMap;
	shadowedScene->setShadowTechnique(st.get());

	osg::Vec4 lightpos(0.0, 0.0, 1, 0.0);
	osg::ref_ptr<osg::LightSource> ls = new osg::LightSource;
	ls->getLight()->setPosition(lightpos);

	shadowedScene->addChild(MyShadow::createModel());
	shadowedScene->addChild(ls.get());

	viewer.setSceneData(shadowedScene);
	viewer.setCameraManipulator(new osgGA::TrackballManipulator());

	// create the windows and run the threads.
	viewer.realize();

	while (!viewer.done())
	{
		if (1)
		{
			float t = viewer.getFrameStamp()->getSimulationTime();

			lightpos.set(sinf(t), cosf(t), 1.0f, 0.0f);
			ls->getLight()->setPosition(lightpos);

			osg::Vec3f lightDir(-lightpos.x(), -lightpos.y(), -lightpos.z());
			lightDir.normalize();

			ls->getLight()->setDirection(lightDir);
		}
		viewer.frame();
	}

	return 0;
}
#endif

运行结果:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值