Board technology to create trees

Source: Internet
Author: User

# Include <osgviewer/Viewer> <br/> # include <OSG/positionattitudetransform> <br/> # include <OSG/matrixtransform> <br/> # include <osgdb/readfile> <br/> # include <OSG/image> <br/> # include <OSG/geometry> <br/> # include <OSG/vec3> <br/> # include <OSG/vec2> <br/> # include <OSG/billboard> <br/> # include <OSG/texture2d> </P> <p> OSG:: ref_ptr <OSG: node> createbillboardtree (OSG: ref_ptr <OSG: Image> image) <br/> {<br/> // create a quadrilateral <br/> OSG: ref_ptr <OSG: geometry> geometry = new OSG: geometry; </P> <p> // set the vertex <br/> OSG: ref_ptr <OSG: vec3array> V = new OSG: vec3array; <br/> V-> push_back (OSG: vec3 (-0.5f, 0.0f,-0.5f); <br/> V-> push_back (OSG: vec3 (0.5f, 0.0f,-0.5f); <br/> V-> push_back (OSG: vec3 (0.5f, 0.0f, 0.5f); <br/> V-> push_back (OSG:: vec3 (-0.5f, 0.0f, 0.5f); </P> <p> geometry-> setvertexarray (v. get (); </P> <p> // set the normal <br/> OSG: ref_ptr <OSG: vec3array> normal = new OSG: vec3array; <br/> normal-> push_back (OSG: vec3 (1.0f, 0.0f, 0.0f) ^ OSG: vec3 (0.0f, 0.0f, 1.0f )); <br/> geometry-> setnormalarray (normal. get (); <br/> geometry-> setnormalbinding (OSG: geometry: bind_overall ); </P> <p> // set texture coordinates <br/> OSG: ref_ptr <OSG: vec2array> VT = new OSG: vec2array; <br/> VT-> push_back (OSG: vec2 (0.0f, 0.0f); <br/> VT-> push_back (OSG: vec2 (1.0f, 0.0f )); <br/> VT-> push_back (OSG: vec2 (1.0f, 1.0f); <br/> VT-> push_back (OSG: vec2 (0.0f, 1.0f )); <br/> geometry-> settexcoordarray (0, VT. get (); </P> <p> // draw a quadrilateral <br/> geometry-> addprimitiveset (New OSG: drawarrays (OSG: primitiveset: quads, 0, 4); </P> <p> If (image. get () <br/>{< br/> OSG: ref_ptr <OSG: stateset> stateset = new OSG: stateset; <br/> OSG :: ref_ptr <OSG: texture2d> Texture = new OSG: texture2d; <br/> // associate image <br/> texture-> setimage (image. get (); <br/> // associate the texture2d texture object. The third parameter defaults to on <br/> stateset-> settextureattributeandmodes (0, texture, OSG: stateattribute:: On); <br/> // enable hybrid <br/> stateset-> setmode (gl_blend, OSG: stateattribute: On ); <br/> // disable illumination <br/> stateset-> setmode (gl_lighting, OSG: stateattribute: Off ); </P> <p> geometry-> setstateset (stateset. get (); <br/>}</P> <p> // create a billboard object <br/> OSG: ref_ptr <OSG :: billboard> billboard1 = new OSG: Billboard; <br/> // you can set the rotation mode to the point of view <br/> billboard1-> setmode (OSG: billboard: point_rot_eye ); <br/> // Add a drawable and set its location. The default location is OSG: vec3 (5.0f, 0.0f, 0.0f). <br/> billboard1-> adddrawable (geometry. get (), OSG: vec3 (5.0f, 0.0f, 0.0f); </P> <p> return billboard1.get (); <br/>}< br/> int main (INT argc, char * argv []) <br/>{< br/>/* qapplication A (argc, argv ); <br/> mytest W; <br/> W. show (); <br/> return a.exe C (); */</P> <p> OSG: ref_ptr <osgviewer: viewer> viewer = new osgviewer :: viewer; <br/> OSG: ref_ptr <OSG: group> group = new OSG: group; <br/> OSG: ref_ptr <OSG :: image> image = osgdb: readimagefile ("images/tree0.rgba"); <br/> // zoom <br/> OSG: ref_ptr <OSG :: positionattitudetransform> PAT = new OSG: positionattitudetransform; <br/> pat-> setscale (OSG: vec3 (5.0f, 5.0f, 5.0f )); <br/> pat-> addchild (createbillboardtree (image. get (); </P> <p> group-> addchild (Pat. get (); <br/> OSG: node * cow = osgdb: readnodefile ("cow. OSG "); <br/> group-> addchild (COW); </P> <p> viewer-> setscenedata (group. get (); <br/> viewer-> realize (); </P> <p> return viewer-> Run (); <br/>}

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.