Android opengles2.0 isosceles right triangle and color triangle (III)

In the last blog, we have drawn a right triangle. Although we set the two waist of the right triangle to be equal relative to the coordinates, it is not shown in fact. Although through calculation, we can calculate the proportion of the two waist of the triangle to make them unequal in the coordinates, they are equal in reality, But when the graphics are more complex, the workload is too huge for us. So what do we do? The answer is to use the transformation matrix and give the calculation to OpenGL.

matrix

In mathematics, a matrix is a set of complex or real numbers arranged according to a rectangular array, which was first formed by the coefficients and constants of the system of equations. This concept was first put forward by Kelly, an English mathematician in the 19th century. Matrix is often used in image processing, game development, geometric optics, linear combination of quantum states, electronics and other fields. We now use matrices in the field of image processing or game development. In the advanced mathematics courses in the University, some people learned matrices. When many people studied advanced mathematics, linear algebra and other courses in the University, they always felt that learning these things was useless (me too). In fact, these are very important for programmers, especially those who need to do game development and image and video processing. It's biased... In 3D graphics, the 4th order matrix is generally used. In DirectX, row vectors are used, such as [xyzw], so when multiplying with the matrix, the vector is in the front and the matrix is in the back. Column vectors, such as [xyzx] t, are used in OpenGL, so when multiplying with the matrix, the matrix is in the front and the vector is in the back. The specific knowledge of matrix is not explained in detail in the blog. Students who need to know can consult it by themselves. If you want to write the transformed matrix yourself, and then hand over the matrix to opengl for processing, it is also a troublesome thing. What should you do? At this time, the camera and projection are needed to generate the required matrix.

Camera and projection

camera

According to our experience in real life, we guide that for a scene, the pictures taken are different with the different position and attitude of the camera. Map the camera to the OpenGL world and determine the camera shooting results (that is, the results displayed on the last screen), including the camera position, camera viewing direction and camera up direction.

In the Android OpenGLES program, we can set the camera through the following methods:

Projection

The 3D world seen by the camera needs to be presented to a 2D plane, which is projection. In Android OpenGL es2.0 (I) -- Understanding OpenGL es2.0 also mentioned about projection. In the world of Android OpenGLES, there are two kinds of projection, one is orthogonal projection and the other is perspective projection.

Using orthogonal projection, the size of an object does not change with its distance from the viewpoint. In the Android OpenGLES program, we can use the following methods to set the orthogonal projection:

With perspective projection, the farther an object is from the viewpoint, the smaller it appears. The closer you are to the point of view, the greater the.. In the Android OpenGLES program, we can use the following methods to set the perspective projection:

Use transformation matrix

In fact, camera settings and projection settings are not real settings, but through setting parameters, we can get a transformation matrix of vertex coordinates after using the camera and the transformation matrix of vertex coordinates under projection. We also need to transfer the matrix to the vertex shader, and multiply the matrix by the vector of coordinates in the vertex shader to get the actually displayed coordinate vector. Note that it is the matrix multiplied by the coordinate vector, not the coordinate vector multiplied by the matrix, and the matrix multiplication does not satisfy the commutative law. Through the above camera settings and projection settings, we get two matrices. For convenience, we need to multiply the camera matrix and projection matrix to get an actual transformation matrix, and then pass it to the vertex shader. Matrix multiplication:

Realization of isosceles right triangle

Based on the previous blog, we need to do the following steps to draw an isosceles right triangle:

1. Modify vertex shader and add matrix transformation:

2. Set the camera and projection, obtain the camera matrix and projection matrix, and then multiply the camera matrix and projection matrix to obtain the actual transformation matrix:

3. Pass transformation matrix into vertex shader:

Run to get an isosceles right triangle:

Colored triangle

The old display of a white triangle is too monotonous. We need to make this triangle color. What should I do? Android opengles2.0 (I) - understand that as mentioned in opengles2.0, vertex shaders determine vertex positions and execute once for each vertex. The slice shader is for the slice color and is executed once for each slice. In our slice shader, we directly assign a color to the slice, and we only pass in a color value externally. To make the triangle appear as color, we need to assign different colors to different slices. In order to simplify the processing, we modified the vertex shader in the last instance of isosceles triangle to keep the slice shader unchanged, so as to render the triangle in color:

You can see that we added an acolor (vertex color) as the input and passed it to vcolor. Vcolor is preceded by a varying. Attributes such as attribute, uniform and varying are used to represent qualifiers in the Shader Language of OpenGL. Attribute is generally used for quantities different from each vertex. Uniform is generally used for the same amount of vertices in a 3D object composed of the same set of vertices. Varying is generally used for the amount passed in from vertex shaders to slice shaders. There is also a const for constants. The shader language will be introduced separately in subsequent blogs. Then we need to pass in three different vertex colors to the vertex shader:

Run to get a colored isosceles triangle:

Source code

All the code is in one project and hosted on GitHub - the demo of Android OpenGLES 2.0 series blog

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>