Friday 13 December 2013

Filled Under:

use GL_QUADS function and Translation.

Use GL_QUADS function and Transformation:

Use this primitive to draw individual convex quadrilaterals. OpenGL renders a quadrilateral for each group of four vertices. . If n isn't a multiple of 4, OpenGL ignores the excess vertices. so we can say that GL_QUADS require vertices.
so we take a example that is clear how to use GL_QUADS function in openGL 
first of we create a window for drawing . how to create the window i have been explain it in my first tutorial here is the link how to create window here
so after initialize the window in glutDisplayFunc(Draw_rectangle);  we call a function Draw_rectangle() here we use the GL_QUADS function

code:

draw_rectangle()
{
glBegin(GL_QUADS);        
    glColor3f(0.0f,1.0f,0.0f);    
    glVertex2f( 0.2, 0.49);    
    glVertex2f(0.4, 0.49);   
    glVertex2f(0.4, 0.76);    
    glVertex2f( 0.2, 0.76);    
   glEnd();
glFlush();
}
 so glBegin() indicate the start of quads vertices and glEnd() indicate the end of vertices of quads.
glFlush(); indicate the end of drawing and output will be display .so the output of the following code will be

screen short:

translation:

in above fig rectangle is showing in left hand side but now i want it to display it in right side of the window so one thing i change the value  of all vertices so it will be time consuming but openGl provide the builtian function rather we change the value of all vertices we just use function that move our body from one position to other the function that we use is glTranslatef(x,y,z); here f in indicate we will give the value in floating point you can also use i for integer. 
so now the following code

code: 

Draw_rentangle() {

glPushMatrix(); 
glTranslatef(0.50f, 0.0f, 0.0f);
glBegin(GL_QUADS);        
    glColor3f(0.0f,1.0f,0.0f);    
    glVertex2f( 0.2, 0.49);    
    glVertex2f(0.4, 0.49);   
    glVertex2f(0.4, 0.76);    
    glVertex2f( 0.2, 0.76);    
        glEnd();
glPopMatrix();
glFlush();
}
screen short:
Before
after using glTranslate function.
After
here we use two new function glPushMatrix(); and glPopMatrix(); these function are just like glBegin() and glEnd() function glPushMatrix() save where from translation start i.e it indicate start of translation and glPopMatrix(); end of translation




1 comments: