Perspective Projection In Opengl Using C

Perspective Projection In Opengl Using C

#include<stdio.h>
#include <GL/glut.h>
#include<math.h>
float w, h, ty1 = 0,ty=0, tx1 = 0;
float tx=0;
float ORG[3] = {0,0,0};

float XP[3] = {100,0,0}, XN[3] = {-100,0,0},
YP[3] = {0,100,0}, YN[3] = {0,-100,0},
ZP[3] = {0,0,100}, ZN[3] = {0,0,-100};


float points[8][4] =
{
{0.0f, 0.0f, 0.0f, 2.0f},
{2.0f, 0.0f, 0.0f, 2.0f},
{2.0f, 2.0f, 0.0f, 2.0f},
{0.0f, 2.0f, 0.0f, 2.0f},
{0.0f, 2.0f, 2.0f, 2.0f},
{0.0f, 0.0f, 2.0f, 2.0f},
{2.0f, 0.0f, 2.0f, 2.0f},
{2.0f, 2.0f, 2.0f, 2.0f}
};

void matrixmultiply(float a[8][4] , float b[4][4])
{       int i,j,k;
float c[8][4] = {0};
for( i = 0 ; i < 8;i++)
{
for( j =0 ; j < 4 ;j++)
{
float sum = 0;
for( k = 0; k < 4; k++)
{
sum+= a[i][k]*b[k][j];
}
c[i][j] = sum;
}
}

for(i= 0 ; i< 8;i++)
{
for(j = 0;j < 4;j++)
{
points[i][j] = c[i][j];

                         

}

}
}

void translate(float Tx, float Ty,float Tz)
{
float b[4][4];
b[0][0] = 1;
b[0][1] = 0;
b[0][2] = 0;
b[0][3] = 0;
b[1][0] = 0;
b[1][1] = 1;
b[1][2] = 0;
b[1][3] = 0;
b[2][0] = 0;
b[2][1] = 0;
b[2][2] = 1;
b[2][3] = 0;
b[3][0] = Tx;
b[3][1] = Ty;
b[3][2] = Tz;
b[3][3] = 1;
matrixmultiply(points,b);
}

void translateX (void)
{
translate(0.5,0,0);
}
void translateNX (void)
{
translate(-0.5,0,0);
}
void translateY (void)
{
translate(0,0.5,0);
}
void translateNY (void)
{
translate(0,-0.5,0);
}
void translateZ (void)
{
translate(0,0,0.5);
}
void translateNZ (void)
{
translate(0,0,-0.5);
}



void rotateX(void)
{

float cost=cos(10*(3.14/180));
float sint=sin(10*(3.14/180));
float b[4][4]=
{
{1,0,0,0},
{0,cost,sint,0},
{0,-sint,cost,0},
{0,0,0,1}
};


matrixmultiply(points,b);
}

void rotateY(void)
{

float cost=cos(10*(3.14/180));
float sint=sin(10*(3.14/180));
float b[4][4]={
{cost,0,-sint,0},
{0,1,0,0},
{sint,0,cost,0},
{0,0,0,1}
};


matrixmultiply(points,b);
}

void rotateZ(void)
{

float cost=cos(10*(3.14/180));
float sint=sin(10*(3.14/180));
float b[4][4]={
{cost,sint,0,0},
{-sint,cost,0,0},
{0,0,1,0},
{0,0,0,1}
};


matrixmultiply(points,b);
}




void reshape (int nw, int nh)
{
w = nw;
h = nh;
}


void Turn (int key, int x, int y)
{
switch (key) {
case GLUT_KEY_RIGHT: tx += 5;tx1 += 5; break;
case GLUT_KEY_LEFT : tx -= 5;tx1 -= 5; break;
case GLUT_KEY_UP : ty -= 5;ty1 -= 5; break;
case GLUT_KEY_DOWN : ty += 5;ty1 += 5; break;

}
}





void Draw_Axes (void)
{
glPushMatrix ();

glTranslatef (0.0, 0.0, -6);
glRotatef (ty1 , 1,0,0);
glRotatef (tx1, 0,1,0);

glLineWidth (2.0);

glBegin (GL_LINES);
glColor3f (1,0,0); // X axis is red.
glVertex3fv (XN);
glVertex3fv (XP );
glColor3f (0,1,0); // Y axis is green.
glVertex3fv (YN);
glVertex3fv (YP );
glColor3f (0,0,1); // z axis is blue.
glVertex3fv (ZN);
glVertex3fv (ZP );
glEnd();

glPopMatrix ();
}



void drawCube (void)
{
glPushMatrix ();
glRotatef (ty , 1,0,0);
glRotatef (tx, 0,1,0);
glTranslatef (0, 0, -6);
glBegin (GL_QUADS);

//Front Side
glColor3f( 0.9, 0.2, 0.2 );
glVertex3f( points[0][0],points[0][1],points[0][2] );
glVertex3f( points[1][0],points[1][1],points[1][2] );
glVertex3f( points[2][0],points[2][1],points[2][2] );
glVertex3f( points[3][0],points[3][1],points[3][2] );

//Back Side
glColor3f( 0.7, 0.3, 0.7 );
glVertex3f( points[5][0],points[5][1],points[5][2] );
glVertex3f( points[6][0],points[6][1],points[6][2] );
glVertex3f( points[7][0],points[7][1],points[7][2] );
glVertex3f( points[4][0],points[4][1],points[4][2] );


//Left Side
glColor3f( 0.5, 0.5, 0.1 );
glVertex3f( points[0][0],points[0][1],points[0][2] );
glVertex3f( points[5][0],points[5][1],points[5][2] );
glVertex3f( points[4][0],points[4][1],points[4][2] );
glVertex3f( points[3][0],points[3][1],points[3][2] );



//Right Side
glColor3f( 0.0, 0.7, 0.8 );
glVertex3f( points[1][0],points[1][1],points[1][2] );
glVertex3f( points[6][0],points[6][1],points[6][2] );
glVertex3f( points[7][0],points[7][1],points[7][2] );
glVertex3f( points[2][0],points[2][1],points[2][2] );


//Top
glColor3f( 0.9, 0.2, 0.6 );
glVertex3f( points[3][0],points[3][1],points[3][2] );
glVertex3f( points[2][0],points[2][1],points[2][2] );
glVertex3f( points[7][0],points[7][1],points[7][2] );
glVertex3f( points[4][0],points[4][1],points[4][2] );

//Bottom
glColor3f( 1.0, 1.0, 1.0 );
glVertex3f( points[0][0],points[0][1],points[0][2] );
glVertex3f( points[1][0],points[1][1],points[1][2] );
glVertex3f( points[6][0],points[6][1],points[6][2] );
glVertex3f( points[5][0],points[5][1],points[5][2] );


glEnd();
glRotatef (45, 0,0,1);
glPopMatrix ();
}





void display (void)
{
glClearColor( 0, 0, 0, 1 );
   
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

    glMatrixMode( GL_PROJECTION );
    glLoadIdentity();
    int w = glutGet( GLUT_WINDOW_WIDTH );
    int h = glutGet( GLUT_WINDOW_HEIGHT );
 gluPerspective( 60, w / h, 0.1, 100 );

    glMatrixMode( GL_MODELVIEW );
    glLoadIdentity();


Draw_Axes ();
drawCube();


glutSwapBuffers ();
}

int main (int argc, char** argv)
{
glutInit(&argc, argv);
glutInitWindowSize (500, 500);
glutInitWindowPosition (300, 300);
glutInitDisplayMode (GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGB);
glutCreateWindow ("Projection");
glutDisplayFunc (display);
glutIdleFunc (display);
glutReshapeFunc (reshape);
glutSpecialFunc (Turn);


glClearColor (0.1, 0.2, 0.1, 1.0);
glEnable (GL_DEPTH_TEST);
glMatrixMode (GL_PROJECTION);
int w = glutGet( GLUT_WINDOW_WIDTH );
int h = glutGet( GLUT_WINDOW_HEIGHT );
 gluPerspective( 60, w / h, 0.1, 100 );
glMatrixMode (GL_MODELVIEW);

glutMainLoop ();

return 0;

Related Posts