0
\$\begingroup\$

I have encountered a very odd error in my program, "odd" in the sense that everything the API says suggests that the error should not occur.

I have a bunch of 2D un-indexed vertex data, and I want to render it as lines. So far, so good. Then, I wanted to make each vertex have its own (RGB) color, so I generate a color for each vertex. For simplicity, I chose red. Works fine, except now only 2/3 of the points are being rendered! The problem arises from the fact that each vertex's position data consists of only 2 numbers, whereas the color data consists of 3 numbers. So, the "position" buffer has 2 elements per vertex while the "color" one has 3 elements per vertex. I thought that using glVertexAttribPointer to tell this information to OpenGL would be enough, but turns out it's not. In fact, if I say that the color data has only 2 elements per vertex, using

glVertexAttribPointer(vertexColorID2,2,GL_DOUBLE,GL_FALSE,0,(void*)0);

(as opposed to 3), it renders all the points - except now I can only specify two numbers for the RGB color, so I can't get the right color. The full code of the issue is below:

    glUseProgram(programID2); //draw the graph
    graph_data = graphData();
    std::vector<double> graphcolordata(graph_data.size()/2*3); for (int i = 0; i < graph_data.size(); i += 3) graphcolordata[i] = 1;
    glEnableVertexAttribArray(vertexPosition_modelspaceID2);
    glBindBuffer(GL_ARRAY_BUFFER, graphbuffer);
    glBufferData(GL_ARRAY_BUFFER, graph_data.size()*sizeof(GLdouble), &graph_data[0], GL_STREAM_DRAW);
    glVertexAttribPointer(vertexPosition_modelspaceID2,2,GL_DOUBLE,GL_FALSE,0,(void*)0);
    glEnableVertexAttribArray(vertexColorID2);
    glBindBuffer(GL_ARRAY_BUFFER, colorbuffer2);
    glBufferData(GL_ARRAY_BUFFER, graphcolordata.size()*sizeof(GLdouble), &graphcolordata[0], GL_STREAM_DRAW);
    glVertexAttribPointer(vertexColorID2,3,GL_DOUBLE,GL_FALSE,0,(void*)0);
    glDrawArrays(GL_LINES, 0, graph_data.size()/2);
    glDisableVertexAttribArray(vertexPosition_modelspaceID2);
    glDisableVertexAttribArray(vertexColorID2);

Note: programID2 is my basic shader program, and the following variable definitions were previously used:

GLuint vertexPosition_modelspaceID2 = glGetAttribLocation(programID2, "vertexPosition_modelspace");
GLuint vertexColorID2 = glGetAttribLocation(programID2, "vertexColor");

Edit: Incredibly stupid error, figured it out immediately after posting when it had previously stumped me for half an hour.

    std::vector<double> graphcolordata(graph_data.size()/2*3); for (int i = 0; i < graph_data.size(); i += 3) graphcolordata[i] = 1;

should be std::vector graphcolordata(graph_data.size()/2*3); for (int i = 0; i < graphcolordata.size(); i += 3) graphcolordata[i] = 1;

When this initialization is fixed, it works fine. I would delete this, but I do not see how.

\$\endgroup\$

1 Answer 1

0
\$\begingroup\$

Solved:

Incredibly stupid error, figured it out immediately after posting when it had previously stumped me for half an hour.

    std::vector<double> graphcolordata(graph_data.size()/2*3); for (int i = 0; i < graph_data.size(); i += 3) graphcolordata[i] = 1;

should be std::vector graphcolordata(graph_data.size()/2*3); for (int i = 0; i < graphcolordata.size(); i += 3) graphcolordata[i] = 1;

When this initialization is fixed, it works fine. I would delete this question, but I'm not sure how.

Sorry about that.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .