2010-06-06 92 views
2

我一直有这个问题一段时间了,而且我还没有得到任何解决方案。这里是这个问题,具体细节: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; 
} 
+0

请添加'header'声明。 – 2010-06-06 22:15:07

回答

2

它看起来确实像你(在LoadUncompressedTGA再一次TGA::loadTGA,然后)消费两次头。

但是,如果是这样的话,我会认为width,heightbpp都将是根本错误的,它不会像它那样正确。

+0

是的。据我所知,标题实际上是18个字节。我认为我的评论实际上是不正确的,但正如你所说的,如果我弄乱了标题,图像就不会像它那样。肯定是一个地方进行调查,所以谢谢你的头。 – Whynne 2010-06-06 23:33:50

+0

看起来你的预感是正确的。我检查了它在读取的距离有多远,我注意到它读取了24个字节,而不是它应该达到的18个字节。加载alpha通道TGA时,这也造成了我的问题。改变它,现在一切正常。 非常感谢。我知道这很简单。 – Whynne 2010-06-07 00:09:37

1

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

它看起来像加载程序打破了 - 显然是“吃”两个像素(假设扫描线从下行到上行)。这不是一个OPENGL问题,它是您的加载例程中的某处的错误

我不熟悉* .tga格式,但您可能想要将您的加载例程与SDL_image库的实现进行比较。据我所知SDL_image正确加载图像。由于你的例程不这样做,这是你的例程中的错误。比较你的日常工作代码,以找到错误。

如果决定将SDL_image代码复制到项目中,请注意授权。

+0

是的,我宁愿写自己的加载程序,正如我前面说过的,我知道这个问题可能是我的加载例程,我似乎无法弄清楚它的部分是什么导致了转变。尽管如此,感谢指针。 – Whynne 2010-06-06 23:32:06