简体   繁体   English

OpenGL 2d纹理不起作用

[英]OpenGL 2d texture not working

I'm working on a 2D game project, and I wanted to wrap the openGl texture in a simple class. 我正在研究2D游戏项目,我想将openGl纹理包装在一个简单的类中。 The texture is read from a 128x128px .png (with alpha channel) using libpng . 使用libpng从128x128px .p​​ng(具有alpha通道)读取纹理。 Since the amount of code is pretty large, I'm using pastebin. 由于代码量很大,因此我使用了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. Texture类 :OpenGL纹理的包装器。 The loadData function sets up the texture in gl (this is the function I suspect that doesn't work). loadData函数在gl中设置纹理(我怀疑这是行不通的函数)。

  • OpenGl code : the debugSetTexture function puts a texture in the temp variable which is used in the graphicsDraw() function. OpenGl代码debugSetTexture函数将纹理放入temp变量中,该变量在graphicsDraw()函数中使用。 This is because it is not in the same source file as main() . 这是因为它与main()不在同一个源文件中。 In the graphicsMainLoop() function, I use the Fork() function which in fact calls fork() , and stores the pid of the spawned process. graphicsMainLoop()函数中,我使用了Fork()函数,该函数实际上调用fork() ,并存储生成的进程的pid

From main() , this is what I do: main() ,这是我的工作:

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. 我尝试了一个名为gDEBugger的应用程序,并且在纹理查看器中生成了一个纹理,但是大小为0x0px。

I suspect that the problem happens when the texture is loaded using Texture::loadTexture(). 我怀疑使用Texture :: loadTexture()加载纹理时会发生问题。

You need to check GL error codes after GL calls. GL调用后,您需要检查GL错误代码。

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? 假设它成功加载了png文件,假设您的程序在glBindTexture中失败了? (strong hint) (强烈提示)

You did call your Error function for your file handling, but does your program halt then or chug on? 您确实调用了Error函数来处理文件,但是程序随后会暂停还是继续运行?

Here's a serious issue: Texture PngReader::generateTexture() returns Texture by value. 这是一个严重的问题:Texture PngReader :: generateTexture()按值返回Texture。 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. 这将导致您的Texture对象在返回时被复制(句柄和全部),然后调用〜Texture(),从而破坏了基于堆栈的副本。 So your program will call glDeleteTextures a couple times! 因此,您的程序将多次调用glDeleteTextures!

If you want to return it by value, you could wrap it in a shared_ptr<> which does reference counting. 如果要按值返回,可以将其包装在shared_ptr <>中,该引用进行引用计数。 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. 使用TexturePtr作为返回类型。 Initialize it in generateTexture() like this: 像这样在generateTexture()中初始化它:

TexturePtr t(new Texture);

then change all the method access to go through -> instead of . 然后将所有方法访问权限更改为->而不是。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM