Transforming a vertex shader, why do we use 4-dimensional vectors?

From this site: http://www.toymaker.info/Games/html/vertex_shaders.html

We have the following code snippet:

// transformations provided by the app, constant Uniform data
float4x4 matWorldViewProj: WORLDVIEWPROJECTION;

// the format of our vertex data
struct VS_OUTPUT
{
  float4 Pos  : POSITION;
};

// Simple Vertex Shader - carry out transformation
VS_OUTPUT VS(float4 Pos  : POSITION)
{
  VS_OUTPUT Out = (VS_OUTPUT)0;
  Out.Pos = mul(Pos,matWorldViewProj);
  return Out;
}

My question is: why does the VS_OUTPUT structure have a 4-dimensional vector as its position? Is this not just the position of x, y and z?

+3
source share
3 answers

Because you need the w coordinate to calculate the perspective. After the output from the vertex shader, than DirectX performs the separation of perspectives by dividing by w.

, 32768, -32768, 32768, 65536 , w 0.5, -0.5, 0.5, 1. w , . , 2D-.

: , , , .

, D3DXMatrixPerspectiveLH

2*zn/w  0       0              0
0       2*zn/h  0              0
0       0       zf/(zf-zn)     1
0       0       zn*zf/(zn-zf)  0

x, y, z, 1 ( w 1) ,

x' = ((2*zn/w) * x) + (0 * y) + (0 * z) + (0 * w)
y' = (0 * x) + ((2*zn/h) * y) + (0 * z) + (0 * w)
z' = (0 * x) + (0 * y) + ((zf/(zf-zn)) * z) + ((zn*zf/(zn-zf)) * w)
w' = (0 * x) + (0 * y) + (1 * z) + (0 * w)

, w z . w z, . z .

, , (2, 1, 5, 1), zn (Z-Near) 1 a zf (Z-Far 10) aw () 1 ah () 1.

,

x' = (((2 * 1)/1) * 2
y' = (((2 * 1)/1) * 1
z' = ((10/(10-1)  * 5 + ((10 * 1/(1-10)) * 1)
w' = 5

x' = 4
y' = 2
z' = 4.4
w' = 5

x'' = 0.8
y'' = 0.4
z'' = 0.88
w'' = 1

. , x y -1 1, z 0 1. , .

, , | x '| | y '| | z '| | w '| z ' 0, . .

, , : D

Edit2: , ROW. .

+9

. "" , 4 x 3:

rx1 rx2 rx3 tx1
ry1 ry2 ry3 ty1
rz1 rz2 rz3 tz1

, , , ( ). ( ):

0   0   0   1

( ).

, "1" , .

+4

- , , . , 2 , (, , , ).

, , . ( ), , , .

, (w), w, , 2 - .

. http://www.opengl.org/resources/faq/technical/depthbuffer.htm#0060 http://en.wikipedia.org/wiki/Transformation_matrix#Perspective_projection.

, , , . , , ...

, , , 4x4 , , . 3x3 . , . - , . , -, , w- . , w, .

+1
source

Source: https://habr.com/ru/post/1720754/


All Articles