Nehe OpenGL Tutorial Lesson five: 3D space

Source: Internet
Author: User

Go from "translation" Nehe OpenGL tutorial

Objective

Statement, this Nehe OpenGL tutorial series of articles by 51 blog yarin Translation (2010-08-19), this blog for reprint and a little collation and modification. Thank you for compiling Nehe's OpenGL pipeline tutorial, as well as yarn translation finishing.

Lesson Five: 3D space

3D Space:

We use polygons and quads to create 3D objects, and in this lesson we turn the triangles into three-dimensional gold tower shapes and turn the quads into cubes.

To expand on the content of the last lesson, we are now starting to build real 3D objects, rather than 2D objects in the 3D world as in the first two lessons. We add a left side to the triangle, a right side, and a rear side to create a pyramid (four pyramid). Adds a cube to the square by adding left, right, top, bottom, and back.
We blend the colors on the pyramid to create a smooth-shaded object. Give each side of the cube a different color.

int Drawglscene (glvoid)//This procedure includes all drawing codes
{
Glclear (Gl_color_buffer_bit | Gl_depth_buffer_bit); Clear screen and depth cache
Glloadidentity (); Resetting the Model observation matrix
Gltranslatef ( -1.5f,0.0f,-6.0f); Move left 1.5 units and move into screen 6.0

Glrotatef (rtri,0.0f,1.0f,0.0f); Rotate the pyramid around the y axis

Glbegin (Gl_triangles); Start drawing each polygon of the pyramid

Some people may have already tried to create their own 3D objects on the code in the last lesson. But often someone wrote to me, "How can my object not rotate around its own axis?" It always seems to be turning around in full screen. "To have your object rotate around its own axis, you must keep the object's center coordinates always (0.0f,0,0f,0,0f)."
The following code creates a pyramid that rotates around the center axis of the person. The top vertex of the pyramid is a unit above the origin, and the center of the bottom is less than one unit from the origin point. The projection of the top vertex at the bottom is centered on the bottom face.
Note that all polygon-triangles are drawn in a counterclockwise order. This is very important and I will explain it in the course of the future. Now, all you need to know is either counterclockwise or clockwise, but never mix the two in order unless you have enough reason to do so.

We began to draw the front side of the pyramid. Because all faces share the top vertex, we set this to red in all triangles. The color of the two vertices on the bottom edge is mutually exclusive. The lower left vertex of the front side is green and the lower right vertex is blue. The lower left vertex of the adjacent right side is blue and the lower right vertex is green. The color of the dots on the underside of the quads is spaced.

  glcolor3f (1.0f,0.0f,0.0f);    //Red
  glvertex3f (0.0f, 1.0f, 0.0f); top vertex of    //triangle (front side)
  glcolor3f (0.0f,1.0f, 0.0f);    //Green
  glvertex3f ( -1.0f,-1.0f, 1.0f);    //Triangle's lower left vertex (front side)
  glcolor3f (0.0f,0.0f,1.0f);    //Blue
  glvertex3f (1.0f,-1.0f, 1.0f);    //the lower-right vertex of the triangle (front side)

Now draw the right side. Notice that the x-coordinate of the two vertices on the bottom edge is at one unit to the right of the center. Vertices are located at a unit on the y-axis, and the z-coordinate is exactly at the z-coordinate center of the two vertices at the bottom. The right side tilts outward from the top vertex to the bottom edge.
This time the lower left vertex is drawn in blue to maintain consistency with the lower right vertex of the front side. Blue expands from this angle to the front and right sides of the pyramid and blends with other colors.
It should also be noted that the three sides and the front side of the back are in the middle of the same glbegin (Gl_triangles) and glend () statements. Because we construct this pyramid by means of triangles. OpenGL knows that each of the three dots constitutes a triangle. When it finishes drawing a triangle, if the remaining points appear, it will assume that the new triangle will begin to draw. OpenGL does not draw four points into a quadrilateral here, but assumes that the new triangle begins. So don't accidentally add any extra points.

glcolor3f (1.0f,0.0f,0.0f); Red
glvertex3f (0.0f, 1.0f, 0.0f); Top vertex of triangle (right side)
glcolor3f (0.0f,0.0f,1.0f); Blue
glvertex3f (1.0f,-1.0f, 1.0f); Lower left vertex of triangle (right side)
glcolor3f (0.0f,1.0f,0.0f); Green
glvertex3f (1.0f,-1.0f, -1.0f); Lower right vertex of triangle (right side)

Now is the rear side. Switch colors again. The lower left vertex goes back to green because the corner is shared with the right side of the rear side.

glcolor3f (1.0f,0.0f,0.0f); Red
glvertex3f (0.0f, 1.0f, 0.0f); Top vertex of triangle (rear side)
glcolor3f (0.0f,1.0f,0.0f); Green
glvertex3f (1.0f,-1.0f, -1.0f); Lower left vertex of triangle (rear side)
glcolor3f (0.0f,0.0f,1.0f); Blue
glvertex3f ( -1.0f,-1.0f, -1.0f); Lower right vertex of triangle (rear side)

Draw the left side at the end. Switch colors again. The lower left vertex is blue, the same as the lower-right vertex on the back side. The lower-right vertex is blue, the same as the lower-left vertex on the front side.
The pyramids are finished here. Because the pyramid rotates only around the y axis, we never see the bottom surface, so there is no need to add the bottom surface. If you feel experienced, try increasing the bottom (square) and rotating the pyramid around the x-axis to see if you are working against it. Make sure that the color of the four vertices on the bottom side matches the color of the side.

glcolor3f (1.0f,0.0f,0.0f); Red
glvertex3f (0.0f, 1.0f, 0.0f); Top vertex of triangle (left side)
glcolor3f (0.0f,0.0f,1.0f); Blue
glvertex3f ( -1.0f,-1.0f,-1.0f); Lower left vertex of triangle (left side)
glcolor3f (0.0f,1.0f,0.0f); Green
glvertex3f ( -1.0f,-1.0f, 1.0f); Lower right vertex of triangle (left side)
Glend (); End of pyramid drawing

Next, start drawing the cube. He is made up of six quads. All quads are drawn in a counterclockwise order. This means that the upper right corner is drawn first, then the upper left corner, lower left corner, and bottom right corner. You might think that this order looks clockwise when you draw the back of the cube, but don't forget that we're looking at the back from behind the cube, just the opposite of what you're thinking. (Translator Note: You are looking at the cube from outside the cube).
Notice that we moved the cube farther away from the screen this time. Because the cube size is larger than the pyramid, and the same moves into 6 units, the cube looks much larger. This is the sake of perspective. The farther away the object looks the smaller:).

Glloadidentity ();
Gltranslatef (1.5f,0.0f,-7.0f); Move right and then move into screen

Glrotatef (rquad,1.0f,1.0f,1.0f); Rotate a cube on the XYZ axis

Glbegin (gl_quads); Start drawing a cube

Draw the top face of the cube first. Move one unit from the center, note that the y-coordinate is always a unit, indicating that the quadrilateral is parallel to the z-axis. Draw the top right vertex first, a unit to the right, and then the screen to a unit. Then the top left vertex, one unit to the left, and then the screen to a unit. Then the lower-left and lower-right vertices are close to the viewer. Is the screen outward one unit.

glcolor3f (0.0f,1.0f,0.0f); Color changed to Blue
glvertex3f (1.0f, 1.0f,-1.0f); Upper right Vertex (top) of quadrilateral
glvertex3f ( -1.0f, 1.0f,-1.0f); Upper left Vertex (top) of quadrilateral
glvertex3f ( -1.0f, 1.0f, 1.0f); Left bottom vertex of quadrilateral (top)
glvertex3f (1.0f, 1.0f, 1.0f); The bottom-right vertex of a quadrilateral (top side)

The drawing of the bottom surface is very similar to the top surface. Just the y-coordinate becomes-1. If we look at the cube from the bottom of the cube, you'll notice that the upper-right corner is closest to the observer, so let's draw the nearest vertex from the Observer. Then the top left vertex is the bottom left and bottom right vertex of the screen.
If you really don't care about the order in which you draw the polygon (clockwise or counterclockwise), you can directly copy the top code and change the Y coordinate from 1 to-1 and work. But once you enter a field like texture mapping, ignoring the drawing order can lead to very bizarre results.

glcolor3f (1.0f,0.5f,0.0f); Color changed to Orange
glvertex3f (1.0f,-1.0f, 1.0f); Top right vertex of quadrilateral (bottom)
glvertex3f ( -1.0f,-1.0f, 1.0f); Top left vertex (bottom) of a quad
glvertex3f ( -1.0f,-1.0f,-1.0f); Left bottom vertex of quadrilateral (bottom)
glvertex3f (1.0f,-1.0f,-1.0f); Right bottom vertex (bottom) of quadrilateral

Then draw the front of the cube. Keep the z-coordinate as a unit, facing us right in front.

glcolor3f (1.0f,0.0f,0.0f); Color changed to Red
glvertex3f (1.0f, 1.0f, 1.0f); Top right vertex of quadrilateral (front)
glvertex3f ( -1.0f, 1.0f, 1.0f); Top left vertex of quadrilateral (front)
glvertex3f ( -1.0f,-1.0f, 1.0f); Left bottom vertex of quadrilateral (front)
glvertex3f (1.0f,-1.0f, 1.0f); The bottom-right vertex of a quad (front)

The drawing method behind the cube is similar to the previous. Just on the inside of the screen. Note that the z-coordinates are now maintained-1 unchanged.

glcolor3f (1.0f,1.0f,0.0f); Color changed to Yellow
glvertex3f (1.0f,-1.0f,-1.0f); Top right vertex of quadrilateral (rear)
glvertex3f ( -1.0f,-1.0f,-1.0f); Top left vertex of quadrilateral (rear)
glvertex3f ( -1.0f, 1.0f,-1.0f); Left bottom vertex of quadrilateral (rear)
glvertex3f (1.0f, 1.0f,-1.0f); Right bottom vertex of quadrilateral (rear)

There are two remaining faces to complete. You will notice that there is always a coordinate that remains the same. This time it was replaced by the x-coordinate. Because we're drawing the left side.

glcolor3f (0.0f,0.0f,1.0f); Color changed to Blue
glvertex3f ( -1.0f, 1.0f, 1.0f); Top right vertex of quadrilateral (left)
glvertex3f ( -1.0f, 1.0f,-1.0f); Left top vertex of quadrilateral (left)
glvertex3f ( -1.0f,-1.0f,-1.0f); Left bottom vertex of quadrilateral (left)
glvertex3f ( -1.0f,-1.0f, 1.0f); Four-sided right bottom vertex (left)

The last face of the cube. The x-coordinate remains one unit. Draw counterclockwise. If you want to, keep this side not painting can also, this is a box:)
Or if you're interested, you can change the color values of all the vertices in the cube, blending colors like pyramids. You will see a very beautiful colored cube, with various colors flowing across its surface.

glcolor3f (1.0f,0.0f,1.0f); Color changed to Violet
glvertex3f (1.0f, 1.0f,-1.0f); Top right vertex of quad
glvertex3f (1.0f, 1.0f, 1.0f); Left top vertex of quadrilateral (right)
glvertex3f (1.0f,-1.0f, 1.0f); Left bottom vertex of quadrilateral (right)
glvertex3f (1.0f,-1.0f,-1.0f); Four-sided right-bottom vertex
Glend (); End of Cube drawing

rtri+=0.2f; Increase the rotation variable of a triangle
rquad-=0.15f; Reducing the rotation variable of a quadrilateral
return TRUE; Continue running
}

Original source code and version of the download:

http://nehe.gamedev.net/data/lessons/lesson.asp?lesson=05

Nehe OpenGL Tutorial Lesson five: 3D space

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.