Hello,
So we have our own particle system which uses MPxSurfaceShapeUI to display our sprites in maya 2009. Our problem is that we wish to simulate setting the vertex colors on our sprites, so we can see a color change in the viewport.
Most attempts to do this using OpenGL either fail where I would expect them to succeed, or end up corrupting the viewport colors in some way (e.g. modifying the wireframe color for other objects). I suspect our dodgy OpenGL code is to blame.
Heres relevant chunks of code from our MPxSurfaceShapeUI.draw() method:
MDrawData data = request.drawData();
SGParticleGeometry* geom = (SGParticleGeometry*)data.geometry();
MMaterial material = request.material();
MPxHwShaderNode* shader = NULL;
int token = request.token();
if (token == kDrawShaded)
{
material.setMaterial(request.multiPath(), request.isTransparent());
shader = material.getHwShaderNode();
if (!shader)
{
drawTexture = material.materialIsTextured();
if (drawTexture)
{
material.applyTexture(view, data);
}
}
}
if (drawTexture)
{
glEnable(GL_TEXTURE_2D);
}
// Draw sprites
for (unsigned int i = 0; i < geom->GetCount(); ++i)
{
MVector quad[4];
MVector sprite_normal;
const MVector& pos = geom->GetPosition(i);
double opacity = geom->GetSpriteOpacity(i);
MColor spriteColor = geom->GetSpriteColor(i);
... // Code to get quad and sprite_normal
glPushMatrix();
glTranslated(pos.x, pos.y, pos.z);
if (token == kDrawShaded)
{
// Draw shaded quad
glBegin(GL_QUADS);
}
else
{
// Draw selection outline
glBegin(GL_LINE_LOOP);
}
glTexCoord2d(1, 0);
glNormal3dv(normal);
glVertex3dv(&quad[3].x);
glTexCoord2d(1, 1);
glNormal3dv(normal);
glVertex3dv(&quad[1].x);
glTexCoord2d(0, 1);
glNormal3dv(normal);
glVertex3dv(&quad[0].x);
glTexCoord2d(0, 0);
glNormal3dv(normal);
glVertex3dv(&quad[2].x);
glEnd();
glPopMatrix();
if(drawTexture)
glDisable(GL_TEXTURE_2D);
}
So with this code, if we have a lambert shader shader on our particles, the ambient value of the shader has no effect. The shader performs as expected otherwise (i.e. can see a texture etc…)
What I want to do is use the MColor spriteColor above to change the ambient color of the sprite, by setting the vertex colors.
I got minimal success by adding this code underneath the glPushMatrix() and glTranslated() section:
GLfloat d1[] = {spriteColor[0], spriteColor[1], spriteColor[2], 1.0};
[B]glPushAttrib(GL_ALL_ATTRIB_BITS);[/B]
if (token == kDrawShaded)
{
// Draw shaded quad
glBegin(GL_QUADS);
}
else
{
// Draw selection outline
glBegin(GL_LINE_LOOP);
}
[B]glMaterialfv(GL_FRONT, GL_DIFFUSE, d1);[/B]
glTexCoord2d(1, 0);
glNormal3dv(normal);
glVertex3dv(&quad[3].x);
// 3 more times for the other 3 vertices
...
glPopAttrib();
However, I have to use GL_DIFFUSE as GL_AMBIENT has no visible effect.
Also, this seems to corrupt the maya viewport, e.g. wireframe colors of other objects.
Any advice?
Cheers!