2010-06-06 2 views
6

OpenGL에 조금 익숙하며 텍스처 사용에 문제가 있습니다. 텍스처가 잘로드되는 것 같지만 프로그램을 실행하면 텍스처 디스플레이가 왼쪽으로 몇 픽셀 이동하고 오른쪽에 교대로 표시되는 부분이 잘립니다. 여기서 문제가 TGA 로더에 있는지 또는 쿼드에 텍스처를 적용하는지 알 수 없습니다. 여기 쿼드에 적용하면 OpenGL 텍스처가 약간 왼쪽으로 이동합니다.

는 로더 :

#include "texture.h" 
#include <iostream> 

GLubyte uncompressedheader[12] = {0,0, 2,0,0,0,0,0,0,0,0,0}; 
GLubyte compressedheader[12] = {0,0,10,0,0,0,0,0,0,0,0,0}; 


TGA::TGA() 
{ 

} 

//Private loading function called by LoadTGA. Loads uncompressed TGA files 
//Returns: TRUE on success, FALSE on failure 
bool TGA::LoadCompressedTGA(char *filename,ifstream &texturestream) 
{ 
return false; 
} 

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);   // Bind Our Texture 
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); // Linear Filtered 
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 
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; 
} 

GLubyte* TGA::getImageData() 
{ 
return imagedata; 
} 

GLuint& TGA::getTexID() 
{ 
return texID; 
} 

그리고 여기에 쿼드입니다 : 스크린 샷은 매우 도움이 될 것

void Square::show() 
{  

glEnable(GL_TEXTURE_2D); 
glBindTexture(GL_TEXTURE_2D, texture.texID); 


    //Move to offset 
    glTranslatef(x, y, 0); 

//Start quad 
    glBegin(GL_QUADS); 

//Set color to white 
glColor4f(1.0, 1.0, 1.0, 1.0); 

//Draw square 
glTexCoord2f(0.0f, 0.0f); glVertex3f(0,   0,    0); 
glTexCoord2f(1.0f, 0.0f); glVertex3f(SQUARE_WIDTH, 0,    0); 
glTexCoord2f(1.0f, 1.0f); glVertex3f(SQUARE_WIDTH, SQUARE_HEIGHT, 0); 
glTexCoord2f(0.0f, 1.0f); glVertex3f(0,   SQUARE_HEIGHT, 0); 

    //End quad 
    glEnd(); 

    //Reset 
glLoadIdentity(); 
} 
+0

이 스크린 샷을 추가하시기 바랍니다 – Bahbar

답변

2

.
내 첫 번째 추측은 행이 4 바이트 정렬되지 않은 것입니다. 그렇다면 glTexImage2D()를 호출하기 전에 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);으로 1 바이트의 정렬을 해제하십시오.

3

텍스처 매개 변수를 설정하여 텍스처가 쿼드에 맞게 확장되도록하고 싶습니다. 다음 코드 줄을 사용하면됩니다.

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 

이러한 텍스처에는 4 분의 1에 맞게 선형으로 위 또는 아래로 텍스처가 있습니다.

당신은 아마 또한 다음 통화와 텍스처 클램프해야

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); 
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); 
관련 문제