برمجة ال shader باستخدام ال CG الدرس الثالث

انواع المتغيرات

float - a 32bit floating point number
half - a 16bit floating point number
int - a 32bit integer
fixed - a 12bit fixed point number
bool - a boolean variable
sampler* - represent a texture object sampler2D sampler1D sampler3D

يمكنك استخدام جميع هذه الانواع ك vectors او matrix باستثناء *sampler

vectors

float2//float2.x,float2.y,float[0],float[1]
fixed3//fixed3.x,fixed3.y,fixed3.z,fixed[0],fixed[1],fixed[2]
int4//int4.x,int4.y,int4.z,int4.w,int4[0],int4[1],int4[2]int4[3]

metrices

float3x3 m = float3x3(
   1.1, 1.2, 1.3, // first row (not column as in GLSL!)
   2.1, 2.2, 2.3, // second row
   3.1, 3.2, 3.3  // third row
);
float3 row0 = float3(0.0, 1.0, 0.0);
float3 row1 = float3(1.0, 0.0, 0.0);
float3 row2 = float3(0.0, 0.0, 1.0);
float3x3 n = float3x3(row0, row1, row2); // sets rows of matrix n

 int2x2 mat;
 mat[0][0]=1;
 mat[0][1]=2;
 mat[1][0]=4;
 mat[1][1]=12;

العمليات الرياضية

*, /, +, -, =, *=, /=, +=, -=

كما هو معروف طريقة عملها مع القيم العادية (scalar value) فانها يمكن استخدامها مع المتجهات ايضا وكذلك المصفوفات

 

عمليات عالمتجهات

float3 a = float3(1.02.03.0);
float3 b = float3(0.10.20.3);
float3 c = a + b; // = float3(1.1, 2.2, 3.3)
float3 d = a * b; // = float3(0.1, 0.4, 0.9)
float3 e = a / b; // = float3(10.0, 10.0, 10.0)
float3 f = 4.0 * a// = float3(4.0, 8.0, 12.0)
float3 g = a / 10.0// = float3(0.1, 0.2, 0.3)
float3 h = 2.0 / a// = float3(2.0/1.0, 2.0/2.0, 2.0/3.0);

عمليات عالمصفوفات

float2x2 m1 = float2x2(1.0, 0.0, 0.0, 1.0); 
float s = 10.0; float2x2 
m2 = s * m1; // = float2x2(10.0, 0.0, 0.0, 10.0) 
float2x2 m3 = m1 * s; // = float2x2(10.0, 0.0, 0.0, 10.0)
float2x2 m4 = float2x2(1.0, 2.0, 0.0, 1.0);
float2x2 m5 = m1+m4; // = float2x2(2.0, 2.0, 1.0, 2.0);

لايمكنك ضرب مصفوفتان ببعضها باستخدام * بدلا عنها استخدم الدالة mul

 

الدوال الرياضية

Function

Description
abs( x ) Absolute value of x .
acos( x ) Arccosine of x in range [0, p], x in [–1, 1].
all( x ) Returns true if every component of x is not equal to 0.

Returns false otherwise.

any( x ) Returns true if any component of x is not equal to 0.

Returns false otherwise.

asin( x ) Arcsine of x in range [–p/2, p/2]; x should be in [–1, 1].
atan( x ) Arctangent of x in range [–p/2, p/2].
atan2( y , x ) Arctangent of y / x in range [–pp].
ceil( x ) Smallest integer not less than x .
clamp( x , a , b ) x clamped to the range [ a , b ] as follows:

  • Returns a if x is less than a .
  • Returns b if x is greater than b .
  • Returns x otherwise.
cos( x ) Cosine of x .
cosh( x ) Hyperbolic cosine of x .
cross( A , B ) Cross product of vectors A and B ;

A and B must be three-component vectors.

degrees( x ) Radian-to-degree conversion.
determinant( M ) Determinant of matrix M .
dot( A , B ) Dot product of vectors A and B .
exp( x ) Exponential function e x .
exp2( x ) Exponential function 2 x .
floor( x ) Largest integer not greater than x .
fmod( x , y ) Remainder of x / y , with the same sign as x .

If y is 0, the result is implementation-defined.

frac( x ) Fractional part of x .
frexp( x , out exp ) Splits x into a normalized fraction in the interval [½, 1), which is returned, and a power of 2, which is stored in exp .

If x is 0, both parts of the result are 0.

isfinite( x ) Returns true if x is finite.
isinf( x ) Returns true if x is infinite.
isnan( x ) Returns true if x is NaN (Not a Number).
ldexp( x , n ) x x 2 n .
lerp( a , b , f ) Linear interpolation:

(1 – f )* a + b * f

where a and b are matching vector or scalar types. f can be either a scalar or a vector of the same type as a and b .

lit( NdotL , NdotH , m) Computes lighting coefficients for ambient, diffuse, and specular light contributions.

Expects the NdotL parameter to contain N • L and the NdotH parameter to contain N • H .

Returns a four-component vector as follows:

  • The x component of the result vector contains the ambient coefficient, which is always 1.0.
  • The y component contains the diffuse coefficient, which is 0 if ( N • L ) < 0; otherwise ( N • L ).
  • The z component contains the specular coefficient, which is 0 if either ( N • L ) < 0 or ( N • H ) < 0; ( N • H ) m otherwise.
  • The w component is 1.0.

There is no vectorized version of this function.

log( x ) Natural logarithm ln( x ) x must be greater than 0.
log2( x ) Base 2 logarithm of x x must be greater than 0.
log10( x ) Base 10 logarithm of x x must be greater than 0.
max( a , b ) Maximum of a and b .
min( a , b ) Minimum of a and b .
modf( x , out ip ) Splits x into integral and fractional parts, each with the same sign as x .

Stores the integral part in ip and returns the fractional part.

mul( M , N ) Matrix product of matrix M and matrix N , as shown below:

304equ01.jpg

If M has size A x B , and N has size B x C , returns a matrix of size A x C .

mul( M , v ) Product of matrix M and column vector v , as shown below:

305equ01.jpg

If M is an A x B matrix and v is a B x 1 vector, returns an A x 1 vector.

mul( v , M ) Product of row vector v and matrix M , as shown below:

305equ02.jpg

If v is a 1 x A vector and M is an A x B matrix, returns a 1 x B vector.

noise( x ) Either a one-, two-, or three-dimensional noise function, depending on the type of its argument. The returned value is between 0 and 1, and is always the same for a given input value.
pow( x , y ) xy .
radians( x ) Degree-to-radian conversion.
round( x ) Closest integer to x .
rsqrt( x ) Reciprocal square root of x x must be greater than 0.
saturate( x ) Clamps x to the [0, 1] range.
sign( x ) 1 if x > 0; –1 if x < 0; 0 otherwise.
sin( x ) Sine of x .
sincos(float x , outs , out c ) s is set to the sine of x , and c is set to the cosine of x .

If both sin( x ) and cos( x ) are needed, this function is more efficient than calculating each individually.

sinh( x ) Hyperbolic sine of x .
smoothstep( min , max, x ) For values of x between min and max , returns a smoothly varying value that ranges from 0 at x = min to 1 at x = max .

x is clamped to the range [ min , max ] and then the interpolation formula is evaluated:

–2*(( x  min )/( max  min ))3 +

3*(( x  min )/( max  min ))2

step( a , x ) 0 if x a ;

1 if x >= a .

sqrt( x ) Square root of x ;

x must be greater than 0.

tan( x ) Tangent of x .
tanh( x ) Hyperbolic tangent of x .
transpose( M ) Matrix transpose of matrix M .

If M is an A x B matrix, the transpose of M is a B x A matrix whose first column is the first row of M , whose second column is the second row of M , whose third column is the third row of M , and so on