Game Development Reference
In-Depth Information
16.5 Operators
HLSL supports many familiar C++ operators. With a few exceptions
noted below, they are used exactly the same way as they are in C++.
The following table lists the HLSL operators:
[]
.
><
=
=
!=
==
!
&&
||
?:
+
=-
-=*
*=
/
/=
%
%=
++
- -
=() ,
Although the operators' behavior is very similar to C++, there are
some differences. First of all, the modulus % operator works on both
integer and floating-point types. In order to use the modulus operator,
both the left side value and right side value must have the same sign
(e.g., both sides must be positive or negative).
Secondly, observe that many of the HLSL operations work on a per
component basis. This is due to the fact that vectors and matrices are
built into the language and these types consist of several components.
By having the operations work on a component level, operations such
as vector/matrix addition, vector/matrix subtraction, and vector/matrix
equality tests can be done using the same operators that we use for
scalar types. See the following examples.
Note: The operators behave as expected for scalars (that is, in the
usual C++ way).
vector u = {1.0f, 0.0f, -3.0f, 1.0f};
vector v = {-4.0f, 2.0f, 1.0f, 0.0f};
// adds corresponding components
vector sum=u+v;//sum=(-3.0f, 2.0f, -2.0f, 1.0f)
Incrementing a vector increments each component:
// before increment: sum = (-3.0f, 2.0f, -2.0f, 1.0f)
sum++; // after increment: sum = (-2.0f, 3.0f, -1.0f, 2.0f)
Multiplying vectors component-wise:
vector u = {1.0f, 0.0f, -3.0f, 1.0f};
vector v = {-4.0f, 2.0f, 1.0f, 0.0f};
// multiply corresponding components
vector sum=u*v;//product = (-4.0f, 0.0f, -3.0f, 0.0f)
Comparison operators are also done per component and return a vector
or matrix where each component is of type bool . The resulting
Search WWH ::




Custom Search