Thursday, July 16, 2015

Window-To-Viewport Mapping.cpp


W2V1 


#include <GL/glut.h>

const int screenWidth = 640;
const int screenHeight = 480;       

// defining the Window
void setWindow(GLdouble left, GLdouble right, GLdouble bottom, GLdouble top)
{
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(left, right, bottom, top);
}

// setting the Viewport
void setViewport(int left, int right, int bottom, int top)
{
    glViewport(left, bottom, right - left, top - bottom);
}
void myInit(void)
{
    glClearColor (1.0, 1.0, 1.0, 0.0);   // set black background color
    glColor3f (1.0f, 0.0f, 0.0f);       // set the drawing color
}

void drawShape()
{  
    glBegin(GL_LINE_LOOP);
        glVertex2i(150,100);
        glVertex2i(350, 100);
        glVertex2i(400, 300);
        glVertex2i(500, 300);
        glVertex2i(250,450);
        glVertex2i(00, 300);
        glVertex2i(100, 300);
    glEnd();
    glFlush();
}

void myDisplay(void)
{
    glClear(GL_COLOR_BUFFER_BIT);        // clear the screen
    setWindow(0.0, 640.0, 0.0, 480.0);  // set the window
   
    int bottomX = 0;
    int bottomY = 0;
    int width = 64;
    int height = 48;
    //setViewport( left,  right,  bottom,  top
    while(bottomY<screenHeight)
    {
         bottomX =0 ;
        while(bottomX<screenWidth)
        {
         //setViewport( left,  right,  bottom,  top)
   
        setViewport(bottomX, bottomX + width, bottomY,  bottomY + height);  // set the viewport

        drawShape();
          bottomX =bottomX+width ;
        }
         bottomY =bottomY+height ;
    }
}

int main(int argc, char** argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
    glutInitWindowSize(screenWidth,screenHeight); //setting the SCREEN WINDOW size
    glutCreateWindow("Window-To-Viewport Mapping");
    glutDisplayFunc(myDisplay);
    myInit();
    glutMainLoop();

    return 0;
}

No comments:

Post a Comment