How-To: Overwrite color and material information in Exchange

This article is only about overwriting already existing colors in a model loaded with HOOPS Exchange, for further explanations about how to interpret them, please look at the following article: How-To: Read color and material information from Exchange

Here’s a code sample that describes how to overwrite the color of a whole model, the main steps are:

  1. Add the color(s) in the global data if it (they) doesn’t (don’t) already exist
  2. Eventually, create a material that refers to the previous color(s) (Optional)
  3. Create a new style that will point to the desired color (or material)
  4. Edit the A3DGraphicsData of the object you want to overwrite
// Specify input file
A3DImport sImport(acSrcFileName); // see A3DSDKInternalConvert.hxx for import and export detailed parameters

sHoopsExchangeLoader.Import(sImport);

// First, we need to add the color to the global data...
A3DUns32 rgbColorIdx = 0;

A3DGraphRgbColorData rgbColorData;
A3D_INITIALIZE_DATA(A3DGraphRgbColorData, rgbColorData);

rgbColorData.m_dRed = 1.0;		// Colors are defined between 0.0 and 1.0
rgbColorData.m_dGreen = 0.0;	// Colors are defined between 0.0 and 1.0
rgbColorData.m_dBlue = 0.0;		// Colors are defined between 0.0 and 1.0

A3DStatus insertColor = A3DGlobalInsertGraphRgbColor(&rgbColorData, &rgbColorIdx);

/*
// ... Then, we may eventually create a rendering material...
A3DUns32 materialIdx = 0;

A3DGraphMaterialData materialData;
A3D_INITIALIZE_DATA(A3DGraphMaterialData, materialData);
materialData.m_uiAmbient = rgbColorIdx;
materialData.m_uiDiffuse = rgbColorIdx;
materialData.m_uiEmissive = rgbColorIdx;
materialData.m_uiSpecular = rgbColorIdx;
materialData.m_dAmbientAlpha = 1.0;
materialData.m_dDiffuseAlpha = 1.0;
materialData.m_dEmissiveAlpha = 1.0;
materialData.m_dSpecularAlpha = 1.0;
materialData.m_dShininess = 0.0;

A3DStatus insertMaterial = A3DGlobalInsertGraphMaterial(&materialData, &materialIdx);
*/

// ... Finally, we must create the new corresponding style
A3DUns32 styleIdx = 0;

A3DGraphStyleData styleData;
A3D_INITIALIZE_DATA(A3DGraphStyleData, styleData);
styleData.m_bMaterial = false;				// set this to true if materials are used
styleData.m_bVPicture = false;
styleData.m_dWidth = 0.1;
styleData.m_ucTransparency = 255;
styleData.m_uiRgbColorIndex = rgbColorIdx;	// or materialIdx if refering materials

A3DStatus insertStyle = A3DGlobalInsertGraphStyle(&styleData, &styleIdx);

// Now we edit the root PO of the modelFile to overwrite the whole model color
// Notice thats it's also possible to do this individually for each RI instead
A3DAsmModelFileData modelData;
A3D_INITIALIZE_DATA(A3DAsmModelFileData, modelData);
A3DAsmModelFileGet(sHoopsExchangeLoader.m_psModelFile, &modelData);

A3DRootBaseWithGraphicsData rbGraphicsData;
A3D_INITIALIZE_DATA(A3DRootBaseWithGraphicsData, rbGraphicsData);
A3DRootBaseWithGraphicsGet(modelData.m_ppPOccurrences[0], &rbGraphicsData);

A3DGraphicsData graphicsData;
A3D_INITIALIZE_DATA(A3DGraphicsData, graphicsData);
A3DGraphicsGet(rbGraphicsData.m_pGraphics, &graphicsData);

graphicsData.m_uiStyleIndex = styleIdx;						// We pass here the index of the new style we have created above
graphicsData.m_usBehaviour |= kA3DGraphicsSonHeritColor;	// It's important to also set the inheritence, otherwise already existing colors will not be overwritten

A3DGraphics* newGraphics;
A3DGraphicsCreate(&graphicsData, &newGraphics);
rbGraphicsData.m_pGraphics = newGraphics;

A3DRootBaseWithGraphicsSet(modelData.m_ppPOccurrences[0], &rbGraphicsData);

// Specify output file
A3DExport sExport(acDstFileName); // see A3DSDKInternalConvert.hxx for import and export detailed parameters

sHoopsExchangeLoader.Export(sExport);

Despite that many objects have a corresponding A3DRootBaseWithGraphics , please notice that some of them should rather be defined on the representation items level !