user1841718
user1841718

Reputation: 187

Vertex array is not being drawn

I have a vertex array that has 8 vertices, every vertex is represented by two coordinates. I have used glVertexPointer function to use this array in drawing with glDrawArray function.

void datasource()
{
    GLfloat vertex1[]={ -1.000000, 0.500000, -0.700000, 0.500000, -1.000000, 0.800000, -0.700000, 0.800000, -0.400000, 0.500000, -0.100000, 0.500000, -0.400000, 0.800000, -0.100000, 0.800000 };

    glEnableClientState(GL_VERTEX_ARRAY);
    glVertexPointer(2, GL_FLOAT, 0, vertex1);
}

void display()
{
    frame++;

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glDrawArrays(GL_LINES, 0, 2);

    glutSwapBuffers();
    glutPostRedisplay();
}

When I try to draw line using first two vertices, I do not see anything but it should appear horizontal line. What am I doing wrong?

Upvotes: 3

Views: 154

Answers (2)

datenwolf
datenwolf

Reputation: 162289

What am I doing wrong?

Your data is on the stack of a function, you probably call to set the array pointer. Unfortunately as soon as the function returns it's stack frame gets deallocated and that pointer becomes invalid.

So either use a VBO to copy that data to a persistent OpenGL object first. Or allocate that memory on the heap or as a global variable.

Also you should call glEnableClientState and glVertexPointer right before the corresponding glDraw… function call.

Upvotes: 4

flamingo
flamingo

Reputation: 506

You should use glVertexPointer function every time, when you call display function

Upvotes: 0

Related Questions