ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

osg学习-3《绘制三维曲面》

2022-08-21 10:37:53  阅读:179  来源: 互联网

标签:get 三维 geom 曲面 include ref ptr osg


 演示了创建曲面节点的函数

(1)首先设置面数据的顶点坐标,设置面数据在I和J方向的网格个数,以及网格间距,随便设置z和颜色。也可以通过读取外部数据的方式获取顶点坐标和网格上的属性值,根据需要的色标计算每个网格的rgb颜色值。

(2)每个网格一个图元,设置它的坐标索引,并设置颜色数组,通过图元绑定的方式渲染进行显示。

效果如下,下面是代码。

 

// osg_hello.cpp : This file contains the 'main' function. Program execution begins and ends there.
//

#include <iostream>
#include <random>

#ifdef _WIN32
#include <windows.h>
#endif

#include <osgViewer/Viewer>

#include <osg/Node>
#include <osg/Geode>
#include <osg/Group>

#include <osgDB/ReadFile>
#include <osgDB/WriteFile>

#include <osgUtil/Optimizer>


/// <summary>
/// 创建曲面节点对象
/// </summary>
/// <returns></returns>
osg::ref_ptr<osg::Node> createSurface() {
    //创建一个节点对象
    osg::ref_ptr<osg::Geode> geode = new osg::Geode();
    //创建一个集合对象
    osg::ref_ptr<osg::Geometry> geom = new osg::Geometry();

    //面数据网格的相关参数
    int ni = 20;
    int nj = 20;
    float dx = 10.0f;
    float dy = 5.0f;
    float xMid = ni * dx / 2;
    float yMid = nj * dy / 2;

    //创建顶点数组
    osg::ref_ptr<osg::Vec3Array> vertx = new osg::Vec3Array();
    for (int j = 0; j <= nj; j++) {
        float y = j * dy;
        for (int i = 0; i <= ni; i++) {
            float x = i * dx;
            float z = -0.5 * sqrt(pow(x - xMid, 2) + pow(y - yMid, 2));         
            vertx->push_back(osg::Vec3(x,y,z));
        }
    }

    std::random_device dev;
    std::default_random_engine rnd(dev());
    std::uniform_real_distribution<float> u(0,1);

    //设置顶点数据
    geom->setVertexArray(vertx.get());
    //创建颜色数组
    osg::ref_ptr<osg::Vec4Array> vcolor = new osg::Vec4Array();

    for (int j = 0; j < nj; j++) {
        for (int i = 0; i < ni; i++) {
            int index = j * (ni+1) + i;
            //创建四边形定点数组,指定绘图基元为四边形,注意添加顺序
            osg::ref_ptr<osg::DrawElementsUInt> quad =
                new osg::DrawElementsUInt(osg::PrimitiveSet::QUADS, 0);
            quad->push_back(index);
            quad->push_back(index + 1);
            quad->push_back(index + ni + 2);
            quad->push_back(index + ni + 1);
            
            //添加到几何体
            geom->addPrimitiveSet(quad.get());           
            //vcolor->push_back(osg::Vec4(1.0f, 0.0f, 0.0f, 1.0f));  //red
            vcolor->push_back(osg::Vec4(u(rnd), u(rnd), u(rnd), 1.0f));  //random color for simple
        }
    }

    //设置颜色数组
    geom->setColorArray(vcolor.get());


    //设置颜色的绑定方式为单个顶点
    //geom->setColorBinding(osg::Geometry::BIND_PER_VERTEX);
    geom->setColorBinding(osg::Geometry::BIND_PER_PRIMITIVE_SET);

    // 创建法线数组
    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);

    //添加到叶节点
    geode->addDrawable(geom.get());

    return geode.get();
}

int main()
{
    // 创建Viewer对象,场景浏览器创建一个节点。viewer->setSceneData(root.get())viewer->realize(viewer - un();
    osg::ref_ptr<osgViewer::Viewer> viewer = new osgViewer::Viewer();

    //创建场最组节点
    osg::ref_ptr<osg::Group> root = new osg::Group();

    //创建面对象
    osg::ref_ptr<osg::Node> node = createSurface();

    //添加到场景
    root->addChild(node.get());

    //优化场景数据
    osgUtil::Optimizer optimizer;
    optimizer.optimize(root.get());

    //设置场景数据
    viewer->setSceneData(root.get());

    //设置渲染的窗口
    //viewer->setUpViewAcrossAllScreens();  //default on all screens
    viewer->setUpViewOnSingleScreen(0);

    //开始渣染
    viewer->run();

    return 0;
}


 

标签:get,三维,geom,曲面,include,ref,ptr,osg
来源: https://www.cnblogs.com/oliver2022/p/16609419.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有