Archived

This topic is now archived and is closed to further replies.

Eight

Model data structures

Recommended Posts

I just started writing an .obj loader for my new 3d engine. Up until this point, I''ve always had a fixed limit on the number of vertices in a mesh - but not in this case. So I implemented my data structure in a linked list. Standard stuff. However, I''ve just realised that for my program to find a specific vertex (e.g. the 2000th) it would have to trawl through the first 1999 vertices too. Can anyone suggest an alternate structure or method which would enable me to extract items quickly? I tried to get an STL Vector list running but the stupid thing just wouldn''t work - it needed that blasted "using namespace std" declaration which screwed everything else up royally. E

Share this post


Link to post
Share on other sites
instead of writing:
    
using namespace std;

vector foo;

you can write:
  
std::vector foo;

They accomplish similar things (except that the topmost version pollutes your global namespace a little)

[EDIT: Messed up my source boxes]

Edited by - Dactylos on July 26, 2001 7:22:57 AM

Share this post


Link to post
Share on other sites
Let me see if I can help u on this.

Why don''t you in your file record the number of vertices and then when you load the file allocate memory on the heap for all yor vertices so that you can use it as an array.

for example check this out:

  

// globals

Vertex *vertex_list=0;


//////////////////////////////////////

int Load_Object (char *file_name)
{
FILE *fp=fopen(file_name,"rb");

// load in vertices

fread (&num_verts,sizeof(int),1,fp);

// now declare memory for the vertex

// list

vertex_list = (Vertex *)malloc(num_verts*sizeof(Vertex));

// test for valid memory

if (!vertex_list)
{
fclose (fp);
return (ERROR);
}

// now load vertices in

fread (vertex_list,sizeof(Vertex),num_verts,fp);

// close file now or load any

// other misc data

// ....

// ....

fclose (fp);

return SUCCESS;
}



Some where in your code if you want to refer to the 2000th vertex then you can just do this for example:

Vertex temp;

temp.x = vertex_List[1999].x;
temp.y = vertex_List[1999].y;
temp.z = vertex_List[1999].z;

easy as arrays ... huh

I hope I have answered this post correctly... the thing is with your application, if you are loading your object in once then why use a linked list.

I thought linked lists are for occassions when during the running of your code you may need to add another peice of data unexpectedly to be added to a structure. In my code I have assumed that the number of vertices have been stored in your object file, so that when you load the object you can know how many vertices are in it and then declare memory for it based on that. With the memory that I have declared in the Load_Object function... you can refernece vertices as arrays and jump straight to the right one without having to right linked list algorithms to traverse a mighty list.

I hope this is will help u... if not then hit me back and I''ll see what I can tell ya

Good luck

Dark Star
UK

















Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Even if your vertex list, face list or whatever changes dynamically run time, you can change the size of the list using realloc() which is very flexible. Linked lists shouldn''t be used where direct indexing is needed.
Linked list structures should only be used if you gain some benefit of using them over dynamically allocated arrays.

Share this post


Link to post
Share on other sites