Study of Qt 3D (ix): Try another method of edge detection

Source: Internet
Author: User
Tags float max

Study of Qt 3D (ix): Try another method of edge detection

Three-dimensional applications, through the FBO, render 3D images into textures, and then image the rendered textures, which are ultimately displayed on the screen, in a stylized pattern. The last time I used an ordinary image processing method: The Bayesian edge detection, which was combined with our cartoon rendering to achieve this effect, I will then use a different edge detection method-the Prevett (Prewitt) edge detection method to re-render the pattern.

Saiyang original article, starting address: http://blog.csdn.net/gamesdev/article/details/44405219. Welcome to come to discuss the peer.

First, let's look at the previous one:

We see that this should not be the edge of the fuselage part, due to the discrete color palette, the edge detection of the bell operator is also mistaken for the edge, while, in the background and the body color is not obvious, but also due to the improper threshold value, is not considered to be the edge. So I think there is a way to solve this problem? So I took this approach:

1, the first time the render pass, not the cartoon coloring color map, but the depth map;

2. Render the depth map to the texture;

3, the edge detection of the texture;

4, with cartoon coloring of the picture to be superimposed, made.

How to extract the depth information of the elements in GLSL? Here I refer to the blog of the predecessor, and then write out such GLSL code:

Depth.vert#version 100//Qt 3D default supplied parameters attribute vec3 vertexposition;uniform mat4 modelview;uniform mat4 mvp;void Main (v OID) {    gl_position = MVP * VEC4 (Vertexposition, 1.0);} Depth.frag#version 110//Self-supplied parameters bool InBetween (float V, float min, float max) {    return v > min && v < ; Max;} void Main (void) {    float exp = 256.0;    Gl_fragcolor = VEC4 (VEC3 (POW (GL_FRAGCOORD.Z, exp)), 1.0);}

Because the element depth information expressed by GL_FRAGCOORD.Z is very close to each other, we need an exponential exponentiation operation to magnify such distinctions so that the values of different depths can be distinguished.

Shortly thereafter, we replace the Prewitt operator with the Sobel operator, and the final shader code is as follows:

Ouput.vert#version 100//Qt 3D default supplied parameters attribute Vec4 vertexposition;uniform mat4 modelmatrix;//own supplied parameters void Main (void) {gl_position = Modelmatrix * vertexposition;} Output.frag#version 100//Self-supplied parameters uniform sampler2d colorattachtex;//uniform sampler2d depthattachtex;uniform vec2  Texsize;uniform float Texoffsetx;uniform float texoffsety;float Gray (vec4 color) {return dot (color.xyz, VEC3 (0.299, 0.587, 0.114));}    void Main (void) {VEC4 texcolor[9];    Texcolor[0] = texture2d (Colorattachtex, Gl_fragcoord.xy/texsize + vec2 (-texoffsetx, texoffsety));    TEXCOLOR[1] = texture2d (Colorattachtex, gl_fragcoord.xy/texsize + vec2 (0.0,-texoffsety));    TEXCOLOR[2] = texture2d (Colorattachtex, Gl_fragcoord.xy/texsize + vec2 (texoffsetx, texoffsety));    TEXCOLOR[3] = texture2d (Colorattachtex, Gl_fragcoord.xy/texsize + vec2 (-texoffsetx, 0));    TEXCOLOR[4] = texture2d (Colorattachtex, gl_fragcoord.xy/texsize + vec2 (0.0, 0.0)); TEXCOLOR[5] = texture2d (Colorattachtex, Gl_fragcoord.xy/texsize + vec2 (texoffsetx, 0));    TEXCOLOR[6] = texture2d (Colorattachtex, Gl_fragcoord.xy/texsize + vec2 (-texoffsetx,-texoffsety));    TEXCOLOR[7] = texture2d (Colorattachtex, gl_fragcoord.xy/texsize + vec2 (0.0,-texoffsety));    TEXCOLOR[8] = texture2d (Colorattachtex, Gl_fragcoord.xy/texsize + vec2 (texoffsetx,-texoffsety));    Prevett operator float prewitt_x[9];    Prewitt_x[0] =-1.0;    PREWITT_X[1] = 0.0;    PREWITT_X[2] = 1.0;    PREWITT_X[3] =-1.0;    PREWITT_X[4] = 0.0;    PREWITT_X[5] = 1.0;    PREWITT_X[6] =-1.0;    PREWITT_X[7] = 0.0;    PREWITT_X[8] = 1.0;    float prewitt_y[9];    Prewitt_y[0] = 1.0;    PREWITT_Y[1] = 1.0;    PREWITT_Y[2] = 1.0;    PREWITT_Y[3] = 0.0;    PREWITT_Y[4] = 0.0;    PREWITT_Y[5] = 0.0;    PREWITT_Y[6] =-1.0;    PREWITT_Y[7] =-1.0;    PREWITT_Y[8] =-1.0;    Convolution operation Vec4 Edgex = VEC4 (0.0);    VEC4 Edgey = vec4 (0.0); for (int i = 0; i < 9; ++i) {Edgex + = Texcolor[i] * Prewitt_x[i];    Edgey + = texcolor[i] * Prewitt_y[i];    } vec4 Edgecolor = sqrt ((Edgex * Edgex) + (Edgey * edgey));    Float edgeintensity = Gray (Edgecolor);    Const FLOAT threshold = 0.05;    if (edgeintensity > Threshold) Gl_fragcolor = VEC4 (0.0, 0.0, 0.0, 1.0); else discard;}

Because the code is long, I've put it on GitHub. There is a need to go with friends and get the code from GitHub. Address: here

Study of Qt 3D (ix): Try another method of edge detection

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.