A vector inside a struct, inside a vector of structs. Plus pointers && ifstreams, OH MY!

Hello. I'm working on a simple real-time animation system. I am using some stuff I haven't touched in a long time and am trying to refresh myself and get some "pro" knowledge or advice...


I have spent the last few nights searching for answers and examples similar to what I'm doing, figured it was about time to take it the the forums. ;)


The code is compiling as is, but I have not implemented any rendering or morphing yet so... Basically trying to get the data setup and initialized properly before moving on.


I have commented some of the code to better help you understand what things I'm having trouble with and/or am trying to learn more about.


My main focus atm is how the std::vector's work. In particular, the use of the nested vectors and structs, and weather or not I need to be using pointers.


Also, from my "research" so far, I believe the way I build the vectors could be costly due to reallocation.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
struct KeyFrame
{
	// Best way to init these to default values?? 
        // Member initialize list? How-to?

	float  m_fKeyTime;

	vector<vec3f> m_vNodeArray;     // vert data array
	// need to use a pointer?

	//////////////////////////////////////////////////////////////////////////
	// Thought about just using an array of vec3's for the verts but I
	// think it's best to use a vector for when I get to more complex
	// animation systems
	//////////////////////////////////////////////////////////////////////////
};


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
	
class Animation
{	friend class Interpolator;
private:
	float m_fDuration;  

	vector<KeyFrame> m_vKeyFrames; // need to use pointer?

public:
	Animation(void){ }
	~Animation(void){ }
...
...

void LoadFromFile(string fileName)  // Better way to implement this?
	{
		ifstream inFile(fileName.c_str(), ios::in | ios::binary );
		
		//needs error checking, do it later
		if (inFile.is_open())
		{
			// Best place to declare/init these vars?
			unsigned int	nNumKeyFrames = 0;
			unsigned int	nNodesInFrame = 0;
			vec3		f3TempNode;	// I call make_zero on this each loop before I load the data, good idea?
			KeyFrame	tTempKeyFrame;  // this seems wrong to me somehow

			inFile.seekg(0, ios::beg);

			inFile.read( (char*)&m_fDuration, sizeof(float) );
			inFile.read( (char*)&nNumKeyFrames, sizeof(unsigned int) );

			// Possible issues with the way I read in/fill out the structs
			// and/or the vectors?
			while(nNumKeyFrames)
			{
             /* How-to clear or reinitialize */ tTempKeyFrame /* before each loop? do I need too?*/
                                inFile.read( (char*)&tTempKeyFrame.m_fKeyTime, sizeof(float) );
				inFile.read( (char*)&nNodesInFrame, sizeof(unsigned int) );

				while(nNodesInFrame)
				{
					f3TempNode.make_zero(); // good practice to clear this each loop?

					inFile.read( (char*)&f3TempNode, sizeof(vec3f) );

					tTempKeyFrame.m_vNodeArray.push_back(f3TempNode);

					nNodesInFrame--;
				}

				m_vKeyFrames.push_back(tTempKeyFrame);

				nNumKeyFrames--;
			}
			inFile.close();
			inFile.clear();
		}
		else
		{
			//error stuffs
			inFile.clear();
		}



1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Interpolator
{
private:
	float m_fCurrentTime;

	const Animation *m_pAnimation;

	KeyFrame m_CurrentKeyFrame;

public:
	Interpolator(void){ }
	~Interpolator(void){ }
...
...



Ok so... The basic layout is as follows...

-My driver class has Animation object and an Interpolator object.

-The Animation class loads and stores the animation data(keyframes) from a binary file.

-The Interpolator takes in a const* to the Animation object and, of course, interpolates between the key-frames.

Reminder: I am mainly concerned with my usage of the data structures. Not the general implementation or concepts of the animation system.

Thanks so much!!!
Last edited on
Line 39 will read the next set of input from "inFile" and store it in "nNodesInFrame", it won't store the same data as "tTempKeyFrame" if that is what you are trying to do. You seem to want "nNodesInFrame" to be a counter instead of a data buffer. Am I reading this wrong?
Hi

You are correct. nNodesInFrame and nNumKeyFrames are integer values that are written to the binary file.

I am using the values as counters to load in however many total KeyFrames and how many Nodes are In each Frame.

Ex. If there are 24 key-frames in the animation then the int value being read into nNumKeyFrames is 24. And if there are 1000 nodes in the current frame we are loading the int value read into nNodesInFrame is 1000.

The vector m_vKeyFrames in the Animation class should be populated by keyframes that contain vector of vert data that it type vec3.

Hi, I kinda put this project to the side and it's been about a month and I was hoping I'd have a few more replies to this topic :(
Topic archived. No new replies allowed.