Hello, I'm trying to create a fractal design with C++ and OpenGL, however I've run into trouble with the function that modifies a vector of vertices in order to make a fractal, originally the vector contains two
vertices
objects, which are defined like so (GLfloat is typedef'd like this in GL/gl.h, btw
typedef float GLfloat;
):
1 2 3
|
struct vertices{
GLfloat x, y;
};
|
Ideally, this function should create a new
vertices
for every two that are already in the vector, and offset this new point perpendicularly to the original two vertices, after doing this calculations, it should insert the new point between the original points in the vector, resulting in a zig-zag pattern.
However, I'm running into trouble with this function, as the condition in the for loop never seems to be true, until the program crashes when I try to insert a new point where it is illegal to do so. Attached are the (I think) relevant parts of my code:
main.cpp, right now initial points of the vector are hardcoded (shift_distance is how much the new points are transported perpendicularly, iterations corresponds to the number of times the vector is "zig-zagged"):
1 2 3 4 5 6 7 8 9 10 11
|
vertices start, end;
start.x = -0.8;
start.y = 0.0;
end.x = 0.8;
end.y = 0.0;
vector < vertices > vert_vector;
vert_vector.push_back(start);
vert_vector.push_back(end);
fractal::zigzag_vector(frac_iterations, shift_distance, vert_vector);
|
fractal.cpp:
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
|
void zigzag_vector(int iterations, GLfloat shift_distance,
vector < vertices >& fractal_vector)
{
typedef vector < vertices >::iterator vec_iter;
// shift_bool: determines if new point is displaced positively or negatively
bool shift_bool = true;
for(int i = 0; i < iterations; ++i){
for(vec_iter iter = fractal_vector.begin();
iter != fractal_vector.end(); ++iter){
// calculate coordinates of new point
vertices new_vert = midpoint(*iter, *(iter + 1));
shift_vertex_parallel(new_vert, shift_distance,
*iter, *(iter + 1), shift_bool);
// insert new point between original points
fractal_vector.insert((iter + 1), new_vert);
std::cout << "inserted new point\n";
// alternate direction of zigzag
shift_bool = !shift_bool;
// two increments needed since a value was inserted at (iter + 1)
++iter;
std::cout << "finished for loop\n";
}
}
return;
}
|
console output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
jaz@jaz-TP67B ~/Documents/Programming/OpenGL/fractal/zigzagzugs $ g++ -o tree_exec tree.cpp ../common/fractal.cpp -lGLEW -lGLU -lGL -lglfw
jaz@jaz-TP67B ~/Documents/Programming/OpenGL/fractal/zigzagzugs $ ./tree_exec
Iterations: 0
Zig-Zag displacement: 0.1
Compiling shader: vertex_shader.vrtx
Compiling shader: fragment_shader.frgmt
Linking program...
jaz@jaz-TP67B ~/Documents/Programming/OpenGL/fractal/zigzagzugs $ ./tree_exec
Iterations: 1
Zig-Zag displacement: 0.1
inserted new point
finished for loop
inserted new point
finished for loop
*** Error in `./tree_exec': malloc(): memory corruption (fast): 0x00000000016623e0 ***
^C
jaz@jaz-TP67B ~/Documents/Programming/OpenGL/fractal/zigzagzugs $
|
So the code runs fine (if you can call it that) when it never goes into the for loop, but when iterations was equal to 1, the for loop executed twice for some reason, which I believe is the reason why I got a memory corruption error when it tried to insert a third point.
Does anyone have any ideas why this is happening, I'll be happy to post more code if anyone believes it'll help find the problem.