Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fill polygon with different color than boundary?

Tags:

c

opengl

I need to draw a polygon that has the boundary lines with one color and fill the interior with another color. Is there an easy way to do this ? I currently draw two polygons one for the interior color and 1 for the boundary. I think there must be a better to do this. Thanks for your help.

       glColor3d (1, 1., .7);
        glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
        glBegin(GL_TRIANGLES);
                glVertex3f(-0.8f, 0.0f, 0.0f);
                glVertex3f(-0.6f, 0.0f, 0.0f);
                glVertex3f(-0.7f, 0.2f, 0.0f);
        glEnd();

        glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
        glColor3d (.5, .5, .7);
        glBegin(GL_TRIANGLES);
                glVertex3f(-0.8f, 0.0f, 0.0f);
                glVertex3f(-0.6f, 0.0f, 0.0f);
                glVertex3f(-0.7f, 0.2f, 0.0f);
        glEnd();

Thank you everyone for answering my question. I am fairly new to openGL and was looking for a simple answer to a simple question. The answer appears to be not so simple and probably can take a semester worth of study.

like image 258
tadpole Avatar asked Feb 28 '11 18:02

tadpole


People also ask

How do you fill a polygon with color in opengl?

first draw your triangle using glPolygonMode(GL_FRONT_AND_BACK,GL_FILL) and use your desired color. then draw the triangle again using glPolygonMode(GL_FRONT_AND_BACK,GL_LINE) using your outline color.

How do I change the color of a polygon in Google Earth?

Change color of all polygonsClick the "Fixed" tab. Click the radio button next to "Use one color." Choose the fill color from the color menu or enter a hex color code. Optional: Set the opacity of the fill color by using the text field at the bottom of the color menu.


1 Answers

A more modern approach would be to implement this via geometry shaders. This would work for OpenGL 3.2 and above as part of the core functionality, or for OpenGL 2.1 with extension GL_EXT_geometry_shader4.

This paper has all the relevant theory : Shader-Based wireframe drawing. It also provides a sample implementation of the simplest technique in GLSL.

Here is my own stab at it, basically a port of their implementation for OpenGL 3.3, limited to triangle primitives:

Vertex shader: (replace the inputs with whatever you use to pass in vertex data an m, v and p matrices)

#version 330

layout(location = 0) in vec4 position;
layout(location = 1) in mat4 mv;

out Data
{
    vec4 position;
} vdata;

uniform mat4 projection;

void main()
{
    vdata.position = projection * mv * position;
}

Geometry shader:

#version 330
layout(triangles) in;
layout(triangle_strip, max_vertices = 3) out;

in Data
{
    vec4 position;
} vdata[3];

out Data
{
    noperspective out vec3 dist;
} gdata;

void main()
{
    vec2 scale = vec2(500.0f, 500.0f); // scaling factor to make 'd' in frag shader big enough to show something
    vec2 p0 = scale * vdata[0].position.xy/vdata[0].position.w;
    vec2 p1 = scale * vdata[1].position.xy/vdata[1].position.w;
    vec2 p2 = scale * vdata[2].position.xy/vdata[2].position.w;

    vec2 v0 = p2-p1;
    vec2 v1 = p2-p0;
    vec2 v2 = p1-p0;
    float area = abs(v1.x*v2.y - v1.y*v2.x);

    gdata.dist = vec3(area/length(v0),0,0);
    gl_Position = vdata[0].position;
    EmitVertex();

    gdata.dist = vec3(0,area/length(v1),0);
    gl_Position = vdata[1].position;
    EmitVertex();

    gdata.dist = vec3(0,0,area/length(v2));
    gl_Position = vdata[2].position;
    EmitVertex();

    EndPrimitive();
}

Vertex shader: (replace the colors with whatever you needed !)

#version 330

in Data
{
    noperspective in vec3 dist;
} gdata;

out vec4 outputColor;

uniform sampler2D tex;

const vec4 wireframeColor = vec4(1.0f, 0.0f, 0.0f, 1.0f);
const vec4 fillColor = vec4(1.0f, 1.0f, 1.0f, 1.0f);

void main()
{
    float d = min(gdata.dist.x, min(gdata.dist.y, gdata.dist.z));
    float I = exp2(-2*d*d);
    outputColor = mix(fillColor, wireframeColor, I);
}
like image 174
Nicolas Lefebvre Avatar answered Nov 11 '22 17:11

Nicolas Lefebvre