【问题标题】:Drawing a filled square with Objective-C / Cocos2D使用 Objective-C / Cocos2D 绘制一个实心正方形
【发布时间】:2012-10-02 18:06:32
【问题描述】:

我正在拼命地尝试用 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) 的示例,但由于它已升级到 version 2.0“最近”,我发现的所有示例都给我编译错误!

有人可以在这里指导我的道路吗?

【问题讨论】:

    标签: objective-c cocos2d-iphone fill


    【解决方案1】:

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

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

    如果你发疯了,想要绘制填充多边形,还有:

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

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

    【讨论】:

    • 天啊天啊:) 非常感谢!顺便说一句,你的书很棒!
    【解决方案2】:

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

    ---编辑 这段代码没测试过,用google找到的,但是好像还可以。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-12-15
      • 1970-01-01
      • 2012-03-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-31
      相关资源
      最近更新 更多