OpenGL 红皮书 - 如果枚举默认为从 0 开始的整数,glBindVertexArray 如何绑定到保留值 0?

OpenGL Red Book - If enums default to ints starting at 0, how does glBindVertexArray bind to the reserved value of 0?

我对 OpenGL 编程指南的第一个示例有点困惑。

作者使用这个例子:

///////////////////////////////////////////////////////////////////////
//
// triangles.cpp
//
///////////////////////////////////////////////////////////////////////
// g++ -g -o tri triangles.cpp LoadShader.cpp -lglut -lGLEW -lGLU -lGL -lX11 -lm
#include <iostream>
using namespace std;

#include "vgl.h"
#include "LoadShader.h"

enum VAO_IDs { Triangles, NumVAOs };
enum Buffer_IDs { ArrayBuffer, NumBuffers };
enum Attrib_IDs { vPosition = 0 };

GLuint  VAOs[NumVAOs];
GLuint  Buffers[NumBuffers];

const GLuint  NumVertices = 6;

//---------------------------------------------------------------------
//
// init
//

void
init(void)
{
    glGenVertexArrays(NumVAOs, VAOs);
    glBindVertexArray(VAOs[Triangles]);

    GLfloat  vertices[NumVertices][2] = {
        { -0.90, -0.90 },  // Triangle 1
        {  0.85, -0.90 },
        { -0.90,  0.85 },
        {  0.90, -0.85 },  // Triangle 2
        {  0.90,  0.90 },
        { -0.85,  0.90 }
    };

    glGenBuffers(NumBuffers, Buffers);
    glBindBuffer(GL_ARRAY_BUFFER, Buffers[ArrayBuffer]);
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertices),
                 vertices, GL_STATIC_DRAW);

    ShaderInfo  shaders[] = {
        { GL_VERTEX_SHADER, "triangles.vert" },
        { GL_FRAGMENT_SHADER, "triangles.frag" },
        { GL_NONE, NULL }
    };

    GLuint program = LoadShaders(*shaders);
    glUseProgram(program);

    glVertexAttribPointer(vPosition, 2, GL_FLOAT,
                          GL_FALSE, 0, BUFFER_OFFSET(0));
    glEnableVertexAttribArray(vPosition);
}

//---------------------------------------------------------------------
//
// display
//

void
display(void)
{
    glClear(GL_COLOR_BUFFER_BIT);

    glBindVertexArray(VAOs[Triangles]);
    glDrawArrays(GL_TRIANGLES, 0, NumVertices);

    glFlush();
}

//---------------------------------------------------------------------
//
// main
//

int
main(int argc, char** argv)
{



     glutInit(&argc, argv);
     glutInitDisplayMode(GLUT_RGBA);
     glutInitWindowSize(512, 512);
     glutInitContextVersion(4, 3);
     glutInitContextProfile(GLUT_CORE_PROFILE);
     glutCreateWindow(argv[0]);

     glewExperimental = GL_TRUE;

     if (glewInit()) {
         cerr << "Unable to initialize GLEW ... exiting" << endl;
         exit(EXIT_FAILURE);
     }

     init();

     glutDisplayFunc(display);

     glutMainLoop();
}

这是我的实现:

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

#include <GLShaderLoader.hpp> 

using namespace std;

// Reserved VAO ID indicies
enum VAO_IDs {TRIS, NUM_VAO};

// Reserved VBO ID indicies
enum VBO_IDs {BUFFER, NUM_BUFFER};

// Reserved names (for VAOs and VBOs)
GLuint VAOs[NUM_VAO];
GLuint buffers[NUM_BUFFER];

// Hard-coded vertex count
const GLuint numVerts = 6;

// Initialize VAOs and VBOs, populate VBOs, and provide OGL server memory access
void glInit() {
    glGenVertexArrays(NUM_VAO, VAOs);
    glBindVertexArray(VAOs[TRIS]);

    GLfloat verts[numVerts][2] {
        { 0.0, 0.5 },
        { 0.5, -0.5 },
        { -0.5, -0.5},
        {-0.9, 0.9},
        {-0.9, 0.7},
        {-0.7, 0.7}
    };

    glGenBuffers(NUM_BUFFER, buffers);
    glBindBuffer(GL_ARRAY_BUFFER, buffers[BUFFER]);
    glBufferData(GL_ARRAY_BUFFER, sizeof(verts), verts, GL_STATIC_DRAW);

    GLuint prog = loadShaders("pass.vert", "pass.frag");

    glUseProgram(prog);
    glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, 0);

    glEnableVertexAttribArray(0);
}

// Main entry point
void main(int argc, char** argv) {
    glewExperimental = GL_TRUE;

    GLFWwindow* window;

    if (!glfwInit()) {
    }

    window = glfwCreateWindow(640, 480, "Hello Fam", NULL, NULL);

    if (!window) {
        glfwTerminate();
    }

    glfwMakeContextCurrent(window);

    GLenum err = glewInit();

    if (GLEW_OK != err) {
        fprintf(stderr, "Error: %s\n", glewGetErrorString(err));
    }

    glInit();

    //*************
    //  MAIN LOOP
    //*************

    while (!glfwWindowShouldClose(window)) {
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        glBindVertexArray(VAOs[TRIS]);
        glDrawArrays(GL_TRIANGLES, 0, numVerts);

        glfwSwapBuffers(window);
        glfwPollEvents();
    }

    glfwTerminate();
}

现在,问题是,我的实现编译和运行都很好,但我很困惑。在书中,它说 glBindVertexArray() 不会取 0,因为 0 是为默认状态保留的。但是,枚举永远不会初始化并默认为它们的索引。这意味着当我调用 glBindVertexArray(VAOs[TRIS]).

时,OpenGL 绑定到一个为 0 的 VAO 名称

我的问题是:OpenGL 真的 绑定到 0 的名称,还是我错过了 something/understanding 错误?

提前致谢!

上一行 glGenVertexArrays(NUM_VAO, VAOs);GLuint 填充数组 VAOs。其中之一就是您传递给 glBindVertexArray(VAOs[TRIS]);

的内容

如果您想查看传入的内容,请设置调试器断点并检查 VAOs

VAOs[TRIS] 表示取 VAOs 数组的第一个(如 TRIS == 0)元素。