OSG's Way to the Pit

Keywords: C++ Attribute Fragment

The so-called "pit entry" is just an excuse for one's lack of proficiency.
Now I have been studying OSG for three weeks. I just do some simple demo s with books and videos. Sometimes I feel like I can't get started.
I don't know if I am a career change. Before I studied point cloud data processing, I had a little opengl foundation. Although I worked in a university, the project was the development of an immersive VR system. So far, I have been working alone.
. . . .
Some go too far. The above is pure nonsense and illogical...

Intermittent use of osg for more than half a month, gives me the feeling that: there is a problem do not know how to deal with it! For example, the status of a graphics node, but visualization does not have the desired effect, the program is not wrong.
The reason for the above troubles may be that they do not have a thorough understanding of opengl and computer graphics, and the lack of theory has resulted in the inability to effectively solve practical problems, which is one of them. Secondly, they may not have enough personal experience and think much. They are accustomed to being pointed out by teachers and classmates in school, and they are lack of in-depth consideration. Experience needs to be accumulated in future work. More thinking, more perception, more summary, not afraid of mistakes
It's broken. It's a bit too far. Let's go back to osg. If you don't have a good memory, just record your little gains.

render state

In osg, when setting the rendering state of a node, the state will be given to the current node and its children. Therefore, in order to achieve multi-node multi-state rendering, we must pay attention to the parent-child relationship between nodes. It is better for a node to set its desired state, unless the parent node and its children have the same rendering state.
The management of rendering state is managed by osg::StateSet, which can be added to any node (node, group, geode, etc.) and DrawAble class. If you want to set the value of the rendering state, you need:

  1. The stateset instance of a node is obtained.

  2. Set the rendering attribute s and mode s of the instance
    Example: osg:: StateSet * state = obj - > getOrCreatStateSet(); where OBJ can be a node or a Drawable instance, and the getOrCreatStateSet() method is declared in osg::Node, which means that geode or group can be used.

state>setMode(GL_LIGHTING,osg::StateAttribute::OFF);//Turn off the lights
state->setTextureAttributeAndModes(0,texture,osg::StateAttribute::ON);//Setting Texture

//Open shader
osg::ref_ptr<osg::Program> shaderProg = new osg::Program;
shaderProg->addShader(new osg::Shader(osg::Shader::VERTEX,vertexShader));
shaderProg->addShader(new osg::Shader(osg::Shader::FRAGMENT,fragShader));
state->setAttributeAndModes(shaderProg,osg::StateAttribute::ON);  //Setting Rendering Properties and Patterns

->setMode(GL_DEPTH_TEST,osg::StateAttribute::OFF);//Management Deep Testing

//Setting the order of rendering, the smaller the first parameter is, the more advanced the rendering is. By default, the first parameter is -1.
state->setRenderBinDetails(10, "RenderBin"); //Default Rendering Sort
state->setRenderBinDetails(100,"DepthSortedBin");  //From far to near
state->setRenderBinDetails(1000,"TraversalOrderBin"); //In ergodic order

//Open Hybrid Transparency
state->setMode(GL_BLEND,osg::StateAttribute::ON);  //Setting Rendering Mode

Wait a minute, wait a minute.

geometry and geode

Obviously, geode is a geometric node and a leaf node. The geometry class manages all kinds of geometries in osg.
Personal summary: When using geode to draw the geometry of osg, always:

  1. Declare geode nodes

  2. Creating Geometric Objects

  3. Setting parameters for geometric objects

  4. Apply for an osg::ShapeDrawable

  5. geode->addDrawable
    The Works of Liezi:

//Draw a cylinder
osg::TessellationHints *hins = new osg::TessellationHints;
hins->setDetailRatio(1.0f);//The precision of setting cylinder is 0.1. The smaller the value, the smaller the precision.
osg::ref_ptr<osg::Cylinder> cy = new osg::Cylinder;  //Cylinder
osg::ref_ptr<osg::ShapeDrawable> sd = new osg::ShapeDrawable(cy); //Initialize shapedrawable instances directly with geometric objects
cy->setCenter(osg::Vec3(400.0,300,0));
cy->setHeight(0.1);
cy->setRadius(150);
sd->setTessellationHints(hins);
geode->addDrawable(sd);  //Necessary (to really draw here)
/*The verbose code above can certainly be written as follows:*/
geode->addDrawable(new osg::ShapeDrawable(osg::Cylinder(center),Radius,Height),teseel);

//Draw a box
osg::ref_ptr<osg::TessellationHints> hints = new osg::TessellationHints;
//Setting accuracy
hints->setDetailRatio(0.1);
osg::ref_ptr<osg::ShapeDrawable> shape  = new osg::ShapeDrawable(new osg::Box(osg::Vec3(x,y,z),long,Wide, high),hints.get());
geode->addDrawable(shape);

The influence of TessellationHints parameters on cylindrical geometry and the shape drawable inheritance relationship:
!

When customizing a geometry:

  1. Apply for geometry objects, customize drawn vertices, normal vectors, colors, etc. (if needed) arrays

  2. Pass various arrays of your own to geode and set the binding mode.

  3. Set the association between vertices (that is, what graphics to draw)

  4. Add geometry objects to geode - > addDrawable (geometry);
    In the third step, geom - > addPrimitiveSet (); the function requires a connection between a Drawarray pointer and the original point

    osg::ref_ptr<osg::Geode> geo = new osg::Geode;
    osg::ref_ptr<osg::Geometry> geom = new osg::Geometry();
    osg::ref_ptr<osg::Vec3Array> vex = new osg::Vec3Array;
    osg::ref_ptr<osg::Vec4Array> color = new osg::Vec4Array;
    osg::ref_ptr<osg::Vec3Array> normal = new osg::Vec3Array;
    osg::ref_ptr<osg::LineWidth> width = new osg::LineWidth;  //line width
    
//Setting Normal Vector and Its Binding Mode
    geom->setNormalArray(normal,osg::Array::Binding::BIND_OVERALL);
    normal->push_back(osg::Vec3(0.0,-1.0,0.0));

 //Setting vertices
    vex->push_back(osg::Vec3(-10.5,5,-10.0));
    vex->push_back(osg::Vec3(10.5,5,-10.0));
    vex->push_back(osg::Vec3(10.0,5,10.0));
    vex->push_back(osg::Vec3(-10.0,5,10.0));
    geom->setVertexArray(vex.get());
    
//Setting colors
    color->push_back(osg::Vec4(0.1,0.2,0.3,0.5));
    color->push_back(osg::Vec4(1.1,0.9,0.3,0.50));
    color->push_back(osg::Vec4(0.2,0.5,0.3,0.50));
    color->push_back(osg::Vec4(0.4,0.2,0.7,0.50));
    geom->setColorArray(color);
    geom->setColorBinding(osg::Geometry::AttributeBinding::BIND_PER_VERTEX); //Setting Texture Binding Mode
    osg::ref_ptr<osg::TessellationHints> hints = new osg::TessellationHints;
    hints->setDetailRatio(0.1);
//Setting Transparency
    geom->getOrCreateStateSet()->setMode(GL_BLEND,osg::StateAttribute::ON);
    

    /*osg::DrawArrays Equivalent to the encapsulation of glDrawarray in opengl*/
    geom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::Mode::LINE_LOOP,0,4)); //Set the way the vertices are associated (here, connected as segments)

    //Setting Linewidth
    width->setWidth(20.0);
    geom->getOrCreateStateSet()->setAttributeAndModes(width);

    geo->addDrawable(geom.get());

Gem - > addPrimitiveSet (new osg:: DrawArrays (osg:: PrimitiveSet:: Mode:: LINE_LOOP, 0,4)); basic primitives and drawable inheritance:

Posted by butsags on Fri, 07 Jun 2019 18:45:09 -0700