Now that we can select on a 3D object, we can easily change it's material properties. This step will review how to modify the color of an object and make it transparent.
Segment or object color is modified by calling the HOOPS/3dGS routine ::Set_Color. There are several variants of this routine, including ::Set_Color_By_Value and ::Set_Color_By_Index, to allow you to set a specific floating-point color value, or specify an index into a HOOPS/3dGS colormap.
Color generally behaves just like any other HOOPS/3dGS attribute, in that you set it on a segment it it applies to all objects in that segment. However, there are a few exceptions. For example, color can be locally set on 'shell' and 'mesh' primitives by first opening up the geometry and then setting the color. This enables you to have a group of shells/meshes in a single segment, but each with a different color. Refer to Section 2.8.2 of the HOOPS/3dGS Programming Guide for more info on setting local attributes on geometry.
Simply calling ::Set_Color("faces = blue") tells the system to set the diffuse channel to blue, but you can specify the values for the various color channels which include specular, transmission, gloss, etc... For more detailed information, refer to Section 5.1 of the HOOPS/3dGS Programming Guide.
Let's enhance the application to change the color of the selected items. Since our application currently selects on 'segments', we'll be opening up the key of a segment and setting segment-level color. First, add a new GUI button that is mapped to the following method:
Note that we've added a new function to HSpheresView called 'ChangeMaterial'. Let's write this method to go through the segments in the selection list and make them 'blue' by calling ::Set_Color_By_Value. We need to DeSelect an item before modifying it's color:
void HSpheresView::ChangeMaterial(){HSpheresSelectionSet * selection = (HSpheresSelectionSet*)GetSelection();while( selection->GetSize() > 0 ){HC_KEY key = selection->GetAt(0);// DeSelect the item firstselection->DeSelect(key);// open up the segment and set the new colorHC_Open_Segment_By_Key(key);HC_Set_Color_By_Value("faces", "RGB", 0.0, 0.0, 1.0);HC_Close_Segment();}// redraw the scene to reflect the attributes changesUpdate();}
Test out the new operator by selecting on some geometry, and then clicking your new 'ChangeMaterial' GUI button.
To make an object transparent in HOOPS/3dGS, we simply change the 'transmission' color channel. The color value used for transmission determines the component of the light that 'shines through' the face. If you want to allow 50% of light to pass-through an object, you would set the transmission to 'gray'. (or R=G=B=0.5, if setting _By_Value). Let's enhance our HSpheresView::ChangeMaterial method so that it sets transmission to gray, by adding the following line and then seeing what the result looks like:
HC_Set_Color("faces = (transmission = gray)");
There are a variety of transparency rendering options which control how the system sorts transparent objects (which is required to ensure a correct picture). These are discussed in detail in transparency rendering options section of the HOOPS/3dGS Programming Guide , and their reference is located in the transparency suboption of ::Set_Rendering_Options. Let's experiment with one of them.
When the user is viewing a 'static' scene (they've finished interacting and are looking at the picture, or a hardcopy is being generated), we generally want to use the default transparency sorting algorithm (z-sort), or perhaps use a more accurate and more costly sorting algorithm to ensure a correct picture (painter's). But when the user is interacting with a large scene containing many transparent objects, we may want to visually 'degrade' the scene in exchange for faster rendering. We can do this by setting the transparency style Rendering_Option to 'screendoor', and turning off 'transparency hsra' entirely:
HC_Set_Rendering_Options("transparency = (style = screendoor, hsra = none)");
The HOOPS/MVO framerate logic, reviewed in the LMV Tutorial, enables the 'screendoor' transparency mode to do it's work. It visually degrades transparent geometry (in addition to various other techniques) in order to maintain a constant framerate.
Exercise: The HoopsPartViewer contains a GUI dialog class called MaterialPropertiesDlg, which provides an nice interface to changing the material properties of selected segments. Copy the dialog class and hook it up inside your 'spheres' application. (The source code for the HoopsPartViewer is located in /<hoops_install_dir>/demo/mfc/hoopspartviewer)