如何有效地覆盖精灵的绘制方法来改变单个像素?

时间:2016-10-25 06:48:32

标签: c++ opengl cocos2d-x cocos2d-x-3.0 cocos2d-x-3.x

显然在cocos2d-x中,你可以覆盖精灵的draw方法,做一些有趣的事情。不幸的是,我没有OpenGL经验,所以我很难搞清楚这种方法的用途:

void Sprite::draw(Renderer *renderer, const Mat4 &transform, uint32_t flags)
{
#if CC_USE_CULLING
    // Don't do calculate the culling if the transform was not updated
    auto visitingCamera = Camera::getVisitingCamera();
    auto defaultCamera = Camera::getDefaultCamera();
    if (visitingCamera == defaultCamera) {
        _insideBounds = ((flags & FLAGS_TRANSFORM_DIRTY)|| visitingCamera->isViewProjectionUpdated()) ? renderer->checkVisibility(transform, _contentSize) : _insideBounds;
    }
    else
    {
        _insideBounds = renderer->checkVisibility(transform, _contentSize);
    }

    if(_insideBounds)
#endif
    {
        _trianglesCommand.init(_globalZOrder, _texture->getName(), getGLProgramState(), _blendFunc, _polyInfo.triangles, transform, flags);
        renderer->addCommand(&_trianglesCommand);

#if CC_SPRITE_DEBUG_DRAW
        _debugDrawNode->clear();
        auto count = _polyInfo.triangles.indexCount/3;
        auto indices = _polyInfo.triangles.indices;
        auto verts = _polyInfo.triangles.verts;
        for(ssize_t i = 0; i < count; i++)
        {
            //draw 3 lines
            Vec3 from =verts[indices[i*3]].vertices;
            Vec3 to = verts[indices[i*3+1]].vertices;
            _debugDrawNode->drawLine(Vec2(from.x, from.y), Vec2(to.x,to.y), Color4F::WHITE);

            from =verts[indices[i*3+1]].vertices;
            to = verts[indices[i*3+2]].vertices;
            _debugDrawNode->drawLine(Vec2(from.x, from.y), Vec2(to.x,to.y), Color4F::WHITE);

            from =verts[indices[i*3+2]].vertices;
            to = verts[indices[i*3]].vertices;
            _debugDrawNode->drawLine(Vec2(from.x, from.y), Vec2(to.x,to.y), Color4F::WHITE);
        }
#endif //CC_SPRITE_DEBUG_DRAW
    }
}

给定像素,我知道如何转换我的图像(单个像素)以获得我想要的结果,但这包括迭代图像中的所有像素并根据需要进行更改,此外,我可以在运行时之前执行此操作(离线,在构建我的应用程序之前)创建静态图像,我在应用程序中使用。我试图弄清楚如何在运行时(有效地)做同样的事情,因为我猜测我用于生成图像的相同技术使用了大量的CPU(因为我在迭代像素和不使用OpenGL)。

有人能指出我正确的方向吗?我没有尝试构建图形引擎或任何东西......我只是想在运行时快速修改像素....

0 个答案:

没有答案