3D Programming: Chapter II A 3d/math Primer

Source: Internet
Author: User
Tags cos scalar square root

ab= (ax *bx) + (ay *by) + (AZ *bz)

This produces a scalar value, because some dot product is also known as scalar product (or inner product). According to the definition of vector length, we can compute the length of vectors by using the square root of the vector and its own dot product. In geometric mathematics, the dot product represents the angle between two vectors. The formula is: a b = | | a| | *|| b| | * COS (theta)

Θ is the angle between vectors a and B. If both vectors A and B have been normalized, the dot product can be reduced to:

A b = cos (theta)

According to this formula, we can sum up the following points: if a b > 0, the angle between the two vectors is less than 90 degrees. If a B < 0, the angle between the two vectors is greater than 90 degrees. If a b = 0, the two vectors are perpendicular to each other. In the following chapters, you will find that dot product has a wide variety of applications in computer graphics. For example, in the light calculation, the point product can be used to calculate the angle between the irradiated surface and the light source. In the second part, "Shader authoring with HLSL" will be explained in detail.

Cross Product (cross product)

Cross product is another very useful vector operation. The cross product of two vectors gets a third vector that is orthogonal to both vectors. The formula for the cross product is:

AXB = (ay * bz–az * by, AZ * Bx–ax * BZ, AX * by–ay * BX)

A cross product can be used to compute a triangle vector (that is, the direction of a triangle).

Matrix

An MXN matrix is a two-dimensional array of m rows and N columns. such as a 4x1 matrix, there are 4 rows of 1 columns, and a 2x3 matrix has two rows of 3 columns, as follows:

In Matrix C, the subscript for each element in the matrix is pointed out. Only a single row or column matrix is sometimes called a row vector and a column vector. A matrix with the same number of rows and columns is called a phalanx. In 3D graphics, 4x4 matrices are the most widely used.

Basic Matrix Operations

You can perform some basic arithmetic operations using matrices. In a vector, the addition and subtraction of two vectors is performed on an individual element. Because some two matrices need to have the same number of rows and columns to do addition and subtraction. Scalar multiplication is also a one-element operation, multiplying the scalar with each element in the matrix. However, the multiplication of matrices is somewhat different.

Matrix multiplication

For matrix multiplication, a matrix with n columns can only be multiplied by a matrix with n rows. In the resulting matrix, each element is computed by dot product for the row of the first matrix and the column corresponding to the second matrix. As shown in the following formula, if you use each row of matrix A as a row vector and each column of matrix B as a column vector, you can define the product matrix element as:

Figure 2.3 illustrates this process.

Figure 2.3 Matrix multiplication.

As shown below:

Matrix A is a two-row 3-column matrix, and Matrix B is a 3-row two-column matrix. The number of rows in matrix B is the same as the number of columns A, so matrix multiplication can be done. Multiplies the result of a 2x3 matrix, calculated as follows:

It should be noted that matrix multiplication is not satisfied with the Exchange law (cannot exchange multiplication, and can not get the same product). In fact, it is generally defined that matrix multiplication a * B is not defined for the case of B * A, such as a 3x3 matrix multiplied by a 2x3 matrix.

Transposition

The transpose of a matrix is obtained by reversing the elements of the matrix by the main diagonal. It is easy to understand a phalanx, as described in the following example:

Another way to transpose a matrix is to exchange the rows and columns of the matrix. This is easy to understand, especially for line vectors and column vectors, or for non squares. For example:

Row-major and Column-major Order

When working with matrices, you must consider how to store matrices in computer memory. Direct3D stores matrices in order of row precedence, that is, if stored in a single connection memory, they are arranged in memory in a row. This is also the storage format for multidimensional arrays in C language. The following example is a 2x3 matrix that connects storage in row precedence:

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.