NWcreate: Color of Geometry does not take effect on Cylinder

By Xiaodong Liang

Issue
I’m using nwcreate API and having a problem with setting color for geometry stream. It’s ok when set color for creating Conic, but no effect when set color for creating Cylinder like this:

LiNwcGeometryStreamBegin (stream, LI_NWC_VERTEX_COLOR);           LiNwcGeometryStreamColor(stream,1,0,0,0);             LiNwcGeometryStreamCylinder(stream,centerPt,centerPt1,Radius);

Solution

It is because the Cylinder functions are creating parametric primitives (as opposed to the Conic function which is just making a standard primitive). Parametric primitives don’t currently support per-vertex colors. We normally recommend you set the color of an item by attaching a material attribute to the node. The code snippet below is a demo.

static     void doExport()    {       // the nwc file to export       LtWideString wfilename  = L"test.nwc";       // nwc scene       LtNwcScene scene;          //nwc Geometry       LtNwcGeometry geom;        // nwc stream       LtNwcGeometryStream stream;                //create the Scene       scene = LiNwcSceneCreate();       // create the geometry       geom = LiNwcGeometryCreate();       // open the stream to define the geometric data that       // will be stored in a geometry node.       stream = LiNwcGeometryOpenStream(geom);               //define the parameters       LtPoint centerPt = { 10, 10, 0 };       LtPoint centerPt1 = { 10, 10, 50 };        LtUnitVector vector = {0,0,1};       double Radius = 10;             // ** wrong way if the geometry creates parametric primitives **        //LiNwcGeometryStreamBegin (stream, LI_NWC_VERTEX_COLOR);           //LiNwcGeometryStreamColor(stream,1,0,0,0);             //LiNwcGeometryStreamCylinder(stream,centerPt,centerPt1,Radius);        //**********************            //**** worked way******       //start defining a piece of geometry        LiNwcGeometryStreamBegin (stream, 0);       // draw a cylinder       LiNwcGeometryStreamCylinder(stream,centerPt,centerPt1,Radius);        // end       LiNwcGeometryStreamEnd(stream);              // define the material       LtNwcMaterial _material;       _material = LiNwcMaterialCreate();       LiNwcMaterialSetDiffuseColor(_material, 0.9,0.0,0.0);       LiNwcMaterialSetAmbientColor(_material, 0.9,0.0,0.0);             //Add an attribute of color to the node        LiNwcNodeAddAttribute(geom, _material);       LiNwcMaterialDestroy(_material);       //*****************    
        //close the stream       LiNwcGeometryCloseStream( geom, stream);            //add the geometry to the scene and cleanup geom       LiNwcSceneAddNode (scene, geom);       LiNwcGeometryDestroy(geom);            //write out the NWC file       LiNwcSceneWriteCacheEx(scene, wfilename, wfilename, 0, 0);         LiNwcSceneDestroy(scene);         }

Comments

Leave a Reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading