
#include <osg/Notify>
#include <osg/MatrixTransform>
#include <osg/PositionAttitudeTransform>
#include <osg/Geometry>
#include <osg/Geode>
#include <osgUtil/Optimizer>
#include <osgDB/Registry>
#include <osgDB/ReadFile>
#include <osgDB/WriteFile>
#include <osgGA/TrackballManipulator>
#include <osgGA/FlightManipulator>
#include <osgGA/DriveManipulator>
#include <osgSim/OverlayNode>
#include <osgViewer/Viewer>
#include <iostream>
//创建一个四边形节点
osg::ref_ptr<osg::Node>createCircle()
{
//创建一个叶节点对象
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;
//添加数据
for (int n = 0; n < 40; n++)
{
v->push_back(osg::Vec3(5*cos(osg::PI*n/40) + 0.0, 5*sin(osg::PI*n / 40), 0.025*n));
v->push_back(osg::Vec3(4 * cos(osg::PI*n / 40) + 0.0f, 4 * sin(osg::PI*n / 40), 0.0));
}
//设置顶点数据z
geom->setVertexArray(v.get());
//创建颜色数组
osg::ref_ptr<osg::Vec4Array>vc = new osg::Vec4Array;
//添加数据
osg::Vec4 colorT;
for (int n = 0; n < 40;n++)
{
/*
//////////////////////////////////////////////////////////////////////////
//色带1,颜色较少
float value = 0.05*n;
colorT.x() = value > 0.5 ? (value - 0.5) * 2 : 0; //x[0.5] = 0, r[1] = 1;
colorT.y() = value < 0.5 ? 2*value : 1-((value-0.5)*2); //y[0] = 0, r[0.5] = 1;
colorT.z() = value < 0.5 ? 1-(2*value) : 0; //z[0] = 1, z[0.5] = 0;
colorT.w() = 1.0f;
//////////////////////////////////////////////////////////////////////////
*/
//////////////////////////////////////////////////////////////////////////
//色带2,颜色较多
//fmode函数:返回x除以y的余数
float value = fmod(0.3*n,6.0);
if (value<1)
{
colorT.x() = 1.0;
colorT.y() = 0;
colorT.z() = value;
}
else if (value < 2)
{
value = value - 1;
colorT.x() = 1.0 - value;
colorT.y() = 0;
colorT.z() = 1.0;
}
else if (value < 3)
{
value = value - 2;
colorT.x() = 0;
colorT.y() = value;
colorT.z() = 1.0;
}
else if (value < 4)
{
value = value - 3;
colorT.x() = 0;
colorT.y() = 1.0;
colorT.z() = 1 - value;
}
else if (value < 5)
{
value = value - 4;
colorT.x() = value;
colorT.y() = 1.0;
colorT.z() = 0;
}
else
{
value = value - 5;
colorT.x() = 1;
colorT.y() = 1.0-value;
colorT.z() = 0;
}
//////////////////////////////////////////////////////////////////////////
//透明度渐变功能
//colorT.w() = 1.0-sin(0.157*n);
colorT.w() = 1.0 ;
vc->push_back(colorT);
vc->push_back(colorT);
}
//设置颜色数组
geom->setColorArray(vc.get());
//设置颜色的绑定方式为单个顶点
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);
//添加图元
geom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::QUAD_STRIP, 0, 80));
//添加到叶节点
geode->addDrawable(geom.get());
// 向Geode类添加几何体(Drawable)并返回Geode
osg::ref_ptr<osg::StateSet>stateset = geode->getOrCreateStateSet();
//Alpha混合开启
stateset->setMode(GL_BLEND, osg::StateAttribute::ON);
//取消深度测试
stateset->setMode(GL_DEPTH_TEST, osg::StateAttribute::ON);
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(createCircle());
//优化场景数据
osgUtil::Optimizer optimizer;
optimizer.optimize(root.get());
viewer->setSceneData(root.get());
viewer->realize();
viewer->run();
return 0;
}
以上代码亲测可用!

本文档详细介绍了如何利用OpenSceneGraph(OSG)库和Visual C++(VC++)编程实现颜色的渐变效果。通过实例代码,展示了在3D场景中创建颜色从一种到另一种平滑变化的技术,为读者提供了一种在图形应用中实现视觉效果增强的方法。
9976

被折叠的 条评论
为什么被折叠?



