Example #1
0
 Quaternion(Vector3D v1, Vector3D v2) {
     const double k = v2.norm() / v1.norm();
     const Vector3D d1 = v1.direction();
     const Vector3D d2 = v2.direction();
     if ( (d1 + d2).norm() < PRECISION ) {
         Vector3D n;
         srand( (unsigned int) time(0));
         do {
             double x = (double) rand() / RAND_MAX;
             double y = (double) rand() / RAND_MAX;
             double z = (double) rand() / RAND_MAX;
             Vector3D v = Vector3D(x, y, z);
             n = v - v1.direction()*(v*v1)/v1.norm();
         } while (n.norm() < PRECISION );
         init( 0.0, n.direction() );
     } else if ( (d1 - d2).norm() < PRECISION ) {
         init( 1.0, Vector3D(0.0, 0.0, 0.0) );
     } else {
         double phi = acos( v1.direction()*v2.direction() );
         Vector3D a = v1.cross(v2).direction();
         assert(a.norm() > PRECISION);
         double w = cos(phi/2) * sqrt(k);
         Vector3D u = a * sin(phi/2) * sqrt(k);
         init(w, u);
     }
 }
Example #2
0
  Vector3D Vertex::normal( void ) const
  // TODO Returns an approximate unit normal at this vertex, computed by
  // TODO taking the area-weighted average of the normals of neighboring
  // TODO triangles, then normalizing.
  {
     // TODO Compute and return the area-weighted unit normal.
	 
		//no boundary polygon
		HalfedgeCIter h = this->halfedge();
		Vector3D nrm(0, 0, 0);
		double totalarea = 0;
		do
		{
			h = h->twin();
			FaceCIter f = h->face();
			if(!f->isBoundary())
			{
				VertexCIter v1 = h->vertex();
				VertexCIter v2 = h->next()->twin()->vertex();
				Vector3D out = cross(v1->position - position, v2->position - position);
				nrm += out;
				totalarea += out.norm();
			}
			h = h->next();
		}
		while(h != this->halfedge());
		nrm /= totalarea;
		nrm.normalize();
		return nrm;
 }
Example #3
0
void CObjectView::OnMouseMove(UINT nFlags, CPoint point) 
{
	if (GetCapture() == this && (nFlags & MK_LBUTTON)){
    if (mode == PAN){
      Point2D pL, pC;
      screenToFrustum(&pL, lastPt);
      screenToFrustum(&pC, point);

      //  update translate
      tr[0] += (pC.x - pL.x)*1000.0/scale;
      tr[1] += (pC.y - pL.y)*1000.0/scale;
      }
    else if (mode == ROTATE || mode == LIGHT){
      Vector3D vL, vC;
      screenToVector(&vL, lastPt);
      screenToVector(&vC, point);

      //  calculate angle prop to length mouse movement
      float dX = vC.x - vL.x;
      float dY = vC.y - vL.y;
      float dZ = vC.z - vL.z;
      float ang = 90.0 * sqrt(dX*dX + dY*dY + dZ*dZ);

      // vector is just cross product
      Vector3D v;
      v.cross(vL, vC);
      v.norm();

      if (mode == ROTATE)
        applyRotation(m, ang, v);
      else
        applyRotation(mL, ang, v);
      }

    lastPt = point;
    RedrawWindow();
    }
	
	CView ::OnMouseMove(nFlags, point);
}
Example #4
0
void CObjectView::OnDraw(CDC* pDC) 
{
  SELECT_CONTEXT();

  setupPerspective();
  drawScene(&dList, tex);

  //  turn off lighting
  glDisable(GL_LIGHTING);

  //  draw tracker ball
  if ((mode == ROTATE || mode == LIGHT)&& GetCapture() == this){
    //  do overlay
    glEnable(GL_CULL_FACE);
    glClear(GL_DEPTH_BUFFER_BIT);
    glDisable(GL_STENCIL_TEST);

    // work out size of tracker
    float rad = 0.85f;

    //  setup transform
    glMatrixMode(GL_MODELVIEW);
    if (mode == LIGHT)
      glLoadMatrixf(mL);
    else
      glLoadMatrixf(m);

    //  mesh
    glPolygonMode(GL_FRONT, GL_LINE);
    glLineWidth(1);

    //  draw light
    if (mode == LIGHT){
      //  get position
      Vector3D v = {1.0f, 1.0f, 1.0f};
      v.norm();
      v.scale(rad);

      //  setup dot size
      GLint dotSize;
      glGetIntegerv(GL_POINT_SIZE, &dotSize);
      glPointSize(3);

      //  draw light
      glColor3f(1.0f, 1.0f, 0.0f);
      glBegin(GL_POINTS);
      glVertex3f(v.x, v.y, v.z);
      glEnd();

      glBegin(GL_LINES);
      glVertex3f(0.0f, 0.0f, 0.0f);
      glVertex3f(v.x, v.y, v.z);
      glEnd();

      //  restore dot size
      glPointSize(dotSize);
      }

    //  draw sphere
    glColor3f(1, 0, 1);
    glRotatef(90, 1, 0, 0);
    GLUquadricObj *q = gluNewQuadric();
    gluSphere(q, rad, 10, 10);
    gluDeleteQuadric(q);
    }

  //  clean up and show picture
  glFlush();
  SwapBuffers(hDC);

  UNSELECT_CONTEXT();
}