Dynamically Allocated Arrays
I have a program that loads three triangles from a text file(yes I know it probably should be binary) but I want the array that stores my verticies to be able to be able to handle any number of triangles.
Here is what have so far:
int mode = ios::in; //Relative path names are allowed
char filename[] = "triangle.txt";
fstream fin(filename, mode);
if(!fin) {
MessageBox(d3d_hWnd, "Could not open file to load data!", "Error!", MB_ICONSTOP | MB_OK);
return false;
}
int i = 0, j = 1;
float v[6];
while( !fin.eof() ) {
fin >> v[i++];
if( !((j++)%6) ) {
triangles[k++].Create(v[0], v[1], v[2], v[3], v[4], v[5]);
i = 0;
}
}
And here is the class definition for CVertex:
class CVertex {
public:
float fx, fy, fz;
DWORD dwColor;
void Create(float Fx, float Fy, float Fz, float R, float G, float B)
{
fx = Fx;
fy = Fy;
fz = Fz;
dwColor = D3DCOLOR_XRGB( (BYTE) (R*255), (BYTE) (G*255), (BYTE) (B*255));
}
};
And I have a declaration for the triangles like this
CVertex triangles[24]; Every element could be thought of as a row with each having 3 coordinates(X, Y, Z) and three more with RGB values. And every three elements make up a whole triangle out of three vertecies. Thanks for your help. I do have an idea now on how to do this but more ideas would be appreciated.
"Ogun''s Laughter Is No Joke!!!" - Ogun Kills On The Right, A Nigerian Poem.
I think you want something like this:
~~~~~~~~~~
Martee
CVertex* triangles = new CVertex[NUM_VERTICIES];
~~~~~~~~~~
Martee
Oh yeah...forgot to say, I did use the new operator but it made a funny blue thing appear on the screen...I think it was a really long triangle that was clipped by the screen dimensions.
How can I legally use pointers in doing this?
CVertex *triangles;
with
triangles = (CVertex *) malloc( numtri * sizeof(CVertex));
does work. It says it cannot covert a void * to type CVertex *.
"Ogun''s Laughter Is No Joke!!!" - Ogun Kills On The Right, A Nigerian Poem.
How can I legally use pointers in doing this?
CVertex *triangles;
with
triangles = (CVertex *) malloc( numtri * sizeof(CVertex));
does work. It says it cannot covert a void * to type CVertex *.
"Ogun''s Laughter Is No Joke!!!" - Ogun Kills On The Right, A Nigerian Poem.
Nevermind...what I said does work...
"Ogun''s Laughter Is No Joke!!!" - Ogun Kills On The Right, A Nigerian Poem.
"Ogun''s Laughter Is No Joke!!!" - Ogun Kills On The Right, A Nigerian Poem.
OK, that DOESN''T work....this is annoying...
Here is the chunk of code to load the datathe whole thing put together)
int mode = ios::in; //Relative path names are allowed
char filename[] = "triangle.txt";
fstream fin(filename, mode);
if(!fin) {
MessageBox(d3d_hWnd, "Could not open file to load data!", "Error!", MB_ICONSTOP | MB_OK);
return false;
}
int i = 0, j = 1, temp, numtri = 0;
while( !fin.eof() ) {
fin >> temp;
numtri++;
}
fin.close;
fin.open;
triangles = (CVertex *) malloc( numtri * sizeof(CVertex) );
float v[6];
while( !fin.eof() ) {
fin >> v[i++];
if( !((j++)%6) ) {
triangles[k++].Create(v[0], v[1], v[2], v[3], v[4], v[5]);
i = 0;
}
}
And at the top I have a CVertex *triangles; declaration. Why doesn''t this work!!! And the -> operator doesn''t work.
"Ogun''s Laughter Is No Joke!!!" - Ogun Kills On The Right, A Nigerian Poem.
Here is the chunk of code to load the datathe whole thing put together)
int mode = ios::in; //Relative path names are allowed
char filename[] = "triangle.txt";
fstream fin(filename, mode);
if(!fin) {
MessageBox(d3d_hWnd, "Could not open file to load data!", "Error!", MB_ICONSTOP | MB_OK);
return false;
}
int i = 0, j = 1, temp, numtri = 0;
while( !fin.eof() ) {
fin >> temp;
numtri++;
}
fin.close;
fin.open;
triangles = (CVertex *) malloc( numtri * sizeof(CVertex) );
float v[6];
while( !fin.eof() ) {
fin >> v[i++];
if( !((j++)%6) ) {
triangles[k++].Create(v[0], v[1], v[2], v[3], v[4], v[5]);
i = 0;
}
}
And at the top I have a CVertex *triangles; declaration. Why doesn''t this work!!! And the -> operator doesn''t work.
"Ogun''s Laughter Is No Joke!!!" - Ogun Kills On The Right, A Nigerian Poem.
Like I said that gives a stange blue triangle on the thing...I used malloc and got it done finally though. The example code that I thought worked and really didn''t, didn''t have a problem with the memory allocation. It was loading the file. After I ran through the file once it was at the eof and wouldn''t load any triangles. And I tried fin.open and fin.close but I forgot they were function calls. Now I have it working perfectly. If anyone still wants to reply if they have a suggestion on doing it better I''m open to them.(other than using new because they shows something funny)
"Ogun''s Laughter Is No Joke!!!" - Ogun Kills On The Right, A Nigerian Poem.
"Ogun''s Laughter Is No Joke!!!" - Ogun Kills On The Right, A Nigerian Poem.
There is absolutely no reason why you cannot use a binary file to store your vertices, even for dynamic allocation of the vertex array.
The benefit of using a binary file is that it takes as much space to store the data in memory as it does in a binary file. So, if you need to allocate a chunk of memory to hold an entire file, then use
triangles = (CVertex *) malloc((unsigned int)filelength);
where filelength is the length in bytes of the file.
If you need to know how many vertices this is, then use
num_vertices = filelength / sizeof(CVertex);
This is far easier than doing it with a text file!!!
Tim
The benefit of using a binary file is that it takes as much space to store the data in memory as it does in a binary file. So, if you need to allocate a chunk of memory to hold an entire file, then use
triangles = (CVertex *) malloc((unsigned int)filelength);
where filelength is the length in bytes of the file.
If you need to know how many vertices this is, then use
num_vertices = filelength / sizeof(CVertex);
This is far easier than doing it with a text file!!!
Tim
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement