2011-05-23 149 views
0

我正在使用opengl和紋理映射。 問題是,當應用的紋理是512×32像素,每個東西都工作正常,但是當它是128×128時,紋理不能正確應用。Opengl紋理映射和圖像資源

紋理不能正確重複,它應用到對象的開始處,而其餘的部分沒有任何紋理。

在代碼

:長度參數是約100。

LoadTexture:一個功能讀取.bmp文件並返回質地指數。

這是我正在使用的代碼。

int LoadTexture(char *filename,int alpha) 
{ 
    using namespace std; 
    int i, j=0; //Index variables 
    static int num_texture; 
    ifstream l_file(filename); 
    unsigned char *l_texture; //The pointer to the memory zone in which we will load the texture 

    // windows.h gives us these types to work with the Bitmap files 
    BITMAPFILEHEADER fileheader; 
    BITMAPINFOHEADER infoheader; 
    RGBTRIPLE rgb; 

    num_texture++; // The counter of the current texture is increased 

    if(!l_file) return (-1); // Open the file for reading 

    l_file.read(reinterpret_cast<char *>(&fileheader), sizeof(fileheader)); // Read the fileheader 

    //fseek(l_file, sizeof(fileheader), SEEK_SET); // Jump the fileheader 
    l_file.read(reinterpret_cast<char *>(&infoheader), sizeof(infoheader)); // and read the infoheader 

    // Now we need to allocate the memory for our image (width * height * color deep) 
    l_texture = new byte [infoheader.biWidth * infoheader.biHeight * 4]; 
    // And fill it with zeros 
    memset(l_texture, 0, infoheader.biWidth * infoheader.biHeight * 4); 

    // At this point we can read every pixel of the image 
    for (i=0; i < infoheader.biWidth*infoheader.biHeight; i++) 
    {    
      // We load an RGB value from the file 
      l_file.read(reinterpret_cast<char *>(&rgb), sizeof(rgb)); 

      // And store it 
      l_texture[j+0] = rgb.rgbtRed; // Red component 
      l_texture[j+1] = rgb.rgbtGreen; // Green component 
      l_texture[j+2] = rgb.rgbtBlue; // Blue component 
      l_texture[j+3] = alpha; // Alpha value 
      j += 4; // Go to the next position 
    } 

    l_file.close(); // Closes the file stream 

    glBindTexture(GL_TEXTURE_2D, num_texture); // Bind the ID texture specified by the 2nd parameter 

    // The next commands sets the texture parameters 
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); // If the u,v coordinates overflow the range 0,1 the image is repeated 
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); 
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); // The magnification function ("linear" produces better results) 
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); //The minifying function 

    // Finally we define the 2d texture 
    glTexImage2D(GL_TEXTURE_2D, 0, 4, infoheader.biWidth, infoheader.biHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, l_texture); 

    // And create 2d mipmaps for the minifying function 
    gluBuild2DMipmaps(GL_TEXTURE_2D, 4, infoheader.biWidth, infoheader.biHeight, GL_RGBA, GL_UNSIGNED_BYTE, l_texture); 

    delete [] l_texture; // Free the memory we used to load the texture 

    return num_texture; // Returns the current texture OpenGL ID 
} 

和這裏就是我怎麼做的貼圖座標

void drawStreetStraight(Vector2d & point1,Vector2d & point2,Vector2d & point3,Vector2d & point4,double length) 
{ 
    glEnable(GL_TEXTURE_2D); 
    glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECR); 
    glBindTexture(GL_TEXTURE_2D,streetTexture); 
    glBegin(GL_QUADS); 
    glTexCoord2f(0,0); 
    glVertex3d(point1.x,0,point1.y); 
    glTexCoord2f(1,0); 
    glVertex3d(point2.x,0,point2.y); 
    glTexCoord2f(1,length/2.0); 
    glVertex3d(point3.x,0,point3.y); 
    glTexCoord2f(0,length/2.0); 
    glVertex3d(point4.x,0,point4.y); 
    glEnd(); 
    glDisable(GL_TEXTURE_2D); 
} 
+0

紋理的範圍是從0到1不能/ 2長度添加到它。爲什麼你在glVertex3d中給出z的y座標值? – 2011-05-23 10:24:07

+1

第一個問題:增加長度/ 2重複即:我想重複紋理,所以我這樣做(長度/ 2)。超過1你得到重複效果或鉗位(根據你的代碼)。 第二個問題:我的對象(道路)鋪設在XZ座標上,Y = 0,它配置了2個座標點(x,y)。 – 2011-05-23 10:33:40

+1

爲什麼在glTexEnvf調用中有GL_DECR? – bombardier 2011-05-23 12:40:39

回答

1

我不知道,如果這是您例如修復,但現在看來,您不使用glGenTextures任何地方。

這是有原因嗎?

我預習紋理像這樣:座標

glGenTextures(1, &texPlane); 
glBindTexture(GL_TEXTURE_2D, texPlane); 
glPixelStorei(GL_UNPACK_ALIGNMENT, 1); 
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); 
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_LINEAR); 
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); 
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_GENERATE_MIPMAP, GL_TRUE); 
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 512, 512, 0, GL_RGBA, GL_UNSIGNED_BYTE, texPlanes); 
+0

我想你可能是對的。我正在閱讀glBindTexture旁邊的註釋,所以我匆忙推測他提供了紋理名稱作爲LoadTexture函數的第二個參數。完全錯過了他在其功能中完成的代碼。 – bombardier 2011-05-25 08:53:03