In the content of the previous section, the extension is made, and now the real 3D object is started, not the 2D object in the 3D world like the first two. We add a left side to the triangle, one right side, one back side to generate a pyramid (four-prism). Add the left, right, upper and lower and lower back and lower back and lower back and lower back and lower back. We mix colors on the pyramid, create a smooth colored object. For each side of the cube, there is a different color.
Procedure gldraw (); begin glclear (gl_color_buffer_bit or gl_depth_buffer_bit); // Clear the screen and depth cache GLLoadIdentity (); // Reset the current model observation matrix
Gltranslatef (-1.5, 0.0, -6.0); // Left shift 1.5 unit, and move into the screen 6.0
GLROTATEF (RTRI, 0.0, 1.0, 0.0); // Rotating triangle around Y
// The code below has no change. Draw a color gradient triangle on the left side of the screen and rotates Glbegin (GL_TRIANGLES) from left to the right around the Y-axis; // Draw a triangle
{
Some people may have already tried to create a 3D object on the code in the previous class. But someone often believes me: "How can my object not rotate around its own axis? It seems to be full of full screen." To let your object rotate around your own axis, you must let the center of the object The coordinates are always (0.0F, 0, 0F, 0, 0F). The following code creates a pyramid that rotates around the axis. The top of the pyramid is also a unit from the center, and the bottom is also a unit. The top spot is located at the center of the bottom surface. Note that all surface-triangles are drawn counterclockwise. This is important, I will explain in the future course. Now, you only need to understand that it is either counterclockwise, or you should happen clockwise, but never make two order, unless you have enough reasons to do this. We started painting the front side of the pyramid. Because all faces share the top point, we set this in all the triangles to red. The color of the two vertices on the bottom is mutually exclusive. The front side of the front side is green, and the right vertex is blue. In this way, the left vertices of the right side of the adjacent side are blue, the lower right vertices are green. The colors of the points on the bottom surface of the quadrangular surface are spaced apart.
}
Glcolor3f (1.0, 0.0, 0.0); // Red GlvertEX3F (0.0, 1.0, 0.0); // Triangular top point (front side) Glcolor3f (0.0, 1.0, 0.0); // Green GlvertEX3F (-1.0, -1.0 , 1.0); // Triangle's left vertices (front side) Glcolor3f (0.0, 0.0, 1.0); // Blue GlvertEX3F (1.0, -1.0, 1.0); // Triangular right lower vertices (front side)
{
Now draw the right side. Note that the X coordinates of the two vertices on the bottom side are located at a unit of the right side. The vertex is located at a single unit on the Y-axis, and the Z coordinate is at the two vertical Z coordinate center of the bottom side. The right side begins from the upper vertex to the bottom to the bottom. This time the left vertices are drawn with blue to keep the right bottom vertex with the front side. Blue will extends from the corner to the front side and right sides of the pyramid and mix with other colors. It should also be noted that the three sides and front sides are in the middle of the same Glbegin (GL_TRIANGLES) and GLEND () statements. Because we are constructed by triangles to construct this pyramid. OpenGL knows that every three points constitute a triangle. When it draws a triangle, if there is still the remaining point, it thinks that the new triangle is to begin to draw. OpenGL will not draw four points into a quadrilateral shape, but a new triangle begins. So don't intend to add any excess points inadvertently.
}
Glcolor3f (1.0, 0.0, 0.0); // Red Glvertex3f (0.0, 1.0, 0.0); // Triangular top point (right side) Glcolor3f (0.0, 0.0, 1.0); // Blue GlvertEX3F (1.0, - 1.0, 1.0); // Triangle's left vertices (right sides) Glcolor3f (0.0, 1.0, 0.0); // Green GlvertEX3F (1.0, -1.0, -1.0); // Triangle upper right vertices (right side ) {Now is the rear side. Switch again again. The upper left vertices are returned to green because the rear side is shared with the right side. }
Glcolor3f (1.0, 0.0, 0.0); // Red Glvertex3f (0.0, 1.0, 0.0); // Triangular top point (rear side) Glcolor3f (0.0, 1.0, 0.0); // Green GlvertEX3F (1.0, -1.0, -1.0); // Triangular left vertices (rear side) Glcolor3f (0.0, 0.0, 1.0); // Blue Glvertex3f (-1.0, -1.0, -1.0); // Triangle Right Count Point (rear side)
{Last painted left side. Also switch color. The lower left vertices are blue, the same as the top of the back side. The top right vertices are blue, the same as the top left vertices of the front side. I will finish the pyramid here.
Glcolor3f (1.0, 0.0, 0.0); // Red Glvertex3f (0.0, 1.0, 0.0); // Triangular top point (left side) Glcolor3f (0.0, 0.0, 1.0); // Blue GlvertEX3F (-1.0, -1.0, -1.0); // Triangular left vertices (left side) Glcolor3f (0.0, 1.0, 0.0); // Green GlvertEX3F (-1.0, -1.0, 1.0); // Triangle's right lower vertices (left Side) glend (); // Pyramid draw end
{Because the pyramid is rotated around the Y-axis, we will never see the bottom surface, so there is no need to add the bottom. You feel that there is experience, try to increase the bottom surface (square), and rotate the pyramid around the X-axis to see if you are constant. Make sure the colors of the four vertices of the bottom surface match the colors of the side. }
{Translator: This is my own bottom.}
Glbegin (GL_QUADS); // Draw the bottom Glcolor3f (0.6, 0.2, 2.0); // Set the current color to purple Glcolor3f (0.0, 1.0, 0.0); // Green GlvertEX3F (-1.0, -1.0, 1.0); // Glcolor3f (0.0, 0.0, 1.0); // Blue Glvertex3f (1.0, -1.0, 1.0); // On the right upper GLCOLOR3F (0.0, 1.0, 0.0); // Green Glvertex3f (1.0, -1.0, -1.0); // The lower left Glcolor3f (0.0, 0.0, 1.0); // Blue Glvertex3f (-1.0, -1.0, -1.0); // Right lower glend (); // Square Drawing End
GLLoadIdentity (); // Reset Model Observation Matrix GLTranslateF (1.5, 0.0, -6.0); // Right shift 1.5 unit, and move into the screen 6.0 GLROTATEF (RQUAD, 1.0, 1.0, 1.0); // Rotate the quadrilateral around the X-axis (New) {
Next, you start painting a cube. He consists of six quadrangular shapes. All quadrangles can be drawn in a counterclockwise. That is to say, first painting the upper right corner, then the upper left corner, the lower left corner, the lower right corner. You may think that this order seems to be clockwise, but don't forget that when we look back from the back of the cube, we think about it now. (Translator Note: You are observing cubes from the outside of the cube). Note that we will move the cubes away from the screen. Because the size of the cube is to be larger than the pyramid, the cube looks large when moving into 6 units. This is a perspective. The farther the object looks smaller :)
}
Glbegin (GL_QUADS); // Draw a square
{
Pursue the top of the cube. Snap from the center, note that the Y coordinates are always one unit, indicating that this quadrangular shape is parallel to the z-axis. Popularly paint the vertices, one unit in the right, and then the screen is one unit. Then the top left vertices, one unit in the left one, and then the screen. Then close to the left and right vertices of the observer. That is, a screen out of the screen.
} Glcolor3F (0.0, 1.0, 0.0); // Color change to blue glvertex3f (1.0, 1.0, -1.0); // quadrilateral upper right vertices (top) GlvertEX3F (-1.0, 1.0, -1.0); // The upper left top of the quadrilateral (top) Glvertex3f (-1.0, 1.0, 1.0); // quadrilateral left vertices (top) Glvertex3f (1.0, 1.0, 1.0); // quadrilateral right bottom vertices (top)
{
The painting and top of the bottom is very similar. Just the Y coordinate becomes -1. If we look at the cubes from the bottom of the cube, you will notice that the observer from the right corner is recently, so we can first draw away the closest vertices of the observers. Then the top left vertex is the top left and right vertices inside the screen. If you really don't care about the order of the polygon (clockwise or counterclockwise), you can copy the top code directly, change the Y coordinate from 1 to -1, and work. But once you enter a field of texture map, ignore the order of drawing leads to a very weird result.
}
Glcolor3f (1.0, 0.5, 0.0); // Color is changed to orange Glvertex3f (1.0, -1.0, 1.0); // quad-shaped upper right vertex (bottom surface) GlvertEX3F (-1.0, -1.0, 1.0); // quadrilateral Vertex (bottom) Glvertex3f (-1.0, -1.0, -1.0); // quadrilateral left vertices (bottom surface) GlvertEX3F (1.0, -1.0, -1.0); // quadrilateral right vertices (bottom surface)
{
Then, the front of the cube is drawn. Keep Z coordinate as a unit, front is facing us.
}
Glcolor3f (1.0, 0.0, 0.0); // color is changed to red glvertex3f (1.0, 1.0, 1.0); // quadrilateral upper right vertices (front) Glvertex3f (-1.0, 1.0, 1.0); // The top left vertex of the quadrilateral ( Front) GlvertEX3F (-1.0, -1.0, 1.0); // The left vertices of the quadrilateral (front) Glvertex3f (1.0, -1.0, 1.0); // quadrilateral right bottom vertices (front)
{
The drawing method behind the cube is similar to the previous. Just in the screen. Note that the Z coordinates now keep -1 unchanged.
}
Glcolor3f (1.0, 1.0, 0.0); // Color is changed to yellow glvertex3f (1.0, -1.0, -1.0); // quadrilateral upper right vertices (back) GlvertEX3F (-1.0, -1.0, -1.0); // quadrilateral Left top vertices (back) Glvertex3f (-1.0, 1.0, -1.0); // quadrilateral left vertices (back) Glvertex3f (1.0, 1.0, -1.0); // quadrilateral upper right vertices (back)
{
There are two faces left. You will notice that there is always a coordinate remained unchanged. This time is replaced with X coordinates. Because we are on the left side.
}
Glcolor3f (0.0, 0.0, 1.0); // Color is changed to blue glvertex3f (-1.0, 1.0, 1.0); // quad-shaped upper right vertices (left) GlvertEX3F (-1.0, 1.0, -1.0); // quad Left top vertices (left) GlvertEX3F (-1.0, -1.0, -1.0); // Tato-shaped top-down vertices (left) GlvertEX3F (-1.0, -1.0, 1.0); // quadrilateral upper right vertices (left)
{
The last foot of the cube. The X coordinate is kept in one unit. Poscope counterclockwise. If you are willing, you can stay in this face. This is a box :) or you want to change the color value of all the vertices of the cube, mix colors like the pyramid. You will see a very beautiful color cube, all kinds of colors flow in its respective surfaces.
}
Glcolor3f (1.0, 0.0, 1.0); // color is changed to violet glvertex3f (1.0, 1.0, -1.0); // quadrilateral upper right vertices (right sides) GlvertEX3F (1.0, 1.0, 1.0); // quad-shaped top vertices (Right side) Glvertex3f (1.0, -1.0, 1.0); // quadrilateral left vertices (right sides) Glvertex3f (1.0, -1.0, -1.0); // quad-shaped right bottom vertices (right side)
glend (); // Square Drawing End
RTRI: = RTRI 0.2; // Increase triangle rotation variable rquad: = rquad - 0.15; // Reduce quadrilateral rotation
END;
Run a look at the effect