我试着做一个简单的乒乓球游戏,但我遇到了一些问题。本质上,我有一个由四个点组成的数组,其x&y值表示硬编码的球,我需要正确地显示这个球。当我尝试使用glDrawArray时,我一直在崩溃,因为我在最后一个参数中传递的四个参数(意思是绘制四个顶点)超出了界限。知道为什么吗?
在我的设置中:
//put in vertices for ball
//point 1
ballPosArr[0] = 0.1; //x
ballPosArr[1] = 0.1; //y
//pt 2
ballPosArr[2] = -0.1;
ballPosArr[3] = 0.1;
//pt 3
ballPosArr[4] = 0.1;
ballPosArr[5] = -0.1;
//pt 4
ballPosArr[6] = -0.1;
ballPosArr[7] = 0.1;
//ball position buffer
GLuint buffer;
glGenBuffers( 1, &buffer);
glBindBuffer( GL_ARRAY_BUFFER, buffer);
glBufferData( GL_ARRAY_BUFFER, 8*sizeof(GLuint), ballPosArr, GL_STATIC_DRAW );
_buffers.push_back(buffer); //_buffers is a vector of GLuint's
// Initialize the attributes from the vertex shader
GLuint bPos = glGetAttribLocation(_shaderProgram, "ballPosition" );
glEnableVertexAttribArray(bPos);
glVertexAttribPointer(bPos, 2, GL_FLOAT, GL_FALSE, 0, &ballPosArr[0]);在我的显示回调中:
GLuint bPos = glGetAttribLocation(_shaderProgram, "ballPosition");
glEnableVertexAttribArray(bPos);
//rebind buffers and send data again
//ball position
glBindBuffer(GL_ARRAY_BUFFER, _buffers[0]);
glVertexAttribPointer(bPos, 2, GL_FLOAT, GL_FALSE, 0, &ballPosArr[0]);
glDrawArrays( GL_POLYGON, 0, 4); //bad access error at 4在我的vshader.txt:
attribute vec2 ballPosition;
void main() {
}发布于 2014-04-21 21:02:07
如果使用VBO,则glVertexAttribPointer的最后一个参数是缓冲区中的相对偏移量,而不是缓冲区的CPU地址。在您的示例中,为最后一个参数传递0,因为要使用的顶点数据位于缓冲区的开头。
https://stackoverflow.com/questions/23205812
复制相似问题