Open In App

Two Dimension Viewing Function

Last Updated : 18 Jan, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Two-dimension viewing in Computer graphics is a technique to map the world coordinates system (actual coordinates of the object in the real world) to device coordinates (actual coordinates of the object on the output screen like a monitor).

2D Viewing Transformation Process

 

2D Viewing Function

In Computer graphics to perform viewing transformations, we need a tool called OpenGL. The basic OpenGL libraries do not support functions for 2D viewing transformations as they are made especially for 3D viewing transformations. But we can apply 3D viewing routines to 2D viewing scenes.  We can use two-dimensional routines along with the OpenGL viewport function.

Two Dimensional routines that can be used are:

  • GLU library functions
  • GLUT library functions

Some functions are:

1. GLU Clipping-Window Function: This function is used to define a two-dimensional clipping window,

    gluOrtho2D (xwmin, xwmax, ywmin, ywmax);  

//xwmin for left width, xwmax for right width,  

ywmin for bottom width, ywmax for top width 

2. GLUT Display Window: This function is used to initialize GLUT,

 glutInit (&argc, argv);

 For choosing GLUT function dimensions and position:

glutCreateWindow (“Title of Display Window”); 

glutInitWindowPosition (xTopLeft, yTopLeft);

glutInitWindowSize (dwWidth, dwHeight); 

Function to expand the current display window to fill the screen:

glutFullScreen ( );

Function to reset the screen location for the current display window:

glutPositionWindow (xNewTopLeft, yNewTopLeft);

Function to convert the current display window to an icon in the form of some small symbol or picture that represents the window:

glutIconifyWindow ( );

We can change the label of the icon with the following command:

glutSetIconTitle (“icon_name”);

This function invokes the particular function that the user want to display at the current display window, if there are more than one display window:

glutDisplayFunc (callback_func);   

//This routine, called callback_func is an example,  

is referred to as a callback function because it is the routine that                     

  // is executed when GLUT determines that display 

window has contents to be updated.   

Finally we have the final GLUT command that directs the execution of the program:

 glutMainLoop ( );    

After this command gets executed display windows and their graphic contents are sent to the screen. Then the program continually checks for new “events, ” such as input from a mouse, keyboard or graphics tablet after entering to GLUT processing loop.

To show two views of a triangle using 2D viewing functions.

 Example 1:

C++




void displayTriangle()
{
    // defining initial position of triangle
    wcPt2D vertices[3] = { { -40.0, -20.0 },
                           { 40.0, -20.0 },
                           { 0.0, 40.0 } };
    glClear(GL_COLOR_BUFFER_BIT);
    // clear display window
    glColor3f(1, 1, 0);
    // to fill yellow color to the triangle
    glViewport(0, 0, 200, 200);
    // to set position of the yellow triangle
    triangle(vertices);
    // to display yellow rotated triangle
  
    // to rotate triangle and display at
    // right half of display window 
    glColor3f(0.0f, 0.0f, 1.0f);
    // change triangle color to blue
    glViewport(300, 0, 300, 300);
    // to set position of the blue triangle
    glRotatef(90.0, 0.0, 0.0, 1.0);
    // to rotate the triangle about z axis.
    triangle(vertices);
    // to display green rotated triangle
    glFlush();
    // to execute all functions
}
  
void main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
    glutInitWindowPosition(60, 60);
    glutInitWindowSize(500, 250);
    glutCreateWindow("2D viewing function Example");
    init();
    glutDisplayFunc(displayTriangle);
    glutMainLoop();
}


Output:

two views of a triangle using 2D viewing functions

 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads