C++ OpenGL, using more than one uniform

Viewed 57

I learn OpenGL, below is fragment of my simple code witten in SFML + GLEW ( everything works fine ) :

    const char *vertexShaderSource =
        "#version 330 core\n"
        "layout (location = 0) in vec3 pos;\n"
        //"uniform vec2 offset = vec2(0.0f, 0.0f);\n";
        "uniform vec4 OurColor = vec4(0.0f, 0.0f, 0.0f, 1.0f);\n"
        "out vec4 ourColor;\n"
        "void main()\n"
        "{\n"
        "   gl_Position = vec4(pos.x + 0.0f, pos.y + 0.0f, pos.z, 1.0);\n"
            "ourColor = OurColor;\n"
        "}\0";
    
    const char *fragmentShaderSource =
        "#version 330 core\n"
        "out vec4 color;\n"
        "in vec4 ourColor;\n"
        "void main()\n"
        "{\n"
        "   color = ourColor;\n"
        "}\0";

    // --------------------------------------

    GLfloat vertices[] = {
        -0.5f,  1.0f, 0.0f,
         0.5f,  1.0f, 0.5f,
         0.0f,  0.0f, 0.0f,
        -0.5f, -1.0f, -0.5f,
         0.5f, -1.0f, 1.0f,
    };

    GLuint indices[] = {
        0, 1, 2,
        2, 3, 4
    };

    GLuint VAO;
    glGenVertexArrays(1, &VAO);

    glBindVertexArray(VAO);

    GLuint EBO;
    glGenBuffers(1, &EBO);

    glBindBuffer(
        GL_ELEMENT_ARRAY_BUFFER,
        EBO
    );

    glBufferData(
        GL_ELEMENT_ARRAY_BUFFER,
        sizeof(indices),
        indices,
        GL_STATIC_DRAW
    );

    // create buffer
    GLuint VBO;
    glGenBuffers(1, &VBO);

    glBindBuffer(GL_ARRAY_BUFFER, VBO);

    // copy vertices to buffer
    glBufferData(
        GL_ARRAY_BUFFER,
        sizeof(vertices),
        vertices,
        GL_STATIC_DRAW
    );

    // --------------------------------------

    GLuint vertexShader;
    vertexShader = glCreateShader(GL_VERTEX_SHADER);
    glShaderSource(
        vertexShader,
        1,
        &vertexShaderSource,
        nullptr
    );
    glCompileShader(vertexShader);

    GLint success;
    GLchar infoLog[512];
    glGetShaderiv(
        vertexShader,
        GL_COMPILE_STATUS,
        &success
    );

    if (!success) {
        glGetShaderInfoLog(vertexShader, 512, nullptr, infoLog);
        std::cerr << "Vertex shader compilation error: " << infoLog << "\n";
        std::cin.get();
        return EXIT_FAILURE;
    }

    // --------------------------------------

    GLuint fragmentShader;
    fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
    glShaderSource(
        fragmentShader,
        1,
        &fragmentShaderSource,
        nullptr
    );
    glCompileShader(fragmentShader);

    glGetShaderiv(
        fragmentShader,
        GL_COMPILE_STATUS,
        &success
    );

    if (!success) {
        glGetShaderInfoLog(fragmentShader, 512, nullptr, infoLog);
        std::cerr << "Fragment shader compilation error: " << infoLog << "\n";
        std::cin.get();
        return EXIT_FAILURE;
    }

    // --------------------------------------

    GLuint shaderProgram = glCreateProgram();
    glAttachShader(shaderProgram, vertexShader);
    glAttachShader(shaderProgram, fragmentShader);
    glLinkProgram(shaderProgram);
    glDeleteShader(vertexShader);
    glDeleteShader(fragmentShader);

    glGetShaderiv(
        shaderProgram,
        GL_LINK_STATUS,
        &success
    );

    if (!success) {
        glGetProgramInfoLog(shaderProgram, 512, nullptr, infoLog);
        std::cerr << "Shader link error: " << infoLog << "\n";
        std::cin.get();
        return EXIT_FAILURE;
    }

    // --------------------------------------

    glUseProgram(shaderProgram);

    glEnableVertexAttribArray(0);
    glVertexAttribPointer(
        0,
        3,
        GL_FLOAT,
        GL_FALSE,
        3 * sizeof(GL_FLOAT),
        (GLvoid*)0
    );

    // --------------------------------------

    glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);

    bool running = true;

    sf::Clock clock;

    while (running) {
        sf::Event windowEvent;

        while (window.pollEvent(windowEvent)) {
            switch (windowEvent.type) {
                case sf::Event::Closed: running = false; break;
            }
        }

        float greenValue = std::sin(clock.getElapsedTime().asSeconds()) / 2.0 + 0.5f;

        // ---------------- OpenGL here ---------------

        glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
        glClear( GL_COLOR_BUFFER_BIT );

        GLint vertexColorLocation = glGetUniformLocation(shaderProgram, "OurColor");
        //GLint vertexMovementLocation = glGetUniformLocation(shaderProgram, "offset");

        glUseProgram(shaderProgram);

        glUniform4f(vertexColorLocation, 0.f, greenValue, 0.f, 1.f);
        //glUniform2f(vertexMovementLocation, 0.5f, 1.f);

        glBindVertexArray(VAO);
        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
        glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);

        // --------------------------------------------

        window.display();
    }

Result as i expected:

enter image description here

The problem is with vertex shader, when i uncomment line:

uniform vec2 offset = vec2(0.0f, 0.0f);\n

i get:

enter image description here

I don't even use offest uniform anywhere. I have no idea what is going on, nothing works correct when I add one more uniform to vertex shader... I didn't get any shader compilation error.

Thank you in advance for your help!

1 Answers

This is a simple typo in the C++ source code.

    //"uniform vec2 offset = vec2(0.0f, 0.0f);\n";

There is an extra ; at the end of the line, outside the string literal. Remove it.

You may find it easier to detect these errors with higher warning levels (-Wall -Wextra) enabled, or by using a C++ autoformatter like clang-format. Here is the warning I get:

test.cpp: In function ‘int main()’:
test.cpp:7:9: warning: statement has no effect [-Wunused-value]
    7 |         "uniform vec4 OurColor = vec4(0.0f, 0.0f, 0.0f, 1.0f);\n"
      |         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    8 |         "out vec4 ourColor;\n"
      |         ~~~~~~~~~~~~~~~~~~~~~~
    9 |         "void main()\n"
      |         ~~~~~~~~~~~~~~~
   10 |         "{\n"
      |         ~~~~~
   11 |         "   gl_Position = vec4(pos.x + 0.0f, pos.y + 0.0f, pos.z, 1.0);\n"
      |         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   12 |             "ourColor = OurColor;\n"
      |             ~~~~~~~~~~~~~~~~~~~~~~~~
   13 |         "}\0";
      |         ~~~~~
[Exit: 1]

This is why I recommend using -Wall -Wextra when writing C++ code, or similar levels of warnings if you are using a different compiler. You may also consider enabling -Werror while you are developing.

Related