使用c ++库GLEW在openGl中绘制一个红色三角形

时间:2018-03-11 10:00:34

标签: c++ opengl shader glfw glew

我正在尝试使用GLEW在OpenGL中绘制一个红色三角形。首先,我在屏幕中央画了一个三角形。这是正确绘制的。然后,我试图使用着色器获得一个红色三角形。但是,我无法得到一个红色三角形,因为我的着色器编译失败了。什么可能是错的?

#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <iostream>

static unsigned int CompileShader(unsigned int type, const std::string& source)
{
    unsigned int id = glCreateShader(type);
    const char* src = source.c_str();
    glShaderSource(id, 1, &src, nullptr);
    glCompileShader(id);

    int result; 
    glGetShaderiv(id, GL_COMPILE_STATUS, &result);
    if (result == GL_FALSE)
    {
        std::cout << "failed to compile shader" << std::endl;
    }
    return id;
}

static int CreateShader(const std::string& vertexShader, const std::string& fragmentShader) 
{
    unsigned int program = glCreateProgram();
    unsigned int vs = CompileShader(GL_VERTEX_SHADER,vertexShader);
    unsigned int fs = CompileShader(GL_FRAGMENT_SHADER,fragmentShader);

    glAttachShader(program, vs);
    glAttachShader(program, fs);
    glLinkProgram(program);
    glValidateProgram(program);

    glDetachShader(program,vs);
    glDetachShader(program,fs);
    glDeleteShader(vs); 
    glDeleteShader(fs);

    return program;
}

int main(void)
{
    GLFWwindow* window;

    /* Initialize the library */
    if (!glfwInit())
        return -1;

    /* Create a windowed mode window and its OpenGL context */
    window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL);
    if (!window)
    {
        glfwTerminate();
        return -1;
    }
    /* Make the window's context current */
    glfwMakeContextCurrent(window);

    if (glewInit() != GLEW_OK) {
        std::cout << "error..!!" << std::endl;
    }
    float positions[6] = {
        -0.5f,-0.5f,
         0.5f, -0.5f,
         0.0f, 0.5f
    };
    unsigned int buffer;
    glGenBuffers(1, &buffer);
    glBindBuffer(GL_ARRAY_BUFFER, buffer);
    glBufferData(GL_ARRAY_BUFFER, 6 * sizeof(float),positions,GL_STATIC_DRAW);

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

    std::string vertexShader =
        "#version 330 core\n"
        "\n"
        "layout(location=0) in vec4 position;\n"
        "\n"
        "void main()\n"
        "{\n"
        "   gl_Position=position;\n"
        "}\n";

    std::string fragmentShader =
        "#version 330 core\n"
        "\n"
        "layout(location=0) out vec4 color;\n"
        "\n"
        "void main()\n"
        "{\n"
        "   color=vec4(1.0,0.0,0.0,1.0)\n"
        "}\n";

    unsigned int shader = CreateShader(vertexShader,fragmentShader);
    glUseProgram(shader);

    /* Loop until the user closes the window */
    while (!glfwWindowShouldClose(window))
    {
        /* Render here */
        glClear(GL_COLOR_BUFFER_BIT);

        glDrawArrays(GL_TRIANGLES,0,3);
        /* Swap front and back buffers */
        glfwSwapBuffers(window);

        /* Poll for and process events */
        glfwPollEvents();
    }

    glfwTerminate();
    return 0;
}

1 个答案:

答案 0 :(得分:3)

着色器编译失败,因为着色器在片段中color=vec4(1.0,0.0,0.0,1.0)后缺少分号。

如果您打印着色器信息日志,那么您可以轻松找到这样的问题:

glCompileShader(id);

GLint status;
glGetShaderiv( id, GL_COMPILE_STATUS, &status );
if ( status == GL_FALSE )
{
    GLint maxLen;
    glGetShaderiv( id, GL_INFO_LOG_LENGTH, &maxLen );
    std::vector< char >log( maxLen );
    GLsizei len;
    glGetShaderInfoLog( id, maxLen, &len, log.data() );
    std::cout << "compile error:" << std::endl << log.data() << std::endl;
}

此外,我建议使用Raw string literals

std::string fragmentShader = R"(
    #version 330 core

    layout(location=0) out vec4 color;

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