Viewport Window in Opengl Using C

Viewport Window in Opengl Using C


#include <stdio.h>
#include <stdlib.h>
#include "GL/freeglut.h"
#include "GL/gl.h"

float WXMax, WXMin, WYMax, WYMin, WX[10], WY[10];
float VXMax, VXMin, VYMax, VYMin, VX[10], VY[10];
float SX, SY, TX, TY;
float WlengthX, WlengthY;
float VlengthX, VlengthY;
int i,n,vertex;

void renderFunction()
{
    float tempX, tempY;
    float wx[10], wy[10];
    for(i=1;i<=vertex;i++){
   tempX = WX[i] - WXMin;
       if(tempX/WlengthX >= 0.5){
    wx[i] = (tempX/WlengthX - 0.5) * 2;
       }
   else{
wx[i] = -(tempX/WlengthX)*2;
       }printf("wx %d %f\n",i,wx[i]);
    }

for(i=1;i<=vertex;i++){  
tempY = WY[i] - WYMin;
  if(tempY/WlengthY >= 0.5){
wy[i] = (tempY/WlengthY - 0.5) * 2;
   }else{
wy[i] = -(tempY/WlengthY)*2;
   }printf("wy %d %f\n",i,wy[i]);
}
printf("wow \n");

    glClearColor(0.0, 0.0, 0.0, 0.0);
    glClear(GL_COLOR_BUFFER_BIT);
    glColor3f(1.0, 1.0, 1.0);
    glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
    glBegin(GL_POLYGON);
for(i=1;i<=vertex;i++){
        glVertex2f(wx[i], wy[i]);
}      
//glVertex2f(-0.5, 0.5);
        //glVertex2f(0.5, 0.5);
        //glVertex2f(0.5, -0.5);
    glEnd();
    glFlush();
}

void renderFunction1()
{
    float tempX, tempY;
    float vx[10], vy[10];
    for(i=1;i<=vertex;i++){
    tempX = VX[i] - VXMin;
printf(" %d tempX %f , VlengthX %f \n",i,tempX,VlengthX);
printf(" .. %f \n",tempX/VlengthX);
    if(tempX/VlengthX >= 0.5){
vx[i] = (tempX/VlengthX - 0.5) * 2;
printf("in the if .. vx %f\n",vx[i]);
    }else{
vx[i] = -(tempX/VlengthX)*2;
printf("in the else .. vx %f\n",vx[i]);
    }
    }
//printf("vx %f\n",vx);
    for(i=1;i<=vertex;i++){
    tempY = VY[i] - VYMin;
printf("%d tempY %f \n",i,tempY);
    if(tempY/VlengthY >= 0.5){
vy[i] = (tempY/VlengthY - 0.5) * 2;
    }else{
vy[i] = -(tempY/VlengthY)*2;
    }printf("vy %d %f \n",i,vy[i]);
    }


    glClearColor(0.0, 0.0, 0.0, 0.0);
    glClear(GL_COLOR_BUFFER_BIT);
    glColor3f(1.0, 1.0, 1.0);
    glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
    glBegin(GL_POLYGON);
for(i=1;i<=vertex;i++){
        glVertex2f(vx[i], vy[i]);
}
        //glVertex2f(-0.5, 0.5);
        //glVertex2f(0.5, 0.5);
        //glVertex2f(0.5, -0.5);
    glEnd();
    glFlush();
}

float calculateScalingFactorX(float WXMax, float WXMin, float VXMax, float VXMin){
float sx = (VXMax - VXMin)/(WXMax - WXMin);
printf("sx - %f \n",sx);
return sx;
}

float calculateScalingFactorY(float WYMax, float WYMin, float VYMax, float VYMin){
float sy = (VYMax - VYMin)/(WYMax - WYMin);
printf("sy - %f \n",sy);
return sy;
}

float calculateTransformationX(float WXMax, float WXMin, float VXMax, float VXMin){
float tx = ((WXMax * VXMin) - (WXMin * VXMax))/(WXMax - WXMin);
printf("tx - %f \n",tx);
return tx;
}

float calculateTransformationY(float WYMax, float WYMin, float VYMax, float VYMin){
float ty = ((WYMax * VYMin) - (WYMin * VYMax))/(WYMax - WYMin);
printf("ty - %f \n",ty);
return ty;
}

int main(int argc, char** argv){
vertex=3;


printf("Enter XMax, XMin, YMax, YMin \n");
scanf("%f %f %f %f", &WXMax, &WXMin, &WYMax, &WYMin);
printf("Enter XMax, XMin, YMax, YMin \n");
scanf("%f %f %f %f", &VXMax, &VXMin, &VYMax, &VYMin);

for(i=0;i<vertex;i++){
printf("Enter the object Coordinates %d\n",i);
scanf("%f %f", &WX[i], &WY[i]);
}
WlengthX = WXMax - WXMin;
WlengthY = WYMax - WYMin;
VlengthX = VXMax - VXMin;
VlengthY = VYMax - VYMin;
printf("VlengthX, VlengthY %f %f \n",VlengthX,VlengthY);

SX = calculateScalingFactorX(WXMax, WXMin, VXMax, VXMin);
SY = calculateScalingFactorY(WYMax, WYMin, VYMax, VYMin);
TX = calculateTransformationX(WXMax, WXMin, VXMax, VXMin);
TY = calculateTransformationY(WYMax, WYMin, VYMax, VYMin);
printf("sx,sy,tx,ty %f %f %f %f  \n",SX,SY,TX,TY);
for(i=1;i<=vertex;i++){
VX[i] = SX * WX[i] + TX;
VY[i] = SY * WY[i] + TY;
printf("VX,VY %d %f %f\n",i, VX[i], VY[i]);
}

glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE);
glutInitWindowSize(WlengthX, WlengthY);
glutInitWindowPosition(WXMin, WYMin);
glutCreateWindow("OpenGL - World Port Transformation \n");
glutDisplayFunc(renderFunction);

glutInitDisplayMode(GLUT_SINGLE);
glutInitWindowSize(VlengthX, VlengthY);
glutInitWindowPosition(VXMin, VYMin);
glutCreateWindow("OpenGL - View Port Transformation");
glutDisplayFunc(renderFunction1);

glutMainLoop();  
return 0;
}

Related Posts