1

Using C++, I'm trying to load a texture into OpenGL using DevIL. After scrounging around for different code segments, I have a bit of code done (shown below), but it doesn't seem to work completely.

Loading a texture (Part of a Texture2D class):

void Texture2D::LoadTexture(const char *file_name) 
{   
    unsigned int image_ID;

    ilInit();
    iluInit();
    ilutInit();

    ilutRenderer(ILUT_OPENGL);

    image_ID = ilutGLLoadImage((char*)file_name);

    sheet.texture_ID = image_ID;
    sheet.width = ilGetInteger(IL_IMAGE_WIDTH);
    sheet.height = ilGetInteger(IL_IMAGE_HEIGHT);
}

This compiles and works fine. I do realise that I should only do the ilInit(), iluInit(), and ilutInit() once, but if I remove those lines the program instantly breaks upon loading any image (compiles fine, but errors on runtime).

Displaying the texture in OpenGL (Part of the same class):

void Texture2D::Draw() 
{

    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
    glPushMatrix();

    u = v = 0;

    // this is the origin point (the position of the button)
    VectorXYZ point_TL; // Top Left 

    VectorXYZ point_BL; // Bottom Left
    VectorXYZ point_BR; // Bottom Right
    VectorXYZ point_TR; // Top Right

    /* For the sake of simplicity, I've removed the code calculating the 4 points of the Quad. Assume that they are found correctly. */

    glColor3f(1,1,1);
    // bind the appropriate texture frame
    glBindTexture(GL_TEXTURE_2D, sheet.texture_ID);

    // draw the image as a quad the size of the first loaded image
        glEnable(GL_TEXTURE_2D);
        glBegin(GL_QUADS);
            glTexCoord2f (0, 0);
            glVertex3f  (point_TL.x, point_TL.y, point_TL.z); // Top Left

            glTexCoord2f (0, 1);
            glVertex3f  (point_BL.x, point_BL.y, point_BL.z); // Bottom Left

            glTexCoord2f (1, 1); 
            glVertex3f  (point_BR.x, point_BR.y, point_BR.z); // Bottom Right

            glTexCoord2f (1, 0);
            glVertex3f  (point_TR.x, point_TR.y, point_TR.z); // Top Right
        glEnd();
        glPopMatrix();
    glDisable(GL_BLEND);
    glDisable(GL_TEXTURE_2D);
}

Currently, the quad shows up, but its completely white (the background colour it's given). The image I'm loading exists and is loaded fine (verified using the loaded size values).

Another few things I should note: 1) I am using a depth buffer. I've heard this doesn't go well with GL_BLEND? 2) I would really like to use the ilutGLLoadImage function. 3) I appreciate example code, as I'm a newbie to openGL and DevIL as a whole.

4

1 に答える 1

2

はい、問題があります。ilutGLLoadImage()に問題がある可能性があります。

手動でやってみてください:

  1. ilLoadImageを使用して画像をロードします
  2. glGenTexturesを使用してOpenGLテクスチャハンドルを生成します
  3. 画像をOpenGLglTextImage2DおよびilGetData()にアップロードします

実用的なソリューションについては、このリンクを参照してください

http://r3dux.org/tag/ilutglloadimage/

この解決策は「少し」複雑に思えますが、DevILの奥深くに隠されているこのバグと戦うのに多くの時間を費やすのは誰にもわかりません。

物事を修正する別の方法:GLテクスチャ設定コードを確認してください。フィルタリングの内容がGL_INVALID_OPERATIONの理由になる可能性があります。

古いATIカードをプログラミングしているときに、「白いテクスチャ」の問題に何度も遭遇しました。

おー!最大の推測:2の累乗ではないテクスチャ。2 ^ N x 2 ^ Nのテクスチャファイルですか、それとも別のものですか?

非長方形のテクスチャを使用するには、GL拡張機能を使用する必要があります。

そしてもう1つ:同じスレッドまたは他のスレッドでテクスチャを使用していますか?同じスレッドでglGenTextures()とglBindTexture()/ glBegin/glEndを使用する必要があることに注意してください。

于 2012-05-22T17:52:55.213 に答える