簡體   English   中英

當應用於四邊形時,OpenGL紋理稍微向左移動

[英]OpenGL texture shifted somewhat to the left when applied to a quad

我對OpenGL有點新意,我在使用紋理方面遇到了問題。 紋理似乎加載正常,但是當我運行程序時,紋理顯示向左移動幾個像素,該部分被右側出現的移位切斷。 我不知道這里的問題是在我的TGA加載器中還是我將紋理應用於四邊形的方式。

這是裝載機:

#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();
}

屏幕截圖非常有用。
我的第一個猜測是你的行不是4字節對齊的。 如果是這樣,請使用glPixelStorei(GL_UNPACK_ALIGNMENT, 1);將解包對齊更改為1個字節glPixelStorei(GL_UNPACK_ALIGNMENT, 1); 在調用glTexImage2D()之前。

您想要設置紋理參數,以便紋理縮放以適合四邊形。 您可以使用以下代碼行執行此操作:

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

這些將具有紋理線性向上或向下縮放以適合四邊形。

您可能還應該使用以下調用來夾住紋理:

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM