First triangle: Jeff molofee (nehe)'s OpenGL tutorial-Lesson 2

Source: Internet
Author: User
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 (); // resets the view
After you call glloadidentity (), the current point is actually moved to the center of the screen. The X axis is from left to right, the Y axis is from bottom to top, and the Z axis is 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.

{Translator: note that there are two different coordinate transformation methods: X, Y in gltranslatef (x, y, z, Z is relative to the displacement of your current vertex, but glvertex (x, y, z) is relative to the new origin shift after gltranslatef (x, y, z. Therefore, we can consider that gltranslate moves the coordinate origin, and vertex's point is the coordinate value of the latest coordinate origin .}

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 tutorial", 640,480, 16, fullscreen ))
{
Return 0; // exit the program if the window cannot be created
}
}
Markus Knauer note: In ("OpenGL programming guide: the official guide to learning OpenGL, Release 1", J. neider, T. davis, M. woo, Addison-Wesley, 1993) The Book OpenGL programming guide: an official guide to OpenGL learning, the first version clearly explains the concept of moving units in OpenGL referred to by nehe:
"Is there a difference between inches and miles in OpenGL? The answer is one sentence: No. Perspective and other transformations are non-unit. If you want to crop a plane between 1.0 to 20.0, inches, kilometers, and so on, you cannot do it in OpenGL. The only rule is that you must use consistent measurement units. "
In this lesson, I have tried to explain in detail the steps related to Polygon drawing. An OpenGL program is created to draw triangles and squares. 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.
* Download Visual Basic code for this lesson. (conversion by Ross Dawson)
* Download glut code for this lesson. (conversion by Andy restad)
* Download cygwin (free language) code for this lesson. (conversion by Stephen Ferraro)
* Download Delphi code for this lesson. (conversion by Peter de jaegher)
* Download MacOS X/glut code for this lesson. (conversion by raal Goff)
* Download code warrior 5 code for this lesson. (conversion by David dolinar)
* Download Linux/Glx code for this lesson. (conversion by Mihael vrbanec)
* Download MASM code for this lesson. (conversion by Nico (scalkaline ))
* Download ASM code for this lesson. (conversion by foolman)
* Download Visual Fortran code for this lesson. (conversion by Jean-Philippe perois)
* Download Linux code for this lesson. (conversion by Richard Campbell)
* Download IRIX code for this lesson. (conversion by lakmal gunasekara)
* Download Solaris code for this lesson. (conversion by lakmal gunasekara)
* Download Mac OS code for this lesson. (conversion by Anthony Parker)
* Download power basic code for this lesson. (conversion by Angus Law)
* Download BEOs code for this lesson. (conversion by Chris herborth)
* Download Java code for this lesson. (conversion by Darren Hodges)
* Download Borland C ++ Builder 4.0 code for this lesson. (conversion by Patrick salmons)
* Download mingw32 & Allegro code for this lesson. (conversion by Peter puck)
* Download Python code for this lesson. (conversion by John)

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.