基础图元 简单矢量

创建简单矢量图

基础:

OSG坐标系,与笛卡尔三维坐标系方向相同

步骤:

  1. 申请顶点数组,设置垂直xoy面的正方形的四个顶点

    1
    2
    3
    4
    5
    6
    osg::ref_ptr<osg::Vec3Array> coords = new osg::Vec3Array;

    coords->push_back(osg::Vec3(-10.0, 5.0, -10.0));
    coords->push_back(osg::Vec3(10.0, 5.0, -10.0));
    coords->push_back(osg::Vec3(10.0, 5.0, 10.0));
    coords->push_back(osg::Vec3(-10.0, 5.0, 10.0));
  2. 申请颜色数组,设置半透明的颜色组

    1
    2
    3
    4
    5
    6
    osg::ref_ptr<osg::Vec4Array> colors = new osg::Vec4Array;

    colors->push_back(osg::Vec4f(1.0, 0.0, 0.0, 0.5));
    colors->push_back(osg::Vec4f(0.0, 1.0, 0.0, 0.5));
    colors->push_back(osg::Vec4f(0.0, 0.0, 1.0, 0.5));
    colors->push_back(osg::Vec4f(1.0, 1.0, 0.0, 0.5));
  3. 申请法向量,令光源在正方形的一侧,压入一个法向量

    1
    2
    3
    osg::ref_ptr<osg::Vec3Array> norms = new osg::Vec3Array;

    norms->push_back(osg::Vec3(0.0, -1.0, 0.0));
  4. 申请一个几何Geometry

    1
    osg::ref_ptr<osg::Geometry> geom = new osg::Geometry;
  5. 设置顶点和顶点的关联方式

    1
    2
    geom->setVertexArray(coords.get());
    geom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::Mode::QUADS, 0, 4));
  6. 设置顶点颜色

    1
    2
    geom->setColorArray(colors.get());
    geom->setColorBinding(osg::Geometry::AttributeBinding::BIND_PER_VERTEX);
  7. 设置法向量

    1
    2
    geom->setNormalArray(norms.get());
    geom->setNormalBinding(osg::Geometry::AttributeBinding::BIND_OVERALL);
  8. 申请一个Geode

    1
    osg::ref_ptr<osg::Geode> geode = new osg::Geode;
  9. 在Geode中绘制几何,打开透明

    1
    2
    geode->addDrawable(geom.get());
    geode->getOrCreateStateSet()->setMode(GL_BLEND, osg::StateAttribute::ON);
  10. 将上一节中main函数中的CreateBox()改为CreateSimple(),运行

  11. 申请一个限制线宽

    1
    osg::ref_ptr<osg::LineWidth> width = new osg::LineWidth;
  12. 将顶点关联方式改为LINE_LOOP

    1
    geom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::Mode::LINE_LOOP, 0, 4));
  13. 设置线宽

    1
    width->setWidth(5.0);
  14. 运行,查看结果

完整代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <osgViewer/Viewer>
#include <osgDB/ReadFile>
#include <osgGA/GUIEventAdapter>
#include <osgViewer/ViewerEventHandlers>
#include <osg/Geode>
#include <osg/ShapeDrawable>
#include <osg/Material>
#include <osg/StateSet>
#include <osg/Image>
#include <osg/Texture2D>
#include <osg/LineWidth>
#include <iostream>
using namespace std;

osg::ref_ptr<osg::Geode> CreateSimple() {
osg::ref_ptr<osg::Geode> geode = new osg::Geode;
osg::ref_ptr<osg::Geometry> geom = new osg::Geometry;
osg::ref_ptr<osg::Vec3Array> coords = new osg::Vec3Array;
osg::ref_ptr<osg::Vec4Array> colors = new osg::Vec4Array;
osg::ref_ptr<osg::Vec3Array> norms = new osg::Vec3Array;
osg::ref_ptr<osg::LineWidth> width = new osg::LineWidth;

geode->addDrawable(geom.get());
geode->getOrCreateStateSet()->setMode(GL_BLEND, osg::StateAttribute::ON);
geom->setVertexArray(coords.get());
geom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::Mode::LINE_LOOP, 0, 4));
geom->setColorArray(colors.get());
geom->setColorBinding(osg::Geometry::AttributeBinding::BIND_PER_VERTEX);
geom->setNormalArray(norms.get());
geom->setNormalBinding(osg::Geometry::AttributeBinding::BIND_OVERALL);
width->setWidth(5.0);
geode->getOrCreateStateSet()->setAttributeAndModes(width.get(), osg::StateAttribute::ON);

coords->push_back(osg::Vec3(-10.0, 5.0, -10.0));
coords->push_back(osg::Vec3(10.0, 5.0, -10.0));
coords->push_back(osg::Vec3(10.0, 5.0, 10.0));
coords->push_back(osg::Vec3(-10.0, 5.0, 10.0));

colors->push_back(osg::Vec4f(1.0, 0.0, 0.0, 0.5));
colors->push_back(osg::Vec4f(0.0, 1.0, 0.0, 0.5));
colors->push_back(osg::Vec4f(0.0, 0.0, 1.0, 0.5));
colors->push_back(osg::Vec4f(1.0, 1.0, 0.0, 0.5));

norms->push_back(osg::Vec3(0.0, -1.0, 0.0));

return geode;
}

int main() {

osg::ref_ptr<osgViewer::Viewer> viewer = new osgViewer::Viewer;
osg::ref_ptr<osg::Group> group = new osg::Group;

group->addChild(CreateSimple());
viewer->setSceneData(group.get());

return viewer->run();
}