代码中无法识别OpenGL着色器统一

bweufnob  于 2023-10-18  发布在  其他
关注(0)|答案(1)|浏览(104)

我正在学习OpenGL,遇到了以下问题:当我使用glGetUniformLocation来定位着色器制服时,它就是找不到这个制服。你知道为什么吗
我的代码:

const char* vertexShaderSource = R"(
    #version 460 core

    uniform vec3 transformation;

    in vec3 position;
    out vec4 color;

    void main()
    {
        gl_Position = vec4(position, 1.0);
        color = vec4(1.0, 1.0, 0.0, 1.0);
    }
)";

const char* fragmentShaderSource = R"(
    #version 460 core

    in vec4 color;
    out vec4 outColor;

    void main()
    {
        outColor = color;
    }
)";

int main()
{
    SDL_Init(SDL_INIT_VIDEO);
    Window2D window2D("Debug", 800, 600);
    gladLoadGL();
    glViewport(0, 0, 800, 600);

    //Dreieck vertices

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

    //Shader
    //Vertex Shader
    GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
    glShaderSource(vertexShader, 1, &vertexShaderSource, NULL);
    glCompileShader(vertexShader);

    //Fragment Shader
    GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
    glShaderSource(fragmentShader, 1, &fragmentShaderSource, NULL);

    //Programm
    GLuint program = glCreateProgram();
    glAttachShader(program, vertexShader);
    glAttachShader(program, fragmentShader);
    glLinkProgram(program);

    //Uniform
    GLint uniform = glGetUniformLocation(program, "transformation");
    if (uniform == -1) {
    std::cerr << "Error: Not Found" << std::endl;
    }

    //Shader

    //VBO and VAO
    GLuint VBO, VAO;
    glGenVertexArrays(1, &VAO);
    glGenBuffers(1, &VBO);

    glBindVertexArray(VAO);

    glBindBuffer(GL_ARRAY_BUFFER, VBO);
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

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

    glBindBuffer(GL_ARRAY_BUFFER, 0);
    glBindVertexArray(0);

    while(window2D.windowRun() == false)
    {
        glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
        glClear(GL_COLOR_BUFFER_BIT);

        glUseProgram(program);

        glBindVertexArray(VAO);
        glDrawArrays(GL_TRIANGLES, 0, 3);
    }
    window2D.closeWindow();
    return 0;
}

否则,一切实际上都在工作;三角形显示为黄色。只是制服不起作用了
“因为我还是一个初学者,不幸的是,我不知道从哪里开始。我只知道着色器编译正确,因为我可以看到黄色三角形。此外,我知道glGetUniformLocation总是返回-1,因为我每次都通过检查来观察它。

pvabu6sv

pvabu6sv1#

您没有在着色器中使用“transformation”,因此编译器可能会出于优化目的将其删除。尝试使其影响gl_Position,看看是否有效。

相关问题