我对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的名称,还是我错过了某些/理解错误?
提前谢谢!
答案 0 :(得分:1)
上一行VAOs
使用GLuint
填充数组glBindVertexArray(VAOs[TRIS]);
。其中一个是您传递给VAOs
如果要查看传入的内容,请设置调试器断点并检查VAOs[TRIS]
。
TRIS
表示取VAOs
数组的第一个(Add Erlang Solutions repo: wget https://packages.erlang-solutions.com/erlang-solutions_1.0_all.deb && sudo dpkg -i erlang-solutions_1.0_all.deb
Run: sudo apt-get update
Run: sudo apt-get install elixir
== 0)元素。