Entering 3D: Jeff molofee (nehe)'s OpenGL tutorial-Lesson 5

Source: Internet
Author: User
The content of the previous lesson was extended. Now we start to generate real 3D objects, instead of 2D objects in the 3D world as in the previous two lessons. We add a left side, a right side, and a rear side to form a pyramid (pyramid ). Add left, right, top, bottom, and back to the square to generate a cube.

We mix the colors on the pyramid to create a smooth coloring object. Each side of the cube is given a different color.

Int drawglscene (glvoid) // This process includes all the drawing code
{
Glclear (gl_color_buffer_bit | gl_depth_buffer_bit); // clear the screen and depth Cache
Glloadidentity (); // resets the model observation matrix.
Gltranslatef (-1.5f, 0.0f,-6.0f); // move 1.5 units left and 6.0 to the screen

Glrotatef (rtri, 0.0f, 1.0f, 0.0f); // rotate the pyramid around the Y axis

Glbegin (gl_triangles); // start to draw each surface of the pyramid.

Some may have already tried to create 3D objects on their own in the code of the previous lesson. But some people often ask me, "How can my object not rotate around its own axis? It seems that the screen is always full. "To make your object rotate around its own axis, you must make the central coordinates of the object always (0.0f, 0, 0f, 0, 0f ).
The following code creates a pyramid that rotates around the spindle. The top vertex of the pyramid is a unit from the center, and the bottom is also a unit from the center. The projection of the top Vertex on the bottom is located at the center of the bottom.

Note that all the surface triangles are drawn in a counter-clockwise order. This is very important and I will explain it in future courses. Now, you only need to understand either the clockwise or both, but never mix them together unless you have enough reason to do so.

We began to draw the front of the pyramid. Because all faces share the top vertex, we set this to red in all triangles. The colors of the two vertices on the edges are mutually exclusive. The lower left vertex of the front is green, and the lower right vertex is blue. In this way, the lower left vertex of the adjacent right side is blue and the lower right vertex is green. In this way, the color of the points on the bottom of the quadrilateral is arranged at intervals.

Glcolor3f (1.0f, 0.0f, 0.0f); // red
Glvertex3f (0.0f, 1.0f, 0.0f); // top vertex of the triangle (front side)
Glcolor3f (0.0f, 1.0f, 0.0f); // green
Glvertex3f (-1.0f,-1.0f, 1.0f); // the lower left vertex of the triangle (front side)
Glcolor3f (0.0f, 0.0f, 1.0f); // blue
Glvertex3f (1.0f,-1.0f, 1.0f); // The Right Bottom vertex of the triangle (front side)
Now draw the right side. Note that the X coordinates of the two vertices on the bottom edge are located at a unit on the right of the center. The vertex is located at a unit on the Y axis, And the zcoordinate is exactly at the zcoordinate center of the two vertices on the bottom edge. The right side is tilted from the top vertex to the outside to the bottom edge.
This time, the lower left vertex is drawn in blue to keep it consistent with the lower right Vertex on the front. Blue will expand from the front and right sides of the pyramid and blend with other colors.
It should also be noted that the three sides and the front are in the middle of the same glbegin (gl_triangles) and glend () statements. Because we construct this pyramid through triangles. OpenGL knows that every three points form a triangle. After a triangle is drawn, if there are still remaining points, it will assume that the New Triangle will start to be drawn. OpenGL does not draw four points as a quadrilateral here, but assumes that the new triangle starts. Therefore, do not inadvertently add any unnecessary points.
Glcolor3f (1.0f, 0.0f, 0.0f); // red
Glvertex3f (0.0f, 1.0f, 0.0f); // top vertex of the triangle (right side)
Glcolor3f (0.0f, 0.0f, 1.0f); // blue
Glvertex3f (1.0f,-1.0f, 1.0f); // the lower left vertex of the triangle (right side)
Glcolor3f (0.0f, 1.0f, 0.0f); // green
Glvertex3f (1.0f,-1.0f,-1.0f); // The Right Bottom vertex of the triangle (right side)
It is now the back side. Switch the color again. The lower left vertex returns to green, because the rear side shares this angle with the right side.
Glcolor3f (1.0f, 0.0f, 0.0f); // red
Glvertex3f (0.0f, 1.0f, 0.0f); // top vertex of the triangle (rear side)
Glcolor3f (0.0f, 1.0f, 0.0f); // green
Glvertex3f (1.0f,-1.0f,-1.0f); // the lower left vertex of the triangle (rear side)
Glcolor3f (0.0f, 0.0f, 1.0f); // blue
Glvertex3f (-1.0f,-1.0f,-1.0f); // The Right Bottom vertex of the triangle (rear side)
Draw the left side. Switch the color again. The lower left vertex is blue, which is the same as the lower right Vertex on the back side. The lower right vertex is blue, which is the same as the lower left Vertex on the front.
The pyramid is finished here. Because the pyramid only rotates around the Y axis, we can never see the bottom, so there is no need to add the bottom. If you think you have experience, try to add the bottom (square) and rotate the pyramid around the X axis to see if you have done the right thing. Make sure that the color of the four vertices on the bottom matches the color of the side.
Glcolor3f (1.0f, 0.0f, 0.0f); // red
Glvertex3f (0.0f, 1.0f, 0.0f); // top vertex of the triangle (left side)
Glcolor3f (0.0f, 0.0f, 1.0f); // blue
Glvertex3f (-1.0f,-1.0f,-1.0f); // the lower left vertex of the triangle (left side)
Glcolor3f (0.0f, 1.0f, 0.0f); // green
Glvertex3f (-1.0f,-1.0f, 1.0f); // the lower right vertex of the triangle (left side)
Glend (); file: // pyramid painting ends
Next, draw a cube. It consists of six quadrants. All the rectangles are drawn in a counter-clockwise order. That is to say, first draw the upper right corner, then the upper left corner, the lower left corner, and the bottom right corner. You may think that this order looks clockwise when you draw the back of the cube, but don't forget that when we look at the back of the cube, it is exactly the opposite of what you think now. (Note: you observe the Cube from the outside ).
We noticed that this time we moved the cube away from the screen. Because the size of a cube is larger than that of a pyramid, when it is also moved into six units, the cube looks much larger. This is the reason for pivoting. The farther the object looks smaller .
Glloadidentity ();
Gltranslatef (1.5f, 0.0f,-7.0f); // move right first and then to the screen

Glrotatef (rquad, 1.0f, 1.0f, 1.0f); // rotate the cube on the XYZ axis

Glbegin (gl_quads); // starts to draw a cube.

First draw the top surface of the cube. Remove one unit from the center. Note that the Y coordinate is always one unit, indicating that the quadrilateral is parallel to the Z axis. First draw the top right vertex, one unit to the right, and then one unit to the center of the screen. Then the top left vertex is directed to the left unit, and then to the center of the screen. Then it is near the bottom left and bottom right vertices of the observer. Is a unit outside the screen.
Glcolor3f (0.0f, 1.0f, 0.0f); // change the color to blue.
Glvertex3f (1.0f, 1.0f,-1.0f); // top right vertex of the Quadrilateral (top surface)
Glvertex3f (-1.0f, 1.0f,-1.0f); // top left vertex of the Quadrilateral (top surface)
Glvertex3f (-1.0f, 1.0f, 1.0f); // left bottom vertex of the Quadrilateral (top surface)
Glvertex3f (1.0f, 1.0f, 1.0f); // right bottom vertex of the Quadrilateral (top surface)
The painting method on the bottom is very similar to that on the top. The Y coordinate is changed to-1. If we look at the Cube below the cube, you will notice that the top right corner is closest to the observer, so we will first draw the vertex closest to the observer. Then the top left vertex is the bottom left and bottom right vertex in the screen.

If you really don't care about the order of the polygon (clockwise or counterclockwise), you can directly copy the top surface code and change the Y coordinate from 1 to-1. However, when you enter a field such as texture ing, ignoring the rendering order will lead to very weird results.

Glcolor3f (1.0f, 0.5f, 0.0f); // change the color to orange.
Glvertex3f (1.0f,-1.0f, 1.0f); // the top right vertex of the Quadrilateral (bottom)
Glvertex3f (-1.0f,-1.0f, 1.0f); // top left vertex of the Quadrilateral (bottom)
Glvertex3f (-1.0f,-1.0f,-1.0f); // the lower left vertex of the Quadrilateral (bottom)
Glvertex3f (1.0f,-1.0f,-1.0f); // right bottom vertex of the Quadrilateral (bottom surface)
Then draw the front of the cube. The zcoordinate is kept in one unit, and the front is facing us.
Glcolor3f (1.0f, 0.0f, 0.0f); // change the color to red.
Glvertex3f (1.0f, 1.0f, 1.0f); // top right vertex of the Quadrilateral (Front)
Glvertex3f (-1.0f, 1.0f, 1.0f); // top left vertex of the Quadrilateral (Front)
Glvertex3f (-1.0f,-1.0f, 1.0f); // left lower vertex of the Quadrilateral (Front)
Glvertex3f (1.0f,-1.0f, 1.0f); // right bottom vertex of the Quadrilateral (Front)
The method for drawing the back of the cube is similar to that for the front. It is located in the screen. Note that the zcoordinate remains unchanged at-1.
Glcolor3f (1.0f, 1.0f, 0.0f); // change the color to yellow.
Glvertex3f (1.0f,-1.0f,-1.0f); // top right vertex of the Quadrilateral (back)
Glvertex3f (-1.0f,-1.0f,-1.0f); // top left vertex of the Quadrilateral (back)
Glvertex3f (-1.0f, 1.0f,-1.0f); // the lower left vertex of the Quadrilateral (after)
Glvertex3f (1.0f, 1.0f,-1.0f); // right bottom vertex of the Quadrilateral (back)
There are only two sides left to complete. You will notice that there is always a coordinate that remains unchanged. This time it is changed to the X coordinate. Because we are painting the left side.
Glcolor3f (0.0f, 0.0f, 1.0f); // change the color to blue.
Glvertex3f (-1.0f, 1.0f, 1.0f); // top right vertex of the Quadrilateral (left)
Glvertex3f (-1.0f, 1.0f,-1.0f); // top left vertex of the Quadrilateral (left)
Glvertex3f (-1.0f,-1.0f,-1.0f); // left lower vertex of the Quadrilateral (left)
Glvertex3f (-1.0f,-1.0f, 1.0f); // The Right Bottom vertex of the Quadrilateral (left)
The last face of the cube. X coordinates are kept in one unit. Draw it counterclockwise. If you want to, you can leave this area as a box :)

Or you can change the color values of all vertices in the cube to mix colors like the pyramid. You will see a very beautiful color cube with various colors flowing on its surface.

Glcolor3f (1.0f, 0.0f, 1.0f); // change the color to Violet.
Glvertex3f (1.0f, 1.0f,-1.0f); // the top right vertex of the Quadrilateral (right)
Glvertex3f (1.0f, 1.0f, 1.0f); // top left vertex of the Quadrilateral (right)
Glvertex3f (1.0f,-1.0f, 1.0f); // left lower vertex of the Quadrilateral (right)
Glvertex3f (1.0f,-1.0f,-1.0f); // The Right Bottom vertex of the Quadrilateral (right)
Glend (); // The End Of The Cube painting

Rtri + = 0.2f; // increase the rotation variable of the triangle (new)
Rquad-= 0.15f; // reduce the rotation variable of the Quadrilateral (new)
Return true; // continue running
}

This lesson is over again. Now you have mastered how to create objects in 3D space. The OpenGL screen must be imagined as a large drawing paper with many transparent layers. It is almost a cube composed of a large number of points. These points are filled with cubes from left to right, from top to bottom, and from front to back. If you can imagine the depth of the screen, there should be no problem when designing new 3D objects.

Don't be discouraged if you have a hard understanding of 3D space! At the beginning, it was very difficult to comprehend the content. Objects like cubes are a good example of your practice. Continue to work! If you have any comments or suggestions, please email me. If you think there is something wrong or can be improved, please let me know. I want to do the best OpenGL tutorial and be interested in your feedback.


{Translator: the document of nehe seems very simple and arrogant. But how many other experts have you ever seen? I am not a master. I hope you are sincere .}
Below is the source code download link. Good luck!
* Download visual c ++ code for this lesson.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.