Thursday, July 16, 2015

Window-To-Viewport Mapping.cpp

W2V2 


#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;
     int i=0; 
      int j=0; 
    //setViewport( left,  right,  bottom,  top)
   
     for(int bottomX=0; bottomX<screenWidth; bottomX+=width) 
      { 
           for(int bottomY=0;  bottomY<screenHeight; bottomY+=height) 
           { 
                setViewport(bottomX, bottomX + width, bottomY, bottomY + height); // set the viewport 
                if((j%2==0 || i%2==1) && (j%2==1 || i%2==0)){ 
                setWindow(0.0, 640.0, 0.0, 480.0);  
                drawShape(); 
                } 
                else{ 
                     setWindow(0.0,640.0, 480.0, 0.0);  
                     drawShape(); 
                } 
                     j++; 
           } 
           i++; 
      } 
}

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