I am trying to set the values for the 2nd and 3rd corners on three objects of class triangle. If done explicitly, this looks like this:
triangle_mesh[0].set_vector_point2(vector_anchors[1]);
triangle_mesh[1].set_vector_point3(vector_anchors[1]);
triangle_mesh[1].set_vector_point2(vector_anchors[2]);
triangle_mesh[2].set_vector_point3(vector_anchors[2]);
triangle_mesh[2].set_vector_point2(vector_anchors[3]);
triangle_mesh[0].set_vector_point3(vector_anchors[3]);
and this works! Printing these triangles, I get (n.b. the first corner has already been set – this is not an issue):
triangle0 is ( 0, 0, -1) (1, 0, 0) (-0.5, -0.866025, 0)
triangle1 is ( 0, 0, -1) (-0.5, 0.866025, 0) (1, 0, 0)
triangle2 is ( 0, 0, -1) (-0.5, -0.866025, 0) (-0.5, 0.866025, 0)
Firstly, though, this is ugly, and secondly it must generalise to cases where I have more than three triangles to set up. My code for this is:
for (int longitude = 0; longitude < num_longitudes; longitude++){
SurfaceVector current_anchor = vector_anchors[1 + longitude];
triangle_mesh[longitude].set_vector_point2(current_anchor);
triangle_mesh[(longitude + 1) % num_longitudes].set_vector_point3(current_anchor);
}
*n.b. num_longitudes is 3*
I’ve checked everything I can think of, but now when I print out my triangles I get:
triangle0 is ( 0, 0, -1) (-0.5, -0.866025, 0) (-0.5, -0.866025, 0)
triangle1 is ( 0, 0, -1) (-0.5, -0.866025, 0) (-0.5, -0.866025, 0)
triangle2 is ( 0, 0, -1) (-0.5, -0.866025, 0) (-0.5, -0.866025, 0)
Does anyone have any idea what could be going wrong?!
EDIT
The vector_point variables on the triangle are pointers, and are set like:
void set_vector_point1(SurfaceVector vector_point) { vector_point1 = &vector_point; }
There lies your problem:
You are pointing to a temporary (
vector_pointceases to exist once the function call completes). Change it so you copySurfaceVectorproperly.