Values changing in a std::vector?

Started by
3 comments, last by CloudNine 20 years, 4 months ago
Yo, I''ve been working on my lwstodml converter, and I''ve come across a problem. I''m reading keyframe data from the file, and it reads ok (or so it seems), but when I cycle through all the joints and keyframes, some time values are wrong, even though I definitely wrote them to the same memory address! Here''s a snippet of code, which may help (and this time, I am opening my files correctly ) Reading from the file:


    for (int i=0; i<numChannels; i++)
    {
     //Skip Channel n and

     //{ Envelope

     fgets(buffer,255,in);
     fgets(buffer,255,in);
     
     //Get number of keys

     fgets(buffer,255,in);
     sscanf(buffer,"%d",&objectMotion.numKeys);
    
     if (objectMotion.keys==NULL)
      objectMotion.keys=new Keyframe[objectMotion.numKeys];
    
     //Read key data

     for (int j=0; j<objectMotion.numKeys; j++)
     {
      fgets(buffer,255,in);
    
      float* value=NULL;
      
      //Optimize!

      
      switch (i)
      {
       case 0: //X

        value=&objectMotion.keys[j].vPosition.x;
       break;
       
       case 1: //Y

        value=&objectMotion.keys[j].vPosition.y;
       break;
       
       case 2: //Z

        value=&objectMotion.keys[j].vPosition.z;
       break; 
        
       case 3:
        value=&objectMotion.keys[j].vRotation.x;
       break;
       
       case 4:
        value=&objectMotion.keys[j].vRotation.y;
       break;
       
       case 5:
        value=&objectMotion.keys[j].vRotation.z;
       break;
       
       default:
       //Bit hacky. Skips through the whole key since we don''t

       //want to know about scale data

        continue;
      }
    
      char* keyData=strstr(buffer,"Key");
      sscanf(keyData,"Key %f %f",value,&objectMotion.keys[j].time); 
      }
     fgets(buffer,255,in); //Ignore behaviours

     fgets(buffer,255,in); //Ignore }

    }
    fgets(buffer,255,in); //Ignore empty line after motion data

   }
The code to print the keyframe data is just a simple for loop which cycles through all keyframe and data. This is what the typical bone data from a lws file looks like:

AddBone
BoneName center_pivot
ShowBone 1 4
BoneActive 1
BoneRestPosition 0 0.121 -0.022
BoneRestDirection 0 0 0
BoneRestLength 0.027
BoneStrength 1
ScaleBoneStrength 1
BoneMotion
NumChannels 9
Channel 0
{ Envelope
  13
  Key 0 0 0 0 0 0 0 0 0
  Key 0 0.16666667 0 0 0 0 0 0 0
  Key 0 0.2 0 0 0 0 0 0 0
  Key 0 2.0333333 0 0 0 0 0 0 0
  Key 0 3.6666667 0 0 0 0 0 0 0
  Key 0 4.6333332 0 0 0 0 0 0 0
  Key 0 4.6666665 0 0 0 0 0 0 0
  Key 0 4.8666668 0 0 0 0 0 0 0
  Key 0 5.0666666 0 0 0 0 0 0 0
  Key 0 5.1866665 0 0 0 0 0 0 0
  Key 0 5.3466668 0 0 0 0 0 0 0
  Key 0 5.4266667 0 0 0 0 0 0 0
  Key 0 5.6666665 0 0 0 0 0 0 0
  Behaviors 1 1
}
Channel 1
{ Envelope
  13
  Key 0.121 0 0 0 0 0 0 0 0
  Key 0.121 0.16666667 0 0 0 0 0 0 0
  Key 0.121 0.2 0 0 0 0 0 0 0
  Key 0.121 2.0333333 0 0 0 0 0 0 0
  Key 0.121 3.6666667 0 0 0 0 0 0 0
  Key 0.121 4.6333332 0 0 0 0 0 0 0
  Key 0.121 4.6666665 0 0 0 0 0 0 0
  Key 0.121 4.8666668 0 0 0 0 0 0 0
  Key 0.121 5.0666666 0 0 0 0 0 0 0
  Key 0.121 5.1866665 0 0 0 0 0 0 0
  Key 0.121 5.3466668 0 0 0 0 0 0 0
  Key 0.121 5.4266667 0 0 0 0 0 0 0
  Key 0.121 5.6666665 0 0 0 0 0 0 0
  Behaviors 1 1
}
Channel 2
{ Envelope
  13
  Key -0.022 0 0 0 0 0 0 0 0
  Key -0.022 0.16666667 0 0 0 0 0 0 0
  Key -0.022 0.2 0 0 0 0 0 0 0
  Key -0.022 2.0333333 0 0 0 0 0 0 0
  Key -0.022 3.6666667 0 0 0 0 0 0 0
  Key -0.022 4.6333332 0 0 0 0 0 0 0
  Key -0.022 4.6666665 0 0 0 0 0 0 0
  Key -0.022 4.8666668 0 0 0 0 0 0 0
  Key -0.022 5.0666666 0 0 0 0 0 0 0
  Key -0.022 5.1866665 0 0 0 0 0 0 0
  Key -0.022 5.3466668 0 0 0 0 0 0 0
  Key -0.022 5.4266667 0 0 0 0 0 0 0
  Key -0.022 5.6666665 0 0 0 0 0 0 0
  Behaviors 1 1
}
Channel 3
{ Envelope
  13
  Key 0 0 0 0 0 0 0 0 0
  Key 0 0.16666667 0 0 0 0 0 0 0
  Key 0 0.2 0 0 0 0 0 0 0
  Key 0 2.0333333 0 0 0 0 0 0 0
  Key 0 3.6666667 0 0 0 0 0 0 0
  Key 0 4.6333332 0 0 0 0 0 0 0
  Key 0 4.6666665 0 0 0 0 0 0 0
  Key 0 4.8666668 0 0 0 0 0 0 0
  Key 0 5.0666666 0 0 0 0 0 0 0
  Key 0 5.1866665 0 0 0 0 0 0 0
  Key 0 5.3466668 0 0 0 0 0 0 0
  Key 0 5.4266667 0 0 0 0 0 0 0
  Key 0 5.6666665 0 0 0 0 0 0 0
  Behaviors 1 1
}
Channel 4
{ Envelope
  13
  Key 0 0 0 0 0 0 0 0 0
  Key 0 0.16666667 0 0 0 0 0 0 0
  Key 0 0.2 0 0 0 0 0 0 0
  Key 0 2.0333333 0 0 0 0 0 0 0
  Key 0 3.6666667 0 0 0 0 0 0 0
  Key 0 4.6333332 0 0 0 0 0 0 0
  Key 0 4.6666665 0 0 0 0 0 0 0
  Key 0 4.8666668 0 0 0 0 0 0 0
  Key 0 5.0666666 0 0 0 0 0 0 0
  Key 0 5.1866665 0 0 0 0 0 0 0
  Key 0 5.3466668 0 0 0 0 0 0 0
  Key 0 5.4266667 0 0 0 0 0 0 0
  Key 0 5.6666665 0 0 0 0 0 0 0
  Behaviors 1 1
}
Channel 5
{ Envelope
  13
  Key 0 0 0 0 0 0 0 0 0
  Key 0 0.16666667 0 0 0 0 0 0 0
  Key 0 0.2 0 0 0 0 0 0 0
  Key 0 2.0333333 0 0 0 0 0 0 0
  Key 0 3.6666667 0 0 0 0 0 0 0
  Key 0 4.6333332 0 0 0 0 0 0 0
  Key 0 4.6666665 0 0 0 0 0 0 0
  Key 0 4.8666668 0 0 0 0 0 0 0
  Key 0 5.0666666 0 0 0 0 0 0 0
  Key 0 5.1866665 0 0 0 0 0 0 0
  Key 0 5.3466668 0 0 0 0 0 0 0
  Key 0 5.4266667 0 0 0 0 0 0 0
  Key 0 5.6666665 0 0 0 0 0 0 0
  Behaviors 1 1
}
Channel 6
{ Envelope
  13
  Key 1 0 0 0 0 0 0 0 0
  Key 1 0.16666667 0 0 0 0 0 0 0
  Key 1 0.2 0 0 0 0 0 0 0
  Key 1 2.0333333 0 0 0 0 0 0 0
  Key 1 3.6666667 0 0 0 0 0 0 0
  Key 1 4.6333332 0 0 0 0 0 0 0
  Key 1 4.6666665 0 0 0 0 0 0 0
  Key 1 4.8666668 0 0 0 0 0 0 0
  Key 1 5.0666666 0 0 0 0 0 0 0
  Key 1 5.1866665 0 0 0 0 0 0 0
  Key 1 5.3466668 0 0 0 0 0 0 0
  Key 1 5.4266667 0 0 0 0 0 0 0
  Key 1 5.6666665 0 0 0 0 0 0 0
  Behaviors 1 1
}
Channel 7
{ Envelope
  13
  Key 1 0 0 0 0 0 0 0 0
  Key 1 0.16666667 0 0 0 0 0 0 0
  Key 1 0.2 0 0 0 0 0 0 0
  Key 1 2.0333333 0 0 0 0 0 0 0
  Key 1 3.6666667 0 0 0 0 0 0 0
  Key 1 4.6333332 0 0 0 0 0 0 0
  Key 1 4.6666665 0 0 0 0 0 0 0
  Key 1 4.8666668 0 0 0 0 0 0 0
  Key 1 5.0666666 0 0 0 0 0 0 0
  Key 1 5.1866665 0 0 0 0 0 0 0
  Key 1 5.3466668 0 0 0 0 0 0 0
  Key 1 5.4266667 0 0 0 0 0 0 0
  Key 1 5.6666665 0 0 0 0 0 0 0
  Behaviors 1 1
}
Channel 8
{ Envelope
  13
  Key 1 0 0 0 0 0 0 0 0
  Key 1 0.16666667 0 0 0 0 0 0 0
  Key 1 0.2 0 0 0 0 0 0 0
  Key 1 2.0333333 0 0 0 0 0 0 0
  Key 1 3.6666667 0 0 0 0 0 0 0
  Key 1 4.6333332 0 0 0 0 0 0 0
  Key 1 4.6666665 0 0 0 0 0 0 0
  Key 1 4.8666668 0 0 0 0 0 0 0
  Key 1 5.0666666 0 0 0 0 0 0 0
  Key 1 5.1866665 0 0 0 0 0 0 0
  Key 1 5.3466668 0 0 0 0 0 0 0
  Key 1 5.4266667 0 0 0 0 0 0 0
  Key 1 5.6666665 0 0 0 0 0 0 0
  Behaviors 1 1
}

PivotRotation 0 0 -180
ParentItem 10000000
GoalObject 5
GoalStrength 1
I push_back the joints vector whenever I come across an AddBone char string, and there doesn''t seem to be any irregularities. Here''s an example I got from my log data of what the problem is:

//After reading it from the file


0 0 0x890cf4 0
0 1 0x890d14 166
0 2 0x890d34 200
0 3 0x890d54 2033
0 4 0x890d74 3666
0 5 0x890d94 4633
0 6 0x890db4 4666
0 7 0x890dd4 4866
0 8 0x890df4 5066
0 9 0x890e14 5186
0 10 0x890e34 5346
0 11 0x890e54 5426
0 12 0x890e74 5666

//Printing it out later


0 0 0x890cf4 1
0 1 0x890d14 0
0 2 0x890d34 0
0 3 0x890d54 166
0 4 0x890d74 200
0 5 0x890d94 2033
0 6 0x890db4 3666
0 7 0x890dd4 4633
0 8 0x890df4 4666
0 9 0x890e14 4866
0 10 0x890e34 5066
0 11 0x890e54 5186
0 12 0x890e74 5346
Sorry for the really long post, but this issue has been bugging me for the past week or so, and I can''t see a reason why the values should change. CloudNine
Advertisement
two entries disappear from the back, and a 0 and a 1 appear in the front. You''re doing something to your vector.
The fact is, I do nothing to my Joints vector after I print those values. Here's another example of how it goes wrong (the majority of them are correct):

//When reading26 0 0x8925d4 026 1 0x8925f4 16626 2 0x892614 20026 3 0x892634 203326 4 0x892654 366626 5 0x892674 463326 6 0x892694 466626 7 0x8926b4 486626 8 0x8926d4 506626 9 0x8926f4 518626 10 0x892714 534626 11 0x892734 542626 12 0x892754 566627 0 0x892c44 027 1 0x892c64 16627 2 0x892c84 20027 3 0x892ca4 203327 4 0x892cc4 366627 5 0x892ce4 463327 6 0x892d04 466627 7 0x892d24 486627 8 0x892d44 506627 9 0x892d64 518627 10 0x892d84 534627 11 0x892da4 542627 12 0x892dc4 5666//When printing out26 0 0x8925d4 322217042626 1 0x8925f4 322217042626 2 0x892614 201350822426 3 0x892634 026 4 0x892654 026 5 0x892674 026 6 0x892694 026 7 0x8926b4 026 8 0x8926d4 026 9 0x8926f4 026 10 0x892714 026 11 0x892734 026 12 0x892754 027 0 0x892c44 518627 1 0x892c64 534627 2 0x892c84 542627 3 0x892ca4 566627 4 0x892cc4 027 5 0x892ce4 127 6 0x892d04 027 7 0x892d24 027 8 0x892d44 127 9 0x892d64 027 10 0x892d84 027 11 0x892da4 027 12 0x892dc4 2013508224


It just beats me. Also, the keyframe data (including the times, as shown above), new'ed when I read the BoneMotion data. How could the values change?

[edited by - CloudNine on December 13, 2003 4:39:47 AM]
I''m confused... I don''t see you using a std::vector anywhere in your code.

"Sneftel is correct, if rather vulgar." --Flarelocke
I store my joints in a std::vector (which contains keyframe data which is dynamically allocated) and I push_back the vector when I come across a AddBone line.

This topic is closed to new replies.

Advertisement