[C++] how to draw a shaded brep properly?

Ok, I’ve been trying various approaches for a custom object consisting of various BRep patches, but I get mixed results. What I’m trying to do is this: draw a set of Breps in shaded mode with the correct material color and the correct wireframe color.
This is the code I have

// draw the shaded meshes
for (int i = 0; i < m_patches.Count(); ++i)
	if (dp.GetRhinoVP()->DisplayModeIsShaded())
		dp.DrawShadedBrep(m_patches[i], dp.DisplayAttrs()->m_pMaterial, m_caches[i]);

// draw the wireframe
for (int i = 0; i < m_patches.Count(); ++i)
	COLORREF c = dp.DisplayAttrs()->m_ObjectColor;		
	dp.DrawBrep(m_patches[i], c, 1, false, m_caches[i]);

The problems are these:

  • If the layer color is black, the code above gives both black and white edges drawn at the same time, causing z-fighting of both colors giving a jittery color effect (similar when two meshes are overlaid). This is only seen in Perspective mode.
  • If the layer color is anything but black, when selecting the object its material changes to the black (i.e. light gray) material

I don’t know if dp.DisplayAttrs()->m_pMaterial is the right material to use (i.e. my mistake), or if this material is not always set correctly (bug in Rhino WIP).

Also, I’m using the same routine when drawing with custom grips on, where I pass in the display pipeline object found on the CRhinoDrawGripsSettings object. In that case I always get color fighting, except when the layer color is black. And the material is always the black material.

Any hints are welcome on which material, color to use when drawing a custom object BRep representation in shaded mode (with custom grips on and off).

(color fighting - only in perspective mode)

(both selected when layer color is green)

(with grips on - don’t mind the blue grips, that is the custom grip I made)

(color fighting with grips on)

You’ve probably already checked, but since it’s a custom object (and presuming it lives in the doc) is it getting drawn twice - once by Rhino and once by your display pipeline? It looks like what happens when you have two objects on top of each other.

In many cases the display code will call the Draw function for a brep twice per frame. You should be checking the display pipeline functions

  • ObjectsShouldDrawShadedMeshes
  • ObjectsShouldDrawWires

in your draw override

1 Like

Wow, that solves all problems! Thanks :smile: