2

I am trying to draw the shape shown below (in black) in OpenGL (for desktops, not mobile). I'd prefer it in 3D, but 2D would suffice as well. I will be mapping a texture to it as well. I've used triangle fans/strips for other things in the past and I imagine that's what I'd need to use here but I'm just not sure where to start on it. I've drawn the WHITE part before, but never the inverse (the black part). Any thoughts or guidance on what to use (triangle fan, triangle strip, some other odd OpenGL shape I probably didn't know existed, etc...)

enter image description here

Final Solution:

void draw_arch(GLfloat width, GLfloat height, GLint slices)
{
    glPushMatrix();
        GLfloat offset = 0.5f;
        glScalef(width/2,height/(1+offset),1.0f);
        glBegin(GL_QUADS);
        for( unsigned int i = 0; i < slices; ++i ) {
            float curAngle = ( ( i + 0 ) / (float)slices ) * 3.14159;
            float nxtAngle = ( ( i + 1 ) / (float)slices ) * 3.14159;
            glVertex2f( cos( curAngle ), sin( curAngle ) );
            glVertex2f( cos( curAngle ), 1.0f + offset );
            glVertex2f( cos( nxtAngle ), 1.0f + offset );
            glVertex2f( cos( nxtAngle ), sin( nxtAngle ) );
        }
        glEnd();
    glPopMatrix();
}

I can adjust the "offset" variable to make different looking arches, however in this application, I choose 0.5 to make it look the way I wanted it to!

6
  • We're not here to do your work and/or research. You need to do that yourself, then if you encounter any coding related problems, you can ask a question.
    – vallentin
    Dec 10, 2013 at 20:23
  • 1
    I will give you a hint, most triangulations of this will probably have most of the triangles radiating from the top-left and top-right corners and generate triangles in an order that walks around the perimeter of the arc. You cannot use a (single) fan, but a strip or general triangle list would work. Dec 10, 2013 at 20:27
  • @Vallentin I'm not asking anyone to do it for me, I simply asked for a starting direction. Even a simple "you're best bet is triangle fans" would be an acceptable answer. I've done PLENTY of research to find a good answer, however searching for "opengl" and "arch" (or some derivation of those) ends up with a tons of results for OpenGL on Arch Linux(give it a try!) I never post a question on Stack Overflow UNLESS I've exhausted all my research! Look at my previous questions and see that I'm not some student trying to get a homework question answered. I'd appreciate not being treated as such!
    – D.R.
    Dec 10, 2013 at 20:27
  • @AndonM.Coleman Thanks! I would never have started that way. I would have been trying from the bottom corners. I'll try a method that way and see what I can whip up! Thanks!
    – D.R.
    Dec 10, 2013 at 20:31
  • You could stencil out the white part. Or shade fragments that lands outside your circle Dec 10, 2013 at 20:39

1 Answer 1

2

Generate the top half of a circle and an offset above it and link the two with quads/triangles:

hemicircle

#include <GL/glut.h>
#include <cmath>

void glShape( const float height, unsigned int segs )
{
    glBegin( GL_QUADS );
    for( unsigned int i = 0; i < segs; ++i )
    {
        float curAngle = ( ( i + 0 ) / (float)segs ) * 3.14159;
        float nxtAngle = ( ( i + 1 ) / (float)segs ) * 3.14159;
        glVertex2f( cos( curAngle ), sin( curAngle ) );
        glVertex2f( cos( curAngle ), 1 + height      );
        glVertex2f( cos( nxtAngle ), 1 + height      );
        glVertex2f( cos( nxtAngle ), sin( nxtAngle ) );
    }
    glEnd();
}

void display()
{
    glClearColor( 0, 0, 0, 1 );
    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( -2 * ar, 2 * ar, -2, 2, -1, 1 );

    glMatrixMode( GL_MODELVIEW );
    glLoadIdentity();

    glColor3ub( 255, 0, 0 );
    glShape( 0.1f, 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;
}

Probably not the minimal number of quads/triangles but it's quick and easy :)

1
  • 2
    Wow! That was much more than I was expecting and appears to work beautifully. I'm going to do a little bit of tweaking just to make sure and then I'll accept your answer!
    – D.R.
    Dec 10, 2013 at 21:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.