PDA

View Full Version : Help Needed! Setting Vertex Colors on MPxSurfaceShapeUI for maya API


dbsmith
10-17-2011, 11:03 PM
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};

glPushAttrib(GL_ALL_ATTRIB_BITS);

if (token == kDrawShaded)
{
// Draw shaded quad
glBegin(GL_QUADS);
}
else
{
// Draw selection outline
glBegin(GL_LINE_LOOP);
}

glMaterialfv(GL_FRONT, GL_DIFFUSE, d1);
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!

Azrail
10-18-2011, 10:47 AM
You have to make sure you enclose all your drawing calls in glPushAttrib(GL_ALL_ATTRIB_BITS)/glPopAttrib pair, this way the state of maya's viewport will not be 'corrupted'.

dbsmith
10-20-2011, 10:39 PM
Cheers for advice Azrail.
I'm pretty sure I tried this on one of my attempts, I'll have another crack though and make sure I'm doing this.

CGTalk Moderation
10-20-2011, 10:39 PM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.