OpenGL纹理生成

发布于 2024-12-11 08:10:10 字数 687 浏览 0 评论 0原文

我目前正在尝试弄清楚如何使用 OpenGL。我想从字节缓冲区创建纹理。

由于某种原因,当我使用 glTexImage2D 执行此操作时,它不起作用(纹理显示为空白):

glTexImage2D(GL_TEXTURE_2D,
             0,
             GL_RGBA,
             texwidth,
             texheight,
             0,
             GL_RGBA,
             GL_UNSIGNED_BYTE,
             bytes
             );

当我使用 gluBuild2DMipmaps 时,它不起作用工作:

gluBuild2DMipmaps(GL_TEXTURE_2D,
                  3,
                  texwidth,
                  texheight,
                  GL_RGBA,
                  GL_UNSIGNED_BYTE,
                  bytes
                  );

两者有什么区别,我做错了什么?

I'm currently trying to figure out how to use OpenGL. I want to create a texture from a byte buffer.

For some reason, when I do it with glTexImage2D, it does not work (the texture comes out blank):

glTexImage2D(GL_TEXTURE_2D,
             0,
             GL_RGBA,
             texwidth,
             texheight,
             0,
             GL_RGBA,
             GL_UNSIGNED_BYTE,
             bytes
             );

When I use gluBuild2DMipmaps it does work:

gluBuild2DMipmaps(GL_TEXTURE_2D,
                  3,
                  texwidth,
                  texheight,
                  GL_RGBA,
                  GL_UNSIGNED_BYTE,
                  bytes
                  );

What is the difference between the two, and what am I doing wrong?

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

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

发布评论

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

评论(1

段念尘 2024-12-18 08:10:10

由于它与 gluBuild2DMipmaps 一起“工作”,而不是与 glTexImage2D 一起“工作”(我猜纹理是黑色的),我猜你有一个基于 mipmap 的纹理纹理过滤器(这是也是默认的)。尝试调用

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);  //or maybe GL_NEAREST

始终记住,glTexImage2D 仅为选定的 mipmap 级别(在您的情况下为级别 0)设置图像,并且如果您使用基于 mipmap 的缩小过滤器(带有 _MIPMAP_

glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE);

> 在其名称中)仅当您向所有 mipmap 级别提供图像(gluBuild2DMipmaps 为您做的事情)或使用之前 上传图像或

glGenerateMipmap(GL_TEXTURE_2D);

上传后更新的图像。我认为前者需要 OpenGL 1.4,后者需要 FBO 支持,这也是生成 mipmap 的非弃用现代方法。这两种方式(尤其是后者)也优于已弃用的 GLU 功能。

As it "works" with gluBuild2DMipmaps and not with glTexImage2D (texture is black, I guess), I would guess you have a mipmap based texture filter for the texture (which is also the default). Try calling

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);  //or maybe GL_NEAREST

Always remember that glTexImage2D only sets an image for the selected mipmap level (in your case level 0) and if you use a mipmap-based minification filter (a constant with _MIPMAP_ in its name) the results of the texturing are only defined if you supply images to all mipmap levels (which gluBuild2DMipmaps does for you) or generate them automatically using either

glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE);

before uplaoding the image or the newer

glGenerateMipmap(GL_TEXTURE_2D);

once it's uplaoded. The former requires OpenGL 1.4 and the latter requires FBO support, I think, and is also the non-deprectaed modern way to generate mipmaps. These two ways (especially the latter) are also prefered to the deprecated GLU functionality.

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