c++ - OpenGL - 纹理加载不正确

标签 c++ opengl textures

更新:

我已经在下面发布了渲染器代码,因为这里的代码似乎不是问题所在。

我的一些代码出现问题,当我尝试将多个纹理一次上传一个到 openGL 时,它失败得有些惊人,渲染器最终只使用一个纹理。我已经进行了一些调试以将错误跟踪到此函数,但我无法确定函数的哪一部分有问题。是否存在我根本没有看到的特别明显的错误,或者我的代码中是否存在更细微的缺陷?

这是我用来存储纹理信息的结构,通常只是跟踪我所有的指针

typedef struct {
  float Width;
  float Height;
} texInfo;

typedef struct {
  dshlib::utfstr ResourceName;
  texInfo * TextureInfo;
  GLuint TextureNum;
  SDL_Surface * Image;
} texCacheItem;

这是当前的 WIP 图形加载器。基本上,它使用预先编写的库从 .zip 存档中加载一个命名的 .png 文件(顺便说一句,它正在用这个程序进行测试)。然后它用 libpng 加载,然后作为纹理加载,缓存以加快加载速度并避免多次加载单个纹理。我省略了 #include 语句,因为它们很笨拙。

texCacheItem * loadGraphics(dshlib::utfstr FileName) {

  for(int i = 0; i < NumTexCached; i++) { //First see if this texture has already been loaded
    if(TextureCache[i]->ResourceName == FileName)
      return TextureCache[i];
  }

  dshlib::utfstr FullFileName = "Data/Graphics/"; //If not, create the full file path in the archive
  FullFileName += FileName;
  dshlib::FilePtr file = resourceCtr.OpenFile(FullFileName); //And open the file

  if (!file->IsOk()) { //If the file failed to load...
    EngineState = ENGINESTATE_ERR;
    return NULL;
  }

  SDL_Surface * T = loadPNG(file);
  texCacheItem * Texture = new texCacheItem;
  Texture->TextureInfo = new texInfo;

  glGenTextures(1, &Texture->TextureNum); //Allocate one more texture and save the name to the texCacheItem
  glBindTexture(GL_TEXTURE_2D, Texture->TextureNum); //Then create it
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, T->w, T->h, 0, GL_RGBA, GL_UNSIGNED_BYTE, T->pixels);

  Texture->TextureInfo->Width = (float)T->w; //Write the useful data
  Texture->TextureInfo->Height = (float)T->h;
  Texture->ResourceName = FileName; //And the caching info needed
  Texture->Image = T; //And save the image for if it's needed later and for deleting

  if (!TexCacheSize) { //If this is the first load this is 0, so allocate the first 8 Cache slots.
    TexCacheSize = 8;
    TextureCache = new texCacheItem*[8];
  }

  if(NumTexCached == TexCacheSize) { //If we're out of cache space
    if (TexCacheSize == 32768) { //If too many cache items, error out
      EngineState = ENGINESTATE_ERR;
      return NULL;
    }
    TexCacheSize <<= 1; //Double cache size
    texCacheItem ** NewSet = new texCacheItem*[TexCacheSize];
    memcpy(NewSet, TextureCache, NumTexCached * sizeof(texCacheItem*)); //And copy over the old cache
    delete TextureCache; //Delete the old cache
    TextureCache = NewSet; //And assign the pointer to the new one
  }
  TextureCache[NumTexCached++] = Texture; //Store the texCacheItem to the Cache

  file->Close(); //Close the file
  file = NULL;   //And NULL the smart pointer. [NTS: Confirm with Disch this is what won't cause a memory leak]

  return Texture; //And return the loaded texture in texCacheItem form.
}

SDL_Surface *loadPNG(dshlib::FilePtr File)
{
    Uint8 *PNGFile = new Uint8[(long)File->GetSize()];
    File->GetAr<Uint8>(PNGFile, (long)File->GetSize());
    return IMG_LoadPNG_RW(SDL_RWFromMem(PNGFile, (long)File->GetSize()));
}

这是渲染器代码文件。目前比较乱,请见谅。 level->activeMap 基本上告诉渲染器在 tilemap 的哪个“层”(0 是前面,3 是后面)绘制上面的 Sprite 。

#include "../MegaJul.h"
void render(void) {

  //Render the current tilemap to the screen

glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslatef(0.0f, 0.0f, -4.0f);

if (level) {

glBegin(GL_QUADS);
float dT = 32.0f / level->dTex;
float sX, fX, fXa, sY, tX, tY, sYa, sYb, sXa, tXa, tYa;
unsigned long m = level->mapDimensions[0] * level->mapDimensions[1];
float ai; long long t; Sint16 * p;
glBindTexture(GL_TEXTURE_2D, level->tilemap->TextureNum);

for (int i = 3; i >= 0; i--) {

  if (level->layers[i]->mapPosition[0] > 0)
    level->layers[i]->mapPosition[0] = 0;
  if (level->layers[i]->mapPosition[0] < 0 - (signed long)((level->mapDimensions[0] - 21) * 32))
    level->layers[i]->mapPosition[0] = 0 - (signed long)((level->mapDimensions[0] - 21) * 32);

  if (level->layers[i]->mapPosition[1] < 0)
    level->layers[i]->mapPosition[1] = 0;
  if (level->layers[i]->mapPosition[1] > (signed long)((level->mapDimensions[1] - 16) * 32))
    level->layers[i]->mapPosition[1] = (signed long)((level->mapDimensions[1] - 16) * 32);

  if (i == level->activeMap) {
    for (int j = 0; j < NumSprites; j++) {
      glBindTexture(GL_TEXTURE_2D, Sprites[j]->Graphics->TextureNum);
      Sprites[j]->render(level->layers[i]->mapPosition[0], level->layers[i]->mapPosition[1]);
    }
    for (int j = 0; j < NumBullets; j++) {
      glBindTexture(GL_TEXTURE_2D, Bullets[j]->Texture->TextureNum);
      Bullets[j]->render(level->layers[i]->mapPosition[0], level->layers[i]->mapPosition[1]);
    }
  }

  glBindTexture(GL_TEXTURE_2D, level->tilemap->TextureNum);

  t = 0 - ((level->layers[i]->mapPosition[0] - (level->layers[i]->mapPosition[0] % 32)) /32) + (((level->layers[i]->mapPosition[1] - (level->layers[i]->mapPosition[1] % 32)) /32) * level->mapDimensions[0]);
  ai = (float)(3 - i); //Invert Z-Index
  sX = (float)((level->layers[i]->mapPosition[0] % 32));
  sY = (float)((level->layers[i]->mapPosition[1] % 32));
  if (sX > 0) 
      sX -= 32;
  if (sY < 0)
      sY += 32;
  fX = sX /= 32.0f;
  sY /= 32.0f;
  fXa = sXa = sX + 1.0f;
  sYa = sY + 14.0f;
  sYb = sY + 15.0f;

  for (int y = 0; y < 16; y++) {
    for (int x = 0; x < 21; x++) {
      p = level->tiles[level->layers[i]->map[t]]->position;
      tX = p[0] / level->dTex;
      tY = p[1] / level->dTex;
      tXa = tX + dT;
      tYa = tY + dT;
      glTexCoord2f(tX, tYa);     glVertex3f(fX, sYa, ai);   // Bottom Left Of The Texture and Quad
      glTexCoord2f(tXa,tYa);     glVertex3f(fXa, sYa, ai);  // Bottom Right Of The Texture and Quad
      glTexCoord2f(tXa,tY);      glVertex3f(fXa, sYb, ai);  // Top Right Of The Texture and Quad
          glTexCoord2f(tX, tY);      glVertex3f(fX, sYb, ai);     // Top Left Of The Texture and Quad
          fX += 1.0f;
          fXa += 1.0f;
          t++;
          if (t >= m) break;
        }
        sYb -= 1.0f; sYa -= 1.0f;
        fXa = sXa; fX = sX;
        t += level->mapDimensions[0] - 21; //21 is the number of tiles drawn on a line (20 visible + 1 extra for scrolling)
      }

    }
    glEnd();
  }

SDL_GL_SwapBuffers();
}

下面是为 Sprite 和关卡设置瓦片 map 数据的代码段:

等级:

void loadLevel(dshlib::utfstr FileName) {
-snip-
  texCacheItem * Tex = loadGraphics(FileName);

  if (!Tex) { //Load the tile graphics for the level
    unloadLevel();
    EngineState = ENGINESTATE_ERR;
    return;
  } else {
    level->dTex = Tex->TextureInfo->Width;
    level->tilemap = Tex;
  }
-snip-
}

Sprite :

void SpriteBase::created() {
  this->Graphics = loadGraphics(DefaultGFX());
-snip-
}

更新 2:

Sid Farkus 指出了我在渲染器上犯的一个大错误,所以这是一个更新的 renderer.cpp:

#include "../MegaJul.h"
void render(void) {

  //Render the current tilemap to the screen

  glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    glLoadIdentity();
  glTranslatef(0.0f, 0.0f, -4.0f);

  if (level) {

    float dT = 32.0f / level->dTex;
    float sX, fX, fXa, sY, tX, tY, sYa, sYb, sXa, tXa, tYa;
    unsigned long m = level->mapDimensions[0] * level->mapDimensions[1];
    float ai; long long t; Sint16 * p;

    for (int i = 3; i >= 0; i--) {

      if (level->layers[i]->mapPosition[0] > 0)
        level->layers[i]->mapPosition[0] = 0;
      if (level->layers[i]->mapPosition[0] < 0 - (signed long)((level->mapDimensions[0] - 21) * 32))
        level->layers[i]->mapPosition[0] = 0 - (signed long)((level->mapDimensions[0] - 21) * 32);

      if (level->layers[i]->mapPosition[1] < 0)    
        level->layers[i]->mapPosition[1] = 0;
      if (level->layers[i]->mapPosition[1] > (signed long)((level->mapDimensions[1] - 16) * 32))
        level->layers[i]->mapPosition[1] = (signed long)((level->mapDimensions[1] - 16) * 32);

      if (i == level->activeMap) {
        for (int j = 0; j < NumSprites; j++) {
          glBindTexture(GL_TEXTURE_2D, Sprites[j]->Graphics->TextureNum);
          glBegin(GL_QUADS);
          Sprites[j]->render(level->layers[i]->mapPosition[0], level->layers[i]->mapPosition[1]);
          glEnd();
        }
        for (int j = 0; j < NumBullets; j++) {
          glBindTexture(GL_TEXTURE_2D, Bullets[j]->Texture->TextureNum);
          glBegin(GL_QUADS);
          Bullets[j]->render(level->layers[i]->mapPosition[0], level->layers[i]->mapPosition[1]);
          glEnd();
        }
      }

      glBindTexture(GL_TEXTURE_2D, level->tilemap->TextureNum);
      glBegin(GL_QUADS);

  -snipped out renderer since it was bloat

    glEnd();
  }

  SDL_GL_SwapBuffers();
}

最佳答案

根据您的渲染代码,我可以看到您在 glBegin/End block 中调用 BindTexture。来自 opengl 文档:

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

将您的 BindTexture 调用移到 glBegin()/glEnd() block 之外,您应该很高兴。您可能需要多个 block 来适应您的渲染风格。

编辑:

使用更新后的代码,确保几件事;您的 Sprite 位置在当前投影/模型 View 矩阵的屏幕上可见,并且您的 Sprite 纹理 ID 是有效纹理。现在没有什么技术上的问题让我跳出来,但你的值(value)观可能不对。

关于c++ - OpenGL - 纹理加载不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1626081/

相关文章:

Directx顶点渲染: unable to get texture to display correctly for trapetzoids

c++ - VC++中数组到指针的转换

c++ - 将类声明为类的一部分的问题

c++ - 在 C++17 中将 lambda 作为方法参数传递的最简单方法

Java 和 OpenGL;使用 delta 计算帧独立运动时的渲染伪影

c++ - 从顶点到片段着色器的平滑64位输入- “error C7570: 64 bit input should be flat”

c++ - 提高OpenGL渲染性能: rendering multiple times from different POVs

c++ - 从文件路径c++获取目录

opengl - 文本未正确呈现 - 使用 FreeType2 的 OpenGL

c - OpenGL 纹理初始化/渲染问题