linked list problem


I’m having difficulty getting the values stored in a linked list generated by one glut function to be read by another glut function. What I’m trying to do is have MouseFunc record input x, y positions and store them in a linked list of vertices for a polyLine. So far, so good. I can echo back all of the values in the linked list within the Mousefunc function. The initial pointer to the start of the linked list is declared globally. When I try to reference the values within this list from another function with the glutMainLoop list the values are set to 0:

#include <GL/glut.h>
#include <stdlib.h>
#include <stdio.h>

static int width = 640, height = 480;

class pointNode
float x, y;
pointNode *next;

pointNode *polyLine1 = new pointNode;

void init(void)
glClearColor(1.0, 1.0, 1.0, 1.0);
glColor3f(0.0, 0.0, 0.0);

gluOrtho2D(0.0, width, 0.0, height);
glViewport(0, 0, width, height);


/* This routine displays the polyLine /
static void display(void)

curPointer = polyLine1;
while (curPointer-&gt;next != NULL)
    glVertex2f(curPointer-&gt;x, curPointer-&gt;y);
    curPointer = curPointer-&gt;next;
    printf("%f %f

", curPointer->x, curPointer->y );

/* Inputs new control points /
static void myMouse(int button, int state, int x, int y)
float wx, wy;
pointNode* curPointer;

start = polyLine1;
curPointer = polyLine1;
/* Translate back to our coordinate system */
wx = (2.0 * x) / (float)(width - 1) - 1.0;
wy = (2.0 * (height - 1 - y)) / (float)(height - 1) - 1.0;
/*Only get left clicks*/
if (button != GLUT_LEFT_BUTTON || state != GLUT_DOWN)
/*Save the point */
curPointer-&gt;x = wx;
curPointer-&gt;y = wy;
//printf("%f %f

", curPointer->x, curPointer->y );
curPointer->next = new pointNode;
curPointer = curPointer->next;
curPointer->next = NULL;



int main(int argc, char *argv)
Initialize the program /
glutInit(&argc, argv);
glutInitWindowSize(width, height);
glutInitWindowPosition (100, 100);
glutCreateWindow (“Morph 2D”);
Register the callbacks */

return 0;


Thanks for any help. I’m pretty new to glut so it’s probably a fairly basic mistake.

Since this is an OpenGL forum, I’ll think I’ll first point out that you should call glutPostRedisplay() instead of calling the display() function yourself. It may work like you have it, but there may be problems when taking it to other platforms.

Now, to the linked list. You need to take a look at how you’re forming the list itself. You’re replacing the position of the node at the front of the list, and then create a new uninitialised node after this one (replacing any node that was there).

A better (working) list would go:

pointNode *polyLine1First = NULL;
pointNode *polyLine1Last = NULL;

void myMouse(int button, int state, int x, int y)
	if (/* left button down */)
		/* do stuff */
		pointNode *node = new pointNode;
		node->next = NULL;
		node->x = wx;
		node->y = wy;
		// Push node onto end of list
		if (!polyLine1First)
			polyLine1First = node;
			polyLine1Last->next = node;
		polyLine1Last = node;

But please, don’t take my word for it. Understand what is going on, and you will live a far happier life as a programmer :slight_smile:


You’re right, I was clobbering my current index pointer each time through the glutMainLoop. Once I moved the index pointer out of the mouse function (like you showed) it worked.