在OpenGL中使用多边形绘制2D粗弧

时间:2013-04-24 04:03:56

标签: c++ opengl polygon shapes

我想使用多边形绘制一个粗圆弧(类似于模拟表盘的彩色部分)。为此,我已经在多边形中添加了顶点,并且它的外圆周工作正常,但是它连接内圆周的端部(凹面)。

如果我在Line中添加这些顶点,但是相同的逻辑工作正常,但是会创建一个空的/未填充的弧。

我添加顶点的逻辑是:

    for( float i = m_segmentVertex.size() - 1; i < vCount; i++ )
        {
            float x1 = (m_segmentVertex[ i ].x ) * cosA - m_segmentVertex[ i ].y * sinA;
            float y1 = (m_segmentVertex[ i ].x ) * sinA + m_segmentVertex[ i ].y * cosA;
            addVertex( vec3( x1, y1, 0.0f ) );
        }

3 个答案:

答案 0 :(得分:1)

请注意GL_POLYGON仅适用于凸多边形。

你必须对凹多边形进行三角测量。

答案 1 :(得分:0)

尝试使用三角形风扇并将表盘中心作为第一点。 可能是addVertex(vec3(0.0f,0.0f,0.0f));在你的循环之前。

我还建议将i设为int或unsigned int,这里的float没有意义。

答案 2 :(得分:0)

这是我通过三角测量动态创建多边形的方法:

//create thick colored segments
void CreateArcMesh( float sAngle, float eAngle, vec4 color, int thickness, int radius )
{
    ObjectMeshDynamic meshObj = new ObjectMeshDynamic();
    vec3 vertex[0];
    float dAngle = ( ( eAngle - sAngle ) / ( VERTEX_COUNT / 2.0f ) );
    float cosA = cos( DEG2RAD * dAngle );
    float sinA = sin( DEG2RAD * dAngle );

    meshObj.setMaterial( "material_base", "*" );
    meshObj.setProperty( "surface_base", "*" );
    meshObj.setMaterialParameter( "diffuse_color", color, 0 );
    //Add the material on both side as the indices for Triangle strip start from last vertex added
    Material material = meshObj.getMaterialInherit(0);
    material.setTwoSided( 1 );
    meshObj.addTriangleStrip( VERTEX_COUNT + 2 );

    vec3 startPos = vec3( radius * cos( DEG2RAD * sAngle ), radius * sin( DEG2RAD * sAngle ), 0.0f );
    vertex.append( startPos );

    vec3 secondPos = vec3( ( radius - thickness ) * cos( DEG2RAD * sAngle ), ( radius - thickness ) * sin( DEG2RAD * sAngle ), 0.0f );
    vertex.append( secondPos );

    float x1 = startPos.x * cosA - startPos.y * sinA;
    float y1 = startPos.x * sinA + startPos.y * cosA;
    vertex.append( vec3( x1, y1, 0.0f ) );

    x1 = secondPos.x * cosA - secondPos.y * sinA;
    y1 = secondPos.x * sinA + secondPos.y * cosA;
    vertex.append( vec3( x1, y1, 0.0f ) );

    forloop( int k = 0 ; VERTEX_COUNT + 2 )
    {
        x1 = ( vertex[ vertex.size() - 2 ].x ) * cosA - vertex[ vertex.size() - 2 ].y * sinA;
        y1 = ( vertex[ vertex.size() - 2 ].x ) * sinA + vertex[ vertex.size() - 2 ].y * cosA;

        vertex.append( vec3( x1, y1, 0.0f ) );
        meshObj.addVertex( vertex[k] );
    }

    vertex.clear();
    meshObj.updateBounds();
    meshObj.flush();
}