Фундаментальные понятия WebGL

Installing Transformation Matrices and the Camera – The Media Image

Writer: Evgeny Ryzhkov Date of publication: 26.03.2013

On this tutorial, we’ll proceed to study OpenGL. On this lesson you will study: what variables exist for establishing digital camera matrices; what means are there to set the coordinates and parameters of the digital camera; what are the shader constants for and the best way to set them; and what the transformation matrix and the perspective matrix are wanted for.

Kinds of knowledge for storing digital camera settings

The aim of the present lesson is to put in a digital camera in our scene. Camera in video games is used in all places. The effectiveness of the recreation or software is determined by the right set up of the digital camera, as well as you’ll be able to install several cameras and change between them. Also, the digital camera may be tied to the foremost character or car.

There are two varieties of digital camera views: non-directional,

And target, directional digital camera. In an effort to set up a digital camera, usually speaking, it’s essential to know its coordinates, course, and viewing angle. Thus, for any of your cameras, you possibly can outline an object that shops this knowledge:

class CameraDirectional

        glm :: vec3 pos;
        glm :: vec3 path;
        float FOV;

Such a digital camera will probably be referred to as undirected, since there isn’t any particular level at which it appears. As a path, you’ll be able to outline any normalized vector. By multiplying the path vector on one or more rotation matrices, you possibly can rotate the digital camera. The benefit of a non-directional digital camera is that it isn’t needed for it to have some extent to which it seems – and always preserving monitor of which specific point the digital camera is pointing to and what this level is tied to could be very problematic.

If you recognize precisely or approximately the level at which the digital camera is directed, then you’ll be able to decide one other sort of digital camera, this can be a directional digital camera:

class CameraTarget

        glm :: vec3 pos;
        glm :: vec3 goal;
        float FOV;

OpenGL knowledge varieties

In the earlier lesson, we checked out numerous mathematical operations and representations of numbers. Let's digress from the digital camera installation a bit and think about what knowledge varieties shall be needed in OpenGL 4.x:

  • To store scalar values ​​- floating level numbers, the float knowledge sort is used.
  • To store three-component vectors, the knowledge sort is glm :: vec3
  • Glm :: vec4 is used to retailer shade info.
  • Matrices in OpenGL are described by the knowledge sort glm :: mat4

Other OpenGL knowledge varieties additionally exist, however they’re much much less well-liked.

Varieties of knowledge for storing digital camera settings

Let's go back to the digital camera view. So, the digital camera could be non-directional and directional. Nevertheless, if there is a set of primitives, and in the scene and they have to be rotated at the proper angle with respect to the digital camera, then the coordinate of the digital camera itself won’t give that a lot. You’ll be able to see this for your self by making an attempt to outline a three-dimensional object and then rotate it at the right angle to the digital camera. You just remember to want numerous calculations so that each one the vertices of the unique object stand on the display as if they have been seen from the digital camera. Nevertheless, there’s a approach to more simply perform all transformations on vertices. Suppose we’ve got a set of vertices:

struct vertex

        glm :: vec3 coord;

To implement the vital transformations for the digital camera, it is enough to set two matrices:

glm :: mat4 mView;
glm :: mat4 mProjection;

After which multiply the coordinate of each of the vertices by both matrices:

FromViewVertices[].coord = Vertices[].coord * mView * mProjection;

You see that the conversion you need is extremely simple. Thus, you’ll be able to store the digital camera in any format, for instance, the one proven above:

class CameraTarget

        glm :: vec3 pos;
        glm :: vec3 target;
        float FOV;

But, earlier than utilizing in your software, it is essential to convert digital camera knowledge from this format to matrices.

Setting the digital camera matrix and projective matrix in OpenGL

So let's proceed. Prior to installing a CameraTarget digital camera, you’ll need to convert its coordinates to OpenGL matrixes, for this, the following perform exists in OpenGL 4.x:

glm :: mat4 glm :: lookAt (glm :: vec3 Eye, glm :: vec3 At, glm :: vec3 Up);

The output knowledge of the perform shall be the matrix. Incoming parameters decide the position and path of the digital camera, which is appropriate for our object of sort CameraTarget.

Thus, as a way to convert an object from the CameraTarget into the matrices we’d like, do the following. First, fill in the CameraTarget with any knowledge:

CameraTarget MyCamera;
MyCamera.pos = glm :: vec3 (0,1.0f, -2.0f);
MyCamera.goal = glm :: vec3 (0,1.0f, zero);
MyCamera.FOV = glm :: 90.0f;
glm :: vec3 MyUpVector = glm :: vec3 (zero,1.0f, 0);

Observe that together with the activity of setting the digital camera coordinates, we also set the FOV parameter. This fieldofview, that’s, the viewing angle. Just the digital camera can narrow-angle and wide-angle – at your selection. It seems that in OpenGL, you can even change lenses! Let's set in the type of international variables of a matrix

glm :: mat4 mWorld;
glm :: mat4 mView;
glm :: mat4 mProjection;

Now we’ll set the essential matrixes of the digital camera and projections:

mWorld = glm :: translation (1.0f, 1.0f, 1.0f);
mView = glm :: lookAt (MyCamera.pos, MyCamera.goal, MyUpVector);
mProjection = glm :: perspective (MyCamera.FOV, 1.6f.0f, 0.1f, 100.0f);

Notice that although we would have liked two matrices, we installed all three of the required OpenGL matrices. This is carried out with a purpose to merely fill in the vital mWorld matrix. We’ve got crammed mWorld with a unit matrix, which suggests there are not any pre-transformations, akin to scaling and shifting.

Setting digital camera matrices, transforms and projective matrices as shader constants

You completely keep in mind that the show of any objects is carried out in OpenGL 4.x via shaders. Thus, earlier than outputting our objects saved in the vertex and index buffers, it’s necessary to set constants for the vertex shader. The mandatory constants are the matrices of the digital camera, projections and transformations.

There are several features for setting any values ​​of the shader constants in OpenGL. All of those features begin with glUniform token. Subsequent in the identify of the perform is the knowledge sort, since you’ll be able to set values ​​for the scalar, for the vector, you may as well set the constants of the entire matrix. Before you set the value of a continuing, you must get the ID of this fixed. Thus, we first get hold of the identifiers for the constants of the shader matrices:

int iWorld = glGetUniformLocation (spMain.pID (), “mWorld”);
int iView = glGetUniformLocation (spMain.pID (), “mView”);
int iProjection = glGetUniformLocation (spMain.pID (), “mProjection”);

Now every thing is able to set our matrix variables as shader constants.

glUniformMatrix4fv (iWorld, 1, GL_FALSE, glm :: value_ptr (mWorld));
glUniformMatrix4fv (iView, 1, GL_FALSE, glm :: value_ptr (mView));
glUniformMatrix4fv (iProjection, 1, GL_FALSE, glm :: value_ptr (mProjection));

After the matrices are put in, we will instantly launch the render of our primitive, that’s, the contents of the generalized buffer of vertices and indices with the assist of a code acquainted to us.

Helpful stuff: transformation matrix

So, we installed the matrices for the digital camera, but the question stays unclear why the mWorld variable was wanted, which was crammed with the id matrix. This can be a transformation matrix. In reality, this variable might be assigned a worth aside from one. And this can be a helpful matrix, because earlier than displaying an object we will appropriately place it in area.

As a rule, all objects loaded into the recreation are created in 3d editors in order that their middle is at the origin. There are two ways to locate the middle of objects: right in the middle of the object or under and in the middle. The second technique is appropriate for objects situated on the landscape, because it is enough to point out the peak at which the object might be situated and it should fit exactly in order that it touches its backside half to the panorama surface.

With the mWorld transformation matrices, we will transfer objects anyplace earlier than rendering them. In this case, we will first rotate the object round its axis at the desired angle and additionally improve or lower the object. Typically, objects “come” from a 3d editor in such a means that they have to be custom-made in measurement. In rare instances, the editor can accurately guess and choose the desired worth of the created object, since the measurement methods can all the time be totally different.

Pattern software

Now install the digital camera immediately in the software. Now we are accustomed to the technical part of the query, and proceed to the implementation. First, we’ll decide what end result we need to obtain. In our case, suppose that in the middle of coordinates is the planet around which our digital camera rotates. As an alternative of a circular orbit, we’ll choose an elliptical one, since on this case we will monitor promising transformations, because the digital camera will in some instances strategy the middle of coordinates, and the object, and in others transfer away from it.