Example #1
0
/* scales the input vector to the new length and returns it */
Vector3D *V3Scale(Vector3D *v, FixedPoint newlen) {
    FixedPoint len;
    
    len = V3Length(v);
    
    if (len != 0.0) {
	v->x = mulDiv(v->x,newlen,len);
	v->y = mulDiv(v->y,newlen,len);
	v->z = mulDiv(v->z,newlen,len);
    }
    return(v);
}
void myDrawOFF(){
	//PrintObj(&object);
  	obj = &object;
	
	for(i=0; i<obj->Nfaces; i++){
	if (i == 0) {
	glEnable(GL_TEXTURE_2D);
	glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
        glBindTexture(GL_TEXTURE_2D, TextureName);
	}
	glBegin(GL_TRIANGLE_FAN);
	for(j=0; j<obj->nv_face[i]; j++) {
	p.x = obj->vertices[obj->faces[i][1]].x - obj->vertices[obj->faces[i][0]].x;
	p.y = obj->vertices[obj->faces[i][1]].y - obj->vertices[obj->faces[i][0]].y;
	p.z = obj->vertices[obj->faces[i][1]].z - obj->vertices[obj->faces[i][0]].z;
	
	q.x = obj->vertices[obj->faces[i][2]].x - obj->vertices[obj->faces[i][1]].x;
	q.y = obj->vertices[obj->faces[i][2]].y - obj->vertices[obj->faces[i][1]].y;
	q.z = obj->vertices[obj->faces[i][2]].z - obj->vertices[obj->faces[i][1]].z;
	
	r = V3cross(p,q);
	rlength = V3Length(r);	
	//fprintf(stderr,"\n\n(Nx,Ny,Nz)  = (%f,%f,%f) %d\n\n",(r.x)/rlength,(r.y)/rlength,(r.z)/rlength,i);

	glNormal3f((r.x)/rlength,(r.y)/rlength,(r.z)/rlength);
	if (i == 0 && j == 0) glTexCoord2f(0.0,0.0);
	if( i == 0 && j == 1) glTexCoord2f(1.0,0.0);
	if( i == 0 && j == 2) glTexCoord2f(1.0,1.0);
	if( i == 0 && j == 3) glTexCoord2f(0.0,1.0);
	glVertex3f(obj->vertices[obj->faces[i][j]].x,obj->vertices[obj->faces[i][j]].y,obj->vertices[obj->faces[i][j]].z);
	}
   	glEnd();

	if(i == 0) glDisable(GL_TEXTURE_2D);
	
	} 
	
}
Example #3
0
/*
 *---------------------------------------------------------
 * Compute a rotation matrix around an arbitrary axis
 * specified by 2 points p1 and p2; theta is the angle
 * between the two planes that share the same edge
 * defined by p1 and p2
 *---------------------------------------------------------
 */
int MxRotateAxisAlain(Point3D p1, Point3D p2, double theta, Matrix4 *TM, Matrix4 *iTM)
{
	Point3D p;
	double dist, cosX, sinX, cosY, sinY;
	Matrix4 m1, m2, Identity;
	
	loadIdentity( &Identity );
	
	p.x = p2.x - p1.x;
	p.y = p2.y - p1.y;
	p.z = p2.z - p1.z;
	
	if( V3Length( &p ) < 0.0 ) return(FALSE);
	
	dist = sqrt( p.y * p.y + p.z * p.z );
	
	if(dist < DAMN_SMALL){
		cosX = 1.0;
		sinX = 0.0;
	}
	else{
		cosX =  p.z / dist;
		sinX = -p.y / dist;
	}
	
	cosY = dist;
	sinY = -p.x;
	
	loadIdentity( TM );
	
	TM->element[3][0] = -p1.x;
	TM->element[3][1] = -p1.y;
	TM->element[3][2] = -p1.z;
	
	MatrixCopy( TM, &m1);
	loadIdentity( &m2 );
	
	m2.element[1][1] = cosX;
	m2.element[2][1] = sinX;
	m2.element[1][2] = -sinX;
	m2.element[2][2] = cosX;
	
	MatMul( &m1, &m2, TM );
	MatrixCopy( TM, &m1 );
	loadIdentity( &m2 );
	
	m2.element[0][0] = cosY;
	m2.element[2][0] = sinY;
	m2.element[0][2] = -sinY;
	m2.element[2][2] = cosY;
	
	MatMul( &m1, &m2, TM );
	MatrixCopy( TM, &m1 );
	MatRotateZ( theta, &m2 );
	MatMul( &m1, &m2, TM );
	MatrixCopy( TM, &m1 );
	loadIdentity( &m2 );
	
	m2.element[0][0] = cosY;
	m2.element[2][0] = -sinY;
	m2.element[0][2] = sinY;
	m2.element[2][2] = cosY;
	
	MatMul( &m1, &m2, TM );
	MatrixCopy( TM, &m1 );
	loadIdentity( &m2 );
	
	m2.element[1][1] = cosX;
	m2.element[2][1] = -sinX;
	m2.element[1][2] = sinX;
	m2.element[2][2] = cosX;
	
	MatMul( &m1, &m2, TM );
	MatrixCopy( TM, &m1 );
	loadIdentity( &m2 );
	
	m2.element[3][0] = p1.x;
	m2.element[3][1] = p1.y;
	m2.element[3][2] = p1.z;
	
	MatMul( &m1, &m2, TM );
	
	return( MxInvert(TM, iTM) );
}
Example #4
0
/*
 *---------------------------------------------------------
 * Compute a rotation matrix around an arbitrary axis
 * specified by 2 points p1 and p2; theta is the angle
 * between the two planes that share the same edge
 * defined by p1 and p2
 *
 * The matrix TM is the matrix that brings the plane of
 * face 'n' into face 'p'; Matrix iTM is the matrix
 * that brings the plane of face 'p' into 'n'
 * 'p' and 'n' are the names of faces according to the
 * winged edge data structure; 'p' is the previous face
 * and 'n' is the next face
 *---------------------------------------------------------
 */
int MxRotateAxis(Point3D p1, Point3D p2,
				 double theta, Matrix4 *TM, Matrix4 *iTM)
{
	Point3D p;
	double dist, cosX, sinX, cosY, sinY, det;
	Matrix4 m1, m2;
	
	p.x = p2.x - p1.x;
	p.y = p2.y - p1.y;
	p.z = p2.z - p1.z;
	
	if( V3Length( &p ) < 0.0 ) return(FALSE);
	
	V3Normalize( &p );
	
	dist = sqrt( p.y * p.y + p.z * p.z );
	
	/* maybe the 2 points are already aligned with the X-axis */
	if(dist < DAMN_SMALL || dist == 0.0){
		cosX = 1.0;
		sinX = 0.0;
	}
	else{
		cosX = p.z / dist;
		sinX = p.y / dist;
	}
	
	/* fprintf(stderr, "p: x = %3.2f y = %3.2f z = %3.2f dist = %3.2f\n",
	 p.x, p.y, p.z, dist); */
	
	cosY = dist;
	sinY = -p.x;
	
	loadIdentity( TM );
	loadIdentity( &m1 );
	
	/* inverse translation matrix */
	m1.element[0][3] = p1.x;
	m1.element[1][3] = p1.y;
	m1.element[2][3] = p1.z;
	/* fprintf(stderr,"Inverse Translation:\n");
	 printMatrix( m1 ); */
	
	/* Inverse Rotation around X */
	loadIdentity( &m2 );
	m2.element[1][1] = cosX;
	m2.element[2][1] = -sinX;
	m2.element[1][2] = sinX;
	m2.element[2][2] = cosX;
	/* fprintf(stderr,"Inverse Rotation around X:\n");
	 printMatrix( m2 ); */
	
	MatMul( &m1, &m2, TM );
	MatrixCopy( TM, &m1 );
	
	/* Inverse Rotation around Y */
	loadIdentity( &m2 );
	m2.element[0][0] = cosY;
	m2.element[2][0] = sinY;
	m2.element[0][2] = -sinY;
	m2.element[2][2] = cosY;
	/* fprintf(stderr,"Inverse Rotation around Y:\n");
	 printMatrix( m2 ); */
	
	MatMul( &m1, &m2, TM );
	MatrixCopy( TM, &m1 );
	
	/* Rotation around Z */
	loadIdentity( &m2 );
	m2.element[0][0]= cos( theta );
	m2.element[1][0]= -sin( theta );
	m2.element[0][1]= sin( theta );
	m2.element[1][1]= cos( theta );
	
	/* fprintf(stderr,"Rotation around Z:\n");
	 printMatrix( m2 ); */
	
	MatMul( &m1, &m2, TM );
	MatrixCopy( TM, &m1 );
	
	/* Rotation around Y */
	loadIdentity( &m2 );
	m2.element[0][0] = cosY;
	m2.element[2][0] = -sinY;
	m2.element[0][2] = sinY;
	m2.element[2][2] = cosY;
	/* fprintf(stderr,"Rotation around Y:\n");
	 printMatrix( m2 ); */
	
	MatMul( &m1, &m2, TM );
	MatrixCopy( TM, &m1 );
	
	/* Rotation around X */
	loadIdentity( &m2 );
	m2.element[1][1] = cosX;
	m2.element[2][1] = sinX;
	m2.element[1][2] = -sinX;
	m2.element[2][2] = cosX;
	/* fprintf(stderr,"Rotation around X:\n");
	 printMatrix( m2 ); */
	
	MatMul( &m1, &m2, TM );
	MatrixCopy( TM, &m1 );
	
	/* Translation matrix */
	loadIdentity( &m2 );
	m2.element[0][3] = -p1.x;
	m2.element[1][3] = -p1.y;
	m2.element[2][3] = -p1.z;
	/* fprintf(stderr,"Translation matrix:\n");
	 printMatrix( m2 ); */
	
	MatMul( &m1, &m2, TM );
	
	det = MxInvert(TM, iTM);
	if(det < L_EPSILON && det > -L_EPSILON)
		return( FALSE );
	/*return( MxInvert(TM, iTM) );*/
	else return ( TRUE );
}
Example #5
0
void myDrawProject(){

	GLfloat materialDiffuse[] = {0.7, 0.4, 0.2, 1.0};
    	glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, materialDiffuse);
	
	printf("Using texture from file.\n"); /* image file is valid: use it as texture */
        glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
        glGenTextures(2, &TextureName1);
        glBindTexture(GL_TEXTURE_2D, TextureName1);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, TMap1.texc, TMap1.texr, 
                     0, GL_RGB, GL_UNSIGNED_BYTE, TMap1.texture);
	
	 che = &checker;


 	//glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE,
        //    material_diffuse);
        //glDisable(GL_LIGHTING);
	for(i=0; i<che->Nfaces; i++){
	if (i == 0) {
	glEnable(GL_TEXTURE_2D);
	glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
        glBindTexture(GL_TEXTURE_2D, TextureName1);
	}
	glBegin(GL_TRIANGLE_FAN);
	 // glFrontFace(GL_CW);
	for(j=0; j<che->nv_face[i]; j++) {
   	
	p.x = che->vertices[che->faces[i][1]].x - che->vertices[che->faces[i][0]].x;
	p.y = che->vertices[obj->faces[i][1]].y - che->vertices[che->faces[i][0]].y;
	p.z = che->vertices[obj->faces[i][1]].z - che->vertices[che->faces[i][0]].z;
	
	q.x = che->vertices[che->faces[i][2]].x - che->vertices[che->faces[i][1]].x;
	q.y = che->vertices[che->faces[i][2]].y - che->vertices[che->faces[i][1]].y;
	q.z = che->vertices[che->faces[i][2]].z - che->vertices[che->faces[i][1]].z;
	
	r = V3cross(p,q);
	rlength = V3Length(r);	
	fprintf(stderr,"\n\n(Nx,Ny,Nz)  = (%f,%f,%f) %d\n\n",(r.x)/rlength,(r.y)/rlength,(r.z)/rlength,i);
	//if(i == 0 ) glTexCoord2f(1.0,1.0,);
	glNormal3f((r.x)/rlength,(r.y)/rlength,(r.z)/rlength);
	//fprintf(stderr,"%d ", obj->faces[i][j]);
	// fprintf(stderr,"\n\n(Nx,Ny,Nz) = (%f,%f,%f)\n\n",(r.x)/rlength,(r.y)/rlength,(r.z)/rlength);
	//glColor3f(1,1,0);

	if (i == 5 && j == 0) glTexCoord2f(0.0,0.0);
	if( i == 5 && j == 1) glTexCoord2f(1.0,0.0);
	if( i == 5 && j == 2) glTexCoord2f(1.0,1.0);
	if( i == 5 && j == 3) glTexCoord2f(0.0,1.0);
	glVertex3f(che->vertices[che->faces[i][j]].x,che->vertices[obj->faces[i][j]].y,che->vertices[che->faces[i][j]].z);
	}
   	glEnd();
	//glFrontFace(GL_CCW);
	if(i == 0) glDisable(GL_TEXTURE_2D);
	//fprintf(stderr,"\n\n(Nx,Ny,Nz)  = (%f,%f,%f) %d\n\n",(r.x),(r.y),(r.z),i);
	} 
	//glColor3f(0,0,1);
 	//glMaterialf(GL_FRONT,GL_SHININESS,
               //0.5);
	/*for(i=0; i<che->Nfaces; i++){
		
		for(j=0; j<che->nv_face[i]; j++) {
		glLineWidth(3.0);
		if(j == 2) { l = 0;m = 3; n = 2;}
		if(j == 3) { l = 1;m = 0; n = 3;} 
		else { l = j+2 ;m = j+1 ; n = j;}
		
		p.x = che->vertices[che->faces[i][1]].x - che->vertices[che->faces[i][0]].x;
		p.y = che->vertices[che->faces[i][1]].y - che->vertices[che->faces[i][0]].y;
		p.z = che->vertices[che->faces[i][1]].z - che->vertices[che->faces[i][0]].z;
	
		q.x = che->vertices[che->faces[i][2]].x - che->vertices[che->faces[i][1]].x;
		q.y = che->vertices[che->faces[i][2]].y - che->vertices[obj->faces[i][1]].y;
		q.z = che->vertices[che->faces[i][2]].z - che->vertices[obj->faces[i][1]].z;
		glColor3f(0,1,1);
		r = V3cross(p,q);
		rlength = V3Length(r);	
		glDisable(GL_LIGHTING);
		glBegin(GL_LINES);
		
		glVertex3f(che->vertices[che->faces[i][j]].x,che->vertices[che->faces[i][j]].y,che->vertices[che->faces[i][j]].z);
		glVertex3f(che->vertices[che->faces[i][j]].x +(r.x)/rlength,che->vertices[che->faces[i][j]].y+(r.y)/rlength,che->vertices[che->faces[i][j]].z+(r.z)/rlength);
	//glVertex3f((r.x)/rlength,(r.y)/rlength,(r.z)/rlength);
	//	glVertex3f((r.x),(r.y),(r.z));
		//glVertex3f((r.x)/rlength,(r.y)/rlength,(r.z)/rlength);
		//glVertex3f((r.x)+5.0,(r.y)+5.0,(r.z)+5.0);
		glEnd();
		glEnable(GL_LIGHTING);
		}	
	}*/
}
Example #6
0
void myDrawOFF(){
	 PrintObj(&object);
  	 obj = &object;


 	//glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE,
        //    material_diffuse);
        //glDisable(GL_LIGHTING);
	for(i=0; i<obj->Nfaces; i++){
	if (i == 0) {
	glEnable(GL_TEXTURE_2D);
	glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
        glBindTexture(GL_TEXTURE_2D, TextureName);
	}
	glBegin(GL_TRIANGLE_FAN);
	 // glFrontFace(GL_CW);
	for(j=0; j<obj->nv_face[i]; j++) {
   	
	p.x = obj->vertices[obj->faces[i][1]].x - obj->vertices[obj->faces[i][0]].x;
	p.y = obj->vertices[obj->faces[i][1]].y - obj->vertices[obj->faces[i][0]].y;
	p.z = obj->vertices[obj->faces[i][1]].z - obj->vertices[obj->faces[i][0]].z;
	
	q.x = obj->vertices[obj->faces[i][2]].x - obj->vertices[obj->faces[i][1]].x;
	q.y = obj->vertices[obj->faces[i][2]].y - obj->vertices[obj->faces[i][1]].y;
	q.z = obj->vertices[obj->faces[i][2]].z - obj->vertices[obj->faces[i][1]].z;
	
	r = V3cross(p,q);
	rlength = V3Length(r);	
	fprintf(stderr,"\n\n(Nx,Ny,Nz)  = (%f,%f,%f) %d\n\n",(r.x)/rlength,(r.y)/rlength,(r.z)/rlength,i);
	//if(i == 0 ) glTexCoord2f(1.0,1.0,);
	glNormal3f((r.x)/rlength,(r.y)/rlength,(r.z)/rlength);
	//fprintf(stderr,"%d ", obj->faces[i][j]);
	// fprintf(stderr,"\n\n(Nx,Ny,Nz) = (%f,%f,%f)\n\n",(r.x)/rlength,(r.y)/rlength,(r.z)/rlength);
	//glColor3f(1,1,0);

	if (i == 0 && j == 0) glTexCoord2f(0.0,0.0);
	if( i == 0 && j == 1) glTexCoord2f(1.0,0.0);
	if( i == 0 && j == 2) glTexCoord2f(1.0,1.0);
	if( i == 0 && j == 3) glTexCoord2f(0.0,1.0);
	glVertex3f(obj->vertices[obj->faces[i][j]].x,obj->vertices[obj->faces[i][j]].y,obj->vertices[obj->faces[i][j]].z);
	}
   	glEnd();
	//glFrontFace(GL_CCW);
	if(i == 0) glDisable(GL_TEXTURE_2D);
	//fprintf(stderr,"\n\n(Nx,Ny,Nz)  = (%f,%f,%f) %d\n\n",(r.x),(r.y),(r.z),i);
	} 
	//glColor3f(0,0,1);
 	//glMaterialf(GL_FRONT,GL_SHININESS,
               //0.5);
	for(i=0; i<obj->Nfaces; i++){
		
		for(j=0; j<obj->nv_face[i]; j++) {
		glLineWidth(3.0);
		if(j == 2) { l = 0;m = 3; n = 2;}
		if(j == 3) { l = 1;m = 0; n = 3;} 
		else { l = j+2 ;m = j+1 ; n = j;}
		
		p.x = obj->vertices[obj->faces[i][1]].x - obj->vertices[obj->faces[i][0]].x;
		p.y = obj->vertices[obj->faces[i][1]].y - obj->vertices[obj->faces[i][0]].y;
		p.z = obj->vertices[obj->faces[i][1]].z - obj->vertices[obj->faces[i][0]].z;
	
		q.x = obj->vertices[obj->faces[i][2]].x - obj->vertices[obj->faces[i][1]].x;
		q.y = obj->vertices[obj->faces[i][2]].y - obj->vertices[obj->faces[i][1]].y;
		q.z = obj->vertices[obj->faces[i][2]].z - obj->vertices[obj->faces[i][1]].z;
		glColor3f(0,1,1);
		r = V3cross(p,q);
		rlength = V3Length(r);	
		glDisable(GL_LIGHTING);
		glBegin(GL_LINES);
		
		glVertex3f(obj->vertices[obj->faces[i][j]].x,obj->vertices[obj->faces[i][j]].y,obj->vertices[obj->faces[i][j]].z);
		glVertex3f(obj->vertices[obj->faces[i][j]].x +(r.x)/rlength,obj->vertices[obj->faces[i][j]].y+(r.y)/rlength,obj->vertices[obj->faces[i][j]].z+(r.z)/rlength);
	//glVertex3f((r.x)/rlength,(r.y)/rlength,(r.z)/rlength);
	//	glVertex3f((r.x),(r.y),(r.z));
		//glVertex3f((r.x)/rlength,(r.y)/rlength,(r.z)/rlength);
		//glVertex3f((r.x)+5.0,(r.y)+5.0,(r.z)+5.0);
		glEnd();
		glEnable(GL_LIGHTING);
		}	
	}
}