2012-07-31 62 views
0

我使用opengl渲染場景,使用一些示例代碼和FreeImage API加載紋理。Open GL紋理,紋理座標對於呈現的內容不正確

這是我所看到的

鏈接

[圖像刪除]

我可以確認,所有紋理座標0.0F和1.0F之間提供glTexCoord2f與頂點座標一起要求。

每個渲染的三角形似乎都粘貼了整個紋理(並重復)而不是由座標指定的紋理區域。

該紋理是1024x1024。

這是載入紋理

bool loadImageToTexture(const char* image_path, unsigned int &handle) 
{ 
    if(!image_path) 
     return false; 

    FREE_IMAGE_FORMAT fif = FIF_UNKNOWN; 
    FIBITMAP* dib(0); 
    BYTE* bits(0); 
    unsigned int width(0), height(0); 

    //open the file 
    fif = FreeImage_GetFileType(image_path, 0); 
    if(fif == FIF_UNKNOWN) 
     fif = FreeImage_GetFIFFromFilename(image_path); 
    if(fif == FIF_UNKNOWN) 
     return false; 
    if(FreeImage_FIFSupportsReading(fif)) 
     dib = FreeImage_Load(fif, image_path); 
    if(!dib) 
     return false; 

    //is the file of the correct type 
    FREE_IMAGE_COLOR_TYPE type = FreeImage_GetColorType(dib); 
    if(FIC_RGBALPHA != type) 
    { 
     //convert to type 
     FIBITMAP* ndib = FreeImage_ConvertTo32Bits(dib); 
     dib = ndib; 
    } 

    //get data for glTexImage2D 
    bits = FreeImage_GetBits(dib); 
    width = FreeImage_GetWidth(dib); 
    height = FreeImage_GetHeight(dib); 
    if((bits == 0) || (width == 0) || (height == 0)) 
     return false; 

    //create the texture in open gl 
    glGenTextures(1, &handle); 
    glBindTexture(GL_TEXTURE_2D, handle); 
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); 
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); 
    glPixelStorei(GL_TEXTURE_2D, 4); 
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 
       0, GL_RGBA, GL_UNSIGNED_BYTE, bits); 

    //unload the image now loaded 
    FreeImage_Unload(dib); 

    return true; 
} 

渲染

inline void glTexture(float textureSize, float t, float u) 
{ 
    glTexCoord2f(u/textureSize, (textureSize - t)/textureSize); 
} 
void processTriangle(const btVector3* triangle, const textureCoord* tc, const btVector3& normal, 
        const int partId, const int triangleIndex, const bool wireframe) 
{ 
    if(wireframe) 
    { 
     glBegin(GL_LINES); 
     glColor3f(1, 0, 0); 
     glVertex3d(triangle[0].getX(), triangle[0].getY(), triangle[0].getZ()); 
     glVertex3d(triangle[1].getX(), triangle[1].getY(), triangle[1].getZ()); 
     glColor3f(0, 1, 0); 
     glVertex3d(triangle[2].getX(), triangle[2].getY(), triangle[2].getZ()); 
     glVertex3d(triangle[1].getX(), triangle[1].getY(), triangle[1].getZ()); 
     glColor3f(0, 0, 1); 
     glVertex3d(triangle[2].getX(), triangle[2].getY(), triangle[2].getZ()); 
     glVertex3d(triangle[0].getX(), triangle[0].getY(), triangle[0].getZ()); 
     glEnd(); 
    } 
    else 
    { 
     glBegin(GL_TRIANGLES); 

     glColor3f(1, 1, 1); 
     //Normals are per triangle 
     glNormal3f(normal.getX(), normal.getY(), normal.getZ()); 
     glTexture(1024.0f, tc[0].t, tc[0].u); 
     glVertex3d(triangle[0].getX(), triangle[0].getY(), triangle[0].getZ()); 
     glTexture(1024.0f, tc[1].t, tc[1].u); 
     glVertex3d(triangle[1].getX(), triangle[1].getY(), triangle[1].getZ()); 
     glTexture(1024.0f, tc[2].t, tc[2].u); 
     glVertex3d(triangle[2].getX(), triangle[2].getY(), triangle[2].getZ()); 

     glEnd(); 
    } 
} 
void processAllTriangles(const btVector3& worldBoundsMin, const btVector3& worldBoundsMax) 
{ 
    btVector3 triangle[3]; 
    textureCoord tc[3]; 

    //go through the index list build triangles and draw them 
    unsigned int k = 0; 
    for(unsigned int i = 0; i<dVertices.size(); i+=3, k++) 
    { 
     //first vertex 
     triangle[0] = dVertices[i]; 
     tc[0] = dTexCoords[i]; 

     //second vertex 
     triangle[1] = dVertices[i+1]; 
     tc[1] = dTexCoords[i+1]; 

     //third vertex 
     triangle[2] = dVertices[i+2]; 
     tc[2] = dTexCoords[i+2]; 

     processTriangle(triangle, tc, dNormals[k], 0, 0, false); 
    } 
} 
//draw the world given the players position 
void render(btScalar* m, const btCollisionShape* shape, const btVector3& color, int debugMode, 
      const btVector3& worldBoundsMin, const btVector3& worldBoundsMax) 
{ 
    //render the world using the generated OpenGL lists 
    //enable and specify pointers to vertex arrays 
    glPushMatrix(); 
    glMultMatrixf(m); 

    glMatrixMode(GL_TEXTURE); 
    glLoadIdentity(); 
    glMatrixMode(GL_MODELVIEW); 

    glEnable(GL_TEXTURE_2D); 
    glShadeModel(GL_SMOOTH); 
    glEnable(GL_BLEND); 
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); 

    glBindTexture(GL_TEXTURE_2D, m_texturehandle); 

    processAllTriangles(worldBoundsMin, worldBoundsMax); 

    glPopMatrix(); 
} 

回答

0

我在一個更大的代碼庫和紋理被在不同地區做不同的功能,這些功能來自那些其他紋理對象的平均狀態沒有被禁用。

每個頂點紋理完成之前,確保關閉其他紋理變種。

glDisable(GL_TEXTURE_GEN_S); 
glDisable(GL_TEXTURE_GEN_T); 
glDisable(GL_TEXTURE_GEN_R);