i have previously asked a question about appending coordinates of clicks to a singly linked list, in order to loop through the linked list ,put vertices at the positions of the clicks and connect them
The answer given solved my issues with overflow of the linked list but afterwards i had problems with the display function, such that when i loop through the linked list and add those coordinates to glVertex2i();
, it doesn't draw a linestrip when i click multiple times on the graphical window.
I tried to see what would happen when i removed the while
loop, but that would cause segmentation fault.
These are the structs.
typedef struct vector{int x;int y;}Vector;
typedef struct VectorList{Vector X; struct VectorList*next; }node_v;
And i had declared these
Vector P;
node_v * prev;
And with the help of the answer to my previous question, initialized Vector P
with the coordinates of the clicks and appended to the linked list of vectors node_v * prev;
static void _display_CB()
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glOrtho(0,window_width,window_height,0,-1,1);
glClear(GL_COLOR_BUFFER_BIT);
glColor3ub(0,0,0);
glBegin(GL_LINE_STRIP);
while(prev){
glVertex2i( prev->X.x, prev->X.y);
prev=prev->next;
}
glEnd();
glFlush();
glutSwapBuffers();
glutPostRedisplay();
}
What changes should be done in _display_CB()
for the program to be able to draw linestrips like in the picture?
prev
is the head of the list. You actually change the head of the list to the tail of the list when you do elem = elem->next;
. Keep the head of the list and use a local variable elem
to iterate through the list.
Furthermore the orthographic projection matrix glOrtho
should be set to the current projection matrix (GL_PROJECTION
):
void _display_CB() {
node_v * elem = prev;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, window_width, window_height, 0, -1, 1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glClear(GL_COLOR_BUFFER_BIT);
glColor3ub(0, 0, 0);
glBegin(GL_LINE_STRIP);
while(elem){
glVertex2i(elem->X.x, elem->X.y);
elem = elem->next;
}
glEnd();
glutSwapBuffers();
glutPostRedisplay();
}