Introduction to 3D game programming (8) Computer Graphics

Source: Internet
Author: User
Introduction to 3D game programming (8) Computer Graphics

(15:43:13)

ReprintedBytes
   
The year 07 has arrived, and the new year has not brought more joy. Only when the age is one year old, the pressure is heavier, therefore, it is even more important to accumulate knowledge. year 07, a year of double struggle.
  On the first morning of, I was awakened by my dream. In my dream, A friend gave me a comment on his blog and said that I didn't continue to write 3D game programming, and I had no credit. I was preparing to justify it, angry friends somehow hit me in the back of the head with a wooden stick. When I woke up, I found that my clothes were hanging on my face and I was overwhelmed. think over it. I believe there are not many people who want to read these things, but they do bring great benefits to my personal review, I remember a lot better, so I will continue. I believe that the strong sense of guilt in my dream is not unfounded.
  Taking advantage of the three days of the New Year's Day holiday, I will devote all my energy to the 3D review. I expect to release about five logs every day. I should be able to finish vertices, textures, and lights in 15 sections of the three days, related sections. I will review and explain the problem from various aspects.
  Well, start with the question    Computer Graphics
       
          Vector  Before learning about DirectX, we need to understand this part of knowledge.
  Computer Graphics is mainly used to study the principles, algorithms, and systems of input, representation, transformation, calculation, and output graphics using computers and their graphics devices.
  From the definition, we can see that our basic steps can be divided into five steps: Input Source graphics, structure representing graphical attributes, transformation matrices, calculation matrices, and output target graphics. The focus of our understanding is related computing principles, optimization of algorithms, and familiarity with the entire rendering system process.
  First, let's take a look at computer graphics devices. First, let's take a look at the figure below. First, we enter data from the peripheral devices. The system bus places this data in the system memory by category, or sent to the CPU, or to the video memory GPU, and then sent to the video controller based on the corresponding operations in the video card, the display will display it. The most important thing we need to pay attention to is how the data is stored in the CPU and GPU, and how it is output after computation.
  In 3D game programming, the first task of Graphic programming is to simulate various physical properties in the real world and give players a sense of reality. These attributes include the shape, texture, material, relative position between objects, occlusion relationship, and bone movement effect. I will also follow this process to review and describe.
 
          Computer Graphics Basics
 
  Let's get a general idea of what morphology needs to know. Next we will describe its geometric concepts and operations. This part requires a certain amount of space imagination and knowledge of linear algebra or ry. Of course, even if you do not understand these details, they are not as complicated as you think.
  First, let's take a look at vectors. In high school physics, we should know the difference between vectors. The former is a line segment without direction, and the latter is a line segment with direction. All our physical forces are described by vectors. Two major features of a vector are: length, and direction. When two vectors have the same direction and length in space, we think they are equal, even if the relative positions on the graph are different, because we don't care about its location relationship. Therefore, no matter how a vector is translated, we think it remains unchanged because its two attributes have not changed.
  However, when the end of the vector is the same as the origin of the coordinate system, the vector is in the standard position. It is different from the standard vector. The standard vector is defined as a standard unit vector with a length of 1.
  When we have obtained a vector with a standard position, we obtain the end vertex (, 0) of a vector. The vector is a line segment with a direction, the line segment can be determined by two points. We only need to obtain the vertex coordinates of the vector to get the length of the vector and its direction, therefore, in the two-dimensional world, we can use this pattern to determine a vector u = (UX, uy). Imagine that the tail vertex is (0, 0) and the header vertex is (UX, uy) is the vector uniquely identified? In the 3D coordinates, we just represent a vector n = (NX, NY, NZ ). Of course, it is confusing to write it like this. Is this a point or a vector? We must define the difference between the two. A point is a position in a coordinate system, and a vector has a length and a direction.
  Here we will mention the three-dimensional coordinate system. We usually use the left-hand Coordinate System in 3D game programming, that is, the left is the positive direction of the X axis and the Y axis is the positive direction, is the positive direction of the Z axis (that is, your screen is inside the positive direction, so the zcoordinate of our eyes has always been negative ). For your convenience, we will continue to provide the illustration below. Where the I, j, and K vectors are assumed to be 1 in length, they can be expressed as (, 0), (, 0), (, 1 ), because their length is 1, we can call them unit vectors, and because of their special direction, they stretch along the three axes. We also call them basic vectors.
  In the two-dimensional world, how do we calculate the length of a line segment? We assume that the line segment is composed of (0, 0) and (4, 5) points, and we want its length to be (4-0) square + (5-0) 2 times the root number. In 3D, if the tail vertex of the vector is the coordinate origin (, 0) and the header vertex is (, 6), the length is also (4-0) + (5-0) square + (6-0) Square, and then 2 times the root number. We call the length of a vector its modulo. As I said, no matter how the vector is translated in the coordinate system, its direction and length attributes remain unchanged. After all, we regard it as equal. At this time, we can translate any vector, and eventually get an equal vector with the tail vertex as the coordinate origin. Then we can obtain the model of the vector by obtaining its vertex coordinates. For example, a vector whose tail coordinates are (, 3) and whose top coordinates are (, 6) is converted into an ending coordinate, 0), the coordinates of the header vertex are equal vectors (3, 3, 3) (you can think about whether the direction and length of the Vector have changed ). The result is 3 times the root number 3.
    After we obtain the modulo of a vector, we divide it by its modulo to obtain a unit vector with a 1-direction length unchanged (note, not a standard vector ). Take the vector (, 3) (, 6) as an example. After translation, the vector is n (, 3), and the modulo is 3 times the root number 3, divide Nx = 3, NY = 3, NZ = 3 by the modulo, and the unit vector is (root 3/3, root 3/3, root 3/3, whether the length is 1 and the direction is 1.
    It is very important to obtain the unit vector of a vector. If you have learned linear algebra, you can think about what happens when a matrix is multiplied by a unit matrix. I will detail the importance of these features later.
    A friend who has learned physical mechanics should think back: How do we calculate the force of an object when it is affected by two forces? The things involved here are complex and I will not explain them in detail. Let's give you a picture. You can refer to the memories. I will directly give the formula for adding and subtracting two vectors.
  U + V = (UX + VX, Uy + Vy, uz + VZ );
  U-V = (Ux-VX, UY-Vy, uz-VZ)
  Just as in the beginning, when we multiply a vector with a scalar (no direction length), the length of the vector will change, its direction does not change (if it is a negative number, it is reversed). We can also find the new vector. The formula is as follows:
  V = Ku = (kux, KUY, Kuz );
 
    After remembering and understanding the basic formula above, we can understand the point multiplication and cross multiplication of vectors.
    The point multiplication formula is S = U point multiplication v = u modulo multiplied by V modulo multiplied by CoS (angle between Uvs)
    In this way, a scalar s has no direction. However, based on the comparison between this value and 0, we can obtain the relationship between the two flags of U and V.
    If   = 0, then the vector U. V is equal.
    If   > 0, the angle between the vectors u and v is less than 90 degrees.
    If   <0, the angle between the vectors u and v is greater than 90 degrees.
    We can think about why.
   
    Cross-multiplication is very important when we set the normal. You should remember this very well.
    First, the cross multiplication formula is: S = U Cross multiplication v = [(uyvz-uzvy), (uzyx-uxvy), (uxvy-uyvx)];
    We can see that what we get by cross multiplication is no longer an undirected scalar value. It is a vector. So what is this vector. Let's see
    We should remember that the cross multiplication of two vectors produces a new vector perpendicular to the two vectors at the same time. Well, the specific truth is hard to explain. If you can't understand it, remember it.> _ <        Matrix    Well, friends who know programming should be clear about arrays and friends who have learned geometric linear algebra should be clear about matrices. In fact, I personally think there is no difference between the two, at least it is difficult to find the difference in d3d programming.
    Both records a series of numbers in an array of rectangular numbers in the N column of m rows. We often call M. N is the dimension of this matrix. When the dimensions of the two matrices are the same as the elements, we call them equal.
    When a matrix has only one single row or one single column, we call it a row vector or column vector. One of our 3D vector coordinates is a three-column row vector.
   
    The addition and subtraction of the matrix. First, we require that the dimensions of these two matrices be equal. Otherwise, we cannot perform this operation. The sum of the elements at the corresponding positions in the two matrices gives a new matrix.
    Multiplication of the number of matrices. A matrix is multiplied by a scalar number. All the elements in this matrix are multiplied by this number to obtain a new matrix, that is, the number multiplication of the matrix.
    Multiply the matrix. That is, multiply matrix A by matrix B. First, we require that the number of columns in matrix A and the number of rows in matrix B be equal, that is, the requirements for equality before and after the first row. From this point, we should understand that AB is not equal to Ba, and matrix multiplication pays attention to the order before and after it does not satisfy the multiplication exchange law. The formula here is not easy to write. I suggest you go to the Internet to find a simple linear algebra document.
    Unit matrix. We started to talk about three standard vectors and gave them to the graph. When we ordered the three vectors I, j, and K, it was a three-dimensional standard unit matrix. Any matrix multiplied by a matrix of units will not be changed. It is a special matrix with only the primary diagonal line being 1 and other elements being 0.
    Inverse Matrix. Only a square matrix with the same number of rows and columns has an inverse matrix, which swaps the positions of all elements in the matrix with the primary diagonal. We can understand that, replace the column and column subscript of the element to obtain the matrix. When a square matrix is multiplied by its inverse matrix, the result is a matrix of units.
    Transpose matrix. Unlike the inverse matrix, it applies to a matrix of any row and column, but it also exchanges the rows and columns of a matrix, an mxn matrix, and a transpose matrix is the nxm matrix.
    X-axis transformation of the matrix. We have said that there are requirements for Matrix Multiplication. It requires that the number of columns in the first matrix be equal to the number of rows in the second matrix. If we have a 2x2 matrix and a 3x3 matrix, we cannot perform multiplication directly. This requires us to change the number of columns in the 2x2 matrix to 3, and this matrix to a 2x3 matrix to be involved in the operation, in this case, we can add 0 for both elements in the newly added column to obtain a new 2x3 matrix, which is the homogeneous coordinate transformation of the matrix. It exists to facilitate matrix multiplication.  Graph Conversion
 
    After the vector matrix is completed, we can contact the game to illustrate the image transformation.
    Image transformations are generally divided into three types: rotation, translation, and scaling. In two-dimensional games, we do not need to use scaling because it does not have the concept of depth. Can we see if there are any characters in legend getting farther and farther? In 3D games, all three are deeply involved.
    Because a two-dimensional game is easier, let's take it as an example.
    First, we need to know that an image is originally composed of pixels on the screen, and a series of pixel points constitute a matrix. When we want to flip the image, for scaling and translation, You need to perform operations on its matrix points to obtain the changed image. This is the basic principle.
    In general, we multiply the matrix of the image by a transformation matrix to obtain the transformed image matrix.
    A two-dimensional transformation matrix is a matrix of three rows and three columns. Among them, A11, A12, A21, and A22 rotate, scale, and switch the image. A13 and A23 perform projection transformation on the image, which controls the generation of the destroy point. A31, A32 controls the image translation change, and A33 controls the image scaling. If you are interested, you can set a simple matrix group. Each point records three values, x, y, and the color of the point, multiply by different two-dimensional transformation matrices to obtain the new matrix to see the changes.
    A three-dimensional transformation matrix consists of a matrix of four rows and four columns.
    In this matrix, a41 controls the x-axis translation, a42 controls the y-axis translation, and A43 controls the z-axis translation. A11, 12,13, 22, 32, and 33 control the rotation axis and angle of the image. Zooming controls the zooming degrees of X, Y, and Z respectively by A11, A22, and A33.      How, whether it is dizzy? In fact, it is good to understand these basic things and there is no need to strictly memorize them. In the future, in-depth research will be truly useful. It is not a big problem if it is not digested at the moment. Don't give up. If you are not clear, leave a message to ask :)

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.