Draw a curved line from an arc edge

后端 未结 2 1422
面向向阳花
面向向阳花 2021-01-23 15:54

Here\'s the screenshot of what I am doing. Currently, I\'m stuck from drawing a curved borders into this rectangle.

<script

相关标签:
2条回答
  • 2021-01-23 16:18

    You expressed an interest in seeing how this could be solved using the stencil buffer yesterday, so I am following up with some basic pseudo-code.

    glClearStencil (0x0);
    glClear        (GL_STENCIL_BUFFER_BIT);
    
    glEnable       (GL_STENCIL_TEST);
    glStencilFunc  (GL_ALWAYS, 0x0, 0x0);
    
    // Add 1 to stencil buffer at every location the object to be bordered is visible
    glStencilOp    (GL_KEEP, GL_KEEP, GL_INCR);
    
    // Draw your grey object
    
    // Only draw the red border where the grey object was never drawn (stencil = 0x0)
    glStencilFunc  (GL_EQUAL, 0x0, 0xff);
    
    // Draw your red quarter circles
    
    glDisable     (GL_STENCIL_TEST);
    

    Clearing the stencil buffer everytime you draw your outlined object is probably overkill. If you opt to clear the stencil buffer once per-frame instead, you can do some pretty interesting things. For instance, if you drew the outlines as a separate pass after all non-outlined shapes are drawn you could use this stencil buffer setup to outline the union (instead of including the intersection of objects as part of the drawn outline) of any overlapping objects.. this would allow you to construct more complicated shapes from your simple rounded rectangles.

    Of course for this to work, your pixel format must have a stencil buffer. I will have to leave that part up to you, because the process of setting that up is implementation specific.

    0 讨论(0)
  • 2021-01-23 16:42

    GL_POLYGON is only for convex polygons.

    Link together the vertices on your inner and outer radii to form quads/triangles:


    #include <GL/glut.h>
    #include <cmath>
    
    void Torus2d
        ( 
        float angle,            // starting angle in radians
        float length,           // length of arc in radians, >0
        float radius,           // inner radius, >0
        float width,            // width of torus, >0
        unsigned int samples    // number of circle samples, >=3
        )
    {
        if( samples < 3 ) samples = 3;
        const float outer = radius + width;
        glBegin( GL_QUAD_STRIP );
        for( unsigned int i = 0; i <= samples; ++i )
        {
            float a = angle + ( i / (float)samples ) * length;
            glVertex2f( radius * cos( a ), radius * sin( a ) );
            glVertex2f( outer * cos( a ), outer * sin( a ) );
        }
        glEnd();
    }
    
    void display()
    {
        glClear( GL_COLOR_BUFFER_BIT );
    
        glMatrixMode( GL_PROJECTION );
        glLoadIdentity();
        double w = glutGet( GLUT_WINDOW_WIDTH );
        double h = glutGet( GLUT_WINDOW_HEIGHT );
        double ar = w / h;
        glOrtho( -4 * ar, 4 * ar, -4, 4, -1, 1);
    
        glMatrixMode( GL_MODELVIEW );
        glLoadIdentity();
    
        glColor3ub( 255, 0, 0 );
        Torus2d( 0, 1.57079633, 2, 1, 20 );
    
        glutSwapBuffers();
    }
    
    int main( int argc, char **argv )
    {
        glutInit( &argc, argv );
        glutInitDisplayMode( GLUT_RGBA | GLUT_DOUBLE );
        glutInitWindowSize( 640, 480 );
        glutCreateWindow( "GLUT" );
        glutDisplayFunc( display );
        glutMainLoop();
        return 0;
    }
    
    0 讨论(0)
提交回复
热议问题