开发者

Why does drawing my OpenGL-ES VBO grid prevent other triangles from displaying?

I'm pretty new to OpenGl and am having trouble displaying a VBO based grid mixed with normal 3D objects. I can draw textured triangles on their own, but when I include the VBO grid the triangles stop displaying. I get a quick flash of the triangles before they're replaced by the grid - presumably because they're rendered first they feature in the first frame before being disabled.

I've tried to disable all the states I've enabled (and vice versa) and pop all the matrices I've pushed etc...

I've unrolled the OpenGl calls to simplify the code:


// initial OpenGl setup
gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_FASTEST);
gl.glClearColor(0.0f, 0.0f, 0.0f, 1);
gl.glShadeModel(GL10.GL_FLAT);
gl.glDisable(GL10.GL_DEPTH_TEST);
gl.glEnable(GL10.GL_TEXTURE_2D);
gl.glDisable(GL10.GL_DITHER);
gl.glDisable(GL10.GL_LIGHTING);
gl.glTexEnvx(GL10.GL_TEXTURE_ENV, GL10.GL_TEXTURE_ENV_MODE, GL10.GL_MODULATE);
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
gl.glViewport(0, 0, width, height); // width and height of surface


// render loop start

gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
gl.glMatrixMode(GL10.GL_PROJECTION);
gl.glLoadIdentity();
final float halfWidth = State.me.viewWidth / 2;
final float halfHeight = State.me.viewHeight / 2;
gl.glOrthof(-halfWidth, halfWidth, -halfHeight, halfHeight, 0.0f, 1.0f);
gl.glScalef(1f, -1f, 1f);


// draw quad        

gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glPushMatrix();
gl.glLoadIdentity();
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY); 
gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
gl.glEnable(GL10.GL_TEXTURE_2D);
gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_S, GL10.GL_REPEAT);
gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_T, GL10.GL_REPEAT);

gl.glTranslatef(x, y, -1.0f);
gl.glRotatef(angle, 0f, 0f, 1f);
gl.glBindTexture(GL10.GL_TEXTURE_2D, texture);
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, mVertexBuffer);
gl.glEnable(GL10.GL_TEXTURE_2D);
gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, mTexBuffer);
gl.glDrawElements(GL10.GL_TRIANGLES, VERTS, GL10.GL_UNSIGNED_SHORT, mIndexBuffer);

gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);          
gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);   
gl.glDisable(GL10.GL_TEXTURE_2D);
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glPopMatrix();
gl.glLoadIdentity();


// draw VBO grid

gl.glBindTexture(GL10.GL_TEXTURE_2D, texture);
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glPushMatrix();
gl.glLoadIdentity();
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glEnableC开发者_如何学JAVAlientState(GL10.GL_TEXTURE_COORD_ARRAY);
gl.glEnable(GL10.GL_TEXTURE_2D);

GL11 gl11 = (GL11) gl;
gl11.glBindBuffer(GL11.GL_ARRAY_BUFFER, mVertBufferIndex);
gl11.glVertexPointer(3, mCoordinateType, 0, 0);
gl11.glBindBuffer(GL11.GL_ARRAY_BUFFER, mTextureCoordBufferIndex);
gl11.glTexCoordPointer(2, mCoordinateType, 0, 0);
gl11.glBindBuffer(GL11.GL_ELEMENT_ARRAY_BUFFER, mIndexBufferIndex);
gl11.glDrawElements(GL11.GL_TRIANGLES, count, GL11.GL_UNSIGNED_SHORT, startIndex * CHAR_SIZE);
gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glPopMatrix();
gl.glLoadIdentity();

so to re-cap, if I remove the draw VBO grid code the triangles display, but as soon as it's included the triangles disappear.

Any ideas, hints for something to try, a direction for me to research or anything like that would be appreciated. Feel free to point out if I'm doing anything unnecessary or stupid too.

Cheers.


Is mVertexBuffer a client-side buffer? If so, then you'll need to unbind your VBO. Try adding

gl11.glBindBuffer(GL11.GL_ARRAY_BUFFER, 0);

after drawing your VBO grid. (forgive me if I didn't get the syntax right, I've only ever done OpenGL in C/C++.) When a VBO is bound, then calls to glVertexPointer() are interpreted as offsets into your VBO. If no VBO is bound, then they are understood as address on client-side memory.

See here: http://www.opengl.org/sdk/docs/man/xhtml/glBindBuffer.xml

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜