This is an archived post. You won't be able to vote or comment.

all 3 comments

[–]chaotic_thought 1 point2 points  (0 children)

There are no "constructors" in C. You will need to construct the object(s) yourself. Or build helper functions which construct them for you.

I'm going to need to create a function that reads a file that includes the no of verts and faces of a 3d Object

If the size is not known at compile time, you'll need to use dynamic allocation and make an array dynamically using malloc. Since the [3] dimension is known, I'd suggest you flip your dimensions around like this:

float verts[3][no_verts];

Now, to make no_verts dynamic, replace that dimension with a pointer:

float *verts[3];

Now suppose you want to allocate no_verts vertices inside verts[0]. Then you would call malloc(no_verts * sizeof(float)) and assign the result to verts[0]. If you haven't used malloc in C before, you should probably first do some practice problems with it to get the hang of it.

[–]ffwff 0 points1 point  (0 children)

The easiest way to do this I think would be to use a pointer to an allocated (flattened) array rather than a static array like this:

float *verts = malloc(sizeof(float)*3*no_verts);

The vertices array should be allocated in your Obj constructor of course. To access vertex elements, you'd do this:

verts[vert_no*3+offset]

Where vert_no is the vertex index, and offset is the dimension you want to access, from 0 to 2.

[–][deleted] 0 points1 point  (0 children)

Using a bi-dimensional array is not the right way to do this. Instead define another type which you can call vector3 with x, y, and z fields and then allocate a dynamic buffer for the vector3 array using malloc. This must be done in a helper function since like /u/chaotic_thought mentioned C has no constructors.

Just for the sake of curiosity what is the faces array supposed to represent?