Your IP : 216.73.216.40


Current Path : /var/www/html/venkat/check3/file/cg2013/pawan/
Upload File :
Current File : /var/www/html/venkat/check3/file/cg2013/pawan/RIT2013030.cpp

#include <bits/stdc++.h>
#include <GL/gl.h>		   // Open Graphics Library (OpenGL) header
#include <GL/glut.h>	   // The GL Utility Toolkit (GLUT) Header


using namespace std;

#define pi 3.14159265358979323846

#define KEY_ESCAPE 27

typedef struct {
    int width;
	int height;
	char* title;

	float field_of_view_angle;
	float z_near;
	float z_far;
} glutWindow;

glutWindow win;


void display() 
{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);		     // Clear Screen and Depth Buffer
	glLoadIdentity();
	glTranslatef(0.0f,0.0f,-3.0f);			
 
	/*
	 * Triangle code starts here
	 * 3 verteces, 3 colors.
	 */
	glBegin(GL_TRIANGLES);					
	//	glColor3f(0.0f,0.0f,1.0f);			
		glVertex3f( -1.0f, 1.0f, 0.0f);		
	//	glColor3f(0.0f,1.0f,0.0f);			
		glVertex3f(1.0f,1.0f, 0.0f);		
	//	glColor3f(1.0f,0.0f,0.0f);			
		glVertex3f( 0.0f,-1.0f, 0.0f);		
	glEnd();				
	 glBegin(GL_TRIANGLES);
              glColor3f(0.0f,0.0f,1.0f);                      
                glVertex3f( -0.7f, 0.7f, 0.0f);
              glColor3f(0.0f,1.0f,0.0f);                      
                glVertex3f(-0.1f,0.7f, 0.0f);
              glColor3f(1.0f,0.0f,0.0f);                      
                glVertex3f( -0.1f,-0.5f, 0.0f);
        glEnd();
	 glBegin(GL_TRIANGLES);
              glColor3f(0.0f,0.0f,1.0f);
                glVertex3f( 0.1f, 0.7f, 0.0f);
              glColor3f(0.0f,1.0f,0.0f);
                glVertex3f(0.7f,0.7f, 0.0f);
              glColor3f(1.0f,0.0f,0.0f);
                glVertex3f( 0.1f,-0.5f, 0.0f);
        glEnd();
 
	glutSwapBuffers();
}


void initialize () 
{
    glMatrixMode(GL_PROJECTION);												// select projection matrix
    glViewport(0, 0, win.width, win.height);									// set the viewport
    glMatrixMode(GL_PROJECTION);												// set matrix mode
    glLoadIdentity();															// reset projection matrix
    GLfloat aspect = (GLfloat) win.width / win.height;
	gluPerspective(win.field_of_view_angle, aspect, win.z_near, win.z_far);		// set up a perspective projection matrix
    glMatrixMode(GL_MODELVIEW);													// specify which matrix is the current matrix
    glShadeModel( GL_SMOOTH );
    glClearDepth( 1.0f );														// specify the clear value for the depth buffer
    glEnable( GL_DEPTH_TEST );
    glDepthFunc( GL_LEQUAL );
    glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST );						// specify implementation-specific hints
	glClearColor(0.0, 0.0, 0.0, 1.0);											// specify clear values for the color buffers								
}


void keyboard ( unsigned char key, int mousePositionX, int mousePositionY )		
{ 
  switch ( key ) 
  {
    case KEY_ESCAPE:        
      exit ( 0 );   
      break;      

    default:      
      break;
  }
}


double matrix[100][100]={};
double matrix1[100][100]={};
double ans[100][100]={};

void MatrixMul(int n1,int n2,int n3){

	memset(ans,0,sizeof(ans));
	for(int i = 0; i < n1; i++){
		for(int j = 0; j < n3; j++){
			for(int k = 0; k < n2; k++){
				ans[i][j] += matrix[i][k]*matrix1[k][j];
			}
		}
	}
	
}
void rotation_x3D(double x,double y,double z,double thita){
	double cosine = cos((thita*pi)/180);
	double sine = sin((thita*pi)/180);
	memset(matrix,0,sizeof(matrix));
	matrix[0][0] = 1;
	matrix[1][1] = cosine;
	matrix[1][2] = (-1)*sine;
	matrix[2][1] = sine;
	matrix[2][2] = cosine;
	matrix[3][3] = 1;
	
	matrix1[0][0] = x;
	matrix1[1][0] = y;
	matrix1[2][0] = z;
	matrix1[3][0] = 1;

	MatrixMul(4,4,1);
	//cout<<endl;
	//cout<<"x' = "<<(int)ans[0][0]<<endl;
	//cout<<"y' = "<<(int)ans[1][0]<<endl;
	//cout<<"z' = "<<(int)ans[2][0]<<endl;
	//cout<<endl;
}

void perspective_projection(double x,double y,double z,double d){
	memset(matrix,0,sizeof(matrix));
	matrix[0][0] = 1;
	matrix[1][1] = 1;
	matrix[2][2] = 1;
	matrix[3][2] = 1/d;
	
	matrix1[0][0] = x;
	matrix1[1][0] = y;
	matrix1[2][0] = z;
	matrix1[3][0] = 1;
	MatrixMul(4,4,1);
	x = (ans[3][0]!=0?ans[0][0]/ans[3][0]:ans[0][0]);
	y = (ans[3][0]!=0?ans[1][0]/ans[3][0]:ans[1][0]);
	z = (ans[3][0]!=0?ans[2][0]/ans[3][0]:ans[2][0]);
	printf("%lld %lld %lld\n",(long long int )x,(long long int) y,(long long int) z);
}

int main(int argc, char **argv)
{

	// set window values
	win.width = 640;
	win.height = 600;
	win.field_of_view_angle = 45;
	win.z_near = 1.0f;
	win.z_far = 300.0f;

	// initialize and run program
	glutInit(&argc, argv);                                      // GLUT initialization
	glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH );  // Display Mode
	glutInitWindowSize(win.width,win.height);					// set window size
	glutCreateWindow(win.title);								// create Window
	glutDisplayFunc(display);									// register Display Function
	glutIdleFunc( display );									// register Idle Function
    	glutKeyboardFunc( keyboard );								// register Keyboard Handler
	initialize();
	glutMainLoop();
	double mat[][3] = {-25,20,0,
			  0,20,0,
			  25,20,0,
			  -10,10,0,
			  -6,10,0,
			  6,10,0,
			  10,10,0,
			 -6,-20,0,
			  6,-20,0,
			 0,-40,0};
	int a[11][3];
		
	int i;	
	printf("Original triangle\n");
	for(i = 0; i < 10; i++)
	{
		printf("%lld %lld %lld\n",(long long int) mat[i][0],(long long int) mat[i][1],(long long int) mat[i][2]);
	}
	printf("Rotating triangle by 80 degree about x axis\n");
	for(i = 0; i < 10; i++)
	{
		rotation_x3D(mat[i][0],mat[i][1],mat[i][2],80);
		a[i][0] = ans[0][0];
		a[i][1] = ans[1][0];
		a[i][2] = ans[2][0];
		printf("%d %d %d\n",a[i][0],a[i][1],a[i][2]);

	}
	printf("Perspective projection\n");
	for(i = 0; i < 10; i++)
	{
		perspective_projection(a[i][0],a[i][1],a[i][2],2);
	}
	return 0;




}