为什么绘制 OpenGL-ES VBO 网格会阻止其他三角形显示?
我对 OpenGl 还很陌生,在显示与普通 3D 对象混合的基于 VBO 的网格时遇到问题。 我可以自己绘制纹理三角形,但是当我包含 VBO 网格时,三角形将停止显示。 在三角形被网格取代之前,我快速闪现了它们 - 大概是因为它们首先被渲染,它们在被禁用之前出现在第一帧中。
我试图禁用我已启用的所有状态(反之亦然)并弹出我已推送的所有矩阵等...
我已展开 OpenGl 调用以简化代码:
// 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.glEnableClientState(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();
所以重述一下,如果我删除了三角形显示的绘制 VBO 网格代码,但一旦包含它,三角形就会消失。
任何想法、尝试的提示、我研究的方向或类似的东西将不胜感激。 如果我做了任何不必要或愚蠢的事情,请随时指出。
干杯。
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.glEnableClientState(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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
mVertexBuffer 是客户端缓冲区吗?如果是这样,那么您需要解除 VBO 的绑定。尝试
在绘制 VBO 网格后添加。 (如果我的语法不正确,请原谅我,我只用 C/C++ 完成过 OpenGL。)当绑定 VBO 时,对 glVertexPointer() 的调用将被解释为 VBO 中的偏移量。如果没有绑定 VBO,那么它们将被理解为客户端内存上的地址。
请参阅此处: http://www.opengl.org/sdk/docs/ man/xhtml/glBindBuffer.xml
Is mVertexBuffer a client-side buffer? If so, then you'll need to unbind your VBO. Try adding
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