Shahiryar Arif
Shahiryar Arif

Reputation: 49

Draw multi-colored triangle with thick edges in opengl

enter image description here

Is it possible in opengl to draw this type to triangles?

I have already tried

Code and Result i got using glBegin(GL_LINE_LOOP)

Code:

glBegin(GL_LINE_LOOP);
glColor3f(0.0f, 1.0f, 1.0f);
glVertex2f(-30.0, 30.0);
glColor3f(1.0f, 1.0f, 0.0f);
glVertex2f(30.0, 30.0);
glColor3f(0.5f, 0.0f, 1.0f);
glVertex2f(0.0, -30);
glEnd();

Result:

enter image description here

Upvotes: 2

Views: 661

Answers (2)

HolyBlackCat
HolyBlackCat

Reputation: 96013

To remove color gradients, you have at least two options:

  • You could call glShadeModel(GL_FLAT); before rendering the triangle.

  • You could use GL_LINES, like so:

    glBegin(GL_LINES);
    glColor3f(0.0f, 1.0f, 1.0f);
    glVertex2f(-30.0, 30.0);
    glVertex2f(30.0, 30.0);
    glColor3f(1.0f, 1.0f, 0.0f);
    glVertex2f(30.0, 30.0);
    glVertex2f(0.0, -30);
    glColor3f(0.5f, 0.0f, 1.0f);
    glVertex2f(0.0, -30);
    glVertex2f(-30.0, 30.0);
    glEnd();
    

To increase line width, you could use glLineWidth.

But you'll see that ends of wide lines normally don't look as pretty and round as on your image. If that's a problem for you, you'll have to draw the shape using lots of carefully placed GL_TRIANGLES instead.

Upvotes: 1

Spektre
Spektre

Reputation: 51835

I would change your code to this:

glLineWidth(5.0); // this makes 5 pixel thick lines
glBegin(GL_LINES);
glColor3f(0.0f, 1.0f, 1.0f); glVertex2f(-30.0, 30.0); glVertex2f( 30.0, 30.0);
glColor3f(1.0f, 1.0f, 0.0f); glVertex2f( 30.0, 30.0); glVertex2f(  0.0,-30.0);
glColor3f(0.5f, 0.0f, 1.0f); glVertex2f(  0.0,-30.0); glVertex2f(-30.0, 30.0); 
glEnd();
glLineWidth(1.0); // return state to original conditions

So you should have thick lines and the coloring should not interpolate between lines ...

Upvotes: 2

Related Questions