当前位置: 首页 > article >正文

四、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


http://www.kler.cn/a/541128.html

相关文章:

  • 二、通义灵码插件保姆级教学-IDEA(使用篇)
  • Hive之[Hive]详细安装步骤
  • UE5.5 PCGFrameWork--GPU CustomHLSL
  • C++ Primer sizeof运算符
  • 物联网软件开发与应用方向应该怎样学习,学习哪些内容,就业方向是怎样?(文末领取整套学习视频,课件)物联网硬件开发与嵌入式系统
  • node.js + html + Sealos容器云 搭建简易多人实时聊天室demo 带源码
  • 【前端基础】深度理解JavaScript中的异步机制
  • React(三)
  • 每日一题——插入排序实现数据流中的中位数
  • 【Java】多线程和高并发编程(四):阻塞队列(上)基础概念、ArrayBlockingQueue
  • React Hooks 与 Class 组件相比有何优势
  • 速度超越DeepSeek!Le Chat 1100tok/s闪电回答,ChatGPT 4o和DeepSeek R1被秒杀?
  • Haskell语言的云计算
  • 优化GPT API接口链接的方法
  • 解决 npm : 无法加载文件 D:\nodeJS\node_global\npm.ps1,因为在此系统上禁止运行脚本。
  • Android Studio:如何利用Application操作全局变量
  • 用 Java 轻松读取 Word 文档内容
  • dolphinscheduler安装部署
  • Kotlin Bytedeco OpenCV 图像图像51.2 光流背景消除
  • 部署自动化的重要性之骑士资本案例研读
  • vcredist_x64.exe 是 Microsoft Visual C++ Redistributable 的 64 位版本
  • Unity3D实现Shader开发之径向模糊(实现镜头中间不模糊,四周模糊的效果)
  • Reflexxes Type II 机器人和运动控制系统的实时运动规划库
  • 从云原生到 AI 原生,谈谈我经历的网关发展历程和趋势
  • 【快速入门】SpringMVC
  • 【k8s应用管理】kubernetes 存储管理