我的基本片段着色器无效

时间:2015-10-02 08:07:08

标签: opengl glsl

我试图制作一个白色三角形,下面的代码中的坐标出现在窗口上。但它只显示空白屏幕。这些是我的代码。

Vert Shader

#version 450

in vec3 vertPos;

void main() {
    gl_Position = vec4(vertPos, 1);
}

Frag Shader

#version 450

out vec4 Color;

void main() {
    Color = vec4(0.5f, 0.5f, 0.5f, 1.0f);
}

C ++

void Mesh::LoadShader(const std::string VSPath, const std::string FSPath) {
    Program = glCreateProgram();

    Shaders[0] = CreateShader(inputShader(VSPath), GL_VERTEX_SHADER);
    Shaders[1] = CreateShader(inputShader(FSPath), GL_FRAGMENT_SHADER);

    for (unsigned int i = 0; i < 2; i++)
        glAttachShader(Program, Shaders[i]);

    glBindAttribLocation(Program, 0, "vertPos");

    glLinkProgram(Program);
    CheckShaderError(Program, GL_LINK_STATUS, true, "Error: Program linking failed");

    glValidateProgram(Program);
    CheckShaderError(Program, GL_VALIDATE_STATUS, true, "Error: Program is invalid");
}

Mesh::Mesh(Vertex *vertices, unsigned int numVertices)
{

    glGenVertexArrays(1, &VAO);
    glBindVertexArray(VAO);

    glGenBuffers(1, VBO);

    std::vector<glm::vec3> positions;
    std::vector<glm::vec2> texCoords;

    positions.reserve(numVertices);
    texCoords.reserve(numVertices);

    for (unsigned int i = 0; i < numVertices; i++)
    {
        positions.push_back(vertices[i].GetPosition());
        texCoords.push_back(vertices[i].GetTextureCoordinate());
    }

    glBindBuffer(GL_ARRAY_BUFFER, VBO[0]);
    glBufferData(GL_ARRAY_BUFFER, positions.size(), &positions[0], GL_STATIC_DRAW);
    glEnableVertexAttribArray(0);
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);

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

void Mesh::Render(unsigned int TexUnit) {

    glUseProgram(Program);

    glBindVertexArray(VAO);
    glDrawArrays(GL_TRIANGLES, 0, 3);

    glBindVertexArray(0);
    glUseProgram(0);
}

int main(int argc, char** argv)
{
    // Initialize Window
    InitWindow win(800, 600, "Example");
    if (!win.isSuccess())
        return 1;

    // column 1 = Position , column 2 = Texture Coordinate
    Vertex vertices[]={ Vertex(glm::vec3(-0.5,-0.5,0),  glm::vec2(0.0,0.0)),
                    Vertex(glm::vec3(0,0.5,0),      glm::vec2(0.5,1.0)),
                    Vertex(glm::vec3(0.5,-0.5,0),   glm::vec2(1.0,0.0)) };

    // Initialize mesh object
    Mesh mesh(vertices, sizeof(vertices) / sizeof(vertices[0]));

    // Load Shader
    mesh.LoadShader("./res/vertex-shader.vs","./res/fragment-shader.fs");

    // Loop until the user closes the window 
    while (!win.isWindowShouldClosed())
    {
        // Clear Screen
        win.ClearScreen(0, 0.15f, 0.3f, 1);

        // Render here
        mesh.Render(0);

        // Swap front and back buffers
        win.SwapBuffer();

        // Poll for and process events
        win.PollEvents();
    }
    win.CloseWindow();
    return 0;
}

我很确定顶点着色器没有任何问题,因为我使用glGetAttribLocation()来检查&#34; vertPos&#34;的索引。顶点着色器中的attrib。

有人可以帮我查一下吗?我试图修理它一晚,但它没有工作。对不起,我是OpenGL的真正初学者,并且不想长期坚持制作简单的三角形。感谢您的帮助:)

P.S。如果我对这篇文章做错了,我很抱歉。这是我第一次访问这个网站。

2 个答案:

答案 0 :(得分:0)

您取消绑定GL_ARRAY_BUFFER并且不再绑定它。

只需删除此行:

glBindBuffer(GL_ARRAY_BUFFER, 0);

将在VAO中记住缓冲区绑定。

答案 1 :(得分:0)

谢谢你的所有答案。我发现我在&#34; glBufferData&#34;中分配了错误的大小。它必须是

glBufferData(GL_ARRAY_BUFFER, numVertices * sizeof(positions[0]), &positions[0], GL_STATIC_DRAW).

现在正常运作。抱歉我的笨拙。