0

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

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

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调用,但我找不到问题所在。您对如何解决此问题有任何想法吗?

4

1 回答 1

1

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

于 2013-02-21T16:53:00.183 回答