1
\$\begingroup\$

I'm writing a 2D program in which a monocycle follows a Catmull-Rom spline. My problem is when I write the circle, the drawing goes crazy. There is one line on the screenshot (which seems to be 3 when watching it), that rotates with the circle and blinks around it.

Screenshot showing errant red line

It doesn't happen always, comes randomly, sometimes it's fine then it will start this after a few seconds.

I have no clue what could cause it. My calculations are correct, there aren't fauly or nans/inf-s in the array, and if I put printf right before buffering the vbo, the lines don't appear, so I can't even debug it. And sometimes there's a small blinking in the green part too.

Here's some part of my code.

Creating the curve:

void Create() {
    // Curve
    glGenVertexArrays(1, &vaoCurve);
    glBindVertexArray(vaoCurve);

    glGenBuffers(1, &vboCurve); // Generate 1 vertex buffer object
    glBindBuffer(GL_ARRAY_BUFFER, vboCurve);
    // Enable the vertex attribute arrays
    glEnableVertexAttribArray(0);  // attribute array 0
    // Map attribute array 0 to the vertex data of the interleaved vbo
    glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(float), NULL); // attribute array, components/attribute, component type, normalize?, stride, offset
}

void Create() {
    glGenVertexArrays(1, &vaoCyclist);
    glBindVertexArray(vaoCyclist);
    // Curve
    glGenBuffers(1, &vboCyclist); // Generate 1 vertex buffer object
    glBindBuffer(GL_ARRAY_BUFFER, vboCyclist);
    glGenBuffers(1, &vboHuman); // Generate 1 vertex buffer object
    // Enable the vertex attribute arrays
    glEnableVertexAttribArray(0);  // attribute array 0
    // Map attribute array 0 to the vertex data of the interleaved vbo
    glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 2*sizeof(float), NULL); // attribute array, components/attribute, component type, normalize?, stride, offset
}

Calculating:

humanData.clear();
        circleData.clear();

        for (int i = 0; i < 180; i++) {
            cordX = cosf((float)M_PI * i * 2 / 180.0f);
            cordY = sinf((float)M_PI * i * 2 / 180.0f);

            if (i == 0) {
                circleData.push_back(cordX);
                circleData.push_back(cordY);
            }

            else if (i == 179)
            {
                circleData.push_back(cosf((float)M_PI * i * 2 / 180.0f));
                circleData.push_back(sinf((float)M_PI * i * 2 / 180.0f));

                circleData.push_back(cosf((float)M_PI * i * 2 / 180.0f));
                circleData.push_back(sinf((float)M_PI * i * 2 / 180.0f));

                circleData.push_back(cosf((float)M_PI * 0 * 2 / 180.0f));
                circleData.push_back(sinf((float)M_PI * 0 * 2 / 180.0f));
            }

            else {
                circleData.push_back(cordX);
                circleData.push_back(cordY);
                circleData.push_back(cordX);
                circleData.push_back(cordY);
            }
        }

Draw:

mat4 MVPTransform = MrotateTranslate() * camera.V() * camera.P();

MVPTransform.SetUniform(gpuProgram.getId(), "MVP");

int colorLocation = glGetUniformLocation(gpuProgram.getId(), "color");

for (int i = 0; i < circleData.size() - 1; i++)
    printf("%f\n", circleData[i]);

glBindVertexArray(vaoCyclist);
glBindBuffer(GL_ARRAY_BUFFER, vboCyclist);
glBufferData(GL_ARRAY_BUFFER, circleData.size()* sizeof(float), &circleData[0], GL_DYNAMIC_DRAW);
if (colorLocation >= 0) glUniform4f(colorLocation, 0.72f, 0.16f, 0.0f, 1.0f);
glDrawArrays(GL_LINES, 0, circleData.size());

Cyclist attributes:

class Cyclist {
private:
    unsigned int vaoCyclist, vboCyclist;
    unsigned int vboHuman;
    float r= 1;
    float v = 0;
    vec2 wTranslate = vec2(-10.0f, 0.0f);
    vec2 position = vec2(0, 0);
    float sina, fi=0, ds;
    float cordX, cordY;
    vector<float> circleData;
    vector<float> humanData;
    float dir = 1.0f;
\$\endgroup\$
  • \$\begingroup\$ Why does your for loop in the Draw section skip the last element? Is circleData.size() what you think it should be? \$\endgroup\$ – user1118321 Apr 3 at 4:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.