使用CCRenderTexture填充纹理/颜色的多边形

时间:2013-02-21 13:34:39

标签: opengl textures cocos2d-x

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

以下是尝试使用一种颜色获取纹理时的代码:

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-&gt; beginWithClear(1,1,1,0)之间的所有代码;和renderTexture-&gt; end(),将alpha通道设置为1 beginWithClear(1,1,1,1)并正确显示白色方块。所以我猜问题来自opengl调用,但我找不到有什么问题。你对如何解决这个问题有什么想法吗?

1 个答案:

答案 0 :(得分:1)

你已经在cocos2d 2.x.你对凸形的三角测量有效。然后你可以使用CCDrawNode绘制形状,无需编写自己的渲染代码。

相关问题