OpenGL 2d 纹理不起作用

发布于 2025-01-08 07:31:50 字数 1307 浏览 1 评论 0原文

我正在开发一个 2D 游戏项目,我想将 openGl 纹理包装在一个简单的类中。使用 libpng 从 128x128px .p​​ng(带有 Alpha 通道)读取纹理。由于代码量相当大,所以我使用pastebin。

代码文件:

为了避免浪费您的时间,我将稍微解释一下代码位:

  • 纹理类:OpenGL 纹理的包装器。 loadData 函数在 gl 中设置纹理(我怀疑这是不起作用的函数)。

  • OpenGl 代码debugSetTexture 函数将纹理放入 temp 变量中,该变量在 graphicsDraw() 中使用代码>函数。这是因为它与 main() 不在同一源文件中。在graphicsMainLoop()函数中,我使用Fork()函数,它实际上调用fork(),并存储pid生成进程的

main() 中,这就是我所做的:

Strategy::IO::PngReader reader ("/cygdrive/c/Users/Tibi/Desktop/128x128.png");
reader.read();
grahpicsInit2D(&argc, argv);
debugSetTexture(reader.generateTexture());
graphicsMainLoop();
reader.close();

我尝试了一个名为 gDEBugger 的应用程序,在纹理查看器中,生成了一个纹理,但大小为 0x0px。

我怀疑当使用Texture::loadTexture()加载纹理时会出现问题。

I'm working on a 2D game project, and I wanted to wrap the openGl texture in a simple class. The texture is read from a 128x128px .png (with alpha channel) using libpng. Since the amount of code is pretty large, I'm using pastebin.

The code files:

To avoid wasting your time, I will explain the code a little bit:

  • Texture class: a wrapper for an OpenGL texture. The loadData function sets up the texture in gl (this is the function I suspect that doesn't work).

  • OpenGl code: the debugSetTexture function puts a texture in the temp variable which is used in the graphicsDraw() function. This is because it is not in the same source file as main(). In the graphicsMainLoop() function, I use the Fork() function which in fact calls fork(), and stores the pid of the spawned process.

From main(), this is what I do:

Strategy::IO::PngReader reader ("/cygdrive/c/Users/Tibi/Desktop/128x128.png");
reader.read();
grahpicsInit2D(&argc, argv);
debugSetTexture(reader.generateTexture());
graphicsMainLoop();
reader.close();

I tried an application called gDEBugger, and in the texture viewer, there was a texture generated, but size was 0x0px.

I suspect that the problem happens when the texture is loaded using Texture::loadTexture().

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

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

发布评论

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

评论(1

任谁 2025-01-15 07:31:50

您需要在 GL 调用后检查 GL 错误代码。

例如,将此方法添加到您的类中:

GLuint Texture::checkError(const char *context)
{
  GLuint err = glGetError();
  if (err > 0 )  { 
    std::cout << "0x" << std::hex << err << " glGetError() in " << context 
    << std::endl;
  }
  return err;
}

然后像这样调用它:

glBindTexture(GL_TEXTURE_2D, handle);
checkError("glBindTexture");

假设它成功加载 png 文件,假设您的程序在 glBindTexture 中失败? (强烈提示)

您确实调用了 Error 函数来进行文件处理,但是您的程序是否会停止或继续运行?

这是一个严重的问题:Texture PngReader::generateTexture() 按值返回纹理。这将导致您的纹理对象在返回时被复制(句柄和所有),然后调用 ~Texture() ,从而破坏基于堆栈的副本。所以你的程序将调用 glDeleteTextures 几次!

如果你想按值返回它,你可以将它包装在一个shared_ptr<>中。它进行引用计数。这将导致析构函数仅被调用一次:

#include <tr1/memory>
typedef std::tr1::shared_ptr<Texture> TexturePtr;

使用TexturePtr作为返回类型。在generateTexture()中像这样初始化它:

TexturePtr t(new Texture);

然后将所有方法访问更改为经过->;而不是 .

You need to check GL error codes after GL calls.

For example add this method to your class:

GLuint Texture::checkError(const char *context)
{
  GLuint err = glGetError();
  if (err > 0 )  { 
    std::cout << "0x" << std::hex << err << " glGetError() in " << context 
    << std::endl;
  }
  return err;
}

then call it like so:

glBindTexture(GL_TEXTURE_2D, handle);
checkError("glBindTexture");

Assuming it succeeds in loading the png file, suppose your program fails in glBindTexture? (strong hint)

You did call your Error function for your file handling, but does your program halt then or chug on?

Here's a serious issue: Texture PngReader::generateTexture() returns Texture by value. This will cause your Texture object to be copied on return (handle and all) and then ~Texture() to be called, destroying the stack-based copy. So your program will call glDeleteTextures a couple times!

If you want to return it by value, you could wrap it in a shared_ptr<> which does reference counting. This would cause the destructor to be called only once:

#include <tr1/memory>
typedef std::tr1::shared_ptr<Texture> TexturePtr;

Use TexturePtr as your return type. Initialize it in generateTexture() like this:

TexturePtr t(new Texture);

then change all the method access to go through -> instead of .

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