View3D.cpp
上传用户:whjcdz88
上传日期:2011-09-07
资源大小:121k
文件大小:6k
- #include "View3D.h"
- float W2C[4][4] = {0}; // the matrix that transform from world to view;
- float Rotation[4][4] ;
- Surface s[6];
- Line L[12];
- Point p[8] ;
- Point Mid;
- void Init( )
- {
- s[0].L[0] = 0 ; s[0].L[1] = 1 ; s[0].L[2] = 2 ; s[0].L[3] = 3 ;
- s[1].L[0] = 0 ; s[1].L[1] = 7 ; s[1].L[2] = 8 ; s[1].L[3] = 4 ;
- s[2].L[0] = 1 ; s[2].L[1] = 6 ; s[2].L[2] = 9 ; s[2].L[3] = 7 ;
- s[3].L[0] = 2 ; s[3].L[1] = 6 ; s[3].L[2] = 10 ; s[3].L[3] = 5 ;
- s[4].L[0] = 3 ; s[4].L[1] = 5 ; s[4].L[2] = 11 ; s[4].L[3] = 4 ;
- s[5].L[0] = 11 ; s[5].L[1] = 10 ; s[5].L[2] = 9 ; s[5].L[3] = 8 ;
- L[0].p[0] = 0 ; L[0].p[1] = 1 ; L[1].p[0] = 1 ; L[1].p[1] = 2;
- L[2].p[0] = 2 ; L[2].p[1] = 3 ; L[3].p[0] = 3 ; L[3].p[1] = 0;
- L[4].p[0] = 4 ; L[4].p[1] = 0 ; L[5].p[0] = 5 ; L[5].p[1] = 3;
- L[6].p[0] = 6 ; L[6].p[1] = 2 ; L[7].p[0] = 1 ; L[7].p[1] = 7;
- L[8].p[0] = 7 ; L[8].p[1] = 4 ; L[9].p[0] = 7 ; L[9].p[1] = 6;
- L[10].p[0] = 6 ; L[10].p[1] = 5 ; L[11].p[0] = 5 ; L[11].p[1] = 4;
- p[0].wc.x = 0 ; p[0].wc.y = 0 ; p[0].wc .z = 0 ;
- p[1].wc.x = 100 ; p[1].wc.y = 0 ; p[1].wc .z = 0 ;
- p[2].wc.x = 100 ; p[2].wc.y = 100 ; p[2].wc .z = 0 ;
- p[3].wc.x = 0 ; p[3].wc.y = 100 ; p[0].wc .z = 0 ;
- p[4].wc.x = 0 ; p[4].wc.y = 0 ; p[4].wc .z = 100 ;
- p[5].wc.x = 0; p[5].wc.y = 100 ; p[5].wc .z = 100 ;
- p[6].wc.x = 100 ; p[6].wc.y = 100 ; p[6].wc .z = 100 ;
- p[7].wc.x = 100 ; p[7].wc.y = 0 ; p[7].wc .z = 100 ;
- Mid.wc.x = 50 ; Mid.wc.y = 50 ; Mid.wc.z = 50 ;
- }
- void CalWc2Vc( Vector N , Vector V )
- {
- float sum = sqrt( N.x * N.x + N.y * N.y + N.z * N.z );
- Vector U;
- W2C[2][0] = N.x / sum;
- W2C[2][1] = N.y / sum;
- W2C[2][2] = N.z / sum;
- V.x = V.x - N.x ; V.y = V.y - N.y ; V.z = V.z - N.z ;
- U.x = V.y * N.z - V.z * N.y;
- U.y = V.z * N.x - V.x * N.z ;
- U.z = V.x * N.y - V.y * N.x;
- sum = sqrt( U.x * U.x + U.y * U.y + U.z * U.z );
- W2C[0][0] = U.x / sum;
- W2C[0][1] = U.y / sum;
- W2C[0][2] = U.z / sum;
- W2C[1][0] = W2C[2][1] * W2C[0][2] - W2C[2][2] * W2C[0][1];
- W2C[1][1] = W2C[2][2] * W2C[0][0] - W2C[2][0] * W2C[0][2];
- W2C[1][2] = W2C[2][0] * W2C[0][1] - W2C[2][1] * W2C[0][0];
-
- W2C[0][3] = -( W2C[0][0] * N.x + W2C[0][1] * N.y + W2C[0][2] * N.z );
- W2C[1][3] = -( W2C[1][0] * N.x + W2C[1][1] * N.y + W2C[1][2] * N.z );
- W2C[2][3] = -( W2C[2][0] * N.x + W2C[2][1] * N.y + W2C[2][2] * N.z );
- W2C[3][3] = 1;
- }
- point Wc2Vc( point P )
- {
- point temp;
- temp.x = W2C[0][0] * P.x + W2C[0][1] * P.y + W2C[0][2] * P.z + W2C[0][3];
- temp.y = W2C[1][0] * P.x + W2C[1][1] * P.y + W2C[1][2] * P.z + W2C[1][3];
- temp.z = W2C[2][0] * P.x + W2C[2][1] * P.y + W2C[2][2] * P.z + W2C[2][3];
- return temp;
- }
- point Vc2Pc( point pp , point p ) // pp is the vanishing point;
- {
- float i = 1 / ( pp.z - p.z );
- point temp;
- temp.x = ( p.x * pp.z - pp.x * p.z ) * i + pp.x ;
- temp.y = ( p.y * pp.z - pp.y * p.z ) * i + pp.y;
- temp.z = p.z ;
- return temp;
- }
- static void Mul( float result[4][4] , float A[4][4] , float B[4][4] )
- {
- for( int i = 0 ; i < 4 ; i ++ )
- for( int j = 0 ; j < 4 ; j ++ )
- {
- result[i][j] = 0 ;
- for( int k = 0 ; k < 4 ; k ++ )
- result[i][j] += A[i][k] * B[k][j];
- }
- }
- void Clear( int lnum )
- {
- for( int i = 0 ; i < lnum ; i ++ )
- L[ i ].flag = 0 ;
- }
- void IsVisible( Surface s )
- {
- point p1 , p2 , p3 , p4;
- int flag ;
- p1 = p[ L[ s.L[0] ].p[0] ].vc ;
- p2 = p[ L[ s.L[0] ].p[1] ].vc ;
- p3 = p[ L[ s.L[2] ].p[0] ].vc ;
- p4 = p[ L[ s.L[2] ].p[1] ].vc ;
-
- float C = p1.z + p2.z + p3.z + p4.z - 4 * Mid.vc.z ;
- flag = ( C > 0 ) ? 1 : 0 ;
- for( int i = 0 ; i < 4 ; i ++ )
- L[ s.L[i] ].flag = flag || L[ s.L[i] ].flag ;
- }
- void CalRotation( point p1 , point p2 , int degree)
- {
- Vector v , u , n ;
- n.x = p2.x - p1.x ; n.y = p2.y - p1.y ; n.z = p2.z - p1.z ;
- float sum = sqrt( n.x * n.x + n.y * n.y + n.z * n.z );
- n.x = n.x / sum ; n.y = n.y / sum ; n.z = n.z / sum ;
-
- u.x = 0 ; u.y = n.z ; u.z = -n.y ;
- sum = sqrt( u.y * u.y + u.z * u.z );
- u.y = u.y / sum ; u.z = u.z / sum ;
- v.x = n.y * u.z - n.z * u.y ;
- v.y = n.z * u.x - n.x * u.z ;
- v.z = n.x * u.y - n.y * u.x ;
- float R[4][4] , A[4][4] , B[4][4] = {0};
- R[0][0] = v.x ; R[0][1] = v.y ; R[0][2] = v.z ; R[0][3] = -( v.x * p1.x + v.y * p1.y + v.z * p1.z );
- R[1][0] = u.x ; R[1][1] = u.y ; R[1][2] = u.z ; R[1][3] = -( u.x * p1.x + u.y * p1.y + u.z * p1.z );
- R[2][0] = n.x ; R[2][1] = n.y ; R[2][2] = n.z ; R[2][3] = -( n.x * p1.x + n.y * p1.y + n.z * p1.z );
- R[3][0] = R[3][1] = R[3][2] = 0 ; R[3][3] = 1;
-
- float a = ( float )cos( degree * 3.14 / 180 ) ;
- float b = ( float )sin( degree * 3.14 / 180 );
- B[0][0] = a ; B[0][1] = -b ; B[1][0] = b ; B[1][1] = a ; B[2][2] = B[3][3] = 1 ;
- Mul( A , B , R );
-
- R[0][0] = v.x ; R[1][0] = v.y ; R[2][0] = v.z ; R[0][3] = p1.x ;
- R[0][1] = u.x ; R[1][1] = u.y ; R[2][1] = u.z ; R[1][3] = p1.y ;
- R[0][2] = n.x ; R[1][2] = n.y ; R[2][2] = n.z ; R[2][3] = p1.z ;
- R[3][0] = R[3][1] = R[3][2] = 0 ; R[3][3] = 1;
- Mul( Rotation , R , A ) ;
- }
- point Rotate3D( point p)
- {
- point temp;
- temp.x = Rotation[0][0] * p.x + Rotation[0][1] * p.y + Rotation[0][2] * p.z ;
- temp.y = Rotation[1][0] * p.x + Rotation[1][1] * p.y + Rotation[1][2] * p.z ;
- temp.z = Rotation[2][0] * p.x + Rotation[2][1] * p.y + Rotation[2][2] * p.z ;
- return temp;
- }
- void Adjust( int pnum , int cx ,int cy )
- {
- float xmin , xmax , ymin , ymax , det , x , y , temp;
- xmin = p[0].pc.x ; xmax = p[0].pc.x ;
- ymin = p[0].pc.y ; ymax = p[0].pc.y ;
- for( int i = 1 ; i < pnum ; i ++ )
- {
- if( p[i].pc.x > xmax )
- xmax = p[i].pc.x ;
- if( p[i].pc.x < xmin )
- xmin = p[i].pc.x ;
- if( p[i].pc.y > ymax )
- ymax = p[i].pc.y ;
- if( p[i].pc.y < ymin )
- ymin = p[i].pc.y ;
- }
- x = - xmin + cx/4 ; y = - ymin + cy/4 ;
- det = ( ( xmax - xmin ) < ( ymax - ymin ) ) ? ( ymax - ymin ) : ( xmax - xmin );
- temp = cx > cy ? cy : cx ;
- det = temp / (det * 2) ;
- for( i = 0 ; i < pnum ; i ++ )
- {
- p[i].pc.x = ( p[i].pc.x - xmin ) * det + xmin + x ;
- p[i].pc.y = ( p[i].pc.y - ymin ) * det + ymin + y ;
- }
- }
-