0

我将此提交给gamedev,但它们似乎相当慢,所以我希望我能在这里找到答案。

我一直在搞乱 C++ AMP 和 OGRE,试图让我更容易根据自己的喜好写入/更改纹理。在这我一直试图在我的“动态”纹理上绘制一个纹理,结果很奇怪。我的图像的 3/4 似乎被裁剪掉了,这让我发疯,因为我似乎找不到修复方法。

这是问题的视频:http ://www.youtube.com/watch?v=uFWxHtHtqAI

为了便于理解,这里有所有必要的代码,即使内核确实是手头的问题所在:

动态纹理.h

#define ValidTexCoord(x, y, width, height) ((x) >= 0 && (x) < (width) && (y) >= 0 && (y) < (height))

void TextureKernel(array<uint32, 2> &buffer, array_view<uint32, 2> texture, uint32 x, uint32 y, Real rot, Real scale, bool alpha)
{
    Real
        c = cos(-rot) / scale,
        s = sin(-rot) / scale;
    int32
        //e = int32(sqrt((texture.extent[1] * texture.extent[1]) + (texture.extent[0] * texture.extent[0])) * scale * 0.5F),
        dx = texture.extent[1] / 2,
        dy = texture.extent[0] / 2;

    parallel_for_each(buffer.extent, [=, &buffer](index<2> idx) restrict(amp)
    {
        int32
            tex_x = int32((Real(idx[1] - x) * c) - (Real(idx[0] - y) * s)) + dx,
            tex_y = int32((Real(idx[1] - x) * s) + (Real(idx[0] - y) * c)) + dy;

        if(ValidTexCoord(tex_x, tex_y, texture.extent[1], texture.extent[0]))
        {
            if(!alpha || (alpha && texture(tex_y, tex_x) != 0))
            {
                buffer(idx) = texture(tex_y, tex_x);
            }
        }
        else
        {
            buffer(idx) = 0x336699FF;
        }
    });
}

template<typename T, int32 Rank>
void SetKernel(array<T, Rank> &arr, T val)
{
    parallel_for_each(arr.extent, [&arr, val](index<Rank> idx) restrict(amp)
    {
        arr(idx) = val;
    }); 
}

class DynamicTexture
{
    static int32
        id;

    array<uint32, 2>
        buffer;

public:
    const int32
        width, 
        height;

    TexturePtr
        textureptr;

    DynamicTexture(const int32 width, const int32 height, uint32 color = 0) : 
        width(width),
        height(height),
        buffer(extent<2>(height, width))
    {
        SetKernel(buffer, color);

        textureptr = TextureManager::getSingleton().createManual("DynamicTexture" + StringConverter::toString(++id), ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME, TextureType::TEX_TYPE_2D, width, height, 0, PixelFormat::PF_A8R8G8B8);
    }

    ~DynamicTexture()
    {
    }

    void Texture(TexturePtr texture, uint32 x, uint32 y, Real rot = 0.F, Real scale = 1.F, bool alpha = false)
    {
        HardwarePixelBufferSharedPtr 
            pixelbuffer = texture->getBuffer();

        TextureKernel(buffer, array_view<uint32, 2>(texture->getHeight(), texture->getWidth(), (uint32 *)pixelbuffer->lock(HardwareBuffer::HBL_READ_ONLY)), x, y, rot, scale, alpha);

        pixelbuffer->unlock();
    }

    void CopyToBuffer()
    {
        HardwarePixelBufferSharedPtr 
            pixelbuffer = textureptr->getBuffer();

        copy(buffer, stdext::make_checked_array_iterator<uint32 *>((uint32 *)pixelbuffer->lock(HardwareBuffer::HBL_DISCARD), width * height));

        pixelbuffer->unlock();
    }

    void Reset(uint32 color)
    {
        SetKernel(buffer, color);
    }
};

int32 
    DynamicTexture::id = 0;

主文件

void initScene()
{
    dynamictexture = new DynamicTexture(window->getWidth(), window->getHeight());

    TextureManager::getSingleton().load("minotaur.jpg", Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME, Ogre::TextureType::TEX_TYPE_2D, 0);
}

bool frameStarted(const FrameEvent &evt) 
{
    static Real
        ang = 0.F;

    ang += 0.05F;

    if(ang > Math::TWO_PI)
    {
        ang = 0.F;
    }

    dynamictexture->Reset(0);
    dynamictexture->Texture(TextureManager::getSingleton().getByName("minotaur.jpg"), dynamictexture->width / 2, dynamictexture->height / 2, ang, 4.F, true);
    dynamictexture->CopyToBuffer();

    return true; 
}

如您所见,动态纹理是窗口的大小(在本例中为 800x600),minotaur.jpg 为 84x84。我只是将它放置在宽度和高度的一半(中心),将其旋转 ang(弧度),然后将其缩放为 4 倍。

在内核本身中,我只是遵循了一个 2D 旋转矩阵(其中 x 和 y 被参数“x”和“y”偏移):

x' = x cosθ - y sinθ
y' = x sinθ + y cosθ

另请注意, idx[1] 表示数组中的 x 值,而 idx[0] 表示 y,因为它的排列方式value = buffer[y + (x * height)](或类似的方式,但只要知道它的格式正确)。

感谢您的任何帮助!

问候, Tannz0rz

4

1 回答 1

0

感谢这个人,我找到了解决方案:https ://sites.google.com/site/ofauckland/examples/rotating-pixels

const Real
    HALF_PI = Math::HALF_PI;
const int32
    cx = texture.extent[1] / 2,
    cy = texture.extent[0] / 2;

parallel_for_each(buffer.extent, [=, &buffer](index<2> idx) restrict(amp)
{
    int32
        tex_x = idx[1] - x,
        tex_y = idx[0] - y;
    Real
        dist = sqrt(Real((tex_x * tex_x) + (tex_y * tex_y))) / scale,
        theta = atan2(Real(tex_y), Real(tex_x)) - angle - HALF_PI;

    tex_x = int32(dist * sin(theta)) + cx;
    tex_y = int32(dist * cos(theta)) + cy;

    if(ValidTexCoord(tex_x, tex_y, texture.extent[1], texture.extent[0]))
    {
        buffer(idx) = texture(tex_y, tex_x);
    }
});
于 2013-03-09T07:27:16.523 回答