四、OSG学习笔记-基础图元
前一章节:
三、OSG学习笔记-应用基础-CSDN博客https://blog.csdn.net/weixin_36323170/article/details/145514021
代码:CuiQingCheng/OsgStudy - Gitee.com
一、绘制盒子模型
下面一个简单的 demo
#include<windows.h>
#include<osg/Node>
#include<osgViewer/Viewer>
#include<osgViewer/ViewerEventHandlers>
#include<osgDB/ReadFile>
#include<osgGA/TrackballManipulator>
// 图元库
#include<osg/Geode>
#include<osg/ShapeDrawable>
#include<osg/Material> // 材质相关头文件
// 纹理相关头文件
#include<osg/Image>
#include<osg/Texture2D>
osg::ref_ptr<osg::Geode> CreateBox()
{
osg::ref_ptr<osg::Geode> pGeode = new osg::Geode;
// 精度
osg::ref_ptr<osg::TessellationHints> pHints = new osg::TessellationHints;
// 绘制一个盒子
osg::ref_ptr<osg::ShapeDrawable> pShape = new osg::ShapeDrawable(new osg::Box(osg::Vec3(0.0, 0.0, 0.0), 3.0, 5.0, 5.0), pHints.get());
// 设置材质 设置光照之类
osg::ref_ptr<osg::Material> pMaterial = new osg::Material;
// 纹理
osg::ref_ptr<osg::Texture2D> pTexture2D = new osg::Texture2D;
osg::ref_ptr<osg::Image> pImage;
pHints->setDetailRatio(0.5);
pShape->setColor(osg::Vec4(0.5, 0.5, 0.5, 0.2)); // 设置颜色 RGB 透明度范围,均为0~1.0
pMaterial->setAmbient(osg::Material::FRONT_AND_BACK, osg::Vec4(1.0, 1.0, 1.0, 0.2)); // 设置全景光 白色
pMaterial->setDiffuse(osg::Material::FRONT_AND_BACK, osg::Vec4(1.0, 1.0, 1.0, 0.2)); // 设置混合光
pMaterial->setSpecular(osg::Material::FRONT_AND_BACK, osg::Vec4(1.0, 1.0, 1.0, 0.2)); // 设置反射光
pMaterial->setShininess(osg::Material::FRONT_AND_BACK, 60.0); // 设置反射光比例
// 设置纹理
pImage = osgDB::readRefImageFile("Images/whitemetal_diffuse.jpg");
if (pImage.valid())
{
pTexture2D->setImage(pImage.get());
}
pGeode->getOrCreateStateSet()->setTextureAttributeAndModes(0, pTexture2D.get(), osg::StateAttribute::ON); // 应用纹理,并指定为纹理模式
// 应用材质 因为设置透明度为 0.2,
// 所以上面材质的中各种光也需要设置透明度
pGeode->getOrCreateStateSet()->setAttributeAndModes(pMaterial.get(), osg::StateAttribute::ON);
pGeode->getOrCreateStateSet()->setMode(GL_BLEND, osg::StateAttribute::ON); // 设置透明度
pGeode->getOrCreateStateSet()->setMode(GL_DEPTH_TEST, osg::StateAttribute::ON); // 开启深度测试
pGeode->addDrawable(pShape.get());
return pGeode;
}
int main()
{
osg::ref_ptr<osgViewer::Viewer> viewer = new osgViewer::Viewer;
viewer->setUpViewInWindow(100, 100, 1500, 1000);
osg::ref_ptr<osg::Node> node = CreateBox();
viewer->setSceneData(node.get());
return viewer->run();
}
代码运行效果,如下图:
二、简单线段,折线等图元
OSG坐标系:
Y轴垂直屏幕向里;
#include<windows.h>
#include<osg/Node>
#include<osgViewer/Viewer>
#include<osgViewer/ViewerEventHandlers>
#include<osgDB/ReadFile>
#include<osgGA/TrackballManipulator>
// 图元库
#include<osg/Geode>
#include<osg/LineWidth>
osg::ref_ptr<osg::Node> GreateSimple()
{
osg::ref_ptr<osg::Geode> pGeode = new osg::Geode;
osg::ref_ptr<osg::Geometry> pGeome = new osg::Geometry;
// 申请一些顶点 顶点数组
osg::ref_ptr<osg::Vec3Array> pCoords = new osg::Vec3Array;
// 申请颜色
osg::ref_ptr<osg::Vec4Array> pColors = new osg::Vec4Array;
// 申请法向量
osg::ref_ptr<osg::Vec3Array> pNorms = new osg::Vec3Array;
// 申请线宽
osg::ref_ptr<osg::LineWidth> pLineW = new osg::LineWidth;
pGeode->addDrawable(pGeome.get());
// 打开透明度
pGeode->getOrCreateStateSet()->setMode(GL_BLEND, osg::StateAttribute::ON);
// 设置线宽
pLineW->setWidth(15.0);
pGeode->getOrCreateStateSet()->setAttributeAndModes(pLineW.get(), osg::StateAttribute::ON);
// 设置顶点
pGeome->setVertexArray(pCoords.get());
// 设置顶点关联方式
//pGeome->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::Mode::QUADS, 0, 4));// 注意1:关联方式为实心,矩形填充的
pGeome->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::Mode::LINE_LOOP, 0, 4)); // 注意2:关联方式为线框,中间不是实心的
// 设置顶点颜色,关联方式
pGeome->setColorArray(pColors.get());
pGeome->setColorBinding(osg::Geometry::AttributeBinding::BIND_PER_VERTEX);
// 设置法向量
pGeome->setNormalArray(pNorms.get());
pGeome->setNormalBinding(osg::Geometry::AttributeBinding::BIND_OVERALL);
// osg 绘制是按照逆时针进行绘制的
pCoords->push_back(osg::Vec3(-10.0, 5.0, -10.0));
pCoords->push_back(osg::Vec3(10.0, 5.0, -10.0));
pCoords->push_back(osg::Vec3(10.0, 5.0, 10.0));
pCoords->push_back(osg::Vec3(-10.0, 5.0, 10.0));
// 颜色设置, 因为前面打开了透明度设置,这里颜色设置最后一个参数,可以设置成0.5,半透明状态
pColors->push_back(osg::Vec4f(1.0, 0.0, 0.0, 0.5));
pColors->push_back(osg::Vec4f(0.0, 1.0, 0.0, 0.5));
pColors->push_back(osg::Vec4f(0.0, 0.0, 1.0, 0.5));
pColors->push_back(osg::Vec4f(1.0, 1.0, 0.0, 0.5));
// 法向量设置,压入法向量,朝向屏幕外的设置高亮
pNorms->push_back(osg::Vec3(0.0, -1.0, 0.0));
return pGeode;
}
int main()
{
osg::ref_ptr<osgViewer::Viewer> viewer = new osgViewer::Viewer;
viewer->setUpViewInWindow(100, 100, 1500, 1000);
osg::ref_ptr<osg::Group> pGroup = new osg::Group;
osg::ref_ptr<osg::Node> node = GreateSimple();
pGroup->addChild(osgDB::readNodeFile("glider.osg"));
pGroup->addChild(node.get());
viewer->setSceneData(pGroup.get());
return viewer->run();
}
实心绘制,如下,代码中标注,注意1,打开时,运行如下:
注意2打开时, 边框宽度设置生效:
后一章节:
五、OSG学习笔记-矩阵变换-CSDN博客https://blog.csdn.net/weixin_36323170/article/details/145514864