OpenGL Learning Path (ii)

Source: Internet
Author: User

1 Intro

In the last reading note, we analyzed the examples given in the book. The first is to build a frame, then populate the initialization function, provide the vertex information (buffer object) and vertex attribute information (vertex array object) to OpenGL in the initialization function, and enable the vertex array object, and finally fill the drawing function, first empty the color cache, Then call Gldrawarray to draw the basic graphic. The coordinates used in the example are two-dimensional coordinates, so the drawing is a two-dimensional graphic (here is two triangles), and we know that OpenGL is mainly used for three-dimensional graphics rendering, so it is necessary to learn OpenGL related API before the three-dimensional transformation to do a brief introduction. In fact, this part should belong to Red Book in the fifth chapter of the content, here we will be ahead of it, in the reading notes (two) on the introduction- This is our three-dimensional rendering of the most basic knowledge points, but also the most critical knowledge points, understanding also has a certain degree of difficulty . This reading note mainly describes the translation, rotation, scaling transformation of the transformation matrix, projection transformation will be in the next reading notes to record. The reading notes are mainly their own understanding of some mathematical concepts and records, for reference only, if there are different understandings, we can discuss together ha!

2 points, coordinate systems and vectors

Before discussing three-dimensional transformations, you need to understand the basic mathematical concepts of points, vectors, and coordinate systems. This part of the content may be more abstract, the following record is my understanding of these concepts.

2.1 Relative relativity of the position

In daily life, when we describe a strange place to others, we usually choose a place he is familiar with as a reference point. For example: we introduce to foreigners in Hebei an ancient city of Handan, foreigners know Beijing, we will say that Handan in Beijing to go to the southwest 300km; if the foreigner know Shijiazhuang, then we can also tell him, Handan in Shijiazhuang South Walk 100km. This shows that the position is a relative concept, to describe a position, the first to select a reference point, the selection of reference points is arbitrary, the selected reference points are different, the location of the description is different.

In geometry, the position is described by the concept of "point", that is, the point is a quantity that has no size but position. Describing a point and describing a position is one thing, just now that the position is a relative concept, so the reference point should be used first. Let's take the simplest one-dimensional axis as an example to illustrate the location of the description point, as shown in:

For the same point on the Axis $a$, to describe the location of the $a$ point, the first to select any reference point, if the selected reference point is $o_1$, then the $a$ point on the $o_1$ point to the right of $l_1$ place, if the selected reference point is $o_2$, $a$ Point in $o_2$ point to the left $l_2$ place. By the number of axes and reference points, we will express the geometric points on the axis with abstract numbers.

2.2 Coordinate systems and vectors

As can be seen from the figure, the point on the axis can only be changed along the axis direction, that is, it is one-dimensional. If the point changes in a plane or a space, then the axis of the tool is not described. In this case, two-dimensional coordinate system and three-dimensional coordinate system are introduced to describe the point. Before introducing the coordinate system, first introduce the concept of vectors.

When we were 88 years old, we were already exposed to vectors--both size and direction, represented by a directed segment . to be blunt, a vector defines a direction, a length, and a unit length . As in, $O _1a$ and $ao_2$ are two vectors, the size is $l_1$ and $l_2$, the direction is horizontal to the right.

parallelogram principle to decompose any one vector. Parallelogram principle is derived from the decomposition and synthesis of force in physics, which is then introduced into mathematics to describe the decomposition and synthesis of vectors. so-called parallelogram principle, which means that any planar vector can be represented by a non-collinear two vectors . So, in the plane countless vectors can be used Two a vector that is not collinear. The mathematical structure consisting of these two vectors and their public starting point is a two-dimensional coordinate system, which can describe any point on the two-dimensional plane in the coordinate system, and can also describe arbitrary vectors on the two-dimensional plane, which are the two vectors of the linear algebra. base vector . We know that in mathematics, vectors are positional independent (that is, free vectors), so long as they are equal in size, the two vectors with the same direction are the same vector (this is not the same as the force in physics). So to describe the points in the two-dimensional space, we need a reference point, so we define the common starting point of the two vectors as the reference point--that we know coordinates origin . Axis vectors and coordinate origin form a coordinate system that can be used to describe any vector and any point in the coordinate system.

Span style= "Background-color: #00ff00;" > any one-dimensional vector is represented as Three non-coplanar three-dimensional vector (base vector) To indicate that the common starting point for the three vectors moved together is defined as the coordinate origin of the three-dimensional coordinate system. The two-and three-dimensional Cartesian coordinate system is the two-and three-dimensional coordinate system of the base vector, and is the most widely used coordinate system, also known as plane Cartesian coordinate system and space right-angled coordinate system .

Let's take a look at how vectors are represented. Also in our mengmengdongdong of youth, we already know that vectors have two representations: the first is symbolic notation, such as $\mathbf{a}, \mathbf{b}$, and so on, and the other is the coordinate notation , The coordinate representation is described in more detail here. As has been said, any two-dimensional vector can be represented by two non-collinear vectors, assuming that two base vectors are $\mathbf{i}$ and $\mathbf{j}$, and the length is 1. $\mathbf{a}=x_a\mathbf{i} + y_a\mathbf{j}$ for any vector, so that the vector $\mathbf{a}$ can be represented by an ordered pair of $ (x_a, y_a) $, which is the coordinate representation of the vector . The coordinate representation of three-dimensional and even $n$-dimensional vectors is the same. Here, the blogger still wants to emphasize that the coordinates of the vector are not the projection of the vector on the axis, only the Cartesian coordinates are the projections of the vectors on the base vector . Therefore, in the normal coordinate system, the coordinates of a vector are not very good, but in the Cartesian coordinate system, it becomes very good to seek-projection, which is the application of Cartesian coordinate system so extensive reason. Let's take a look at what is the projection, in fact, in mathematics has already come into contact with, as shown:

Assuming that $c$ is a projection of the vector $\mathbf{a}$ on the vector $\mathbf{b}$, then:

\begin{equation} c = \mathbf{a} \cos<\mathbf{a}, \mathbf{b}>\end{equation}

Therefore, in a two-dimensional Cartesian coordinate system, if the two-dimensional vector $\mathbf{a}$ length is $l$, the vector and the $x$ axis and the $y$ axis are $\alpha$ and $\beta$ respectively, then we can easily get the coordinates of the vector is expressed as $\MATHBF = (l\cos\ Alpha, L\cos\beta) ^\text{t}$; Similarly, for three-dimensional space vector $\mathbf{b}$, its length is $l$, and the $x$ axis, the $y $ axis and the $z$ axis are $\alpha$, $\beta$ and $\gamma$, respectively, The coordinates are expressed as $\mathbf{b}= (L\cos\alpha, L\cos\beta, L\cos\gamma) ^\text{t}$.

2.3-point representation

Just now we have defined the coordinate system-the origin of the coordinates and the three non-coplanar vectors, and any vector in the three-dimensional space can be represented by these three vectors alone, but we don't have a point about how the coordinate system is defined. In the three-dimensional Cartesian coordinate system, the coordinates origin is $o$, the three base vectors are $\mathbf{i}$,$\mathbf{j}$,$\mathbf{k}$ respectively, we require the coordinates of the $p$ point, then

$$\vec{op} = x_{1}\mathbf{i} + y_{1}\mathbf{j} + z_{1}\mathbf{k}$$

Thus, the point $p$ can be expressed as

$ $P = x_{1}\mathbf{i} + y_{1}\mathbf{j} + z_{1}\mathbf{k} + \mathbf{o}$$

so, to represent a three-dimensional point, you can use four-dimensional coordinates, for example, just $p$ can be expressed as $p = (\BEGIN{ARRAY}{CCCC} x_1 & Y_1 & z_1 & 1\end{array}) $, This is the homogeneous coordinates. For vertices, the homogeneous coordinates are the true representation. The vectors can be represented as $\mathbf{v} = (\BEGIN{ARRAY}{CCCC} x_1 & Y_1 & z_1 & 0\end{array}) $.

3 linear transformation and homogeneous coordinates3.1 Overview

The concept of linear transformations in algebra is abstract, involving concepts such as vector space and linear mapping, where there is not much explanation, as follows to understand can be Niang or Bing. To a popular point of interpretation, three-dimensional linear transformation is the point/vector coordinate value to do an operation, so that its coordinate value changes, which is reflected in the geometry is the shape of the geometry has been changed. In computer graphics, linear transformations generally refer to translation, rotation, scaling, projection (orthographic projection and perspective projection) and the synthesis of these basic transformations. From what we have just described, we know some information: the points or vectors in the geometry are determined by four coordinate values, and the coordinate values are determined by the coordinate system, and the coordinate system is composed of three non-coplanar vectors and coordinate origin. That is, for the same point, in a different coordinate system, the coordinate value of the description is not the same, and the transformation is to establish the relationship between the two different descriptions-so in the past we called the coordinate transformation . For example: in the coordinate system $\mathbf{o}_1-\mathbf{i}_1\mathbf{j}_1\mathbf{k}_1$ coordinate system, a point can be described as $p$ points can be used with four Yuan zu $ (x_1, y_1, Z_1, o_1) $ description,

\begin{equation}\label{p2}p = \BEGIN{ARRAY}{CCCC} (x_1 & Y_1 & z_1 & O_1) \end{array}\left (\begin{array}{c}\ mathbf{i}_1 \ \mathbf{j}_1 \ \mathbf{k}_1 \ \mathbf{o}_1\end{array}\right) = (\BEGIN{ARRAY}{CCCC} \mathbf{i}_1 & \ Mathbf{j}_1 & \mathbf{k}_1 & \mathbf{o_1}\end{array}) \left (\begin{array}{c}x_1 \ y_1 \ z_1 \ o_1 \end{array}\r ight) \end{equation}

In another coordinate system is $\mathbf{o}_2-\mathbf{i}_2\mathbf{j}_2\mathbf{k}_2$, it can be described with another ordered tuple set to $ (x_2, y_2, Z_2, O_2) $

\begin{equation}\label{p1}p = \BEGIN{ARRAY}{CCCC} (x_2 & y_2 & z_2 & o_2) \end{array}\left (\begin{array}{c}\ mathbf{i}_2 \ \mathbf{j}_2 \ \mathbf{k}_2 \ \mathbf{o}_2\end{array}\right) = (\BEGIN{ARRAY}{CCCC} \mathbf{i}_2 & \ Mathbf{j}_2 & \mathbf{k}_2 & \mathbf{o_2}\end{array}) \left (\begin{array}{c}x_2 \ y_2 \ z_2 \ O_2 \end{array}\r ight) \end{equation}

So how to establish a link between $ (\REF{P1}) $ and $ (\REF{P2}) $? Or what we said before, any three-dimensional vector can be represented by three non-coplanar vectors, so $\mathbf{i}_2, \mathbf{j}_2, \mathbf{k}_2$ can be used $\mathbf{i}_1, \mathbf{j}_1, \ mathbf{k}_1$ Linear table out:

$$\mathbf{i}_2 = t_{11} \mathbf{i}_1 + t_{21} \mathbf{j}_1 + t_{31} \mathbf{k}_1 + 0 \mathbf{o}_1$$

$$\mathbf{j}_2 = t_{12} \mathbf{i}_1 + t_{22} \mathbf{j}_1 + t_{32} \mathbf{k}_1 + 0 \mathbf{o}_1$$

$$\mathbf{k}_2 = t_{13} \mathbf{i}_1 + t_{23} \mathbf{j}_1 + t_{33} \mathbf{k}_1 + 0 \mathbf{o}_1$$

$$\mathbf{o}_2 = t_{14} \mathbf{i}_1 + t_{24} \mathbf{j}_1 + t_{34} \mathbf{k}_1 + t_{44} \mathbf{o}_1$$

That

$$ (\begin{array}{cccc}\mathbf{i}_2 & \mathbf{j}_2 & \mathbf{k}_2 & \mathbf{o}_2 \end{array}) = (\begin{ Array}{cccc}\mathbf{i}_1 & \mathbf{j}_1 & \mathbf{k}_1 & \mathbf{o}_1 \end{array}) \left (\begin{array}{ CCCC}T_{11} & T_{12} & T_{13} & T_{14} \\t_{21} & T_{22} & T_{23} & T_{24}\\t_{31} & T_{32} &A mp T_{33} & t_{34}\\0 & 0 &0& t_{44}\end{array}\right) $$

So we can write the \end{array transform from $ (\begin{array}{cccc}x_1 & y_1 & z_1 & O_1 ^{\text{t}}$}) to $ (\begin{array}{cccc}x_2 & Y_2 & z_2 & O_2 \end{array}) The transformation expression for ^{\text{t}}$ is:

$$\left (\begin{array}{c}x_2 \ y_2 \ z_2 \ o_2 \end{array}\right) = (\begin{array}{cccc}\mathbf{i}_1 & \mathbf{j}_1 & \mathbf{k}_1 & \mathbf{o}_1 \end{array}) \left (\begin{array}{cccc}t_{11} & T_{12} & T_{13} & T_{14} \\T_{21} & T_{22} & T_{23} & T_{24}\\t_{31} & T_{32} & t_{33} & t_{34}\\0.0 & 0.0 &0.0&am P T_{44}\end{array}\right) \left (\begin{array}{c}x_1 \ y_1 \ z_1 \ o_1 \end{array}\right) $$

Which, will

$ $T =\left (\begin{array}{cccc}t_{11} & T_{12} & T_{13} & T_{14} \\t_{21} & T_{22} & T_{23} & T_{24} \\T_{31} & T_{32} & t_{33} & t_{34}\\0.0 & 0.0 &0.0& t_{44}\end{array}\right) $$

is called a coordinate transformation matrix. The next step is to explain how to find the basic coordinate transformation (affine transformation) matrix.

3.2 Scaling

Scaling should be the simplest of all linear transformations. Performs a scaling operation, such as scaling a vector to the original $s$ times, equivalent to the original point, $x $, $y $, $z $ three axes scaled to the original $s$ times. As described in 3.1, the transformation matrix for the scaling operation is:

$ $T _s = \left (\begin{array}{cccc}s & 0 & 0 & 0 \ 0 & S & 0 & 0 \ 0 & 0 & S & 0 \ 0 & 0 & 0 & 1\end{array}\right) $$

3.3 Panning

The so-called panning is that the three axes in the coordinate system remain unchanged, and the origin moves along the translation vector to the new position. Suppose that the translation vector is $v_p = (\begin{array}{cccc}x_p & y_p & z_p & 0 \end{array}) $ Similarly, depending on what you can get, the transformation matrix of the translation operation is:

$ $T _p = \left (\begin{array}{cccc}1 & 0 & 0 & x_p \ 0 & 1 & 0 & y_p \ 0 & 0 & 1 & z_p \ 0 & 0 & 0 & 1\end{array}\right) $$

3.4 Rotation

Finally, the most difficult rotation transformation matrix is deduced. Unlike the translation and rotation matrices, the rotation matrix is less intuitive. Here, let's take a concrete look at the derivation of the rotation matrix, which is the decomposition of the parallelogram principle of the three-times vector, as shown in the entire decomposition process:

Three breaks are represented by different colors, namely, red, light blue, and purple.

Known conditions: $\mathbf{i}$, $\mathbf{j}$, and $\mathbf{k}$ are the base vectors of the three-dimensional Cartesian coordinate system, the origin is $o$, the axis of rotation is $\mathbf{u}$, and is the unit vector, Vector $\mathbf{i '}$ for $x$ The base vector of the direction $\mathbf{i}$ around the axis of rotation $\mathbf{u}$ the new vector after rotating $\theta$-the base vector of the rotated coordinate system $x$ axis.

Our purpose: The vector $\mathbf{i '}$ is represented by the base vector $\mathbf{i}$, $\mathbf{j}$, and $\mathbf{k}$.

first step vector decomposition: $\mathbf{i '}$ is decomposed into a vector $\vec{oa}$ along the axis of rotation $\mathbf{u}$ and a vector $\mathbf{u}$ perpendicular to $\vec{ob}$:

\begin{equation}\label{341}\mathbf{i '} = \vec{oa} + \vec{ob}\end{equation}

And:

\begin{equation}\label{342}\vec{oa} = u_x \mathbf{u} = u_x^2\mathbf{i} + u_xu_y\mathbf{j} + u_xu_z\mathbf{k}\end{ Equation}

second-step vector decomposition: $\mathbf{i}$ is decomposed into vectors along the axis of rotation $\mathbf{u}$ $\vec{oa}$ and vectors perpendicular to $\mathbf{u}$ $\vec{oc}$, then

$$\mathbf{i} = \vec{oa} + \vec{oc}$$

And:

\BEGIN{EQUATION}\LABEL{344}\VEC{OC} = \mathbf{i}-u_x\mathbf{u} = (1-u_x^2) \mathbf{i}-u_xu_y\mathbf{j}-u_xu_z\ Mathbf{k} \end{equation}

third step vector decomposition: Establish the connection between $\vec{ob}$ and $\vec{oc}$, and decompose the vector $\vec{ob}$ into vectors $\vec{od}$ along the $\vec{oc}$ direction and vectors perpendicular to $\vec{ob}$ $\ vec{oe}$, then

\begin{equation}\label{345}\vec{ob} = \vec{od} + \vec{oe}\end{equation}

According to $\ref{344}$, you can get:

\begin{equation}\label{346}\vec{od} = |\vec{ob}|\cos\theta\frac{\vec{oc}}{|\vec{oc}|} = \cos\theta \vec{OC} = \cos\ Theta (\mathbf{i}-u_x\mathbf{u}) = (1-u_x^2) \cos\theta \mathbf{i}-U_xu_y\cos\theta \mathbf{j}-u_xu_z\cos\theta \ Mathbf{k} \end{equation}

In addition, notice that $\vec{oe}$ and $\vec{oc}$ are perpendicular, $\mathbf{u}$ is the axis of rotation, then $\mathbf{u}$ is perpendicular to the plane $oebd$, so $\mathbf{u}$ is perpendicular to $oe$, then $\vec{oe}$ in Vector $\ mathbf{u}$ and Vector $\vec{oc}$ on the cross-multiply vectors, assuming $\vec{of} = \mathbf{u} \times \vec{oc}$, so:

$$\vec{oe} = k\vec{of} = K\mathbf{u}\times \vec{oc}=k\mathbf{u}\times (\mathbf{i}-u_x\mathbf{u}) = k \mathbf{u}\times \m athbf{i}$$

So now we can find out the $k$, by the cross-multiplication definition: $|\vec{of}| = |\mathbf{u}| | \vec{oc}|sin (+) = |\vec{oc}| = |\vec{ob}|$, so: $k =\sin\theta$, and finally get

\begin{equation}\label{349}\vec{oe}=\sin\theta \mathbf{u}\times (\mathbf{i}-u_x\mathbf{u}) = \sin\theta \left (\ Begin{array} \mathbf{i} & \mathbf{j} & \mathbf{k} \ u_x & u_y & u_z \ 1 & 0 & 0 \end{array}\righ T) = u_z\sin\theta\mathbf{j}-u_y\sin\theta\mathbf{k}\end{equation}

Put $ (\ref{346}) $ and $ (\ref{349}) $ into $ (\ref{345}) $:

\begin{equation}\label{3410}\vec{ob} = (1-u_x^2) \cos\theta\mathbf{i}-(U_xu_y\cos\theta-u_z\sin\theta) \mathbf{j}- (U_xu_z\cos\theta + U_y\sin\theta) \mathbf{k} \end{equation}

Finally, put $ (\ref{342}) $ and $ (\ref{3410}) $ into $ (\ref{341}) $ available

$$\mathbf{i '} = (\cos\theta + u_x^2 (1-\cos\theta)) \mathbf{i} + (U_xu_y (1-cos\theta) + U_z\sin\theta) \mathbf{j} + (u_xu_z (1-\cos\theta)-U_y\sin\theta) \mathbf{k} + 0\mathbf{o}$$

The remaining two transformed base vectors $\mathbf{i '}$ and $\mathbf{j '}$ can also be represented by $\mathbf{i}$, $\mathbf{j}$, and $\mathbf{k}$, eventually getting a homogeneous rotation matrix of

$ $M _r = \left (\begin{array}{cccc}\cos\theta+u_x^2 (1-\cos\theta) & U_xu_y (1-\cos\theta)-u_z\sin\theta & U_xu_ Z (1-\cos\theta+u_y\sin\theta & 0 \ u_yu_x (1-\cos\theta) +u_z\sin\theta & \cos\theta+u_y^2 (1-\cos\theta) & U_yu_z (1-\cos\theta)-u_x\sin\theta & 0 \ u_zu_x (1-\cos\theta)-u_y\sin\theta & U_zu_y (1-\cos\theta) +u_x\sin\ Theta & \cos\theta+u_z^2 (1-\cos\theta) & 0 \ 0 & 0 & 0 & 1\end{array}\right) $$

4 Summary

Finally, in this blog post we describe the point and its relativity, then introduce the concept of vector, from the concept of parallelogram principle, then introduce the point in the coordinate system of the representation, and finally introduced the concept of coordinate transformation and transformation matrix, gave three basic transformations-translation transformation, The transformation matrix of the rotation transformation and the scaling transformation. The comprehensive application of these matrices constitutes a complex transformation in three-dimensional space, the three-dimensional transformation is the basis of three-dimensional graphics, but also a difficult to understand when learning OpenGL one of the points of knowledge.

OpenGL Learning Path (ii)

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.