使用C++库GLEW在openGl中绘制一个红色三角形[关闭]

Posted

技术标签:

【中文标题】使用C++库GLEW在openGl中绘制一个红色三角形[关闭]【英文标题】:to draw a red triangle in openGl using c++ library GLEW [closed] 【发布时间】:2018-03-11 10:00:33 【问题描述】:

我正在尝试使用 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;

【问题讨论】:

打印着色器信息日志怎么样? khronos.org/opengl/wiki/… 【参考方案1】:

着色器编译失败,因为着色器在片段中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);
    
)";

【讨论】:

以上是关于使用C++库GLEW在openGl中绘制一个红色三角形[关闭]的主要内容,如果未能解决你的问题,请参考以下文章

无法使用 GLEW (mac,c++) 在 openGL 中绘制任何内容

OpenGL学习--02--绘制一个红色三角形

使用 GLFW/GLEW 进行 OpenGL 编程

Blender源码分析之OpenGL基本例子

使用 c++ 在 OpenGL 中生成两个对象

Blender源码分析之OpenGL基本例子