1

我拼命地尝试用 Cocos2D 绘制一个实心正方形,但我无法找到一个关于如何做到这一点的示例:

这是我的绘制方法。我成功地画了一个正方形,但我无法填充它!

我读过我需要使用一个glDrawArrays带有参数的OpenGL方法GL_TRIANGLE_FAN来绘制一个填充的正方形,这就是我尝试过的。

-(void) draw
{
    // Disable textures - we want to draw with plaine colors
    ccGLEnableVertexAttribs( kCCVertexAttribFlag_Position | kCCVertexAttribFlag_Color );

    float l_fRedComponent = 0;
    float l_fGreenComponent = 0;
    float l_fBlueComponent = 0;
    float l_fAlphaComponent = 0;
    [mpColor getRed:&l_fRedComponent green:&l_fGreenComponent blue:&l_fBlueComponent alpha:&l_fAlphaComponent];

    ccDrawColor4F(l_fRedComponent, l_fGreenComponent, l_fBlueComponent, l_fAlphaComponent);
    glLineWidth(10);

    CGPoint l_bottomLeft, l_bottomRight, l_topLeft, l_topRight;
    l_bottomLeft.x = miPosX - miWidth / 2.0f;
    l_bottomLeft.y = miPosY - miHeight /  2.0f;
    l_bottomRight.x = miPosX + miWidth /  2.0f;
    l_bottomRight.y = miPosY - miHeight /  2.0f;
    l_topRight.x = miPosX + miWidth /  2.0f;
    l_topRight.y = miPosY + miHeight /  2.0f;
    l_topLeft.x = miPosX - miWidth /  2.0f;
    l_topLeft.y = miPosY + miHeight /  2.0f;

    CGPoint vertices[] = { l_bottomLeft, l_bottomRight, l_topRight, l_topLeft, l_bottomLeft };
    int l_arraySize = sizeof(vertices) / sizeof(CGPoint) ;

    // My old way of doing this, it draws a square, but not filled.
    //ccDrawPoly( vertices, l_arraySize, NO);

    // Deprecated method :(
    //glVertexPointer(2, GL_FLOAT, 0, vertices);

    // I've found something related to this method to replace the deprecated one, but can't understand this method !
    glVertexAttribPointer(kCCVertexAttrib_Position, 3, GL_FLOAT, GL_FALSE, 0, vertices);
    glDrawArrays(GL_TRIANGLE_FAN, 0, l_arraySize);  
}

我找到了一些旧版本的 Cocos2D (1.0) 的示例,但是由于它“最近”升级到了2.0 版,所以我发现的所有示例都给了我编译错误!

任何人都可以在这里启发我的道路吗?

4

2 回答 2

7

我不知道今天是“重新发明轮子”日。:)

ccDrawSolidRect(CGPoint origin, CGPoint destination, ccColor4F color);

如果您要发疯并想绘制填充的多边形,那么还有:

ccDrawSolidPoly(const CGPoint *poli, NSUInteger numberOfPoints, ccColor4F color);

“solid”方法是 Cocos2D 2.x 中的新方法。

于 2012-10-02T19:21:27.740 回答
0

您可以简单地创建具有所需内容大小的 CCLayerColor 实例并将其用作填充正方形。在其他情况下,您必须对多边形进行三角剖分(如果是正方形,它将有两个三角形)并使用 OpenGL 绘制它。

---编辑没有测试这段代码,用谷歌找到它,但它似乎工作正常。

http://www.deluge.co/?q=cocos-2d-custom-filled-polygon

于 2012-10-02T18:11:28.407 回答