简体   繁体   English

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

[英]Draw 2D thick arc using polygon in OpenGL

I want to draw a thick Arc(something like colored segment of analog dial) using polygon. 我想使用多边形绘制一个粗圆弧(类似于模拟转盘的彩色部分)。 For that i have added vertices in polygon and its working fine for the outer circumference BUT its joining the ends for inner circumference(the concave side). 为此,我在多边形中添加了顶点,并且在外部圆周上工作良好,但在内部圆周(凹面)的两端连接了顶点。

The same logic works fine if I add those vertices in Line, but that creates an empty/non-filled arc. 如果在“直线”中添加这些顶点,则相同的逻辑也可以正常工作,但是会创建一个空/未填充的弧。

My logic of adding vertices is : 我添加顶点的逻辑是:

    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 ) );
        }

Be aware that GL_POLYGON only works with convex polygons. 请注意, GL_POLYGON仅适用于凸多边形。

You'll have to triangulate concave polygons. 您必须对凹面多边形进行三角剖分。

Try using a triangle fan and making the center of your dial the first point. 尝试使用三角形风扇,使表盘的中心成为第一点。 Possibly addVertex( vec3( 0.0f, 0.0f, 0.0f ) ); 可能addVertex(vec3(0.0f,0.0f,0.0f)); before your loop. 在循环之前。

I'd also recommend making i an int or unsigned int, a float here doesn't make sense. 我还建议将我设置为int或unsigned int,此处的浮点数没有意义。

This is how I created the polygon dynamically by triangulating it : 这就是我通过三角剖分动态创建多边形的方式:

//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();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM