我使用 glBindTexture 有什么问题吗?

发布于 2024-12-13 23:35:56 字数 948 浏览 0 评论 0原文

我正在渲染一个棋盘,使用两种不同的纹理。一种用于黑色方块,一种用于白色方块。然而,并不是每个不同的方块都有自己的纹理,而是它们都采用我调用 glBindTexture(GL_TEXTURE_2D, id); 绑定的最后一个纹理。

这是我的方法:

glEnable(GL_TEXTURE_2D);    
glBegin(GL_QUADS);
    // square 0, 0 ( front left )
    glBindTexture(GL_TEXTURE_2D, textureBlackSquare->texID);
    glNormal3f(0, 1, 0);
    glTexCoord2f(0, 0); glVertex3f(-8.0, 0.5,  8.0);
    glTexCoord2f(1, 0); glVertex3f(-6.0, 0.5,  8.0);
    glTexCoord2f(1, 1); glVertex3f(-6.0, 0.5,  6.0);
    glTexCoord2f(0, 1); glVertex3f(-8.0, 0.5,  6.0);
glEnd();

glBegin(GL_QUADS);
    // square 1, 0        
    glBindTexture(GL_TEXTURE_2D, textureWhiteSquare->texID);
    glTexCoord2f(0, 0); glVertex3f(-6.0, 0.5,  8.0);
    glTexCoord2f(1, 0); glVertex3f(-4.0, 0.5,  8.0);
    glTexCoord2f(1, 1); glVertex3f(-4.0, 0.5,  6.0);
    glTexCoord2f(0, 1); glVertex3f(-6.0, 0.5,  6.0);
glEnd();

当我运行此代码时,两个四边形都有白色纹理绑定。如何让每个四边形都有自己的纹理?

I am rendering a chess board, using 2 different textures. One for the black squares and one for the white squares. However instead of each different square having their own texture, they all take on the last texture that I bound calling glBindTexture(GL_TEXTURE_2D, id);.

This is my approach:

glEnable(GL_TEXTURE_2D);    
glBegin(GL_QUADS);
    // square 0, 0 ( front left )
    glBindTexture(GL_TEXTURE_2D, textureBlackSquare->texID);
    glNormal3f(0, 1, 0);
    glTexCoord2f(0, 0); glVertex3f(-8.0, 0.5,  8.0);
    glTexCoord2f(1, 0); glVertex3f(-6.0, 0.5,  8.0);
    glTexCoord2f(1, 1); glVertex3f(-6.0, 0.5,  6.0);
    glTexCoord2f(0, 1); glVertex3f(-8.0, 0.5,  6.0);
glEnd();

glBegin(GL_QUADS);
    // square 1, 0        
    glBindTexture(GL_TEXTURE_2D, textureWhiteSquare->texID);
    glTexCoord2f(0, 0); glVertex3f(-6.0, 0.5,  8.0);
    glTexCoord2f(1, 0); glVertex3f(-4.0, 0.5,  8.0);
    glTexCoord2f(1, 1); glVertex3f(-4.0, 0.5,  6.0);
    glTexCoord2f(0, 1); glVertex3f(-6.0, 0.5,  6.0);
glEnd();

When I run this code, both quads have the white texture bound. How do I get each quad to have its own texture?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

归属感 2024-12-20 23:35:56

您不能在 glBegin/End 中间调用 glBindTexture。您只能在 begin/end 内调用顶点函数。

另外,为什么不只制作一个纹理作为 8x8 棋盘,然后渲染一个四边形来绘制整个棋盘呢?

You cannot call glBindTexture in the middle of glBegin/End. You can only call vertex functions within begin/end.

Also, why don't you just make a single texture as an 8x8 checkerboard, and then just render a single quad to draw the whole checkerboard?

秋叶绚丽 2024-12-20 23:35:56

来自文档

如果在以下时间执行 glBindTexture,则会生成

GL_INVALID_OPERATION
glBegin 的执行和 glEnd 的相应执行。

您忘记检查错误,因此错过了您的程序无效的情况。

From the documentation:

GL_INVALID_OPERATION is generated if glBindTexture is executed between
the execution of glBegin and the corresponding execution of glEnd.

You forgot to check for errors, and thus missed that your program is invalid.

无边思念无边月 2024-12-20 23:35:56

您无法在 glBegin-glEnd 块内绑定纹理。此外,您还应该尽可能避免切换纹理,因为切换纹理是您可以要求 GPU 执行的最昂贵的操作之一(纹理切换会使所有纹素获取缓存失效)。

相反,您可以根据场景对象使用的纹理对它们进行排序,并按此对它们进行分组。因此,您首先使用第一个纹理(例如白色)渲染所有棋盘四边形,然后使用第二个纹理(然后是黑色)渲染所有四边形。

You can't bind a texture within a glBegin-glEnd block. Also you should avoid switching textures where possible, since switching the texture is among the most expensive things you can ask the GPU to do (a texture switch invalidates all texel fetch caches).

Instead you sort your scene objects by the texture they use and group them by this. So you first render all checkerboard quads using the first texture (say white), and after that all the quads using the second texture (black then).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文