PDA

View Full Version : linked list problem



mjs1
04-03-2004, 02:54 PM
Hello!

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
{
public:
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);
glPointSize(1.0);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, width, 0.0, height);
glViewport(0, 0, width, height);
}

/* This routine displays the polyLine */
static void display(void)
{
pointNode* curPointer;

curPointer = polyLine1;

glClear(GL_COLOR_BUFFER_BIT);

glBegin(GL_LINE_STRIP);
while (curPointer->next != NULL)
{
glVertex2f(curPointer->x, curPointer->y);
curPointer = curPointer->next;
printf("%f %f\n", curPointer->x, curPointer->y );
}
glEnd();
glFlush();
}

/* Inputs new control points */
static void myMouse(int button, int state, int x, int y)
{
float wx, wy;
pointNode* start;
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 &#0124;&#0124; state != GLUT_DOWN)
return;

/*Save the point */
curPointer->x = wx;
curPointer->y = wy;
//printf("%f %f\n", curPointer->x, curPointer->y );
curPointer->next = new pointNode;
curPointer = curPointer->next;
curPointer->next = NULL;

display();
}

int main(int argc, char **argv)
{
/* Initialize the program */
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB);
glutInitWindowSize(width, height);
glutInitWindowPosition (100, 100);
glutCreateWindow ("Morph 2D");
init();
/* Register the callbacks */
glutDisplayFunc(display);
glutMouseFunc(myMouse);

glutMainLoop();
return 0;
}

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

hh10k
04-03-2004, 08:14 PM
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;
else
polyLine1Last->next = node;
polyLine1Last = node;

glutPostRedisplay();
}
}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 :)

mjs1
04-03-2004, 09:35 PM
Thanks!

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.