我已经为此困扰了一段时间,而且还没有任何可行的解决方案。这是问题所在,以及具体细节:

我正在将256x256未压缩的TGA加载到一个简单的OpenGL程序中,该程序在屏幕上绘制一个四边形,但是当它显示时,它向左移动了大约两个像素,裁剪部分出现在右侧。最长的一段时间以来,这一直困扰着我,人们提出了钳位之类的建议,但是以某种方式,我认为我的问题可能确实很简单,但我只是不知道这是什么!

为了清晰起见,下面的屏幕截图比较了TGA(左)和它在程序中的运行方式(右)。另请注意,右上角有一个很小的黑色像素,我希望这与相同的问题有关。

alt text http://img64.imageshack.us/img64/2686/helpmed.png

这是加载程序的代码,我确信问题出在加载纹理的方式上。

在此先感谢可以解决我的问题的任何人。

bool TGA::LoadUncompressedTGA(char *filename,ifstream &texturestream)
{
 cout << "G position status:" << texturestream.tellg() << endl;
 texturestream.read((char*)header, sizeof(header));     //read 6 bytes into the file to get the tga header
 width  = (GLuint)header[1] * 256 + (GLuint)header[0];    //read and calculate width and save
 height = (GLuint)header[3] * 256 + (GLuint)header[2];    //read and calculate height and save
 bpp    = (GLuint)header[4];           //read bpp and save

 cout << bpp << endl;

 if((width <= 0) || (height <= 0) || ((bpp != 24) && (bpp !=32))) //check to make sure the height, width, and bpp are valid
 {
  return false;
 }
 if(bpp == 24)
 {
  type = GL_RGB;
 }
 else
 {
  type = GL_RGBA;
 }
 imagesize = ((bpp/8) * width * height);          //determine size in bytes of the image
 cout << imagesize << endl;
 imagedata = new GLubyte[imagesize];           //allocate memory for our imagedata variable

 texturestream.read((char*)imagedata,imagesize);        //read according the the size of the image and save into imagedata

 for(GLuint cswap = 0; cswap < (GLuint)imagesize; cswap += (bpp/8))         //loop through and reverse the tga's BGR format to RGB
 {
  imagedata[cswap] ^= imagedata[cswap+2] ^=                    //1st Byte XOR 3rd Byte XOR 1st Byte XOR 3rd Byte
     imagedata[cswap] ^= imagedata[cswap+2];
 }

 texturestream.close();              //close ifstream because we're done with it
 cout << "image loaded" << endl;

 glGenTextures(1, &texID);             // Generate OpenGL texture IDs
 glBindTexture(GL_TEXTURE_2D, texID);

    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);

 glTexImage2D(GL_TEXTURE_2D, 0, type, width, height, 0, type, GL_UNSIGNED_BYTE, imagedata);



    delete imagedata;
 return true;
}

//Public loading function for TGA images.  Opens TGA file and determines
//its type, if any, then loads it and calls the appropriate function.
//Returns: TRUE on success, FALSE on failure

bool TGA::loadTGA(char *filename)
{
 cout << width << endl;
 ifstream texturestream;
 texturestream.open(filename,ios::binary);
 texturestream.read((char*)header,sizeof(header));     //read 6 bytes into the file, its the header.                //if it matches the uncompressed header's first 6 bytes, load it as uncompressed
 LoadUncompressedTGA(filename,texturestream);
 return true;
}

最佳答案

看起来您好像两次使用了 header (一次在TGA::loadTGA中使用,然后再次在LoadUncompressedTGA中使用)。

但是,如果是这种情况,那么我认为widthheightbpp都将完全是错误的,并且看起来不像它那样正确。

09-08 04:46