• 12
• 27
• 9
• 9
• 20

Trouble with modelview matrices

This topic is 4136 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

I am having trouble with matrix transformations. I am trying to display a model on the screen. Every triangle in the model has its own triangle information, which describes changes that have occured to each triangle in the model. The idea is that initially every triangles information will be indicate no change, until I 'explode' the model, at which point all the triangles will be given different velocities and rotations, etc, making it looks like the model explodes. The trouble is that when I start an 'explosion,' none of the triangles are being drawn in their changed locations, and I am not sure why. I know that the rotation and displacement information for each triangle is being updated properly, so I am pretty sure it has to do with the way I am pushing matrices onto the stack before I draw each triangle. I am hoping somebody can look at this and give me some help as to why it isn't correct.
void ZModelInstance::drawSolid() {
if (model != NULL) {
//get the triangles
vector<struct Triangle*> *triangles = model->getTriangles();

glPushMatrix();

float scale = 1.8f / model->getSizeMax();

struct Vect *centroid = model->getCentroid();

//move and scale model appropriate for display on the screen
glScalef(scale, scale, scale);
glTranslatef(-centroid->x, -centroid->y, -centroid->z);

struct Triangle *currentTriangle = NULL;
struct TriangleInfo *currentInfo = NULL;

glBegin(GL_TRIANGLES);

cout << tInfos.at(3)->angle << "  " << tInfos.at(3)->axis.x << "  " << tInfos.at(3)->axis.y << "  " << tInfos.at(3)->axis.z << endl;

for (int i = 0; i < (int)triangles->size(); i++) {
//do transformations on triangle
currentTriangle = triangles->at(i);
currentInfo = tInfos.at(i);

glTranslatef(currentTriangle->centroid->x + currentInfo->displacement.x, currentTriangle->centroid->y + currentInfo->displacement.y, currentTriangle->centroid->z + currentInfo->displacement.z);

glRotatef(currentInfo->angle, currentInfo->axis.x, currentInfo->axis.y, currentInfo->axis.z);

glTranslatef(-currentTriangle->centroid->x, -currentTriangle->centroid->y, -currentTriangle->centroid->z);

//draw triangle
glNormal3f(currentTriangle->normal->x, currentTriangle->normal->y, currentTriangle->normal->z);
glVertex3f(currentTriangle->vertex1->x, currentTriangle->vertex1->y, currentTriangle->vertex1->z);
glVertex3f(currentTriangle->vertex2->x, currentTriangle->vertex2->y, currentTriangle->vertex2->z);
glVertex3f(currentTriangle->vertex3->x, currentTriangle->vertex3->y, currentTriangle->vertex3->z);

//glPopMatrix();
}

glEnd();

glPopMatrix();
}
}
Here's what I'm trying to do: Before I draw each triangle, translate the triangle to the origin based on its coordinates, so it can be rotated properly. Next, move the triangle back to its proper location, move it again based on its displacement. Lastly, center the model and scale it so it fits on the screen properly. Any help is extremely welcome. I am not sure what is wrong. Thanks!

Share on other sites
I think that when you do the GL_BEGIN, the matrices are 'locked in' until the GL_END (and that's why it's faster to draw lots of things in one begin/end set). But don't quote me on that, it's a long time since I did any 3D stuff and I have forgotten the details.