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

问题描述 投票:0回答:1

我目前正在尝试学习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每次都通过我的检查。”

c++ c++11 opengl glsl opengl-4
1个回答
0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.