我对 OpenGL Programming Guide 的第一个示例有点困惑。
作者使用了这个例子:
///////////////////////////////////////////////////////////////////////
//
// 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 是为默认状态保留的。但是,枚举永远不会初始化并默认为其索引。这意味着 OpenGL 绑定到一个 VAO 名称,当我调用glBindVertexArray(VAOs[TRIS])
.
我的问题是:OpenGL真的绑定到 0 的名称,还是我遗漏了什么/理解错误?
先感谢您!