使用OpenGL / Cocos2dx

时间:2016-01-19 17:41:18

标签: c++ opengl cocos2d-x

我试图为Cocos2dx场景中的某个区域着色。场景只是一个精灵(测试的黑白背景图片)。我有一个点击事件的监听器。

Scene* HelloWorld::createScene()
{
    auto scene = Scene::create();
    auto layer = HelloWorld::create();
    scene->addChild(layer);
    return scene;
}

bool HelloWorld::init()
{
    if ( !Layer::init() )
    {
        return false;
    }

    Vec2 origin = Director::getInstance()->getVisibleOrigin();

    // creating the sprite
    Sprite* background = Sprite::create("image.jpg");
    background->setPosition(origin);
    background->setAnchorPoint(origin);
    addChild(background,-2);

    // creating listener
    auto dispatcher = Director::getInstance()->getEventDispatcher();
    auto listener = EventListenerTouchOneByOne::create();

    listener->onTouchBegan = CC_CALLBACK_2(HelloWorld::onTouchBegan, this);

    dispatcher->addEventListenerWithSceneGraphPriority(listener, this);

    return true;
}


// event when clicked
bool HelloWorld::onTouchBegan(cocos2d::Touch *touch, cocos2d::Event *event)
{
    cocos2d::Vec2 touchPosition = touch->getLocation();
    Color whiteColor = { 1.0f, 1.0f, 1.0f };
    Color newColorTest = { 1.0f, 0.0f, 0.0f };

    floodFill(touchPosition.x, touchPosition.y, whiteColor, newColorTest);

    return true;
}

我试图用OpenGL为连续的白色区域着色。以下是我的尝试方式。

Color HelloWorld::getPixelColor(GLint x, GLint y) 
{
    Color color;
    float pixels[3];
    glReadPixels(x, y, 1, 1, GL_RGB, GL_FLOAT, pixels);
    color.r = pixels[0];
    color.g = pixels[1];
    color.b = pixels[2];

    return color;
}
// drawing pixel by pixel.
void HelloWorld::setPixelColor(GLint x, GLint y, Color color) 
{

    cocos2d::log("bar");
    glColor3f(color.r, color.g, color.b);
    glBegin(GL_POINTS);
        glVertex2i(x, y);
    glEnd();

    glFlush();
}

void HelloWorld::floodFill(GLint x, GLint y, Color oldColor, Color newColor)
{
    Color color;
    std::stack<GLPoint> stack;
    GLPoint clickedPoint = { x, y };
    int w = Director::getInstance()->getVisibleSize().width;
    int h = Director::getInstance()->getVisibleSize().height;

    cocos2d::log("test");

    if (x < 0 || y < 0 || x > w || y > h)
    {
        cocos2d::log("out of bonds");
        return;
    }


    stack.push(clickedPoint);

    // while there is pixels to process.
    while (!stack.empty())
    {
        GLPoint point = stack.top();
        stack.pop();

        // if out of bonds
        if (point.x < 0 || point.y < 0 || point.x > w || point.y > h)
            continue;

        color = getPixelColor(point.x, point.y);
        // if the pixel is of the colors looked for.
        if (color.r == oldColor.r && color.g == oldColor.g && color.b == oldColor.b)
        {

            cocos2d::log("foo");
            // colour it
            setPixelColor(point.x, point.y, newColor);

            // add its neighbour to the stack
            stack.push({ x + 1, y });
            stack.push({ x - 1, y });
            stack.push({ x , y + 1 });
            stack.push({ x, y - 1});
        }
    }
}

我在前面的代码中也使用了这两种结构。

struct GLPoint {
    GLint x;
    GLint y;
};

struct Color {
    GLfloat r;
    GLfloat g;
    GLfloat b;
};

在执行期间,洪水填充迭代似乎正确发生,但是在调用setPixelColor方法时没有像素改变颜色。我不知道为什么。这是我第一次使用OpenGL,也是第一次使用Cocos。

编辑:我添加了一些用于创建窗口的OpenGL代码:

void AppDelegate::initGLContextAttrs()
{
    //set OpenGL context attributions,now can only set six attributions:
    //red,green,blue,alpha,depth,stencil
    GLContextAttrs glContextAttrs = {8, 8, 8, 8, 24, 8};

    GLView::setGLContextAttrs(glContextAttrs);
}

bool AppDelegate::applicationDidFinishLaunching() {
    // initialize director
    auto director = Director::getInstance();
    auto glview = director->getOpenGLView();
    if(!glview) {
        glview = GLViewImpl::createWithRect("Coloriage", Rect(0, 0, 357, 500));
        director->setOpenGLView(glview);
    }

    director->getOpenGLView()->setDesignResolutionSize(357, 500, ResolutionPolicy::SHOW_ALL);

    // rest of method is code needed for Cocos (setting animation interval, //turning off the fps data display and creating the first cocos2d::Scene)
    return true;
}

0 个答案:

没有答案