Direct3d improvement: HLSL programming achieves Photoshop filter effect-relief

Source: Internet
Author: User
"Embossed" image effect refers to the foreground convex background of the image. It is common in the carving of some monuments. Implementation Relief is actually very simple. Calculate the difference between a pixel in the image and a pixel in the upper left corner, and add a gray level. The gray level indicates the background color. Set this interpolation to 128 (the RGB value of the image is 0-255 ). At the same time, the difference between the two colors should also be converted to the brightness information, otherwise the embossed image will appear color J

When using HLSL to handle relief effects, pay attention to the two issues.

One of them is the problem of image boundary.ImplementationWe all know that when processing the border pixel, the upper left corner of the pixel may not be obtained. In this case, special processing should be performed. Generally, the relief result of the boundary position is set to the background color. However, when using HLSL, you do not need to perform special processing on the boundary of the image in the HLSL shader, but need to set the filter for the texture. This filter can be set to the clamp mode.

The second problem to be addressed is that the texture sampling coordinate in pixelshader is 0-1.0. If you want to get the size of the texture image on the top left, in this way, the offset of a pixel can be converted to a value ranging from 0. Assume that the texture size is [W, H] and the current texture coordinate is [U, V]. Then, the texture coordinate of the pixel in the upper left corner of the image is [U-1.0/W, v-1.0/h]. The size of the texture image cannot be known in rendermonkey. If it is fully implemented using VC ++, you can load the image or3DGet the texture size in the texture9 object and set it to HLSL as a constant. Or it is acceptable to assume that the texture size is 1024x1024.

Now we will show the HLSL for relief effect.Code:

Click to view the chart

 


Source image


Embossed Image

Readers should find that the code in the Code HLSL of C ++ is very clean, with no branches or loops. The most important thing is that it is very fast. For a 2048x2048 image, it can achieve a real-time processing capability of> 30fps without consuming a lot of CPU time.

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.