Transformation Of Triangl in Opengl Using C

Transformation Of Triangl in Opengl Using C

#include<GL/glut.h>
#include<stdio.h>
#include<math.h>
#define SIDE 50
#define TILT 10
#define TX 10
#define TY 10
#define SXP 1.5
#define SYP 1.5
#define SXM 0.5
#define SYM 0.5
#define ANGLE 30
#define UP 37
#define DOWN 38
#define RIGHT 39
#define LEFT 40

float rect[4][3] = {{0,0,1},{SIDE,0,1},{SIDE-TILT,SIDE,1},{-TILT,SIDE,1}};

void translate(int TransX,int TransY);
void scale(float ScaleX, float ScaleY);
void initialize();
void drawRect();
void transform(GLubyte key, GLint x, GLint y);

int main(int argc, char **argv)
{
glutInit(&argc,argv);
glutInitWindowPosition(0,0);
glutInitDisplayMode(GLUT_RGB|GLUT_DEPTH|GLUT_SINGLE);
glutInitWindowSize(WINDOWSIZE,WINDOWSIZE);
glutCreateWindow("Transformation1");
glEnable(GL_DEPTH_TEST);
initialize();
glutDisplayFunc(drawRect);
glutKeyboardFunc(transform);
glutMainLoop();
return 0;
}

void initialize()
{
glClearColor(0.0,0.0,0.0,0.0);
glShadeModel(GL_FLAT);
glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
  glMatrixMode(GL_PROJECTION);              
  glLoadIdentity();
glOrtho(-WINDOWSIZE/2, WINDOWSIZE/2, -WINDOWSIZE/2, WINDOWSIZE/2,-1,1);
    glEnable(GL_DEPTH_TEST);
}


void drawRect()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glBegin(GL_QUADS);

// Red Coloured
glColor3f(1.0,0.0,0.0);
glVertex2f(rect[0][0],rect[0][1]);
glVertex2f(rect[1][0],rect[1][1]);
glVertex2f(rect[2][0],rect[2][1]);
glVertex2f(rect[3][0],rect[3][1]);

glEnd();
glFlush();
}

void transform(GLubyte key, GLint x, GLint y)
{
float currentX,currentY;
switch(key)
{
case 119://w up
translate(0,TY);
glutPostRedisplay();
break;
case 97://a left
translate(-TX,0);
glutPostRedisplay();
break;
case 115://s down
translate(0,-TY);
glutPostRedisplay();
break;
case 100://d right
translate(TX,0);
glutPostRedisplay();
break;
case 112://p enlarge
currentX = rect[0][0];
currentY = rect[0][1];
translate(-1*currentX,-1*currentY);
scale(SXP,SYP);
translate(currentX,currentY);
glutPostRedisplay();
break;
case 109://m compression
currentX = rect[0][0];
currentY = rect[0][1];
translate(-1*currentX,-1*currentY);
scale(SXM,SYM);
translate(currentX,currentY);
glutPostRedisplay();
break;
case 111 ://ESC
break;

}
}

void translate(int TransX,int TransY)
{
int i=0,j=0;
for(i=0;i<4;i++)
{
rect[i][0] = rect[i][0] + TransX;
rect[i][1] = rect[i][1] + TransY;
}
}

void scale(float ScaleX, float ScaleY)
{
int i=0,j=0;
for(i=0;i<4;i++)
{
rect[i][0] = rect[i][0]*ScaleX;
rect[i][1] = rect[i][1]*ScaleY;
}
}

Related Posts