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
", 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 || state != GLUT_DOWN)
return;
/*Save the point */
curPointer->x = wx;
curPointer->y = wy;
//printf("%f %f
", 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.