1

クワッドにテクスチャを適用しようとしていますが、テクスチャではなくブラック ボックスしか表示されません。私は DevIL を使用してファイルから画像を読み込み、OpenGL が残りを行います。

これが私がこれまでに行っていることです:

次のクラスは、画像の DevIL 表現を抽象化します。

#include "Image.h"

Image::Image()
{
    ilGenImages(1, &this->imageId);
}

Image::~Image()
{
    ilDeleteImages(1, &this->imageId);
}

ILint Image::getWidth()
{
    return this->width;
}

ILint Image::getHeight()
{
    return this->height;
}

ILint Image::getDepth()
{
    return this->depth;
}

ILint Image::getBpp()
{
    return this->bpp;
}

ILint Image::getFormat()
{
    return this->format;
}

ILubyte* Image::getData()
{
    return ilGetData();
}

bool Image::loadFromFile(wchar_t *filename)
{
    // Load the image from file.
    ILboolean retval = ilLoadImage(filename);
    if (!retval) {
        ILenum error;
        while ((error = ilGetError()) != IL_NO_ERROR) {
            wcout << error << L" " << iluErrorString(error);
        }
        return false;
    }

    this->width = ilGetInteger(IL_IMAGE_WIDTH);
    this->height = ilGetInteger(IL_IMAGE_HEIGHT);
    this->depth = ilGetInteger(IL_IMAGE_DEPTH);
    this->bpp = ilGetInteger(IL_IMAGE_BPP);
    this->format = ilGetInteger(IL_IMAGE_FORMAT);

    return true;
}

bool Image::convert()
{
    ILboolean retval = ilConvertImage(IL_RGBA, IL_UNSIGNED_BYTE);
    if (!retval) {
        ILenum error;
        while ((error = ilGetError()) != IL_NO_ERROR) {
            wcout << error << L" " << iluErrorString(error);
        }
        return false;
    }
    return true;
}

bool Image::scale(ILint width, ILint height, ILint depth)
{
    ILboolean retval = iluScale(width, height, depth);
    if (!retval) {
        ILenum error;
        while ((error = ilGetError()) != IL_NO_ERROR) {
            wcout << error << L" " << iluErrorString(error);
        }
        return false;
    }
    return true;
}

void Image::bind()
{
    ilBindImage(this->imageId);
}

このクラスは、OpenGL のテクスチャ表現を抽象化します。

#include "Texture.h"

Texture::Texture(int width, int height)
{
    glGenTextures(1, &this->textureId);

    this->width = width;
    this->height = height;
}

int Texture::getWidth()
{
    return this->width;
}

int Texture::getHeight()
{
    return this->height;
}

void Texture::initFilter()
{
    // We will use linear interpolation for magnification filter.
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    // We will use linear interpolation for minifying filter.
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

}

void Texture::unpack()
{
    glPixelStoref(GL_UNPACK_ALIGNMENT, 1);
}

void Texture::bind()
{
    glBindTexture(GL_TEXTURE_2D, this->textureId);
}

Texture::~Texture()
{
    glDeleteTextures(1, &this->textureId);
}

次のクラスには、テクスチャの読み込みプロセスが含まれています。

#include "TextureLoader.h"

void TextureLoader::initialize()
{
    if (ilGetInteger(IL_VERSION_NUM) < IL_VERSION) {
        debug("Wrong DevIL version detected.");
        return;
    }

    ilInit();
    ilutRenderer(ILUT_OPENGL);
}

Texture* TextureLoader::createTexture(wchar_t *filename, Color *color)
{
    // Generate some space for an image and bind it.
    Image *image = new Image();
    image->bind();

    bool retval = image->loadFromFile(filename);
    if (!retval) {
        debug("Could not load image from file.");
        return 0;
    }

    retval = image->convert();
    if (!retval) {
        debug("Could not convert image from RGBA to unsigned byte");
    }

    int pWidth = getNextPowerOfTwo(image->getWidth());
    int pHeight = getNextPowerOfTwo(image->getHeight());
    int size = pWidth * pHeight;

    retval = image->scale(pWidth, pHeight, image->getDepth());
    if (!retval) {
        debug("Could not scale image from (w: %i, h: %i) to (w: %i, h: %i) with depth %i.", image->getWidth(), image->getHeight(), pWidth, pHeight, image->getDepth());
        return 0;
    }

    // Generate some space for a texture and bind it.
    Texture *texture = new Texture(image->getWidth(), image->getHeight());
    texture->bind();

    // Set the interpolation filters.
    texture->initFilter();

    // Unpack pixels.
    texture->unpack();

    ILubyte *imageData = image->getData();

    TextureLoader::setColorKey(imageData, size, new Color(0, 0, 0));
    TextureLoader::colorize(imageData, size, new Color(255, 0, 0));

    debug("bpp: %i", image->getBpp());
    debug("width: %i", image->getWidth());
    debug("height: %i", image->getHeight());
    debug("format: %i", image->getFormat());

    // Map image data to texture data.
    glTexImage2D(GL_TEXTURE_2D, 0, image->getBpp(), image->getWidth(), image->getHeight(), 0, image->getFormat(), GL_UNSIGNED_BYTE, imageData);

    delete image;

    return texture;
}

void TextureLoader::setColorKey(ILubyte *imageData, int size, Color *color)
{
    for (int i = 0; i < size * 4; i += 4)
    {
        if (imageData[i] == color->r && imageData[i + 1] == color->g && imageData[i + 2] == color->b)
        {
            imageData[i + 3] = 0;
        }
    }
}

void TextureLoader::colorize(ILubyte *imageData, int size, Color *color)
{
    for (int i = 0; i < size * 4; i += 4)
    {
        int rr = (int(imageData[i]) * int(color->r)) >> 8;
        int rg = (int(imageData[i + 1]) * int(color->g)) >> 8;
        int rb = (int(imageData[i + 2]) * int(color->b)) >> 8;
        int fak = int(imageData[i]) * 5 - 4 * 256 - 138;

        if (fak > 0)
        {
            rr += fak;
            rg += fak;
            rb += fak;
        }

        rr = rr < 255 ? rr : 255;
        rg = rg < 255 ? rg : 255;
        rb = rb < 255 ? rb : 255;

        imageData[i] = rr > 0 ? (GLubyte) rr : 1;
        imageData[i + 1] = rg > 0 ? (GLubyte) rg : 1;
        imageData[i + 2] = rb > 0 ? (GLubyte) rb : 1;
    }
}

最後のクラスは絵を描きます。

#include "Texturizer.h"

void Texturizer::draw(Texture *texture, float x, float y, float angle)
{
    // Enable texturing.
    glEnable(GL_TEXTURE_2D);

    // Bind the texture for drawing.
    texture->bind();

    // Enable alpha blending.
    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

    int width = texture->getWidth();
    int height = texture->getHeight();

    // Create centered dimension vectors.
    b2Vec2 vertices[4];
    vertices[0] = 0.5f * b2Vec2(- width, - height);
    vertices[1] = 0.5f * b2Vec2(+ width, - height);
    vertices[2] = 0.5f * b2Vec2(+ width, + height);
    vertices[3] = 0.5f * b2Vec2(- width, + height);

    b2Mat22 matrix = b2Mat22();
    matrix.Set(angle);

    glBegin(GL_QUADS);
    for (int i = 0; i < 4; i++) {
        float texCoordX = i == 0 || i == 3 ? 0.0f : 1.0f;
        float texCoordY = i < 2 ? 0.0f : 1.0f;
        glTexCoord2f(texCoordX, texCoordY);

        // Rotate and move vectors.
        b2Vec2 vector = b2Mul(matrix, vertices[i]) + meter2pixel(b2Vec2(x, y));
        glVertex2f(vector.x, vector.y);
    }
    glEnd();

    glDisable(GL_BLEND);
    glDisable(GL_TEXTURE_2D);
}

最後になりましたが、次のメソッドは OpenGL を初期化します (そして、DevIL の初期化をトリガーします)。

void GraphicsEngine::initialize(int argc, char **argv)
{
    // Initialize the window.
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
    glutInitWindowSize(WIDTH, HEIGHT);

    // Set shading model.
    glShadeModel(GL_SMOOTH);

    // Create the window.
    this->mainWindow = glutCreateWindow(TITLE);

    // Set keyboard methods.
    glutKeyboardFunc(&onKeyDownCallback);
    glutKeyboardUpFunc(&onKeyUpCallback);
    glutSpecialFunc(&onSpecialKeyDownCallback);
    glutSpecialUpFunc(&onSpecialKeyUpCallback);

    // Set mouse callbacks.
    glutMouseFunc(&onMouseButtonCallback);
#ifdef FREEGLUT
    glutMouseWheelFunc(&onMouseWheelCallback);
#endif
    glutMotionFunc(&onMouseMotionCallback);
    glutPassiveMotionFunc(&onMousePassiveMotionCallback);

    // Set display callbacks.
    glutDisplayFunc(&onDrawCallback);
    glutReshapeFunc(&onReshapeCallback);

    // Set a timer to control the frame rate.
    glutTimerFunc(FRAME_PERIOD, onTimerTickCallback, 0);

    // Set clear color.
    glClearColor(1.0f, 1.0f, 1.0f, 1.0f);

    Camera::getInstance()->subscribe(this);

    // Initialize texture loader.
    TextureLoader::initialize();
}

私が使用しているイメージは、別の OpenGL/DevIL プロジェクトで既に機能しているため、問題の原因になることはありません。

テクスチャは、ワールド オブジェクトを表すすべてのクラス内で作成されます (これはゲームです...)。このキャラクターは Blobby と呼ばれ、その実装の最も重要な部分は次のとおりです。

#include "Blobby.h"

Blobby::Blobby()
{
    this->isJumping = false;
    this->isRotating = false;
    this->isWalking = false;
    this->isDucking = false;
    this->isStandingUp = false;
    this->isOnGround = false;
    this->isTouchingWall = false;
    this->angle = 0;
    this->direction = DIRECTION_UNKNOWN;
    this->wallDirection = DIRECTION_UNKNOWN;

    // Create a red blobby texture.
    this->texture = TextureLoader::createTexture(L"D:/01.bmp", new Color(255, 0, 0));

    ContactListener::getInstance()->subscribe(this);
}

void Blobby::draw()
{
    GraphicsEngine::drawString(35, 40, "isOnGround     = %s", this->isOnGround ? "true" : "false");
    GraphicsEngine::drawString(35, 55, "inJumping      = %s", this->isJumping ? "true" : "false");
    GraphicsEngine::drawString(35, 70, "isRotating     = %s", this->isRotating ? "true" : "false");
    GraphicsEngine::drawString(35, 85, "isTouchingWall = %s (%i)", this->isTouchingWall ? "true" : "false", this->wallDirection);

    Texturizer::draw(this->texture, this->getBody(0)->GetPosition().x, this->getBody(0)->GetPosition().y, this->getBody(0)->GetAngle());

    AbstractEntity::draw(); // draws debug information... not important
}

OpenGL タイマー コールバックは、ここで終了する step メソッドを呼び出します。

void Simulator::step()
{
    // Update physics.
    this->gameWorld->step();

    b2Vec2 p = Camera::convertWorldToScreen(meter2pixel(this->cameraBlobby->getBody(0)->GetPosition().x), 300.0f);
    if (p.x < 300) {
        Camera::getInstance()->setViewCenter(Camera::convertScreenToWorld(400 - (300 - int(p.x)), 300));
    } else if (p.x > 500) {
        Camera::getInstance()->setViewCenter(Camera::convertScreenToWorld(400 + (int(p.x) - 500), 300));
    }


    for (unsigned int i = 0; i < this->gameWorld->getEntityCount(); i++) {
        IEntity *entity = this->gameWorld->getEntity(i);
        entity->draw();
    }
}

IEntity は純粋な仮想クラス (つまりインターフェイス) であり、AbstractEntity はこのインターフェイスを実装し、グローバル メソッドを追加します。Blobby は AbstractEntity を継承し、このワールド オブジェクトに特別なルーチンを追加します。

編集: コードの最新バージョン (依存関係を含むプロジェクト全体) をここにアップロードしました: http://upload.visusnet.de/uploads/BlobbyWarriors-rev19.zip (~9.5 MB)

4

3 に答える 3

3

DevIL には詳しくありませんが、頂点に適切な拡散色を提供していますか? 照明が有効になっている場合、クワッドを指しているライトはありますか? カメラはクワッドの前面を見ていますか?

編集:

コードにバグがありますが、ここに投稿したものではなく、リンクしたアーカイブのバージョンにバグがあります。

を呼び出すとglColor3i(255, 255, 255)、期待どおりに拡散色が (ほぼ) 黒に設定されます。ターゲット (計算またはフレームバッファ) 範囲内のカラー値を受け入れglColor3iません。可能な値は、int型の範囲全体にスケーリングされます。これは、最大値 (float の 1.0) が MAX_INT (2,147,483,647) で表され、0 が 0、-1.0 が MIN_INT (-2,147,483,648) であることを意味します。あなたが提供した255の値は約 0.000000118 を表し、これはほぼゼロです。

次の(完全に同等の)形式のいずれかを意図していたと思います。

glColor3f(1.0, 1.0, 1.0)glColor3ub(255, 255, 255)

glColor3i(2147483647, 2147483647, 2147483647).

于 2010-11-08T20:47:00.603 に答える
0

私はずっと前にこのような問題を抱えていました。当時は、テクスチャの寸法が 2 の指数 (128x128、512x512 など) ではないという問題だったと思います。彼らは今ではそれを修正していると確信していますが、試してみる価値があるかもしれません.

于 2010-11-09T02:24:08.467 に答える
0

b2Mat22 マトリックスには何がありますか? この行列を掛けると、頂点が時計回りに描画される可能性があります。その場合、正方形の背面があなたに面し、テクスチャが反対側 (見えない) にある可能性があるためです。

于 2010-11-08T14:43:39.880 に答える