A Crash Course in 3D Graphics Math (vomiting blood of 3D graphics in math)

Monday to Friday, a day, 7 am Beijing time on time updates -

First, we do not pretend here that we will cover everything that is important for you to know (First of all, we would not be here to cover all of the things that are important to you). In fact, we will not even try to cover everything you should know (in fact, we will not even cover the things you should know). in this chapter, we are just going to cover what you really need to know (in this section, we introduce only those you need to know something). If you're already a math whiz, you should skip immediately to the section ahead on the standard 3D transformations (if you have to learn math well, then you should skip this paragraph). Not only do you already know what we are about to cover, but most math fans will be somewhat offended that we did not give sufficient space to their favorite feature of homogeneous coordinate spaces (not just because you already know what we want to introduce, and for many mathematics spree, they would not even think of this paragraph ass did not say).Imagine one of those reality TV shows where you must escape a virtual swamp filled with crocodiles (Imagine arts channel in those from the virtual swamp full of crocodiles escape in a game (this may be seen what the author's own variety show, France g, who can imagine ah)). How much 3D math do you really need to know to survive (to survive, how much 3D math need it?)? That's what the next two sections are going to be about-3D math survival skills (the next 2 small segment is about how to survive the 3D mathematics). the crocodiles do not care if you really know what a homogeneous coordinate space is (if you really know homogeneous coordinates, then not mention crocodiles, every minute can be suspended or beaten in front of his girlfriend's face it eat its fleshy)

Vectors, or Which Way Is Which? (Vector or what is what?)

The main input to OpenGL is the vertex, which has a number of attributes that normally include a position (input data OpenGL is a vertex, it usually will include the location of this property). Basically, this is a position in xyz coordinate space, and a given position in space is defined by exactly one and only one unique xyz triplet (basically this position in a given space only exists). an xyz triplet, however, can be represented by a vector (in fact, for the mathematically pure of heart, a position is actually a vector too-there, we threw you a bone) (a xyz location can use a vector to represent). a vector is perhaps the single most important foundational concept to understand when it comes to manipulating 3D geometry (3D geometry when Men began to talk when the vector is perhaps the single most important thing a) Those three values ​​(x, y, and z) combined represent two important values:. a direction and a magnitude (the three vectors capable of expressing two very important things: party And the length)

Figure 4.1 shows a point in space (picked arbitrarily) and an arrow drawn from the origin of the coordinate system to that point in space (Figure 4.1 shows a point in space and a emitted from the coordinate origin, directed to the point an arrow). the point can be thought of as a vertex when you are stitching together triangles, but the arrow can be thought of as a vector (this point can be treated as you go to piece together data that triangle, however, this arrow can be seen as a vector). a vector is first, and most simply, from the origin toward a point in space first simple expression of a direction (vector pointing from the origin point in a direction). We use vectors all the time in OpenGL to represent directional quantities (in OpenGL we always used to describe the direction which the vector data). For example, the x axis is the vector (1, 0, 0) (x-axis, such a vector is (1,0,0 )). This says to go positive one unit in the x direction, and zero in the y and z directions (meaning that points in the x-axis positive direction of a single And the distance y, z directions where the distance 0 units). A vector is also how we point to where we are going-for example, which way is the camera pointing,
A Crash Course in 3D Graphics Math (vomiting blood of 3D graphics in math)
The vector is so fundamental to the operation of OpenGL that vectors of various sizes are first-class types in GLSL and are given names such as vec3 and vec4 (representing three- and four-element vectors, respectively) (vector are many operations in OpenGL basis, depending on the number of vector data is divided into N-dimensional vector, such vec4, vec3 this way). the second quantity a vector can represent is the magnitude (vector capable of expressing what is the length of the second). the magnitude of a vector is the length of the vector (magnitude vector length is the amount of points, so we can literally grow before a degree in foreign countries stay there knows this basic concept). For our x-axis vector (1, 0, 0), the length of the vector is 1 (x-axis for our vector, its length is 1). a vector with a length of 1 we call a unit vector (a vector length of only 1 unit vector called ). If a vector is not a unit vector and we want to scale it to make it one, we call that normalization (if a length of the vector is not 1, but we To its length becomes 1, then, this process is called unitization).

Vectors (and matrices) are such important concepts in 3D graphics that they are firstclass citizens in GLSL-the language in which you write your shaders (vector and matrix is ​​so important that when you write shader, they are supported GLSL native). However, this is not so in languages ​​like C ++ (However, C ++ does not support this cargo). to allow you to use them in your C ++ programs, the vmath library, which is provided with this book's source code, contains classes that can represent vectors and matrices that are named similarly to their GLSL counterparts (in order to allow you can also use the C ++ program, wrote vmath library will provide this part of the capabilities, its name and use plagiarism GLSL the inside). For instance, vmath :: vec3 can represent a three-component floating-point vector (x, y, z), vmath :: vec4 can represent a four-component floating-point vector (x, y, z, w), and so on (e.g., vmath :: vec3 vector on three floating-point numbers can be expressed, vmath :: vec4 capable of expressing four floating point numbers Vector, etc.). The w coordinate is added to make the vector homogeneous but is typically set to 1.0 (w component into a vector such that the vector odd, the value is substantially 1.0 that guy). The x, y, and z values ​​might later be divided by w, which, when it is 1.0, essentially leaves the xyz values ​​alone (values ​​of x, y, z may be in addition to w in the back when it is time to 1, the value xyz will remain unchanged). the classes in vmath are actually templated classes with type definitions to represent common types such as single- and doubleprecision floating-point values, and signed- and unsigned-integer variables. (vmath is a template class, feel free to use it, if the principle is not clear template the academy can watch the Eastern Han C ++ Tricks course, as long as 9 8, catch up with old man is being held in the College countryside, support the new socialist countryside construction, we solemnly promise, buy one get one! buy a course to send a strong girl, 9 8 can not buy a disadvantage, not buy fooled 9 8, 9 8 What you can not buy, ha ha ha ha. but to be serious, especially the students need to write core engine Let you stand in-depth look at doing C ++ code in the end, after reading how to use someone else's C ++ template library is very helpful) vmath :: vec3 and vmath :: vec4 are defined simply as follows (vmath: : vec3 and vmath :: vec4 defined below) which, when it is 1.0, essentially leaves the xyz values ​​alone (values ​​of x, y, z may be in addition to w in the back when it is time to 1, the value xyz will remain unchanged). The classes in vmath are actually templated classes with type definitions to represent common types such as single- and doubleprecision floating-point values, and signed- and unsigned-integer variables. (vmath is a template class, feel free to use it, if you do not know the principles of the template can be viewed Eastern College C ++ Tricks course, as long as 9 8, catch up with old man is being held in the College countryside, support the new socialist countryside construction, we solemnly promise, buy one get one! buy a course to send a strong girl, 9 8 buy disadvantage, not buy fooled 9 8, 9 8 What you can not buy, ha ha ha ha. but to be serious, especially the students need to write core engine, so you stand in-depth look at the C ++ code in the end why, after reading about how others use the C ++ template library is very helpful.) vmath :: vec3 and vmath :: vec4 are defined simply as follows (vmath :: vec3 and vmath :: vec4 of Defined below) which, when it is 1.0, essentially leaves the xyz values ​​alone (values ​​of x, y, z may be in addition to w in the back when it is time to 1, the value xyz will remain unchanged). The classes in vmath are actually templated classes with type definitions to represent common types such as single- and doubleprecision floating-point values, and signed- and unsigned-integer variables. (vmath is a template class, feel free to use it, if you do not know the principles of the template can be viewed Eastern College C ++ Tricks course, as long as 9 8, catch up with old man is being held in the College countryside, support the new socialist countryside construction, we solemnly promise, buy one get one! buy a course to send a strong girl, 9 8 buy disadvantage, not buy fooled 9 8, 9 8 What you can not buy, ha ha ha ha. but to be serious, especially the students need to write core engine, so you stand in-depth look at the C ++ code in the end why, after reading about how others use the C ++ template library is very helpful.) vmath :: vec3 and vmath :: vec4 are defined simply as follows (vmath :: vec3 and vmath :: vec4 of Defined below)

typedef Tvec3 vec3;
typedef Tvec4 vec4;

Declaring a three-component vector is as simple as (an example of a three-dimensional vector as stated below)

vmath::vec3 vVector;

If you include using namespace vmath; in your source code, you can even write (if you vmath directly to all of the namespace into your project, you can even use this as vmath below)

vec3 vVector;

However, in these examples, we'll always qualify our use of the vmath library by explicitly using the vmath :: namespace (However, in these examples, we will still be displayed vmath to use this namespace). All of the vmath classes define a number of constructors and copy operators, which means you can declare and initialize vectors as follows :( vmath all the class defines a number of copy constructors and operators, that is to say, it goes like this can you to use the library)

vec3 vmath::vVertex1(0.0f, 0.0f, 1.0f);
vec4 vmath::vVertex2 = vec4(1.0f, 0.0f, 1.0f, 1.0f);
vec4 vmath::vVertex3(vVertex1, 1.0f);

Now, an array of three-component vertices, such as for a triangle, can be declared as (a three-dimensional point may be defined as follows codes similar :)

vec3 vmath::vVerts[] = { vmath::vec3(-0.5f, 0.0f, 0.0f),
    vmath::vec3(0.5f, 0.0f, 0.0f),
    vmath::vec3(0.0f, 0.5f, 0.0f) } ;

This should look similar to the code that we introduced in the "Drawing Our First Triangle" section in Chapter 2 (this should follow that code long like when we draw a triangle in Chapter 2, anyway, I had forgotten the first two chapters that code long-sawed a). the vmath library also includes lots of math-related functions and overrides most operators on its class to allow vectors and matrices to be added, subtracted, multiplied, transposed, and so on (vmath libraries contains many relevant mathematical function and with a lot of operator overloading, so that those vectors and matrices can be carried out the basic arithmetic operations)

We need to be careful here not to gloss over that fourth w component too much (we need to note that, do not be attracted by birds w vector). Most of the time when you specify geometry with vertex positions, a three-component vertex is all you want to store and send to OpenGL (in most cases, you need to express a point of a geometric figure, when three-dimensional vector is enough). For many directional vectors, such as a surface normal (a vector pointing perpendicular to a surface that is used for lighting calculations), a three-component vector suffices (direction vector for many, such as normal, it is enough for the three-dimensional vector). However, we will soon delve into the world of matrices, and to transform a 3D vertex, you must multiply it by a 4 × 4 transformation matrix (of course, we will soon enter the world of the matrix, and we need to use a 4x4 matrix of 3D point to operate). the rules are you must multiply a four-component vector by a 4 × 4 matrix (It should be noted that you have to use a Dimensional vector multiplication operations to a 4x4 matrix); if you try and use a three-component vector with a 4 × 4 matrix,

Translations of this day to get here, see you tomorrow, bye ~

Get the latest plot first time, please pay attention to the Eastern Han Dynasty academy and the heart of the public graphic No.

Han College, waiting for you to play Oh

Guess you like

Origin blog.51cto.com/battlefire/2424705