简体   繁体   English

OpenGL不会绘制三角形,而是遵循精确格式

[英]OpenGL won't draw a triangle, followed exact format

I was able to get to the point where it creates a window and everything is fine, but when I attempt to make a Triangle, it wouldn't show up. 我能够到达创建一个窗口的位置,一切都很好,但是当我尝试制作一个三角形时,它不会显示出来。

I've created a buffer and bind it and also made my shader, so I don't understand why it wouldn't work. 我创建了一个缓冲区并绑定了它,还制作了我的着色器,所以我不明白为什么它不起作用。

I am using MacOSX and Xcode. 我正在使用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;

}

A couple things: 几件事:

  1. Call glfwInit() before calling glfwWindowHint() . 在调用glfwInit()之前,先调用glfwWindowHint() Otherwise it won't have any effect and you'll (probably) end up with a Compatibility context instead of the Core context you were asking for. 否则它将不会有任何效果,并且(可能)您最终会获得一个Compatibility上下文,而不是您想要的Core上下文

    If you set 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; ... 

    ...you can see GLFW error stream caused by calling (most) GLFW functions before glfwInit() : ...您可以看到由于在glfwInit()之前调用(大多数) 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) aren't optional in Core contexts. 顶点数组对象(VAO)在Core上下文中不是可选的。

    Create & bind a VAO before trying to enable/set vertex attributes & drawing: 在尝试启用/设置顶点属性和绘图之前,创建并绑定VAO:

     ... GLuint vao = 0; glGenVertexArrays( 1, &vao ); glBindVertexArray( vao ); glEnableVertexAttribArray(0); ... 

All together: 全部一起:

#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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM