示例#1
0
Mat4f CameraControls::getCameraToWorld(void) const
{
    Mat3f orient = getOrientation();
    Mat4f r;
    r.setCol(0, Vec4f(orient.col(0), 0.0f));
    r.setCol(1, Vec4f(orient.col(1), 0.0f));
    r.setCol(2, Vec4f(orient.col(2), 0.0f));
    r.setCol(3, Vec4f(m_position, 1.0f));
    return r;
}
void drawCurve( const Curve& curve, float framesize )
{
    // Save current state of OpenGL
    glPushAttrib( GL_ALL_ATTRIB_BITS );

    // Setup for line drawing
    glDisable( GL_LIGHTING ); 
    glColor4f( 1, 1, 1, 1 );
    glLineWidth( 1 );
    
    // Draw curve
    glBegin( GL_LINE_STRIP );
    for( unsigned i = 0; i < curve.size(); ++i )
    {
        glVertex( curve[ i ].V );
    }
    glEnd();

    glLineWidth( 1 );

    // Draw coordinate frames if framesize nonzero
    if( framesize != 0.0f )
    {
        Mat4f M;

        for( unsigned i = 0; i < curve.size(); ++i )
        {
            M.setCol( 0, Vec4f( curve[i].N, 0 ) );
            M.setCol( 1, Vec4f( curve[i].B, 0 ) );
            M.setCol( 2, Vec4f( curve[i].T, 0 ) );
            M.setCol( 3, Vec4f( curve[i].V, 1 ) );

            glPushMatrix();
            glMultMatrixf( M.getPtr() );
            glScaled( framesize, framesize, framesize );
            glBegin( GL_LINES );
            glColor3f( 1, 0, 0 ); glVertex3d( 0, 0, 0 ); glVertex3d( 1, 0, 0 );
            glColor3f( 0, 1, 0 ); glVertex3d( 0, 0, 0 ); glVertex3d( 0, 1, 0 );
            glColor3f( 0, 0, 1 ); glVertex3d( 0, 0, 0 ); glVertex3d( 0, 0, 1 );
            glEnd();
            glPopMatrix();
        }
    }
    
    // Pop state
    glPopAttrib();
}
Curve evalBspline( const vector< Vec3f >& P, unsigned steps )
{
    // Check
    if( P.size() < 4 )
    {
        cerr << "evalBspline must be called with 4 or more control points." << endl;
        exit( 0 );
    }

    cerr << "\t>>> evalBSpline has been called with the following input:" << endl;

    cerr << "\t>>> Control points (type vector< Vec3f >): "<< endl;
    for( unsigned i = 0; i < P.size(); ++i )
    {
        cerr << "\t>>> "; printTranspose(P[i]); cerr << endl;
    }

	
    cerr << "\t>>> Steps (type steps): " << steps << endl;

	// Use your evalBezier function to evaluate the B-spline.
    // Convert the input control points to suitable coordinate system.
    // See lecture notes for further instructions.
    vector<Vec3f> Pbz;

	for (int i = 0; i < P.size()-3; i++) {

		// Create the G matrix for conversion
		Mat4f G;

		for (int j = 0; j < 4; j++) {
			G.setCol(j, Vec4f(P[i+j].x, P[i+j].y, P[i+j].z, 0.0));
		}

		// Calculate the conversion matrix: G_2 = G_1 * B_1 * (B_2)^-1
		G = G * Base::bsplineBase * Base::bezierBase.inverted();

		// Detach the converted control points from the G matrix
		for (int j = 0; j < 3; j++) {
			Pbz.push_back(Vec3f(G.getCol(j)[0], G.getCol(j)[1], G.getCol(j)[2]));
		}

		// Only add the last column for the very last index
		if (i+3 == P.size()-1) {
			Pbz.push_back(Vec3f(G.getCol(3)[0], G.getCol(3)[1], G.getCol(3)[2]));
		}
	}

    return evalBezier(Pbz, steps);
}