No announcement yet.

How to draw many (about millions) rectangles with different colors?

  • Filter
  • Time
  • Show
Clear All
new posts

  • How to draw many (about millions) rectangles with different colors?

    Dear openInentor programmers,
    How to draw many (about millions) rectangles with different colors? and they are showed like attchments.
    I use SoIndexedFaced,and find that it is not easy to realize it? Could you tell me how to realize ?
    Thank you very much.
    Attached Files

  • #2
    I can give you some general advice on this topic. Also some specific advice (eventually), but I am not sure what part of this task is causing difficulty.

    The most convenient nodes to use for rectangles (quads) are:
    • SoFaceSet : if rectangles do not share vertices.
    • SoIndexedFaceSet : if rectangles DO share vertices
    One other option is SoBufferedShape, which can be more efficient for large geometry, but is more complicated to use. It's usually best to start with one of the FaceSet nodes and get the right rendering before trying to use BufferedShape.

    Here are the general steps for using SoIndexedFaceSet. I'll use C++, but it's similar in .NET or Java.
    Create the geometry:
    1. Create an SoVertexProperty node.
    2. Set the coordinates in the 'vertex' field of the SoVertexProperty node.
      Note: If your coordinates are already stored in a float array as X, Y, Z, X, Y, Z, ... you can set them without copying the data. See SoMFVec3f::setValuesPointer().
    3. Create an SoIndexedFaceSet node.
    4. Set the VertexProperty node in the 'vertex' field of the IndexedFaceSet.
    5. Set the coordinate indices in the 'coordIndex' field of the IndexedFaceSet.
      • For each rectangle you need 5 values: 4 indices plus a "-1" value to mark the end of the face.
        For example: 0, 1, 2, 3, -1, 2, 3, 4, 5, -1, ...
      • Just like vertices, if you already have coordinate indices in an int array, you can set them without copying the data.
      • After adding the IndexedFaceSet to your scene graph, you should be able to see the shape rendered (without color).
    Set a color for each rectangle:
    • First, you need to tell the IndexedFaceSet that the colors are "per-face".
    • Second, you need to store the color values in the scene graph.
    There are two ways to do that:
    1. Using the SoVertexProperty node.
      This is the most efficient way, because packed RGBA values can be sent directly to the GPU.
      Using the VertexProperty node you created:
      • Set the 'materialBinding' field to PER_FACE.
      • In the 'orderedRGBA' field, set N color values, where N is the number of rectangles.
        Each RGBA value is a 32-bit, unsigned int.
        Using the SbColor class, the R, G, and B values are float in the range 0..1.
        You can convert the color value to packed RGBA using the method SbColor::getPackedValue().

    2. Using SoMaterialBinding and SoMaterial nodes
      ​​​​​​​This is more convenient and Open Inventor will convert the color values to RGBA internally.
      • Create an SoMaterialBinding node and set the 'value' field to PER_FACE.
      • Create an SoMatieral node and set N values in the 'diffuseColor' field, where N is the number of rectangles.
      • Add both nodes to the scene graph before the IndexedFaceSet node.
    Let us know if you have more questions.