Nehe OpenGL Lesson 2: Polygon

Source: Internet
Author: User

Original works can be reprinted. During reprinting, you must mark the original source, author information, and this statement in hyperlink form. Otherwise, legal liability will be held. Http://yarin.blog.51cto.com/1130898/380265

Lesson 2: Polygon


Your first polygon:

Based on the first tutorial, we added a triangle and a quadrilateral. You may think this is simple, but you have taken a big step to know that any model drawn in OpenGL will be broken down into these two simple graphics.

After reading this lesson, you will learn how to place models in the space and understand the concept of deep cache.

In Lesson 1, I teach you how to create an OpenGL window. In this lesson, I will teach you how to create a triangle and a quadrilateral. We will use it to create a gl_triangles triangle and gl_quads to create a quadrilateral.
Based on the code of Lesson 1, we only need to add the Code during the drawglscene () process. Next I will rewrite the entire process. If you plan to modify the code of the previous lesson, you only need to overwrite the original drawglscene () with the following code.

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 (); // reset the current model observation matrix

After you call glloadidentity (), you actually move the current point to the center of the screen, X axis from left to right, Y axis from bottom to top, and Z axis from inside to outside. The coordinates of the OpenGL screen center are 0.0f points on the X and Y axes. The coordinate value on the left of the center is a negative value, and the right is a positive value. Moving to the top of the screen is positive, and moving to the bottom of the screen is negative. Moving to the depth of the screen is a negative value, and moving out of the screen is a positive value.
Gltranslatef (x, y, z) moves along the X, Y, and Z axes. According to the preceding order, the following code shifts 1.5 units to the left of the X axis, the Y axis does not move (0.0f), and finally moves to the 6.0f unit of the screen. Note that when you move in gltranslatef (x, y, z), you do not move relative to the center of the screen, but to the current position of the screen.

Gltranslatef (-1.5f, 0.0f,-6.0f); // move 1.5 units left and 6.0 to the screen

Now we have moved to the left half of the screen and pushed the view to the back of the screen, so that we can see all the scenes-create a triangle. Glbegin (gl_triangles) means to start to draw a triangle, and glend () tells OpenGL that the triangle has been created. You usually need to draw three vertices. You can use gl_triangles. On the vast majority of graphics cards, it is quite fast to draw triangles. If you want to draw four vertices, it is more convenient to use gl_quads. But as far as I know, most video cards use triangles to color objects. Finally, use gl_polygon if you want to draw more vertices.
In this simple example, we draw only one triangle. If you want to draw a second triangle, you can add three lines of code (3 points) after the three points ). All the six-point code should be included between glbegin (gl_triangles) and glend. There will be no extra points between them, that is, the points between (gl_triangles) and glend () are all set by three points. This applies to quadrilateral. If you know that a quadrilateral is actually drawn, you must add four points after the first four points to a vertex group of a set. On the other hand, a polygon can be composed of any vertex. (gl_polygon) does not care about the number of lines of code between glbegin (gl_triangles) and glend.

The first vertex of the polygon is set in the first line after glbegin. The first parameter of glvertex is X coordinate, followed by Y coordinate and zcoordinate. The first vertex is the upper vertex, then the lower left vertex and the lower right vertex. Glend () tells OpenGL that there are no other points. This will display a filled triangle.

Glbegin (gl_triangles); // draw a triangle
Glvertex3f (0.0f, 1.0f, 0.0f); // top Vertex
Glvertex3f (-1.0f,-1.0f, 0.0f); // bottom left
Glvertex3f (1.0f,-1.0f, 0.0f); // bottom right
Glend (); // draw the triangle

After the triangle is drawn in the left half of the screen, we need to move to the right half to draw a square. Therefore, use gltranslate again. This shift to the right, so the X coordinate value is positive. Because the first 1.5 units are moved to the left, this time we need to first move back to the center of the screen (1.5 units), and then move 1.5 units to the right. A total of 3.0 units are to be shifted to the right.

Gltranslatef (3.0f, 0.0f, 0.0f); // shifts right to 3 units

Use gl_quads to draw a square. Similar to the code used to draw a triangle, it is easy to draw a quadrilateral. The only difference is that gl_quads replaces gl_triangles. A vertex is added. We draw a square in clockwise order-top left-top right-bottom left. Clockwise draw is the back surface of the object. This means we see the back of the square. The square drawn counter-clockwise is toward us. This is not important to you, but you must know it later.

Glbegin (gl_quads); // draw a square
Glvertex3f (-1.0f, 1.0f, 0.0f); // top left
Glvertex3f (1.0f, 1.0f, 0.0f); // upper right
Glvertex3f (1.0f,-1.0f, 0.0f); // bottom left
Glvertex3f (-1.0f,-1.0f, 0.0f); // bottom right
Glend (); // draw the square
Return true; // continue running
}

Finally, change the title content in window mode.
 
If (Keys [vk_f1]) // is the F1 key pressed?
{
Keys [vk_f1] = false; // If yes, set the value in the corresponding key array to false.
Killglwindow (); // destroy the current window
Fullscreen =! Fullscreen; // switch to full screen/window mode
// Recreate the OpenGL window (modify)
If (! Createglwindow ("nehe's first polygon program", 640,480, 16, fullscreen ))
{
Return 0; // exit the program if the window cannot be created
}
}
Download the source code of the original text and its versions:

Http://nehe.gamedev.net/data/lessons/lesson.asp? Lesson = 02

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.