问题描述
这很重要:如果我将代码留给glGenBuffers(1, vertexBuffers)
,则代码可以编译并运行.但是,我认为应该为2
,因为vertexBuffers
的大小为2.
Here is the deal: If I leave the code with glGenBuffers(1, vertexBuffers)
, the code compiles and works. But, I thought that it should be 2
since vertexBuffers
is of size two.
我错过了什么吗?
以下代码:
-(void)drawRect:(NSRect)dirtyRect
{
// get program ID for shader program
GLuint programID = [self loadShaders];
// get new dimensions
NSSize dim = [self frame].size;
// clear the background with color
glClearColor(0.0, 0.0, 0.0, 0.4);
glDepthRange(0.1, 100.0);
glViewport(0, 0, dim.width, dim.height);
glClear(GL_COLOR_BUFFER_BIT);
// vertex data
GLfloat vertexPositionData[] = {-dim.width/2, -dim.height/2, 0.0, 5.0,
dim.width/2, -dim.height/2, 0.0, 5.0,
0.0, dim.height/2, 0.0, 5.0};
GLfloat vertexColorData[] = {1.0, 0.0, 0.0, 1.0, 0.0, 1.0, 0.0, 0.5, 0.0, 0.0, 1.0, 0.5};
GLfloat scaleMatrixData[] = {1/(dim.width/2), 0.0, 0.0, 0.0,
0.0, 1/(dim.height/2), 0.0, 0.0,
0.0, 0.0, 1.0, 0.0,
0.0, 0.0, 0.0, 1.0};
GLint scaleMatrixUniform = glGetUniformLocation(programID, "scaleMatrix");
// generate a buffer for our triangle
glGenBuffers(1, vertexBuffers);
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffers[0]);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertexPositionData), vertexPositionData, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffers[1]);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertexColorData), vertexColorData, GL_STATIC_DRAW);
//glBindBuffer(GL_ARRAY_BUFFER, 0);
glUseProgram(programID);
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffers[0]);
glEnableVertexAttribArray(VERTEX_POS_INDEX);
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffers[1]);
glEnableVertexAttribArray(VERTEX_COLOR_INDEX);
glVertexAttribPointer(VERTEX_POS_INDEX, VERTEX_POS_SIZE, GL_FLOAT, GL_FALSE, 0, vertexPositionData);
glVertexAttribPointer(VERTEX_COLOR_INDEX, VERTEX_COLOR_SIZE, GL_FLOAT, GL_FALSE, 0, vertexColorData);
glUniformMatrix4fv(scaleMatrixUniform, 1, GL_FALSE, scaleMatrixData);
glDrawArrays(GL_TRIANGLES, 0, 3);
glDisableVertexAttribArray(0);
glDisableVertexAttribArray(1);
glUseProgram(0);
// flush buffer
glFlush();
[[self openGLContext] flushBuffer];
}
推荐答案
glGenBuffers
不能像您期望的那样工作.当您调用glGenBuffers
时,它实际上并没有创建任何东西.它只是返回一个当前不用作缓冲区名称的整数列表.
glGenBuffers
doesn't work quite like what you expect. When you call glGenBuffers
, it doesn't actually create anything. It just returns a list of integers that are not currently used as buffer names.
在调用glBindBuffer
之前,不会创建实际的对象".因此,您只需组成您喜欢的任何整数,然后将其传递给glBindBuffer,就会在该索引处创建一个有效的缓冲区.实际上根本不需要glGenBuffers
,它只是一个方便的功能,可以为您提供未使用的整数.
The actual 'object' is not created until you call glBindBuffer
. So you can just make up any integer you like and pass it to glBindBuffer and a valid buffer will be created at that index. glGenBuffers
is actually not required at all, it's just there as a convenience function to give you an unused integer.
因此,如果您只创建一个随机整数数组,只要它们之间没有重叠,就可以将其用作缓冲区列表,而无需调用glGenBuffers
.这就是为什么无论告诉glGenBuffers创建1个还是2个缓冲区,代码都可以工作的原因.只要您有两个具有两个不同名称的缓冲区,整数的来源就无关紧要.
So if you just create an array of random integers, as long as none of them overlap, you can use that as your list of buffers without calling glGenBuffers
. That's why your code works whether you tell glGenBuffers to create 1 or 2 buffers. As long as you have two buffers with two different names, it doesn't matter where the integer came from.
一些示范:
int buf;
glGenBuffers(1, &buf);
glIsBuffer(buf); //FALSE - buffer has not been created yet
glBindBuffer(GL_ARRAY_BUFFER, buf);
glIsBuffer(buf); //TRUE - buffer created on bind
这篇关于协助我了解OpenGL glGenBuffers()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!