Geometry in the OSG

Source: Internet
Author: User

Osg::shape class

Inherit from the Osg::object class;

The Osg::shape class is a base class of various embedded geometries that can be used not only for culling and collision detection, but also for generating pre-defined geometry objects;

Common inline geometries include:

Osg::shapedrawable class:

Derived from OSG::D rawable class;

The method of associating Osg::shape is provided in the constructor of Osg::shapedrawable class.

Also, because it inherits from OSG::D the Rawable class, its instance needs to be added to the leaf node in order to be drawn by the instance.

Createtexturedquadgeometry function parsing

API Explanation:

Osg_export geometry* osg::createtexturedquadgeometry (const VEC3 & Corner,const VEC3 & Widthvec,const VEC3 & Heightvec,float l,float b,float R,float  for-from-left bottom (l,b) to right Top (r,t). 

It seems that the corner variable is the starting point, drawing a quad rectangle from this starting point according to Widthvec and Heightvec, as for the l,b,r,t behind it, which seems to be related to the two-dimensional map, that is, to take the part of the map.

According to this a little explanation, completely not enough, after various attempts, the function of each parameter is basically as follows.

First say l,b,r,t,l,b generally take 0.0,r,t if not equal, then the two-dimensional map will become a rectangle. In other words, my black-and-white checkers will turn into black and white squares. If they are equal, they become black and white squares.

Again corner, this is the starting point from the lower left corner, which is the world coordinate of this starting point.

For example, I want the resulting square ground, facing up, and in the center of the world, so Corner,widthvec,heightvec should be set.

Corner ( -50.0, -50.0, -3.0) Widthvec (0.0, 100.0, -3.0) Heightvec (100.0, 0.0, 0.0)

That is, the starting point is at the left rear of the camera, and 3.0 is the meaning of making it slightly sinking. Then go 100 in the Y direction and draw 100 in the x direction. Note that 3.0 is specified on one of the widht and height, if the z-values of the two vectors are 3.0, the drawn plane is oblique.

Osg--geometry

In OpenSceneGraph modeling and drawing tools, the Osg::geometry class has a very important position. Using the Geometry class, users can draw simple line segments, triangles, and polygons by specifying the vertex, color, and normal way. and add the result of the drawing to the leaf node Geode of the scene.

Using the Gemetry class for simple graphical drawing can generally be divided into the following steps:

1, set up a new geometry instance, for input vertex, color and other data Osg::vec3array,osg::vec4array variable array, and used to establish the point index OSG::UBYTEARRAY,OSG: intarray, etc. The sample code is as follows:

Osg::ref_ptr<osg::geometry> Geo =NewOsg::geometry;osg::vec3array* Vecarray =NewOsg::vec3array;//array of vertex coordinatesOsg::ubytearray* Vecindex =NewOsg::ubytearray;//Vertex index ArrayOsg::vec4array* Colarray =NewOsg::vec4array;//array of color RGB valuesOsg::ubytearray* Colindex =NewOsg::ubytearray;//Color Index ArrayOsg::vec3array* Norarray =NewOsg::vec3array;//array of normal coordinatesOsg::ubytearray* Norindex =NewOsg::ubytearray;//Normal index Array

2, input vertex data to the vertex coordinate variable array, Osg::vec3array is a template class, inherit from the STL vector, so you can use the Push_back method to feed osg::vec3 coordinate data. Color data, the normal coordinate data is saved similar to this, but generally the color data is saved with Osg::vec4array, in addition to the RGB values, an alpha component is included. The sample code is as follows:

Vecarray->push_back (OSG::VEC3 (1.0,0.0,1.0)); Vecarray->push_back (OSG::VEC3 (-1.0,0.0,1.0)); Vecarray->push_back (OSG::VEC3 (-1.0,0.0, -1.0)); Vecarray->push_back (OSG::VEC3 (1.0,0.0, -1.0)); Colarray->push_back (OSG::VEC4 (1.0,0.0,0.0,1.0));//RedColarray->push_back (OSG::VEC4 (0.0,1.0,0.0,1.0));//GreenColarray->push_back (OSG::VEC4 (0.0,0.0,1.0,1.0));//BlueColarray->push_back (OSG::VEC4 (1.0,1.0,1.0,1.0));// WhiteNorarray->push_back (OSG::VEC3 (0.0, -1.0,0.0));

3. Set the index values for vertex coordinates, color, and normal coordinates. Index values can be stored using an integer-type list of vector groups that the user uses to get the data at the specified position from an array of vertex coordinates, thereby connecting each vertex in the specified order to form the specified shape. The user can also choose not to set the indexed array, at which point the interior of the OSG is sorted in the default index order. For example, the above coordinate and color information, if the default index arrangement to draw a quadrilateral, then the resulting results such as left-hand image, and if you use an array of indexed values to retrieve and arrange the coordinates or color data, then you can obtain a different result, such as:

Colindex->push_back (3); Colindex->push_back (2); Colindex->push_back (1); Colindex->push_back (0);

Note that you can index the same coordinate or color data multiple times, but if you have specified that an index group will be used in the drawing, the vector group cannot be empty, or a memory error will be raised.

4, set the data to the Geometry class.

Geo->Setvertexarray (Vecarray), geo-setvertexindices (vecindex), Geo - Setcolorarray (Colarray), geo-setcolorindices (colindex), Geo-setcolorbinding (OSG:: Geometry::bind_per_vertex), Geo-Setnormalarray (Norarray), Geo-setnormalindices ( Norindex); Geo->setnormalbinding (osg::geometry::bind_per_primitive);

If you want to use the default index order, you do not have to set the index list of vertices, colors, or normals, that is, Block setvertexindices,setcolorindices and setnormalindices three segments.

It is important to note that the previous program also set the color data, the way the normal data and vertex data binding, namely setcolorbinding and setnormalbinding, binding data in the following ways:

Bind_off Unbind At this point, the color data or the normal data does not have any relation to the vertex data, and the color and normal direction of the vertex data are determined entirely by default values.
Bind_overall Bind all Geometry At this point, the color array or the normal coordinate array only needs to hold one data, which affects all vertex coordinates of this geometry class. For example, if you bind red to all of the geometry, all objects drawn by this class are red.
Bind_per_primitive Binding geometry-by-individual At this point, the number of data stored in the array of colors or in the normal coordinate arrays should be the same as the number of geometry the user will draw. For example, when a user draws two quads based on 8 vertices, you can set two normal coordinates for each of them, and use this parameter to bind.
Bind_per_vertex Binding points by point Points-by-point binding. For example above, the four color data is bound to four vertex coordinates, which can achieve the transition between vertex colors.

Reprinted from: http://hi.baidu.com/wangr02/blog/item/78924216e871bd4b20a4e9db.html

Original OSG Basic drawing of Discussion–2:geometry class (2)

5. Add geometry to the Geometry class and use the Adddrawable method of the Geode class to add the geometry class to a leaf node of the scene graph.

Geo->addprimitiveset (new OSG::D rawarrays (osg::P rimitiveset::quads,

0,

4));

In this line of code, the Drawarrays class is used to feed the geometry class information about the new geometry, which is a quads whose vertex coordinates are read from the indexed array, starting with the 1th index value, and reading the 4 index values together to form a quadrilateral shape.

In addition to quads, there are several ways in which Geometry's shape parameters are used for different user needs, as shown in the following list:

POINTS Draw points Draws all the vertices specified by the user.
LINES Draw a line The start point and end point of the line are determined by the two points in the array that are successively adjacent, and when the user provides more than two points, an attempt continues to draw a new line.
Line_strip Draw a multi-segment line The first paragraph of a multi-segment line is determined by the top two points in the array, the beginning position of the remaining segments is the end coordinate of the previous paragraph, and the end position is determined by the subsequent points in the array.
Line_loop Draw a closed line The drawing is the same as a multi-segment line, but the line is closed automatically at the end.
Triangles Draw triangles The three vertices of the triangle are determined by the three adjacent points in the array and plotted in a counterclockwise order, and when the user provides more than three points, an attempt continues to draw a new triangle.
Triangle_strip Draw a multi-segment triangle The first triangle is determined by the first three points in the array, the rest of the triangles are drawn, the starting edge is determined by the last two points of the previous triangle, and the 3rd is determined by the subsequent point in the array.
Triangle_fan Draw triangle Slices The first segment of the triangle is determined by the first three points in the array, the rest of the triangle is drawn, the starting edge is determined by the 1th of the entire array and the last point of the previous triangle, and the 3rd is determined by the subsequent point in the array.
QUADS Draw Quadrilateral The four vertices of the quads are determined by the four adjacent points in the array, plotted in a counterclockwise order, and the user provides more than four points, and will attempt to continue drawing the new quadrilateral.
Quad_strip Draw a multi-segment quadrilateral The starting edge of the first quadrilateral is determined by the first two points in the array, and the vector direction of the edge is determined by the direction of the two-point extension; the opposite edge of the starting edge is determined by the two subsequent points, and if the starting and opposite vectors are in different directions, the quadrilateral will be distorted; Its opposite edge is determined by the subsequent two points and its extended direction.
POLYGON Draw Freeform Draws a polygon based on the number of vertices provided by the user.

Geometry in the OSG

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.