如何填写多边形不同的颜色比边界? [英] How to fill polygon with different color than boundary?

查看:213
本文介绍了如何填写多边形不同的颜色比边界?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要绘制具有边界线用一种颜色的多边形和填充用另一种颜色的内饰。有没有一种简单的方法来做到这一点?目前,我得出两个多边形一个是内饰颜色和1的边界。我认为必须有一个更好的做到这一点。感谢您的帮助。

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

谢谢大家回答我的问题。我是相当新的OpenGL和正在寻找一个简单的回答一个简单的问题。答案似乎不那么简单,或许可以学习一个学期的价值。

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.

推荐答案

一个更现代的方法是通过几何着色器来实现这一点。这对OpenGL 3.2及以上工作为核心功能的一部分,或对OpenGL 2.1的扩展GL_EXT_geometry_shader4。

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.

本文有所有相关的理论:着色器为基础的线框图。它还提供了GLSL最简单的技术的一个样本实现。

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

下面是我自己的刺,基本实现了对OpenGL 3.3的执行港口,限于三角形图元:

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

顶点着色器:(替换不管你使用的顶点数据传递的输入的M,V和p矩阵)

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

几何着色器:

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

这篇关于如何填写多边形不同的颜色比边界?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆