Binding a second vertex buffer seems to spoil my first vertex buffer, OpenGL OES ios 5.1

♀尐吖头ヾ 提交于 2019-12-12 11:22:04

问题


I'm creating two different vertex buffers, that use two different shaders to render them. As soon as I bind the second vertex buffer, the data I parked in the first vertex buffer seems to be corrupted or lost.

If I generate and draw one vertex buffer only, like so:

glGenBuffers( 1, &vb1 ) ;
glBindBuffer( GL_ARRAY_BUFFER, vb1 ) ;

// fill it..
glBufferData( .. )

Then, in the draw() loop,

glUseProgram( shader1 ) ;
glBindBuffer( vb1 ) ; // make sure it is bound
glDrawArrays( ... )   // draw it

Then it works fine, no problems, no errors (I am glGettingLastError() after every gl* call, so it seems this code is perfectly fine)

Now if I so much as generate and bind a second vertex buffer, any time after the first one was generated and bound,

// ran in init() -- then previously working code in draw() is broken
glGenBuffers( 1, &vb2 ) ;              // ok.. no problems with this line
glBindBuffer( GL_ARRAY_BUFFER, vb2 ) ; // spoils data in vb1?

As soon as I call glBindBuffer with this newly generated vb2 buffer, it seems the data in vb1 is completely dumped or lost. On attempts to draw vb1 (not vb2!), i get this crash:

I even filled the array with GL_STATIC_DRAW.

I don't understand, I thought these vertex buffers were supposed to retain data, even if another vertex buffer is created and initialized? .. what am I doing wrong?


回答1:


I found the answer to this question to be very subtle and difficult to find.

I thought I didn't need the vertex array declarations that the standard example uses, and you can see I left them out in the question. But it turns out using vertex array objects is vital to correctly binding indices.

So for example,

// Make vertex array OBJECT, _this does not store any data!_
// A "vertex array object" is a bit like a display list in
// that it will just remember and repeat the function calls you make,
// with the values you make them with.
glGenVertexArraysOES( 1, &va ) ; // make vertex array OBJECT. NOT used
// to store data..

// Bind the vertex array
glBindVertexArrayOES( va ) ; // "start recording the calls I make.."

// Make a buffer for the vertex array (place to store the data)
glGenBuffers( 1, &vb ) ;     // make the vertex BUFFER (used to
// store the actual data)

// Make the vertex array buffer active
glBindBuffer( GL_ARRAY_BUFFER, vb ) ;

glEnableVertexAttribArray( ... ) ;  // REMEMBERED IN VAO (vertex array object)
glVertexAttribPointer( ... ) ;      // REMEMBERED IN VAO

glBufferData( .. ) ; // send data down to gpu, not remembered by vao

// "Stop remembering" calls to glBindBuffer( .. ) and glEnableVertexAttribArray( .. )
glBindVertexArrayOES( 0 ) ; // "stop recording"

At draw time, simply:

glBindVertexArrayOES( va ) ; // runs ALL your glEnableVertexAttribArray() calls,
// as well as your glBindBuffer( .. ) call,

// now all that's left to do is draw
glDrawArrays( glDrawingStyle, 0, vertexCount );


来源:https://stackoverflow.com/questions/12426507/binding-a-second-vertex-buffer-seems-to-spoil-my-first-vertex-buffer-opengl-oes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!