drawing solid geometry osg

According to friends of the code, implement, and then draw out their own cube or something, it is not difficult to find. . . Was added and then is drawn to the vertex coordinates coordinate array

A, openGL, define 10 kinds of primitives: GL_POINTS, GL_LINES, GL_LINE_STRIP, GL_LINE_LOOP, GL_TRIANGLES, GL_TRIANGLE_STRIP, GL_TRIANGLE_FAN, GL_QUADS, GL_QUAD_STRIP, GL_POLYGON

The role of ten kinds of primitives is relatively simple, not introduced.

Two, OSG is provided in FIG element by

bool addPrimitiveSet(PrimitiveSet* primitiveset);

All elements in classes inherit PrimitiveSet, different classes of primitives can be interpreted (not just those of OpenGL embodiment) in a different manner vertex array

四, osg :: DrawArrays

Five, osg :: DrawElementsUInt

Such interpretation relies on two things vertex array, a first course, a vertex array, a second array index. What indexed array is it? for example.

There follows two triangles (ABC) (BCD), if you want to draw GL_TRIANGLES manner, if DrawArrays, interpolation is necessary to vertex arrays 6 vertex ABCBCD, if huge number of triangles, there are so many repeated vertices are unacceptable . The use osg :: DrawElementsUInt can solve this problem.

DrawElementsUInt to accept an indexed array IndexArray, is an index of the vertex array, so vertex arrays can only put four vertices ABCD, and the index array is (123234) six figures, so the cost of storage on much smaller.

DrawElemetnsUInt(osg::PrimitiveSet::TRIANGLES, IndexArray->size(), &IndexArray->front())

#include <Windows.h>
#include <osgViewer/Viewer>
#include <osg/Group>
#include <osgDB/ReadFile>

int main(void)
{
osg::ref_ptrosgViewer::Viewer viewer = new osgViewer::Viewer;

osg::ref_ptr<osg::Group> sceneRoot = new osg::Group;

//创建几何体
osg::ref_ptr<osg::Geometry> geometry = new osg::Geometry;
osg::Vec4dArray* color = new osg::Vec4dArray;
color->push_back(osg::Vec4d(0.0, 0.0, 1.0, 1.0));
geometry->setColorArray(color);
osg::Vec3Array* vecArray = new osg::Vec3Array;
vecArray->push_back(osg::Vec3d(0.0,0.0,0.0));
vecArray->push_back(osg::Vec3d(4.0,0.0,0.0));
vecArray->push_back(osg::Vec3d(2.0, 4.0, 0.0));
vecArray->push_back(osg::Vec3d(2.0, 2.0, 4.0));
geometry->setVertexArray(vecArray);

osg::ref_ptr<osg::DrawElementsUInt> drawElemUInt = new osg::DrawElementsUInt(GL_TRIANGLE_FAN);
drawElemUInt->push_back(0);
drawElemUInt->push_back(1);
drawElemUInt->push_back(1);
drawElemUInt->push_back(2);
drawElemUInt->push_back(2);
drawElemUInt->push_back(0);

drawElemUInt->push_back(0);
drawElemUInt->push_back(1);
drawElemUInt->push_back(1);
drawElemUInt->push_back(3);
drawElemUInt->push_back(3);
drawElemUInt->push_back(0);

drawElemUInt->push_back(0);
drawElemUInt->push_back(2);
drawElemUInt->push_back(2);
drawElemUInt->push_back(3);
drawElemUInt->push_back(3);
drawElemUInt->push_back(0);


drawElemUInt->push_back(1);
drawElemUInt->push_back(3);
drawElemUInt->push_back(3);
drawElemUInt->push_back(2);
drawElemUInt->push_back(2);
drawElemUInt->push_back(1);

geometry->addPrimitiveSet(drawElemUInt);
osg::Geode* geode = new osg::Geode;
geode->addDrawable(geometry);

sceneRoot->addChild(geode);

viewer->setSceneData(sceneRoot);
viewer->realize();
return viewer->run();

}

Guess you like

Origin blog.csdn.net/u013693952/article/details/90711812