【问题标题】:Fill in a polygon with texture/color using CCRenderTexture使用 CCRenderTexture 用纹理/颜色填充多边形
【发布时间】:2013-02-21 13:34:39
【问题描述】:

我有一个凹多边形。我要做的第一件事是对其进行三角测量并将所有三角形点存储在顶点数组中。 我正在尝试用纹理或颜色填充该多边形,但这两种解决方案都不起作用。

这是尝试获取具有一种颜色的纹理时的代码:

CCTexture2D* TextureTest::textureColorWithVertices(CCPoint* vertices,
                                                  int count,
                                                  CCRect spriteRect)
{
    CCRenderTexture *renderTexture = CCRenderTexture::create(spriteRect.size.width, spriteRect.size.height);

    renderTexture->beginWithClear(1, 1, 1, 0);

    mShaderProgram->use();
    mShaderProgram->setUniformForModelViewProjectionMatrix();

    ccVertex2F* verts = new ccVertex2F[count];
    for( int i=0;i<count;i++) {
        verts[i].x = vertices[i].x + spriteRect.size.width / 2;//*CC_CONTENT_SCALE_FACTOR();
        verts[i].y = vertices[i].y + spriteRect.size.height / 2;//*CC_CONTENT_SCALE_FACTOR();
    }

    ccColor4F* colors = new ccColor4F[count];
    ccColor4F red = ccc4f(1, 0, 0, 1);
    for (int i = 0; i < count; i++)
    {
        colors[i] = red;
    }
    glDisable(GL_TEXTURE_2D);
    mShaderProgram->setUniformLocationWith4f(mColorLocation, 1.0f, 1.0f, 1.0f, 1.0f);

    glVertexAttribPointer(kCCVertexAttrib_Position, 2, GL_FLOAT, GL_FALSE, 0, verts);
    glVertexAttribPointer(kCCVertexAttrib_Color, 4, GL_FLOAT, GL_FALSE, 0, colors);
    glDrawArrays(GL_TRIANGLES, 0, count);
    glEnable(GL_TEXTURE_2D);

    CC_INCREMENT_GL_DRAWS(1);
    delete[] colors;
    delete[] verts;


    renderTexture->end();

    return renderTexture->getSprite()->getTexture();

}

另外一个是256*256的无缝纹理:

CCTexture2D* TextureTest::textureWithVertices(CCPoint* vertices,
                                             int count,
                                             CCTexture2D* baseTexture,
                                             CCRect spriteRect)
{    
     CCRenderTexture *renderTexture = CCRenderTexture::create(spriteRect.size.width, spriteRect.size.height);

     renderTexture->beginWithClear(1, 1, 1, 0);

     mShaderProgram->use();
     mShaderProgram->setUniformForModelViewProjectionMatrix();

     ccVertex2F* verts = new ccVertex2F[count];
     for( int i=0;i<count;i++) 
     {
         verts[i].x = vertices[i].x + spriteRect.size.width / 2;//*CC_CONTENT_SCALE_FACTOR();
         verts[i].y = vertices[i].y + spriteRect.size.height / 2;//*CC_CONTENT_SCALE_FACTOR();
     }

     ccTex2F* uvs = new ccTex2F[count];
     float baseTexturePixelsWide = baseTexture->getPixelsWide();
     float baseTexturePixelsHigh = baseTexture->getPixelsHigh();
     for( int i=0;i<count;i++) 
     {
         uvs[i].u = ((spriteRect.size.width / 2 + vertices[i].x) / baseTexturePixelsWide) * CC_CONTENT_SCALE_FACTOR();
         uvs[i].v = ((spriteRect.size.height / 2 - vertices[i].y) / baseTexturePixelsHigh) * CC_CONTENT_SCALE_FACTOR();
     }

     ccGLBindTexture2D( baseTexture->getName() );

     mShaderProgram->setUniformLocationWith4f(mColorLocation, 1.0f, 1.0f, 1.0f, 1.0f);

     glVertexAttribPointer(kCCVertexAttrib_TexCoords, 2, GL_FLOAT, GL_FALSE, 0, uvs);
     glVertexAttribPointer(kCCVertexAttrib_Position, 2, GL_FLOAT, GL_FALSE, 0, verts);
     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
     glDrawArrays(GL_TRIANGLES, 0, count);


     CC_INCREMENT_GL_DRAWS(1);
     delete[] uvs;
     delete[] verts;


     renderTexture->end();

     return renderTexture->getSprite()->getTexture();
 }

最后我在这段代码中调用了这些函数,rockPolygon就是原来的精灵:

    //Get the seamless square texture
    CCSpriteBatchNode* rockBatch = CCSpriteBatchNode::create("rock_small.png", kDefaultSpriteBatchCapacity);
    //Triangulate the old sprite
    vector<CCPoint> triangles = triangulateSprite(rockPolygon);

    //CCTexture2D* rockTexture = textureColorWithVertices(&triangles[0], triangles.size(), rockPolygon->getOriginalRect());
    CCTexture2D* rockTexture = textureWithVertices(&triangles[0], triangles.size(), rockBatch->getTexture(), rockPolygon->getOriginalRect());
    CCSprite* genSprite = new CCSprite();
    genSprite->initWithTexture(rockTexture);
    genSprite->setPosition(ccp(200,200));
    addChild(genSprite); //add generated sprite to main layer

我检查了三角测量,它工作正常。我还删除了 renderTexture->beginWithClear(1, 1, 1, 0); 之间的所有代码和 renderTexture->end(),将 alpha 通道设置为 1 beginWithClear(1, 1, 1, 1),白色方块正确显示。所以我猜问题来自opengl调用,但我找不到问题所在。请问您有什么办法解决这个问题吗?

【问题讨论】:

    标签: opengl textures cocos2d-x


    【解决方案1】:

    您已经在 cocos2d 2.x 上。您对凸形的三角剖分有效。然后就可以用CCDrawNode绘制图形了,不用自己写渲染代码。

    【讨论】:

    • 我实际上使用的是 cocos2d-x 2.0.4,CCDrawNode 从 2.1 开始可用。顺便说一句,我还在使用第三方库,它迫使我将 CCSprite 的派生类用于我的多边形。另外,CCDrawNode 是否可以通过调用 initWithTexture 来填充无缝纹理?无论如何,我做了一些测试,当从我的代码的另一部分调用它时,上面的代码可以正常工作。所以我正在调查什么可能导致这个问题,但这真的很奇怪,相同的代码,两个不同的调用位置,在一种情况下它有效,在另一种情况下它没有。谢谢你回答我:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-26
    • 2012-06-02
    • 2013-04-20
    相关资源
    最近更新 更多