MyTricksLab.com (Computer Science & Tech Tutorials)




      PRACTICING OPENGL-
      PRIMITIVES
OPENGL OUTPUT PRIMITIVES




 Each geometric object is described by a set of vertices and the type of primitive to be
 drawn. Whether and how the vertices are connected is determined by the primitive
 type.
OPENGL PRIMITIVES- GL_POINTS
 Lab Activity (1): Add the following code to display() method.

         glPointSize(4.0);              /* specify point to be 4 pixels thick */
         glBegin(GL_POINTS);
             glVertex2f(0.0f, 2.0f);    //note 2D form
             glVertex2f(1.0f, 2.0f);
             glVertex2f(0.0f, -2.0f);
             glVertex2f(-1.0f, 0.0f);
         glEnd();
OPENGL PRIMITIVES- LINES
   Three different line primitives can be created:


GL_LINES: glColor3f(0.0,0.0,0.0);
          glLineWidth(5.0);

                 glBegin(GL_LINES);
                         glVertex2f (-20.0, -20.0);
                         glVertex2f (20.0, 20.0);

                            glColor3f(1.0,1.0,0.0);
                            glVertex2f (10.0, -20.0);
                            glVertex2f (10.0, 20.0);
                 glEnd();
OPENGL PRIMITIVES- LINES
GL_LINE_STRIP:       Draws a connected set of line segments from the first vertex to the
 last.
         glColor3f(0.5,0.6,0.0);
         glLineWidth(4);
         glBegin(GL_LINE_STRIP);
             glVertex2f (5.0, 10.0);
             glVertex2f (10.0, 10.0);
             glVertex2f (10.0, 0.0);
             glVertex2f (5.0, 0.0);
         glEnd();
OPENGL PRIMITIVES- LINES
GL_LINE_LOOP:
        glColor3f(0.9,0.8,0.0);
        glLineWidth(4);
        glBegin(GL_LINE_LOOP);
                 glVertex2f (-10.0, 9.0);
                 glVertex2f (5.0, 9.0);
                 glVertex2f (5.0, -1.0);
                 glVertex2f (-10.0, -1.0);
        glEnd();
OPENGL PRIMITIVES –GL_TRIANGES
glColor3f(1.0,0.0,1.0);
glLineWidth(4);                  -The default is GL_FILL for both front-and back-facing
                                 polygons.
glBegin(GL_TRIANGLES);
        glVertex2f (-10.0, -10.0);
        glVertex2f (-5.0, -10.0);
        glVertex2f (-5.0, -5.0);
glEnd();

   Insert this command before glLineWidth, and note the effect.
                      glPolygonMode(GL_FRONT, GL_LINE);

                                 GL_BACK                                GL_POINT
                                  or                                     or
                         GL_FRONT_AND_BACK                              GL_FILL
OPENGL PRIMITIVES –
GL_TRIANGLE_STRIP
glColor3f(0.0,1.0,0.0);
glLineWidth(4);
glPolygonMode(GL_FRONT, GL_LINE);
glBegin(GL_TRIANGLE_STRIP);
    glVertex2f (-10.0, -10.0);
    glVertex2f (-19.0, -10.0);
    glVertex2f (-10.0, -20.0);
         glVertex2f (-18.0,-19.0);      //create 2ndtriangle
         glVertex2f (-5.0, -25.0);     //create 3rd triangle
         glVertex2f (-20.0, -29.0);   //create 4th triangle
glEnd();

 Note:
 • Order of points does matter!
 • If the vertices are defined clockwise, the front of the polygon will
 be shown. Otherwise, the back of the polygon will be shown.
OPENGL PRIMITIVES –
GL_TRIANGLE_FAN
Draws a connected set of triangles. One triangle is defined for each vertex
  presented after the first two vertices.
Note: Order of points does matter!
                        glColor3f(0.0,0.0,1.0);
                        glLineWidth(4);

                       /* draw only the outline of polygon */
                       glPolygonMode(GL_FRONT, GL_LINE);

                       glBegin( GL_TRIANGLE_FAN);
                           glVertex2f (12.0, -30.0);
                           glVertex2f(30.0, -30.0);
                           glVertex2f (30.0, -20.0);
                           glVertex2f (22.0, -15.0);
                           glVertex2f (12.0, -12.0);
                       glEnd();
OPENGL PRIMITIVES –GL_QUADS
Note: Order of points does matter!
            /* creating 2 quadrilaterals */
            glColor3f(0.0,0.0,0.0);
            glPolygonMode(GL_FRONT, GL_LINE);
            glLineWidth(4);
            glBegin( GL_QUADS );
                 glVertex2f (-28.0, 25.0);
                 glVertex2f (-28.0, 10.0);
                 glVertex2f (-20.0, 10.0);
                 glVertex2f (-20.0, 20.0);

                glVertex2f (-15.0, 20.0);
                glVertex2f (-2.0, 20.0);
                glVertex2f (-2.0, 28.0);
                glVertex2f (-15.0, 28.0);
            glEnd();
OPENGL PRIMITIVES –
GL_QUAD_STRIP
/* creating 2 quadrilaterals using GL_QUAD_STRIP */
glColor3f(0.0,1.0,1.0);
glPolygonMode(GL_FRONT, GL_LINE);
glLineWidth(4);
glBegin( GL_QUAD_STRIP);
    glVertex2f (20.0, 10.0);
    glVertex2f (29.0, 13.0);
    glVertex2f (18.0, 20.0);
    glVertex2f (26.0, 20.0);



    glVertex2f (22.0, 25.0);
    glVertex2f (30.0, 25.0);

    glVertex2f (15.0, 30.0);
    glVertex2f (35.0, 30.0);
glEnd();
OPENGL PRIMITIVES –
GL_POLYGON
glBegin(GL_POLYGON);
  glVertex2f(2.0,1.0);
  glVertex2f(12.0,1.0);
  glVertex2f(14.0,3.0);
  glVertex2f(1.0,3.0);
glEnd();
CREATIVE DRAWING USING OPENGL
PRIMITIVES
LAB EXERCISE: TIME TO SHOW YOUR
CREATIVITY
                    “Sample 2D Scenary are given Lab folder”

1.   Show your best to Design your virtual World (2D Scene) onto graph
     paper.
           With Well defined coordinates Points of each object in Scene.

           Also Define Color of each Object Of Your Scene


1.   Use Open GL built in primitives to Implement your
     Designed virtual world (2D Scene).


       MyTricksLab.com (Computer Science & Tech Tutorials)

Practicing 2d drawing primitives

  • 1.
    MyTricksLab.com (Computer Science& Tech Tutorials) PRACTICING OPENGL- PRIMITIVES
  • 2.
    OPENGL OUTPUT PRIMITIVES Each geometric object is described by a set of vertices and the type of primitive to be drawn. Whether and how the vertices are connected is determined by the primitive type.
  • 3.
    OPENGL PRIMITIVES- GL_POINTS Lab Activity (1): Add the following code to display() method. glPointSize(4.0); /* specify point to be 4 pixels thick */ glBegin(GL_POINTS); glVertex2f(0.0f, 2.0f); //note 2D form glVertex2f(1.0f, 2.0f); glVertex2f(0.0f, -2.0f); glVertex2f(-1.0f, 0.0f); glEnd();
  • 4.
    OPENGL PRIMITIVES- LINES  Three different line primitives can be created: GL_LINES: glColor3f(0.0,0.0,0.0); glLineWidth(5.0); glBegin(GL_LINES); glVertex2f (-20.0, -20.0); glVertex2f (20.0, 20.0); glColor3f(1.0,1.0,0.0); glVertex2f (10.0, -20.0); glVertex2f (10.0, 20.0); glEnd();
  • 5.
    OPENGL PRIMITIVES- LINES GL_LINE_STRIP: Draws a connected set of line segments from the first vertex to the last. glColor3f(0.5,0.6,0.0); glLineWidth(4); glBegin(GL_LINE_STRIP); glVertex2f (5.0, 10.0); glVertex2f (10.0, 10.0); glVertex2f (10.0, 0.0); glVertex2f (5.0, 0.0); glEnd();
  • 6.
    OPENGL PRIMITIVES- LINES GL_LINE_LOOP: glColor3f(0.9,0.8,0.0); glLineWidth(4); glBegin(GL_LINE_LOOP); glVertex2f (-10.0, 9.0); glVertex2f (5.0, 9.0); glVertex2f (5.0, -1.0); glVertex2f (-10.0, -1.0); glEnd();
  • 7.
    OPENGL PRIMITIVES –GL_TRIANGES glColor3f(1.0,0.0,1.0); glLineWidth(4); -The default is GL_FILL for both front-and back-facing polygons. glBegin(GL_TRIANGLES); glVertex2f (-10.0, -10.0); glVertex2f (-5.0, -10.0); glVertex2f (-5.0, -5.0); glEnd();  Insert this command before glLineWidth, and note the effect. glPolygonMode(GL_FRONT, GL_LINE); GL_BACK GL_POINT or or GL_FRONT_AND_BACK GL_FILL
  • 8.
    OPENGL PRIMITIVES – GL_TRIANGLE_STRIP glColor3f(0.0,1.0,0.0); glLineWidth(4); glPolygonMode(GL_FRONT,GL_LINE); glBegin(GL_TRIANGLE_STRIP); glVertex2f (-10.0, -10.0); glVertex2f (-19.0, -10.0); glVertex2f (-10.0, -20.0); glVertex2f (-18.0,-19.0); //create 2ndtriangle glVertex2f (-5.0, -25.0); //create 3rd triangle glVertex2f (-20.0, -29.0); //create 4th triangle glEnd(); Note: • Order of points does matter! • If the vertices are defined clockwise, the front of the polygon will be shown. Otherwise, the back of the polygon will be shown.
  • 9.
    OPENGL PRIMITIVES – GL_TRIANGLE_FAN Drawsa connected set of triangles. One triangle is defined for each vertex presented after the first two vertices. Note: Order of points does matter! glColor3f(0.0,0.0,1.0); glLineWidth(4); /* draw only the outline of polygon */ glPolygonMode(GL_FRONT, GL_LINE); glBegin( GL_TRIANGLE_FAN); glVertex2f (12.0, -30.0); glVertex2f(30.0, -30.0); glVertex2f (30.0, -20.0); glVertex2f (22.0, -15.0); glVertex2f (12.0, -12.0); glEnd();
  • 10.
    OPENGL PRIMITIVES –GL_QUADS Note:Order of points does matter! /* creating 2 quadrilaterals */ glColor3f(0.0,0.0,0.0); glPolygonMode(GL_FRONT, GL_LINE); glLineWidth(4); glBegin( GL_QUADS ); glVertex2f (-28.0, 25.0); glVertex2f (-28.0, 10.0); glVertex2f (-20.0, 10.0); glVertex2f (-20.0, 20.0); glVertex2f (-15.0, 20.0); glVertex2f (-2.0, 20.0); glVertex2f (-2.0, 28.0); glVertex2f (-15.0, 28.0); glEnd();
  • 11.
    OPENGL PRIMITIVES – GL_QUAD_STRIP /*creating 2 quadrilaterals using GL_QUAD_STRIP */ glColor3f(0.0,1.0,1.0); glPolygonMode(GL_FRONT, GL_LINE); glLineWidth(4); glBegin( GL_QUAD_STRIP); glVertex2f (20.0, 10.0); glVertex2f (29.0, 13.0); glVertex2f (18.0, 20.0); glVertex2f (26.0, 20.0); glVertex2f (22.0, 25.0); glVertex2f (30.0, 25.0); glVertex2f (15.0, 30.0); glVertex2f (35.0, 30.0); glEnd();
  • 12.
    OPENGL PRIMITIVES – GL_POLYGON glBegin(GL_POLYGON); glVertex2f(2.0,1.0); glVertex2f(12.0,1.0); glVertex2f(14.0,3.0); glVertex2f(1.0,3.0); glEnd();
  • 13.
    CREATIVE DRAWING USINGOPENGL PRIMITIVES
  • 14.
    LAB EXERCISE: TIMETO SHOW YOUR CREATIVITY “Sample 2D Scenary are given Lab folder” 1. Show your best to Design your virtual World (2D Scene) onto graph paper.  With Well defined coordinates Points of each object in Scene.  Also Define Color of each Object Of Your Scene 1. Use Open GL built in primitives to Implement your Designed virtual world (2D Scene). MyTricksLab.com (Computer Science & Tech Tutorials)