OpenGL不会绘制三角形,遵循格式

时间:2018-04-10 23:19:39

标签: c++ xcode macos opengl glfw

我能够达到创建窗口的程度,一切都很好,但是当我尝试制作一个三角形时,它就不会显示出来。

我已经创建了一个缓冲区并将其绑定并制作了我的着色器,因此我不明白为什么它不起作用。

我正在使用MacOSX和Xcode。

#include <iostream>

using namespace std;
//GLEW
#define GLEW_STATIC
#include <GL/glew.h>

//GLFW
#include <GLFW/glfw3.h>

const GLint WIDTH = 800, HEIGHT = 600;


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)
    {
        int length;
        glGetShaderiv(id, GL_INFO_LOG_LENGTH, &length);
        char* message = (char*)alloca(length * sizeof(char));
        glGetShaderInfoLog(id, length, &length, message);
        std::cout << "Failed to compile" << (type == GL_VERTEX_SHADER ? "vertex" : "fragment") << " shader!" <<std::endl;
        std::cout << message << std::endl;
        glDeleteShader(id);
        return 0;
    }

    return id;
}

static unsigned 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);

    glDeleteShader(vs);
    glDeleteShader(fs);

    return program;


}

int main(void)
{
    //glfwInit();


    glfwWindowHint( GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint( GLFW_CONTEXT_VERSION_MINOR, 3);
    glfwWindowHint( GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
    glfwWindowHint( GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
    glfwWindowHint( GLFW_RESIZABLE, GL_FALSE );

    GLFWwindow * window;

    if (!glfwInit())
        return -1;

    window = glfwCreateWindow( WIDTH, HEIGHT, "Learn OpenGL", NULL, NULL);

    int screenWidth, screenHeight;
    glfwGetFramebufferSize( window, &screenWidth, & screenHeight);

    if (!window)
    {
        cout << "Failed to create" << endl;
        glfwTerminate();

        return -1;
    }

    glfwMakeContextCurrent( window );

    glewExperimental = GL_TRUE;

    if ( glewInit() != GLEW_OK )
    {
        cout << "Failed to Initialize Glew" << endl;

        return -1;
    }

    glViewport ( 0, 0, screenWidth, screenHeight);

    //vertex buffer

    float positions[6] = {
       -0.5f, -0.5f,
        0.0f, 0.5f,
        0.5f, -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"
    "void main()\n"
    "{\n"
    "   color = vec4(1.0, 0.0, 0.0, 1.0);\n"
    "}\n";

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

    while ( !glfwWindowShouldClose ( window ))
    {

        glClearColor(0.2f, 0.3F, 0.3, 1.0f);
        glClear( GL_COLOR_BUFFER_BIT );


        glDrawArrays(GL_TRIANGLES, 0, 3);


        glfwSwapBuffers( window );
        glfwPollEvents();

    }

    glfwTerminate( );
    return 0;

}

1 个答案:

答案 0 :(得分:0)

一些事情:

  1. 在致电glfwInit()之前致电glfwWindowHint()。否则它将没有任何效果,你(可能)最终得到你要求的a Compatibility context instead of the Core context

    如果设置an error callback

    int main(void)
    {
        glfwSetErrorCallback( []( int, const char* str )
        {
            std::cerr << "GLFW error: " << str << std::endl;
        });
    
        glfwWindowHint( GLFW_CONTEXT_VERSION_MAJOR, 3);
        glfwWindowHint( GLFW_CONTEXT_VERSION_MINOR, 3);
        glfwWindowHint( GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
        glfwWindowHint( GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
        glfwWindowHint( GLFW_RESIZABLE, GL_FALSE );
    
        if (!glfwInit())
            return -1;
    ...
    

    ...您可以看到glfwInit()之前调用(most) GLFW函数导致的GLFW错误流:

    GLFW error: The GLFW library is not initialized
    GLFW error: The GLFW library is not initialized
    GLFW error: The GLFW library is not initialized
    GLFW error: The GLFW library is not initialized
    GLFW error: The GLFW library is not initialized
    
  2. Vertex Array Objects (VAOs)在Core上下文中不是可选的。

    创建&amp;在尝试启用/设置顶点属性之前绑定VAO&amp;附图中:

    ...
    GLuint vao = 0;
    glGenVertexArrays( 1, &vao );
    glBindVertexArray( vao );
    
    glEnableVertexAttribArray(0);
    ...
    
  3. 所有在一起:

    #include <iostream>
    
    using namespace std;
    //GLEW
    #define GLEW_STATIC
    #include <GL/glew.h>
    
    //GLFW
    #include <GLFW/glfw3.h>
    
    const GLint WIDTH = 800, HEIGHT = 600;
    
    
    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)
        {
            int length;
            glGetShaderiv(id, GL_INFO_LOG_LENGTH, &length);
            char* message = (char*)alloca(length * sizeof(char));
            glGetShaderInfoLog(id, length, &length, message);
            std::cout << "Failed to compile" << (type == GL_VERTEX_SHADER ? "vertex" : "fragment") << " shader!" <<std::endl;
            std::cout << message << std::endl;
            glDeleteShader(id);
            return 0;
        }
    
        return id;
    }
    
    static unsigned 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);
    
        glDeleteShader(vs);
        glDeleteShader(fs);
    
        return program;
    
    
    }
    
    int main(void)
    {
        if (!glfwInit())
            return -1;
    
        glfwWindowHint( GLFW_CONTEXT_VERSION_MAJOR, 3);
        glfwWindowHint( GLFW_CONTEXT_VERSION_MINOR, 3);
        glfwWindowHint( GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
        glfwWindowHint( GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
        glfwWindowHint( GLFW_RESIZABLE, GL_FALSE );
    
        GLFWwindow * window;
        window = glfwCreateWindow( WIDTH, HEIGHT, "Learn OpenGL", NULL, NULL);
    
        int screenWidth, screenHeight;
        glfwGetFramebufferSize( window, &screenWidth, & screenHeight);
    
        if (!window)
        {
            cout << "Failed to create" << endl;
            glfwTerminate();
    
            return -1;
        }
    
        glfwMakeContextCurrent( window );
    
        glewExperimental = GL_TRUE;
    
        if ( glewInit() != GLEW_OK )
        {
            cout << "Failed to Initialize Glew" << endl;
    
            return -1;
        }
    
        glViewport ( 0, 0, screenWidth, screenHeight);
    
        //vertex buffer
    
        float positions[6] = {
           -0.5f, -0.5f,
            0.0f, 0.5f,
            0.5f, -0.5f
        };
    
    
        unsigned int buffer;
        glGenBuffers(1, &buffer);
        glBindBuffer(GL_ARRAY_BUFFER, buffer);
        glBufferData(GL_ARRAY_BUFFER, (6 * sizeof(float)), positions, GL_STATIC_DRAW);
    
        GLuint vao = 0;
        glGenVertexArrays( 1, &vao );
        glBindVertexArray( vao );
    
        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"
        "void main()\n"
        "{\n"
        "   color = vec4(1.0, 0.0, 0.0, 1.0);\n"
        "}\n";
    
        unsigned int shader = CreateShader(vertexShader, fragmentShader);
        glUseProgram(shader);
    
        while ( !glfwWindowShouldClose ( window ))
        {
    
            glClearColor(0.2f, 0.3F, 0.3, 1.0f);
            glClear( GL_COLOR_BUFFER_BIT );
    
    
            glDrawArrays(GL_TRIANGLES, 0, 3);
    
    
            glfwSwapBuffers( window );
            glfwPollEvents();
    
        }
    
        glfwTerminate( );
        return 0;
    }