Camera definition
In the game, the camera is used to show the scene to the user, and the camera is like a camera, and the scene in the camcorder is the camera's display range, as shown in:
In 3D space, the camera is defined as a position with a unit "direction" vector and an "up" unit vector, and the direction and upward vectors tell OpenGL how the current camera is directed.
viewing body
Another important feature of the camera is the visual flat-cut (View frustrum), which can be seen in the picture above with a cut-off of the top corner cone, which is a visual cut-off body, so the visual level of the inside of the object can be seen on the screen, the visual level is determined by six sections, near, Far, left, right, top, bottom. The near section also has a special property: You can think of it as the plane in the photo that the camera shines on. The process of converting from 3D to 2D, called projection, usually has two projections: orthographic projection (orthographic projection) and perspective projection (Perspective projection, center projection), orthogonal projections are often used in 2D graphics, Objects are always the same size on the screen, regardless of the object's distance from the camera. Perspective projection is the imaging principle in the real world: the farther away the object is from the eye, the smaller the object. Different types of projection is to change the shape of the visual body, perspective projection of the shape of the vision of the shape of the cone, the orthogonal projection of the visual cut-off is a box, the projection process is simple: for each point on the object, calculate whether the connection between the camera and the near section intersect. An example of a picture with orthographic projection and perspective projection:
As a result, it can be seen that in perspective projection, the farther on the screen the smaller the object, while the orthogonal projection, the object remains the same size. OpenGL generally uses 3D space display, whether you are drawing or output string, when you use orthogonal projection, you can assume that the z axis does not exist, that is, a 2D sprite in 3D space appearance:
Therefore, it is possible to ignore the difference between the 3D and the above, which is an orthogonal visual body that uses SpriteBatch drawing without any matrix
All we do is let our elves move around the X/y plane, ignoring the z-axis, and keeping the illusion that we've been working in 2D space.
Perspective projection uses two properties to define its projection rules: wide angle and aspect ratio (aspect ratio), wide angle is an angular value that defines how much "openings" the field of view is, such as
The aspect ratio is the width and height ratio of the viewport. The viewport is a rectangle, where the camera is used to render the image, a 480*320 pixel window whose aspect ratio is 480/320.
Camera related classes
Camera: base class
Orthographiccamera: Orthogonal camera inherits from camera
Perspectivecamera: Projector camera inherits in camera
First look at the camera class
Java code
- Public Abstract class Camera {
- public final Vector3 position = new Vector3 ();
- public final Vector3 direction = new Vector3 (0, 0,-1);
- public final Vector3 up = new Vector3 (0, 1, 0);
The three public members are the camera's position, orientation, and up vectors, with default properties, so the camera is at the origin by default, and the z axis is up by default.
Java code
- Public final Matrix4 projection = new Matrix4 ();
- Public final MATRIX4 view = new Matrix4 ();
- Public final MATRIX4 combined = new Matrix4 ();
- Public final Matrix4 invprojectionview = new Matrix4 ();
This is a series of matrices in OpenGL ES 2.0, the first two are projection and model view matrix, the third is the first two, the fourth is the third flip, often used to do extraction, these are not often used to
Java code
- Public Float near = 1;
- Public Float far = 100;
- Public Float viewportwidth = 0;
- Public Float viewportheight = 0;
This is the distance between the near and far profile distances from the camera, as well as the width and height of the viewport, and near and far must follow the 0<=near<far formula. By default, the near section is 1 units away from the camera, and for orthographic projections, close is often set to 0,width and height to calculate the aspect ratio of the perspective projection or the scene body box of an orthographic projection.
Java code
- Public final Frustum Frustum = new Frustum ();
The final member is frustum, which consists of 6 planes, frustum can be used for cropping: detects if an object is inside the frustum, and does not draw it if it is not. Frustum has some methods for detecting whether a box, a sphere, or a point is in frustum.
Java code
- Public abstract void Update ();
- Public void Apply (GL10 gl);
This is the two common method that the Update method uses to recalculate the camera's proof, which is called when the camera's properties change. The Apply method is used to set the gl_projection or Gl_modelview matrix of the camera, but this method is not available on OpenGL ES 2.0.
Java code
- Public void Unproject (Vector3 VEC);
- Public Void Project (Vector3 VEC);
- Public Ray Getpickray (float x, float y);
Finally there are several advanced methods you may use. The Unproject method is used to get a point from the screen coordinates and produce the three-bit coordinates of the point. The same as the Orthographiccamera.screentoworld () of the original class, also equivalent to the Gluunproject method. The x and y coordinates are tactile coordinates, the z coordinate of the parameter is a value between 0 and 1, 0 indicates that the point is in the near plane, and 1 indicates that the point is in the far plane;
The project method is exactly the opposite, taking a point from the 3D world and converting it to a 2d point on the screen;
The Getpickray method returns a line that can be imagined as a straight line from the camera position to X, y in the three-dimensional world, to detect if the line is in contact with other shapes or objects.
Java code
- Public class Orthographiccamera extends Camera {
- public Float zoom = 1;
- Public Orthographiccamera (float viewportwidth, float viewportheight);
Orthographiccamera has an additional member, zoom, to define the zoom factor. The construction parameters define the camera's viewport width and height, and if you want to make the best use of machine pixels, just define graphics.getwidth ()/getheight () here, or use different units, such as centimeters.
Java code
- Public class Perspectivecamera extends Camera {
- public Float Fieldofview = 67;
- Public Perspectivecamera (float fieldofview, float viewportwidth, float viewportheight);
Perspectivecamera also has an extra member, wide angle.
After translating the article, the understanding of the camera went to the next level.
The LIBGDX Learning Camera