HOME
OPENGL
CONTACT
 If you would like to be
 informed if there is
 new tutorial, please
 submit your e-mail to
 us. Thank you.
 
 
5. CREATE POLYGON IN OPENGL
1. If we want to create a Triangle, we have to specify three points, let's say V0 for first point, V1 for second point and V2 for third point. Identify the coordinate of each of the point. Refer to this diagram for easier understanding:
2. This time in the same program (main.cpp), replace line 17 (glutSolidCube...) with line 17 to 21 in this program. GL_TRIANGLES will be used to create the triangle.
    glBegin(GL_TRIANGLES);
        glVertex3f( 0.0, 0.0, 0.0);        
// V0 (    0,  0,  0 )
        glVertex3f( 1.0f, 0.0, 0.0);      
//  V1 (    1,  0,  0 )
        glVertex3f( 0.5f, 1.0f, 0.0);     
//  V2 ( 0.5,  1,  0 )
    glEnd();
3. The ouput of the program will be like this:
4. If we want to create a Square, we have to specify four points, let's say V0 for first point, V1 for second point, V2 for third point and V3 for fourth point. Identify the coordinate of each of the point. Refer to this diagram for easier understanding:
5. This time in the same program (main.cpp), replace line 17 (glutSolidCube...) with line 17 to 21 in this program. GL_QUADS will be used to create the square or rectangle.
    glBegin(GL_QUADS);
        glVertex3f( 0.0, 0.0, 0.0);        
// V0 (0,0,0)
        glVertex3f( 1.0f, 0.0, 0.0);       
// V1 (1,0,0)
        glVertex3f( 1.0f, 1.0f, 0.0);      
// V2 (1,1,0)
        glVertex3f( 0.0, 1.0f, 0.0);        
// V3 (0,1,0)
    glEnd();
6. The ouput of the program this time will be like this:
7. If we want to create a polygon which have more than four points, we have to specify all its' points, let's say V0 for first point, V1 for second point, V2 for third point, V3 for fourth point and V4 for the fifth point. Identify the coordinate of each of the point. Refer to this diagram for easier understanding:
8. This time in the same program (main.cpp), replace line 17 (glutSolidCube...) with line 17 to 21 in this program. GL_POLYGON will be used to create the polygons which have more than four points.
    glBegin(GL_POLYGON);
        glVertex3f( 0.0, 0.0, 0.0);      
// V0 ( 0, 0, 0)
        glVertex3f( 1.0f, 0.0, 0.0);     
// V1 ( 1, 0, 0)
        glVertex3f( 1.0f, 1.0f, 0.0);    
// V2 ( 1, 1, 0)
        glVertex3f( 0.5f, 1.5f, 0.0);    
// V3 (0.5, 1.5, 0)
        glVertex3f( 0.0, 1.0f, 0.0);      
// V4 ( 0, 1, 0)
    glEnd();
9. The ouput of the program will be like this:
10. Now, you can try to combine all of the polygons inside your program. Enjoy :)

PREVIOUS   INDEX   NEXT
Copyright © 2005 VREMPIRE LAIR - All rights reserved.