我一直有这个问题一段时间了,而且我还没有得到任何解决方案。这里是这个问题,具体细节:OpenGL纹理在四角形上没有对齐
我将一个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'声明。 – 2010-06-06 22:15:07