A problem with sub-model offsets and scaling

Started by
-1 comments, last by Thaumaturge 16 years, 10 months ago
As part of my current project I want to implement a simple-to-code animation system, as time is fairly short in this project. To this end I have decided upon a hierarchical system: sub-models (in this project called "Appendages") each have a list of child sub-meshes, and has rotations and a position relative to its parent, thus forming a tree structure. Clunky and old-fashioned, I know, but I feel that it should be easier and faster to implement than a more visually-pleasing system, such as skeletal animation, which seems to me to be a distinct advantage in this situation. Each active in-game object (that is, objects in the game-world that I might want to move, animate or, for convenience, be interactive - currently called "Creatures", although that will most probably change to better fit their purview) has an Appendage called "body", which is the root of its Appendage tree. When an object's draw function is called, it performs its own transformations, and then calls its body's draw function. Each Appendage's draw function performs its own transformations, calls its display list, and then calls the draw functions of each of its children. Thus a parent's changes should be propagated to its children, whose transformations should be performed relative to their parents. Furthermore, each "Creature" has a scaling factor. This, it seems, is where the problem comes in. When the scaling factor is set to 1.0, the object seems to display correctly. When it is greater, however, the offsets of child objects seem to become incorrect, as shown in the following images (the correct version taken at a higher zoom than was the incorrect version): Free Image Hosting at www.ImageShack.us Free Image Hosting at www.ImageShack.us Note in the second picture the head being placed in the middle of the chest, and the middle and lower hind legs being mis-placed. (It's meant to be a wolf walking on its hind legs, hence the odd pose.) Sine the positioning at a scaling factor of 1.0 seems to be correct, I believe that my offsets are correct, but that my method of handling scaling with respect to them is incorrect. Here are my drawing functions - I suspect that the problem lies somewhere here, but have not managed to find it thus far:

//The draw method of a "Creature".
// Translates to the creature's position, then rotates to the
// appropriate direction and scales the object by "size", which
// is a member of the "Creature" class.
// That done, the creatures "body" Appendage is drawn.
// (See the Appendage drawing code, below.)
void Creature::draw()
 {
  glPushMatrix();
   glTranslatef(position.x, position.y, position.z);
   glRotatef(direction, 0.0, 1.0, 0.0);
   glRotatef(180.0, 0.0, 1.0, 0.0);
   glScalef(size, size, size);
   glTranslatef(0.0, body.getPosition().y*2.0, 0.0);
   body.draw();
  glPopMatrix();
 }



//The draw method of "Appendage".
// Translates the Appendage to its position (theoretically an offset from
// its parent) plus an offset (for possible use in animation), then rotates
// around three axes (all currently set to rotate by 0.0).  The scaling
// by x_mirror is intended to allow flipping of the Appendage about the x-axis,
// but at the moment I'm pretty sure that in all cases this is set to 1.0.
// Thereafter the Appendage's display list is called, and each of its children's
// draw methods are called, within the scope of the Appendage's transformations.
//(This code is located in the Appendage header file, for better or worse,
// hence the lace of a namespace identifier and the presence of a semicolon
// at the end of the function.)
void draw()
 {
 glPushMatrix();
  glTranslatef(position.x+offset.x, position.y+offset.y, position.z+offset.z);
  glRotatef(rotationy, 0.0, 1.0, 0.0);
  glRotatef(rotationz, 0.0, 0.0, 1.0);
  glRotatef(rotationx, 1.0, 0.0, 0.0);
  glScalef(x_mirror, 1.0, 1.0);
  if(glIsList(list)) glCallList(list);
  for(int i = 0; i < subParts.size(); i++)
   {
    subParts.draw();
   }
 glPopMatrix();
};


My logic is that since the scaling is applied before any of the Appendage models are called, and the matrix state in which it is placed is only replaced by a glPopMatrix after those draws, the scaling should apply appropriately the child models... but this doesn't seem to be the case. [edit] I'm sorry, I realised a little late that I hadn't actually stated an actual question - my question, of course, is this, to anyone who might have an answer: what am I doing wrong, and how might I rectify it? [/edit] My thanks for any help offered. ^_^ [edit 2] No matter, I found the problem - I had a second scaling occurring in my display list, meaning that the transformations were being scaled once, as they should be, and the models twice, leading to incorrect offsets. [/edit] [Edited by - Thaumaturge on June 26, 2007 3:01:48 PM]

MWAHAHAHAHAHAHA!!!

My Twitter Account: @EbornIan

This topic is closed to new replies.

Advertisement