Simple description of Shadow2dprojext functions in GLSL in iOS platform and small attention points of transformation matrices

Source: Internet
Author: User

I. Shadow2dprojext function needs to pass in a depth texture and coordinates of a point (4 dimension)

1. The first thing to note here is that this texture must be sampler2dshadow using the sampler type, and the direct use of sampler2d is not possible, and there will be an error.

2. Depth texture must be set to the correct format gl_depth_component, in addition to specify the corresponding comparison function, can be gltexparameteri to set the Gl_texture_compare_func_ext to Gl_lequal,gl_ Texture_compare_mode_ext to Gl_compare_ref_to_texture_ext

3. The incoming coordinates are 4 dimensions (x,y,z,w), which is done internally by the Shadow2dprojext function

Shadow2dprojext (Depthtexture, Shadowcoord); in fact, in two steps, the first step is to take out the true texture coordinates that SHADOWCOORD.XY/ The corresponding depth value is taken out of the corresponding position in the SHADOWCOORD.W coexisting depthtexture, which is equivalent to performing float depth = texture2d (DEPTHTEXTURE,SHADOWCOORD.XY/SHADOWCOORD.W). The second step is to compare the depth value in the texture to the depth value in the coordinate the depth value in the texture is depth, and the depth value in the coordinates is calculated by: float depth1 = SHADOWCOORD.Z/SHADOWCOORD.W, then depth Compare with Depth1.

4. The return value problem, the return value of this function is 0 or 1, if depth < Depth1 returns 0 means that the depth test failed, and vice versa 1 indicates a depth test pass.


Second, the above calculation has a point to note, is the third step, in the texture coordinate calculation and the coordinates of the depth value of the calculation should be aware that Shadowcoord is converted coordinates, here the meaning of the conversion must be Shadowcoord x, Y, z from [ -1,1] to [ 0,1], so it is usually implemented directly in front of the MVP matrix by the previous offset matrix,

Bias = 0.5, 0.0, 0.0, 0.5

0.0,0.5, 0.0,0.5

0.0, 0.0,0.5,0.5

0.0, 0.0,0.0,1.0

This bias * (X,Y,Z,W), and then after the fourth component is removed (x + 1) *0.5, (y+1) *0.5, (z+1) *0.5, so that the conversion is achieved.

However, since the OpenGL matrix is column-first, we need to transpose the matrix when we are setting up the matrix.

Bias =

0.5, 0.0, 0.0, 0.0

0.0,0.5, 0.0,0.0

0.0, 0.0,0.5,0.0

0.5, 0.5,0.5,1.0

This will be OK.


In the usual process we do not need to consider the problem of the main sequence of the column, because we calculate the matrix (such as through LookAt, or GLM related functions obtained by the matrix) and shader in the required format is consistent, so we can use the incoming, There is only one case where we need to consider the column main or row main order relationship of the Matrix, that is, the GLSL matrix into a custom memory block is, when the matrix is passed to the uniform block Chinese, you need to consider this problem, for example, above, we wrote a matrix stored in a custom memory block, So when we pass it to the shader, we're going to have to transpose the columns so we can use the matrix correctly.

Simple description of Shadow2dprojext functions in GLSL in iOS platform and small attention points of transformation matrices

Related Article

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.