【问题标题】:OpenGl displaying white texture (not the normal stuff)OpenGL显示白色纹理(不是正常的东西)
【发布时间】:2013-01-10 19:38:43
【问题描述】:

我觉得很愚蠢...发布后我几乎立即解决了它...不知何故我设法将内存地址作为纹理的值。我决定停止发送它作为参考,而不是静态值(任何 cmets?)。

我将它作为参考寄回的唯一地方是在我将它从 SDL 转换为纹理之后,也许这里也不需要它。任何想法都会很棒!

如果其他人遇到和我一样的问题,我会把我原来的帖子留在这个下面。


*已解决*

所以这是一个虽然可以破解的问题,因为它在我绑定纹理之前一直有效。我会解决的。

首先我使用 SDL 加载图像并将其转换为纹理,转换代码与此处完全相同:http://content.gpwiki.org/index.php/SDL:Tutorials:Using_SDL_with_OpenGL(链接,所以我不会输入太多)。初始化代码也是一样的。

加载纹理后,它会像这样保存到地图中:

功能:

GLuint& Loader::loadTex(const string name)

代码:

//Check if the text is already loaded, if it is return it
map<string, GLuint&>::const_iterator it = m_loadedTextures.find(name);
if(it != m_loadedTextures.end())
{
    return it->second;
}

SDL_Surface* surface = NULL;

//Load the file
string loadPath =  m_prePath + name;
surface = lEntity.loadImage(loadPath.c_str(), true);

if(surface == NULL)
{
    //load default texture?
}

//convert the SDL_Surface to a texture
GLuint newTexture = lEntity.glEntity.GetTextureFromSurface(surface);
GLenum h = glGetError();


//free the surface
SDL_FreeSurface(surface);

//add to map
m_loadedTextures.insert(pair<string, GLuint&>(name, newTexture));

return newTexture;

所以在我收到纹理后,我尝试像这样绘制它:

功能:

bool OpenGlEntity::renderOpenGl(int x, int y, int w, int h, GLuint& image, SDL_Rect& clip)

代码:

GLuint texture = image;

// Bind the texture to which subsequent calls refer to
glBindTexture( GL_TEXTURE_2D, texture );

glBegin( GL_QUADS );
    //Bottom-left vertex (corner)
    glTexCoord2i( 0, 0 );
    glVertex3f( 100.f, 100.f, 0.0f );

    //Bottom-right vertex (corner)
    glTexCoord2i( 1, 0 );
    glVertex3f( 228.f, 100.f, 0.f );

    //Top-right vertex (corner)
    glTexCoord2i( 1, 1 );
    glVertex3f( 228.f, 228.f, 0.f );

    //Top-left vertex (corner)
    glTexCoord2i( 0, 1 );
    glVertex3f( 100.f, 228.f, 0.f );
glEnd();

这里的问题是,如果我在绘制纹理时不绑定纹理,它将显示最后绑定的纹理(绑定在我链接的转换代码中)。但是,如果我绑定纹理,我将改为显示白色 BG。

我意识到绑定的纹理可能以某种方式损坏,但是如何!?我似乎无法追踪到这个问题,我从昨天开始就在使用它。

我非常感谢帮助。感谢您的宝贵时间。

【问题讨论】:

  • 我的 SDL 生锈了,但是如果不释放表面会怎样?你得到同样的结果吗?
  • 我刚刚编辑了我的帖子,感谢您花时间尝试了解我的问题。
  • 谢谢,我不知道我能做到这一点!现在就做!编辑:我必须等 2 天才能做到这一点:/

标签: opengl textures sdl render


【解决方案1】:

我觉得很愚蠢...发布后我几乎立即解决了它...不知何故我设法将内存地址作为纹理的值。我决定停止发送它作为参考,而不是静态值(任何 cmets ?)。

我将它作为参考寄回的唯一地方是在我将它从 SDL 转换为纹理之后,也许这里也不需要它。任何想法都会很棒!

如果其他人遇到和我一样的问题,我会留下我原来的帖子。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多