To 3D into: Jeff Molofee (Nehe) OpenGL Tutorial - Fifth Class

zhaozj2021-02-08  218

Jeff Molofee (Nehe) OpenGL Tutorial The Lesson TraSlated By CKer is based on the content of the previous class, and we now start to generate a real 3D object, not 2D objects in the 3D world like the first two lessons. 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. INT Drawglscene (GLVOID) // This process includes all draw code {glclear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear the screen and depth cache GLLoadIndIndentity (); // Reset model observation matrix GLTranslateF (-1.5F, 0.0F, -6.0F); // Left shift 1.5 unit, and move into the screen 6.0 GLROTATEF (RTRI, 0.0F, 1.0F, 0.0F); // Rotate the pyramid of the pyramid of the pyramid around the Y-axis; // Start drawing the various faces of the pyramid 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.0F, 0.0F, 0.0F); // red GlvertEX3F (0.0F, 1.0F, 0.0F); // triangular upper top (front side) Glcolor3f (0.0F, 1.0F, 0.0F); / / Green GlvertEX3F (-1.0F, -1.0F, 1.0F); // Triangular left vertices (front side) Glcolor3f (0.0F, 0.0F, 1.0F); // Blue GlvertEX3F (1.0F, -1.0F , 1.0F); // Triangular Right Count (front side) Now draws 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.0F, 0.0F, 0.0F); // Red GlvertEX3F (0.0F, 1.0F, 0.0F); // Triangular upper top (right side) Glcolor3f (0.0F, 0.0F, 1.0F); // Blue GlvertEX3F (1.0F, -1.0F, 1.0F); // Triangular left vertices (right side) Glcolor3f (0.0F, 1.0F, 0.0F); // Green Glvertex3f (1.0F, -1.0 f, -1.0f); // Triangle Right Count (right side) is now 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.0F, 0.0F, 0.0F); // red GlvertEX3F (0.0F, 1.0F, 0.0F); // Triangular top point (rear side) Glcolor3f (0.0F, 1.0F, 0.0F); / / Green GlvertEX3F (1.0F, -1.0F, -1.0F); // Triangular left vertices (rear side) Glcolor3f (0.0F, 0.0F, 1.0F); // Blue GlvertEX3F (-1.0F, -1.0 F, -1.0F); // Triangular Right lower vertices (rear sides) finally draw the 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. It's the painting here. 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. If you feel experience, try to increase the bottom (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. Glcolor3f (1.0F, 0.0F, 0.0F); // Red GlvertEX3F (0.0F, 1.0F, 0.0F); // Triangular top point (left side) Glcolor3f (0.0F, 0.0F, 1.0F); // Blue GlvertEX3F (-1.0F, -1.0F, -1.0F); // Triangle's left vertices (left side) Glcolor3f (0.0F, 1.0F, 0.0F); // Green Glvertex3f (-1.0F , -1.0F, 1.0F); // Triangular right vertex (left side) glend (); file: // Pyramid Drawing end Next starts to paint cubes. 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 :). GLLoadIdentity (); GLTRANSLATEF (1.5F, 0.0F, -7.0F); // First right shift and move into the screen GLROTATEF (RQUAD, 1.0F, 1.0F, 1.0F); // Rotate the cube glbegin on the XYZ axis (GL_QUADS ); // Start drawing the top of the cube pamper. 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.0F, 1.0F, 0.0F); // color change to blue glvertex3f (1.0F, 1.0F, -1.0F); // quadrilateral right up vertices (top) GlvertEX3F (-1.0F, 1.0F , -1.0F); // Tatogram left top vertices (top) Glvertex3f (-1.0F, 1.0F, 1.0F); // quadrilateral left vertices (top) GlvertEX3F (1.0F, 1.0F, 1.0F) (// quadrilateral right vertices (top) The paintings and tops of the bottom surface are 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.0F, 0.5F, 0.0F); // The color is changed to orange Glvertex3f (1.0F, -1.0F, 1.0F); // quad-shaped upper right vertex (bottom surface) GlvertEX3F (-1.0F, -1.0F, 1.0F); // quadrilateral left top vertices (bottom surface) GlvertEX3F (-1.0F, -1.0F, -1.0F); // quadrilateral left vertices (bottom surface) GlvertEX3F (1.0F, -1.0F, -1.0F) The // The top right vertex (bottom surface) is then drawn in front of the cube. Keep Z coordinate as a unit, front is facing us. Glcolor3f (1.0F, 0.0F, 0.0F); // Color changed to red glvertex3f (1.0f, 1.0f, 1.0f); // quadrilateral upper right vertices (front) Glvertex3f (-1.0F, 1.0F, 1.0F ); // quad-shaped top vertices (front) Glvertex3f (-1.0F, -1.0F, 1.0F); // quadrilateral left vertices (front) Glvertex3f (1.0F, -1.0F, 1.0F); // quadrilateral The lower right vertex (front) The drawing method behind the cube is similar to the front. Just in the screen. Note that the Z coordinates now keep -1 unchanged. Glcolor3f (1.0F, 1.0F, 0.0F); // Color changed to yellow glvertex3f (1.0F, -1.0F, -1.0F); // quad-shaped upper right vertices (back) GlvertEX3F (-1.0F, -1.0F , -1.0F); // quad-shaped left top vertices (back) GlvertEX3F (-1.0F, 1.0F, -1.0F); // quad-shaped left vertices (back) GlvertEX3F (1.0F, 1.0F, -1.0F) ; // The four-sided right vertex (behind) will be completed. 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.0F, 0.0F, 1.0F); // color is changed to blue glvertex3f (-1.0F, 1.0F, 1.0F); // quad-shaped upper right vertices (left) GlvertEX3F (-1.0F, 1.0F, -1.0F); // quad-shaped top vertices (left) Glvertex3f (-1.0F, -1.0F, -1.0F); // quadrilateral left vertices (left) GlvertEX3F (-1.0F, -1.0F, 1.0F ); // quad-shaped right vertices (left) cubes' last face. 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.0F, 0.0F, 1.0F); // color is changed to violet glvertex3f (1.0F, 1.0F, -1.0F); // Quad-shaped upper right vertices (right sides) GlvertEX3F (1.0F, 1.0F, 1.0 f); // quad-shaped top vertices (right sides) Glvertex3f (1.0F, -1.0F, 1.0F); // quadrilateral left vertices (right sides) GlvertEX3F (1.0F, -1.0F, -1.0F); //// The four-dimensional right bottom vertices (right sides) glend (); // cube draw end RTRI = 0.2F; // increase triangle rotation variable (new) RQUAD- = 0.15F; // reduce quantity of rotation of quadrangularities (new) RETURN TRUE; // Continue to run} This lesson is over. Here you should have a better way to create an object in 3D space. The OpenGL screen must be imagined into a large drawing paper, and there are many transparent layers. Almost is a cube consisting of a large number of points. These points are covered from left to right, from top to bottom, from above, filled with this cube. If you can imagine the depth direction of the screen, there should be no problem in designing the new 3D object. If you have difficulty understanding 3D space, don't be discouraged! When you start, you will be difficult. The object like a cube is a good example of your exercise. keep it up! If you have any comments or suggestions, please give me email. If you think there is something wrong or improve, please tell me. I want to be the best OpenGL tutorial and interested in your feedback. {Translator: NEHE's document seems very simple, it seems very much. But if you want to come to such a master, have you seen a few? Or that sentence, I am not a master, I hope you are sincere. } The following is the source code download link.

转载请注明原文地址:https://www.9cbs.com/read-2201.html

New Post(0)