Example #1
0
Model::Model(char *tris_file)
{
  FILE *fp = fopen(tris_file,"r");
  if (fp == NULL)
  { 
    fprintf(stderr,"Model Constructor: Couldn't open %s\n",tris_file); 
    exit(-1); 
  }

  fscanf(fp,"%d",&ntris);
  tri = new ModelTri[ntris];

  int i;

  for (i = 0; i < ntris; i++)
  {
    // read the tri verts

    fscanf(fp,"%lf %lf %lf %lf %lf %lf %lf %lf %lf",
           &tri[i].p0[0], &tri[i].p0[1], &tri[i].p0[2],
           &tri[i].p1[0], &tri[i].p1[1], &tri[i].p1[2],
           &tri[i].p2[0], &tri[i].p2[1], &tri[i].p2[2]);

    // set the normal

    double a[3],b[3];
    VmV(a,tri[i].p1,tri[i].p0);
    VmV(b,tri[i].p2,tri[i].p0);
    VcrossV(tri[i].n,a,b);
    Vnormalize(tri[i].n);
  }
  
  fclose(fp);

  // generate display list

  display_list = glGenLists(1);
  glNewList(display_list,GL_COMPILE);
  glBegin(GL_TRIANGLES);
  for (i = 0; i < ntris; i++)
  {
    glNormal3dv(tri[i].n);
    glVertex3dv(tri[i].p0);
    glVertex3dv(tri[i].p1);
    glVertex3dv(tri[i].p2);
  }
  glEnd();
  glEndList();  
}
Example #2
0
//sets all the members of Tri3B given the values of the vertices
void Tri3B::Set( const PQP_REAL *pt1, const PQP_REAL *pt2, 
				const PQP_REAL *pt3, int iden )
{
	id = iden;

	//vertices of triangle in model's coord. frame
	p1[0] = pt1[0];
	p1[1] = pt1[1];
	p1[2] = pt1[2];
	p2[0] = pt2[0];
	p2[1] = pt2[1];
	p2[2] = pt2[2];
	p3[0] = pt3[0];
	p3[1] = pt3[1];
	p3[2] = pt3[2];

	//normalized vectors along side of triangle
	VmV( s12, p2, p1 );
	Vnormalize( s12 );

	VmV( s23, p3, p2 );
	Vnormalize( s23 );

	VmV( s31, p1, p3 );
	Vnormalize( s31 );

	//normal vector to plane of triangle
	VcrossV( n, s31, s12 );
	Vnormalize( n );

	//normal vectors to each side in the plane of the triangle
	//from cross product of side-vector and normal
	VcrossV( ns12, s12, n );
	Vnormalize( ns12 );

	VcrossV( ns23, s23, n );
	Vnormalize( ns23 );

	VcrossV( ns31, s31, n );
	Vnormalize( ns31 );	
}