osg中设置线宽和点的大小

本文介绍如何使用OSG(OpenSceneGraph)库调整线条宽度和点的大小,通过具体代码示例展示了如何设置宽线和大点属性,以增强3D图形效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

#include <osg/LineWidth>
#include <osg/Point>



osg::ref_ptr<osg::StateSet> stateset = lineGeode->getOrCreateStateSet();
osg::ref_ptr<osg::LineWidth> lineWid = new osg::LineWidth(10.0f);

stateset->setAttribute(lineWid);


//注意设置点的大小前提是几何图元必须是osg::PrimitiveSet::POINTS
	osg::StateSet* stateSet = root->getOrCreateStateSet();
	osg::Point* pointSize = new osg::Point;
	pointSize->setSize(4.0);
	stateSet->setAttribute(pointSize);

 

#include <Windows.h> #include <vector> #include <iostream> #include <fstream> #include <cstring> #include <time.h> #include <osgEarth/MapNode> #include <osgEarthUtil/EarthManipulator> #include <osgEarthUtil/ExampleResources> //键盘事件 #include<osgViewer/ViewerEventHandlers> //osgGA相关的库 #include<osgGA/StateSetManipulator> #include <osgViewer/Viewer> #include <osgDB/WriteFile> #include <osg/LineWidth> using namespace std; int main(int argc, char* argv[]) { osg::ref_ptr<osgViewer::Viewer>viewer = new osgViewer::Viewer; //添加状态事件,可以相应键盘鼠标事件,响应L T B W viewer->addEventHandler(new osgGA::StateSetManipulator(viewer->getCamera()->getOrCreateStateSet())); //窗口大小变化,响应F viewer->addEventHandler(new osgViewer::WindowSizeHandler); //添加路径记录 Z viewer->addEventHandler(new osgViewer::RecordCameraPathHandler); //帮助文档显示H viewer->addEventHandler(new osgViewer::HelpHandler); //截屏 C viewer->addEventHandler(new osgViewer::ScreenCaptureHandler); //添加一些常用状态设置,响应S viewer->addEventHandler(new osgViewer::StatsHandler); //操作器 osg::ref_ptr<osgEarth::Util::EarthManipulator> earthManipulator = new osgEarth::Util::EarthManipulator; //设置相机操作器 viewer->setCameraManipulator(earthManipulator); //根节 osg::ref_ptr<osg::Group>root = new osg::Group; //加载地球节 osg::Node* earthNode = osgDB::readNodeFile("D:/test_osg/simple.earth"); //将地球节加入根节 root->addChild(earthNode); //设置现场数据 viewer->setSceneData(root.get()); //实现 viewer->realize(); //检测地图节是否创建好 osgEarth::MapNode* mapNode = osgEarth::MapNode::findMapNode(earthNode); if (!mapNode) return 0; //空间设置,水平垂直 const osgEarth::SpatialReference* geoSRS = mapNode->getMapSRS()->getGeographicSRS(); ///添加模型 osg::Node* model = osgDB::readNodeFile("D:/test_osg/zhengti.ive"); //osg中光照只会对有法线的模型起作用,而模型经过缩放后法线是不会变得, //所以需要手动设置属性,让法线随着模型大小变化而变化。GL_NORMALIZE 或 GL_RESCALE_NORMAL model->getOrCreateStateSet()->setMode(GL_RESCALE_NORMAL, osg::StateAttribute::ON); osg::Matrix Lmatrix; geoSRS->getEllipsoid()->computeLocalToWorldTransformFromLatLongHeight(osg::DegreesToRadians(40.0), osg::DegreesToRadians(116.0), 500000.0, Lmatrix); //维度,经度,高度,localToWorld //放大一些,方便看到 Lmatrix.preMult(osg::Matrix::scale(osg::Vec3(30000, 30000, 30000)));//x,y,z轴放大倍数 /*//画出四个定的线 osg::ref_ptr<osg::Geode> geode = new osg::Geode; root->addChild(geode); //设置数组 osg::ref_ptr<osg::Vec3Array> vex = new osg::Vec3Array; vex->push_back(osg::Vec3(-3.0, 0.0, 0.0)); vex->push_back(osg::Vec3(3.0, 0.0, 0.0)); vex->push_back(osg::Vec3(0.0, 5.0, 0.0)); vex->push_back(osg::Vec3(0.0, 0.0, 5.0)); osg::ref_ptr<osg::Geometry> geometry = new osg::Geometry; geometry->setVertexArray(vex); //设置颜色数组 osg::ref_ptr<osg::Vec4Array> colors = new osg::Vec4Array; colors->push_back(osg::Vec4(1.0, 1.0, 0.0, 0.5)); colors->push_back(osg::Vec4(1.0, 1.0, 0.0, 0.5)); colors->push_back(osg::Vec4(1.0, 1.0, 0.0, 0.5)); colors->push_back(osg::Vec4(1.0, 1.0, 0.0, 0.5)); geometry->setColorArray(colors); geometry->setColorBinding(osg::Geometry::BIND_PER_VERTEX); osg::ref_ptr<osg::PrimitiveSet> primitiveSet = new osg::DrawArrays(osg::PrimitiveSet::LINE_LOOP, 0, 4); geometry->addPrimitiveSet(primitiveSet); //设置线宽 osg::ref_ptr<osg::LineWidth> lw = new osg::LineWidth(6.0); geometry->getOrCreateStateSet()->setAttribute(lw, osg::StateAttribute::ON); geode->addDrawable(geometry); */ // 创建一个几何体来表示直线 osg::ref_ptr<osg::Geometry> line = new osg::Geometry(); // 设置数据变化为动态 line->setDataVariance(osg::Object::DYNAMIC); // 添加绘制模式 line->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::LINES, 0, 2)); // 定义顶坐标 line->setVertexArray(new osg::Vec3Array(2)); auto vertices = dynamic_cast<osg::Vec3Array*>(line->getVertexArray()); if (!vertices) { std::cerr << "无法获取 Vec3Array" << std::endl; return -1; } vertices->at(0) = osg::Vec3(-3.0, 0.0, 0.0); // A vertices->at(1) = osg::Vec3(3.0, 0.0, 0.0); // B // 设置线宽 osg::LineWidth* lineWidth = new osg::LineWidth(); lineWidth->setWidth(5.0f); // 设置线宽为5个单位 // 获取状态集并添加线宽属性 osg::StateSet* stateSet = line->getOrCreateStateSet(); stateSet->setAttributeAndModes(lineWidth, osg::StateAttribute::ON); // 设置颜色 osg::Vec4Array* colors = new osg::Vec4Array(2); colors->at(0) = osg::Vec4(1.0f, 0.0f, 0.1f, 1.0f); // 红色 colors->at(1) = osg::Vec4(1.0f, 0.0f, 0.1f, 1.0f); // 红色 line->setColorArray(colors); line->setColorBinding(osg::Geometry::BIND_PER_VERTEX); // 确保材质启用 osg::StateSet* stateset = line->getOrCreateStateSet(); stateset->setMode(GL_LIGHTING, osg::StateAttribute::ON); osg::MatrixTransform* mt = new osg::MatrixTransform; mt->setMatrix(Lmatrix); mt->addChild(model); //mt->addChild(geode); mt->addChild(line); root->addChild(mt); root->addChild(line); viewer->setSceneData(root); //视定位北京地区,此句代码运行后可以直接定位到该坐标,注释后仍能正常显示模型,不过不会自动定位 earthManipulator->setViewpoint(osgEarth::Viewpoint("模拟无人机", 116, 40, 0.0, 0.0, -90.0, 1.5e6)); return viewer->run(); } 根据以上代码 添加模型在地球上的轨迹线
最新发布
06-21
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值