These Android 3D Game Development articles are originally written by Martin, a German, at droidnova.com. lixinso is translated into Chinese.
The sixth part of this series focuses on how to create the right view, Because 3D makes no sense without the right view.
Before starting, we need to discuss the two views provided by OpenGL: Orthogonal and projection.
Orthogonal orthographic (projection without disappearing points)
The orthogonal view cannot see whether an object is far away from itself or in front of us. Why? Because it does not shrink according to the distance. So if you draw a fixed-size object in front of the viewpoint and draw an object of the same size in front of the first object, you cannot say that the object is the first object. Because both are of the same size, the root distance is irrelevant. They will not contract with the distance.
Perspective perspective (with scatter projection)
The perspective view is the same as the view we see from the eyes. For example, a tall person standing in front of you looks very tall. If this person is standing 100 metres away, he is not even as big as your thumb. He seems to narrow down with distance, but we all know that it is still tall. This effect is called perspective. The two objects mentioned in the above example will show a smaller area, so we can distinguish which is the object that is close to us and that is the object that is far away from us.
Because my examples may confuse you. I recommend this blog post again: iPhone development: OpenGL ES from the ground up, Part 3: viewports in perspective. Here we use or track as an example.
The first view we want to create is Yunna orthographic. This creation process only takes one time, that is, every time the surface is created. So we need to change the code. Some methods in ondrawframe () will be transferred to the onsurfacecreated () method. In this way, they will only be executed when the program starts or rotates.
You can see that the glclear () and glloadidentity () methods are not moved from ondrawframe () to onsurfacecreate. The reason is simple: they will be drawn at each frame.
Because we need to use the screen size to calculate the screen ratio, we introduced two object variables: _ width and _ height. We need to set it in the onsurfacechanged () method and call it each time the rotation changes.
Now we have everything we need to start a viewpoint. We need to change the onsurfacechanged () method.
Wow, now there are a lot of new code, don't be afraid, let's step by step.
In the third row, we can see glmatrixmode (), use gl10.gl _ rpojection as the parameter, and we can see this method again in row 8th, but use gl10.gl _ modelview as the variable. The reason for this is 3-8 lines of intermediate code. In these rows, 4-7 sets our viewpoint, so we set our projection. In line 9-17, we set our model environment. In this context, the two calls use different parameters. TIPS: You can often try to delete some code lines to view the results. This makes it easy to understand which lines of code are useful.
The fourth line calculates the same screen ratio. In this line (6 rows), we set our viewpoint to be orthographic view. These parameters are set for the boundary. The order is as follows: Left, right, bottom, top, znear, zfar.
In row 3, we set the viewpoint. We know the usage of this method because we have used it in onsurfacechanged.
In line 3, we switched matrixmode to gl10.gl _ modelview and set OpenGL to accept the call to change the model drawing method.
In row 9th, we call glenable () and use the gl10.gl _ depth_test parameter. This causes OpenGL ES to check the Z-Order of the object. If we do not enable it, we will see that the last drawn object is always at the beginning. This means that we can still see this object in time even if it should have been covered by a closer and larger object.
We have already introduced other code lines in the previous sections.
The perspective view is the same, except that the parameters of the glfrustumf (). glfrustumf () function are slightly different from those of glorthof. Because we didn't narrow down the object, but the cone we defined will be cut in a funnel shape. Take a look at this picture to see the difference between glorthof () and glfrustumf.
Orthogonal orthographic:
Perspective perspective:
Return to our code:
Information: Remember to calculate the variable size (five rows). We will see why it can be used when we discuss the matrix.
In row 3, we use glfrustumf () to replace glorthof (). This is all the changes we need to make before switching between orthographic view and perspective view.
However, hey, we can see the result of the last item. OK. Let's change the ondrawframe () method.
OK. What have we changed?
Row 3: we modified the parameter to ensure that the depth buffer will be cleared.
In row 3, we start a loop to create 10 objects.
In row 3, we can see glloadidentity (). Now it resets the matrix here. This is required because we need to use glrotatef () and gltranslatef () to modify our objects. However, to ensure that we only modify the objects currently in the loop, we call glloadindentify (). Therefore, we reset every call to gltranslatef () and glrotatef () for previous objects.
On line 11, we can see the new gltranslatef () method, which moves our object to another position. In this example, we do not modify the position on the X axis. But we modify-1.0f on the Y axis, which means it is close to the low end of our screen. As you can see from the final calculation, the position of the Z axis is modified to indicate the object's God-to-god. The first object is in-2.5f, the second is in-4.0f, and so on. So we placed 10 objects in the center of the screen.
If you call glfrustumf (), you can see this result:
If you switch between glfrustumf () and glorthof (), you will see this result:
Hey, wait. Why do you only see one object? Because we do not have any projection in Orthogonal situations. So every single object has the same size, so there is no scaling, so there is no disappearing point, so in fact all objects are behind the first object.
Source code: vortex Part VI