As already told by @zipit, I'm terribly sorry for coming so late here.
I've prepared a full example showing how to fill a VertexColorTag in the ObjectData::GetVirtualObjects() and use this values to color the triangles shown up in the ObjectData::Draw() method. My code actually uses the VertexColorTag::SetColor()/GetColor().
5b4213d8-e4b4-434a-ad71-5ef0d69be6ea-image.png
The VertexColorTag in this case is not specify to each vertex of the triangle but specifies the color for the whole triangle: this is because the tag is created in the ObjectData::GetVirtualObjects() and store color for each point of the cloud, but then it's used in the ObjectData::Draw() method to specify the ObjectColorProperty used to color the drawn PolygonObject. As far as I know the BaseDraw::DrawPolygonObject() ignores any VertexColorTag assigned to the PolygonObject to be drawn.
BaseObject* PC_12974::GetVirtualObjects(BaseObject* op, HierarchyHelp* hh)
{
// check cache before continue
BaseObject* cache = op->GetCache();
if (cache)
return cache;
// init the random gen and get a arbitrary number of tris
_rndGen.Init(GeGetTimer());
_randomTrisCnt = (SAFEINT32(_rndGen.Get01() * (1 + _max - _min)) + _min);
// allocate a PolygonObject and fill it with points randomly
PolygonObject* po = PolygonObject::Alloc(_randomTrisCnt, 0);
if (!po)
return BaseObject::Alloc(Onull);
// fill the points array with random positions
Vector* points = po->GetPointW();
for (Int32 i = 0; i < _randomTrisCnt; i++)
points[i] = Vector (100.0 * _rndGen.Get11(), 100.0 * _rndGen.Get11(), 100.0 * _rndGen.Get11());
// look for VertexColor tag
BaseTag* tag = po->GetTag(Tvertexcolor);
// if not existing just create a new one
VertexColorTag* vcTag = nullptr;
if (!tag)
{
tag = VertexColorTag::Alloc(_randomTrisCnt);
po->InsertTag(tag);
}
vcTag = static_cast<VertexColorTag*>(tag);
vcTag->SetPerPointMode(true);
VertexColorHandle handle = vcTag->GetDataAddressW();
// fill the vertexcolor tag with random values
for (Int32 i = 0; handle && i < _randomTrisCnt; i++)
VertexColorTag::SetColor(handle, nullptr, nullptr, i, maxon::Color32(_rndGen.Get01(), _rndGen.Get01(), _rndGen.Get01()));
// update host object
po->Message(MSG_UPDATE);
return po;
}
Bool PC_12974::TriangleOnPoint(const Vector &pos, PolygonObject& po)
{
// given a point create a triangle centered over there.
Vector* points = po.GetPointW();
const Matrix poMG = po.GetMg();
const Vector posMG = poMG * pos;
if (!points)
return false;
// set vertexes positions for the triangle.
points[0] = Vector(0, 0, _triangleRad) + posMG;
points[1] = Vector(_triangleRad * Cos(-PI / 6), 0, _triangleRad * Sin(-PI / 6)) + posMG;
points[2] = Vector(_triangleRad * Cos(-PI * 5 / 6), 0, _triangleRad * Sin(-PI * 5 / 6)) + posMG;
// create the polygon
CPolygon* polys = po.GetPolygonW();
if (!polys)
return false;
polys[0] = CPolygon(0, 1, 2);
return true;
}
DRAWRESULT PC_12974::Draw (BaseObject *op, DRAWPASS drawpass, BaseDraw *bd, BaseDrawHelp *bh)
{
// skip anything but OBJECT drawpass
if (drawpass != DRAWPASS::OBJECT)
return DRAWRESULT::SKIP;
// check for the object and its cache
if (op && op->GetCache())
{
// get the PolygonObject from the cache
PolygonObject* opPO = static_cast<PolygonObject*>(op->GetCache());
// look for the attached VertexColor Tag
BaseTag* const tag = opPO->GetTag(Tvertexcolor);
if (!tag)
return DRAWRESULT::SKIP;
VertexColorTag* const vcTag = static_cast<VertexColorTag*>(tag);
// retrieve the read-only handle
ConstVertexColorHandle vcTagHandleR = vcTag->GetDataAddressR();
// get the points and iterate over the VertexColor to set the color
// of each triangle drawn
maxon::Int pointCnt = opPO->GetPointCount();
Vector const *points = opPO->GetPointR();
for (maxon::Int i = 0; i < pointCnt; i++)
{
maxon::Color32 vtxCol(0.0);
// get the color
if (vcTagHandleR && vcTag->IsPerPointColor())
vtxCol = VertexColorTag::GetColor(vcTagHandleR, nullptr, nullptr, (Int32)i);
// set the color in the ObjectColorProperties
ObjectColorProperties colProp;
colProp.usecolor = ID_BASEOBJECT_USECOLOR_ALWAYS;
colProp.color.x = vtxCol.r;
colProp.color.y = vtxCol.g;
colProp.color.z = vtxCol.b;
colProp.xray = false;
// allocate the temp triangle
PolygonObject* tri = PolygonObject::Alloc(3, 1);
// set the color prop, create the tri and draw!
tri->SetColorProperties(&colProp);
if (TriangleOnPoint(op->GetMg() * points[i], *tri))
bd->DrawPolygonObject(bh, tri, DRAWOBJECT::NONE, nullptr);
// free the tri
PolygonObject::Free(tri);
}
}
return DRAWRESULT::OK;
}
Feel free to comment and again sorry for the silence.
Riccardo