Opengl(三)——渲染出一个三角形

#define GLEW_STATIC
#include 
#include 
#include 
using namespace std;
//顶点着色器GLSL代码的字符串
const GLchar* vertexshader = "#version 330 core\n"                     
    "layout (location = 0) in vec3 position;\n"
    "void main()\n"
    "{\n"
    "gl_Position = vec4(position.x, position.y, position.z, 1.0);\n"
    "}\0";
//片段着色器的GLSL代码的字符串
const GLchar* fragmentshader = "#version 330 core\n"
"out vec4 color;\n"
"void main()\n"
"{\n"
"color = vec4(1.0f, 0.5f, 0.2f, 1.0f);\n"
"}\n\0";
//顶点坐标
GLfloat vertices[] = {
    -0.5f, -0.5f, 0.0f,
    0.5f, -0.5f, 0.0f,
    0.0f, 0.5f, 0.0f
};
void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode)
{
    if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
        glfwSetWindowShouldClose(window, GL_TRUE);
}
int main()
{
    glfwInit();
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
    glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);

    GLFWwindow* window = glfwCreateWindow(800, 600, "NB", nullptr, nullptr);
    if (window == nullptr)
    {
        std::cout << "Failed to create GLFW window" << std::endl;
        glfwTerminate();
        return -1;
    }
    glfwMakeContextCurrent(window);
    glewExperimental = GL_TRUE;
    GLenum err = glewInit();

    if (GLEW_OK!=err)
    {
        cout << "Failed to initialize GLEW" << endl;

    }

    glViewport(0, 0, 800, 600);

    glfwSetKeyCallback(window, key_callback);
    //初始化顶点缓存对象
    GLuint VBO;
    glGenBuffers(1, &VBO);
    //上面的顶点坐标数据传入到缓存中
    glBindBuffer(GL_ARRAY_BUFFER, VBO);
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
    //初始化顶点着色器对象
    GLuint vertexShader;
    vertexShader = glCreateShader(GL_VERTEX_SHADER);
    glShaderSource(vertexShader, 1, &vertexshader, NULL);
    //初始化片段着色器对象
    GLuint fragmentShader;
    fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
    glShaderSource(fragmentShader, 1, &fragmentshader, NULL);
    //创建着色器程序对象,链接并编译所有着色器
    GLuint shaderProgram;
    shaderProgram = glCreateProgram();
    glAttachShader(shaderProgram, vertexShader);
    glAttachShader(shaderProgram, fragmentShader);
    glLinkProgram(shaderProgram);

    //检测着色器的编译是否成功并输出调试信息
    GLint success;
    GLchar infoLog[512];
    glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success);
    if (!success)
    {
        glGetShaderInfoLog(vertexShader, 512, NULL, infoLog);
        std::cout << "ERROR::SHADER::VERTEX::COMPILATION_FAILED\n" << infoLog << std::endl;
    }
    GLint successf;
    GLchar infoLogf[512];
    glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &successf);
    if (!successf)
    {
        glGetShaderInfoLog(fragmentShader, 512, NULL, infoLogf);
        std::cout << "ERROR::SHADER::Fragment::COMPILATION_FAILED\n" << infoLogf << std::endl;
    }
    glGetProgramiv(shaderProgram, GL_LINK_STATUS, &success);
    if (!success)
    {
        glGetProgramInfoLog(shaderProgram, 512, NULL, infoLog);
    }
    glUseProgram(shaderProgram);
    glDeleteShader(vertexShader);
    glDeleteShader(fragmentShader);

    //初始化顶点数组对象,它可以存储单位物体所有顶点变量
    GLuint VAO;
    glGenVertexArrays(1, &VAO);
    //绑定VAO
    glBindVertexArray(VAO);
    //配置顶点属性
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), (GLvoid*)0);
    glEnableVertexAttribArray(0);
    //解绑VAO
    glBindVertexArray(0);
    while (!glfwWindowShouldClose(window))
    {
        glfwPollEvents();
        glClearColor(0.2f, 0.3f, 0.3f, 1);
        glClear(GL_COLOR_BUFFER_BIT);
        glUseProgram(shaderProgram);         //选择shader
        glBindVertexArray(VAO);              //绑定VAO
        glDrawArrays(GL_TRIANGLES, 0, 3);    //绘制
        glBindVertexArray(0);                //解绑VAO
        glfwSwapBuffers(window);
    }
    glfwTerminate();
    return 0;
}

上述代码是一个简单的渲染流程,基本上分为如下几个步骤:
1,将顶点数据放入缓存
2,配置着色器
3,配置VAO,VAO可以看作是单个渲染物体的所有顶点属性的配置集合,所谓配置就是告诉cpu该怎样使用缓存中的数据,以把数据传递给gpu
4,在游戏循环中在当下环境中调用函数渲染,当下环境包括着色器,VAO.

由于当绘制多个图形的时候,点可能会重合,因此可以使用索引缓冲对象来解决这个问题,简称EBO,它和VBO一样,是一种缓存,同样使用glgenbuffer()来获取ID,获取id之后,需要在VAO绑定后用glbindbuffer()绑定EBO,然后用glbufferdata()把索引数组复制进缓存中,特别要注意的是,在VAO绑定期间,会记录EBO的绑定,这样EBO会随着VAO的绑定而绑定,所以在解绑VAO之前不能解绑EBO,这一点与VBO不同,VAO在进行顶点属性配置的时候就相当于绑定VBO了,在配置好之后,在循环中需要调用glDrawElement()来进行绘制。

你可能感兴趣的:(OpenGL)