S
S
sddvxd2019-12-31 04:21:25
C++ / C#
sddvxd, 2019-12-31 04:21:25

Why is the program behaving strangely?

Hello
If my code contains the following line:
sideY++;
then the program displays a triangle on the screen. If I remove it, then the program stops working normally (a blank screen without a triangle). I was very surprised, because sideY does not interact with anything at all:

bool sideY = true;
    glClearColor(1.0f, 1.0f, 1.0f, 1.0f);

    while(!glfwWindowShouldClose(window))
    {
        sideY++;
            
        GLuint VBO, VAO;
        glGenBuffers(1, &VBO);
        glBindBuffer(GL_ARRAY_BUFFER, VBO);
        glBufferData(GL_ARRAY_BUFFER, 9 * sizeof(GLfloat), vertexTriangle, GL_DYNAMIC_DRAW);
        glGenVertexArrays(1, &VAO);
        glBindVertexArray(VAO);
        glEnableVertexAttribArray(0);
        glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, NULL);
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        GLfloat timeValue = glfwGetTime();
        GLfloat greenValue = (sin(timeValue) / 2) + 0.5;
        GLint vertexColorLocation = glGetUniformLocation(shaderProgram, "ourColor");
        glUseProgram(shaderProgram);
        glUniform4f(vertexColorLocation, 0.0f, greenValue, 0.0f, 1.0f);
        glDrawArrays(GL_TRIANGLES, 0, 3);
        glfwPollEvents();
        glfwSwapBuffers(window);
    }

and if I remove the line with its increment - the program stops working normally... help please, I've broken my head, I don't know what could be the problem

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Saboteur, 2019-12-31
@saboteur_kiev

Apparently it's just a limitation for drawing a single triangle, sydeY was false, after ++ became true, it's been true ever since. Apparently not all the code is given, but the variable is global.
PS Starting from version 17, the increment does not work for bool types, so in any case you need to change it.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question