Search code examples
copenglglutnurbs

How to continue looping through GLfloat Points


I am having trouble making a simple drawing.

I'm not sure what the issues is here but I have a set of points:

GLfloat ctrlpoints[13][3] = {
        {0.1, 0.1, 0.0},
        {0.7, 0.1, 0.0},
        {0.9, 0.1, 0.0},
        {0.9, 0.3, 0.0},
        {0.7, 0.3, 0.0},
        {0.6, 0.3, 0.0},
        {0.55, 0.25, 0.0},
        {0.5, 0.2, 0.0},
        {0.2, 0.2, 0.0},
        {0.12, 0.2, 0.0},
        {0.1, 0.3, 0.0},
        {0.07, 0.2, 0.0},
        {0.1, 0.1, 0.0} };

And I want all the points in the code to connect but instead it stops after the 6th set of points.

Rest of my code:

void init(void)
{
    glClearColor(0.0, 0.0, 0.0, 0.0);
    glShadeModel(GL_FLAT);
    glMap1f(GL_MAP1_VERTEX_3, 0.0, 1.0, 3, 5, &ctrlpoints[0][0]);
    glEnable(GL_MAP1_VERTEX_3);
}

void display(void)
{
    int i;
    glClear(GL_COLOR_BUFFER_BIT);
    glColor3f(1.0, 1.0, 1.0);
    glBegin(GL_LINE_STRIP);
    for (i = 0; i <= 30; i++)
        glEvalCoord1f((GLfloat)i / 30.0);
    glEnd();
    glPointSize(0.5);
    glColor3f(1.0, 1.0, 0.0);
    glBegin(GL_POINTS);
    for (i = 0; i < 12; i++)
        glVertex3fv(&ctrlpoints[i][0]);
    glEnd();
    glFlush();
}

int main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
    glutInitWindowSize(500, 500);
    glutInitWindowPosition(100, 100);
    glutCreateWindow(argv[0]);
    init();
    glutDisplayFunc(display);
    glutMainLoop();
    return 0;
}

Solution

  • Based off the documentation, the 5th argument to glMap1f is the number of the points. You set it to 5, so that's the number of control points that are used for evaluation. You should replace it with 13, or use sizeof to compute that number automatically:

    int npoints = sizeof(ctrlpoints)/sizeof(ctrlpoints[0]);
    glMap1f(GL_MAP1_VERTEX_3, 0.0, 1.0, 3, npoints, &ctrlpoints[0][0]);
    

    This is not what you were asking, but in this loop you also render less points (twelve) than you declare (thirteen):

    for (i = 0; i < 12; i++)
    

    You should similarly use npoints to get the right number. Magic constants like these are bad exactly because they cause mistakes of this sort.