View3D.cpp
上传用户:whjcdz88
上传日期:2011-09-07
资源大小:121k
文件大小:6k
源码类别:

图形图象

开发平台:

WINDOWS

  1. #include "View3D.h"
  2. float W2C[4][4] = {0}; // the matrix that transform from world to view;
  3. float Rotation[4][4] ;
  4. Surface s[6];
  5. Line L[12];
  6. Point p[8] ;
  7. Point Mid;
  8. void Init( )
  9. {
  10. s[0].L[0] = 0 ; s[0].L[1] = 1 ; s[0].L[2] = 2 ; s[0].L[3] = 3 ;
  11. s[1].L[0] = 0 ; s[1].L[1] = 7 ; s[1].L[2] = 8 ; s[1].L[3] = 4 ;
  12. s[2].L[0] = 1 ; s[2].L[1] = 6 ; s[2].L[2] = 9 ; s[2].L[3] = 7 ;
  13. s[3].L[0] = 2 ; s[3].L[1] = 6 ; s[3].L[2] = 10 ; s[3].L[3] = 5 ;
  14. s[4].L[0] = 3 ; s[4].L[1] = 5 ; s[4].L[2] = 11 ; s[4].L[3] = 4 ;
  15. s[5].L[0] = 11 ; s[5].L[1] = 10 ; s[5].L[2] = 9 ; s[5].L[3] = 8 ;
  16. L[0].p[0] = 0 ; L[0].p[1] = 1 ; L[1].p[0] = 1 ; L[1].p[1] = 2;
  17. L[2].p[0] = 2 ; L[2].p[1] = 3 ; L[3].p[0] = 3 ; L[3].p[1] = 0;
  18. L[4].p[0] = 4 ; L[4].p[1] = 0 ; L[5].p[0] = 5 ; L[5].p[1] = 3;
  19. L[6].p[0] = 6 ; L[6].p[1] = 2 ; L[7].p[0] = 1 ; L[7].p[1] = 7;
  20. L[8].p[0] = 7 ; L[8].p[1] = 4 ; L[9].p[0] = 7 ; L[9].p[1] = 6;
  21. L[10].p[0] = 6 ; L[10].p[1] = 5 ; L[11].p[0] = 5 ; L[11].p[1] = 4;
  22. p[0].wc.x = 0 ; p[0].wc.y = 0 ; p[0].wc .z = 0 ;
  23. p[1].wc.x = 100 ; p[1].wc.y = 0 ; p[1].wc .z = 0 ;
  24. p[2].wc.x = 100 ; p[2].wc.y = 100 ; p[2].wc .z = 0 ;
  25. p[3].wc.x = 0 ; p[3].wc.y = 100 ; p[0].wc .z = 0 ;
  26. p[4].wc.x = 0 ; p[4].wc.y = 0 ; p[4].wc .z = 100 ;
  27. p[5].wc.x = 0; p[5].wc.y = 100 ; p[5].wc .z = 100 ;
  28. p[6].wc.x = 100 ; p[6].wc.y = 100 ; p[6].wc .z = 100 ;
  29. p[7].wc.x = 100 ; p[7].wc.y = 0 ; p[7].wc .z = 100 ;
  30. Mid.wc.x = 50 ; Mid.wc.y = 50 ; Mid.wc.z = 50 ;
  31. }
  32. void CalWc2Vc( Vector N , Vector V )
  33. {
  34. float sum = sqrt( N.x * N.x + N.y * N.y + N.z * N.z );
  35. Vector U;
  36. W2C[2][0] = N.x / sum;
  37. W2C[2][1] = N.y / sum;
  38. W2C[2][2] = N.z / sum;
  39. V.x = V.x - N.x ; V.y = V.y - N.y ; V.z = V.z - N.z ;
  40. U.x = V.y * N.z - V.z * N.y;
  41. U.y = V.z * N.x - V.x * N.z ;
  42. U.z = V.x * N.y - V.y * N.x;
  43. sum = sqrt( U.x * U.x + U.y * U.y + U.z * U.z );
  44. W2C[0][0] = U.x / sum;
  45. W2C[0][1] = U.y / sum;
  46. W2C[0][2] = U.z / sum;
  47. W2C[1][0] = W2C[2][1] * W2C[0][2] - W2C[2][2] * W2C[0][1];
  48. W2C[1][1] = W2C[2][2] * W2C[0][0] - W2C[2][0] * W2C[0][2];
  49. W2C[1][2] = W2C[2][0] * W2C[0][1] - W2C[2][1] * W2C[0][0];
  50. W2C[0][3] = -( W2C[0][0] * N.x + W2C[0][1] * N.y + W2C[0][2] * N.z );
  51. W2C[1][3] = -( W2C[1][0] * N.x + W2C[1][1] * N.y + W2C[1][2] * N.z );
  52. W2C[2][3] = -( W2C[2][0] * N.x + W2C[2][1] * N.y + W2C[2][2] * N.z );
  53. W2C[3][3] = 1;
  54. }
  55. point Wc2Vc( point P )
  56. {
  57. point temp;
  58. temp.x = W2C[0][0] * P.x + W2C[0][1] * P.y + W2C[0][2] * P.z + W2C[0][3];
  59. temp.y = W2C[1][0] * P.x + W2C[1][1] * P.y + W2C[1][2] * P.z + W2C[1][3];
  60. temp.z = W2C[2][0] * P.x + W2C[2][1] * P.y + W2C[2][2] * P.z + W2C[2][3];
  61. return temp;
  62. }
  63. point Vc2Pc( point pp , point p ) // pp is the vanishing point;
  64. {
  65. float i = 1 / ( pp.z - p.z );
  66. point temp;
  67. temp.x = ( p.x * pp.z - pp.x * p.z ) * i + pp.x ;
  68. temp.y = ( p.y * pp.z - pp.y * p.z ) * i + pp.y;
  69. temp.z = p.z ;
  70. return temp;
  71. }
  72. static void Mul( float result[4][4] , float A[4][4] , float B[4][4] )
  73. {
  74. for( int i = 0 ; i < 4 ; i ++ )
  75. for( int j = 0 ; j < 4 ; j ++ )
  76. {
  77. result[i][j] = 0 ;
  78. for( int k = 0 ; k < 4 ; k ++ )
  79. result[i][j] += A[i][k] * B[k][j];
  80. }
  81. }
  82. void Clear( int lnum )
  83. {
  84. for( int i = 0 ; i < lnum ; i ++ )
  85. L[ i ].flag = 0 ;
  86. }
  87. void IsVisible( Surface s )
  88. {
  89. point p1 , p2 , p3 , p4;
  90. int flag ;
  91. p1 = p[ L[ s.L[0] ].p[0] ].vc ;
  92. p2 = p[ L[ s.L[0] ].p[1] ].vc  ;
  93. p3 = p[ L[ s.L[2] ].p[0] ].vc  ;
  94. p4 = p[ L[ s.L[2] ].p[1] ].vc ;
  95. float C =  p1.z + p2.z + p3.z + p4.z - 4 * Mid.vc.z ;
  96. flag = ( C > 0 ) ? 1 : 0 ;
  97. for( int i = 0 ; i < 4 ; i ++ )
  98. L[ s.L[i] ].flag = flag || L[ s.L[i] ].flag ;
  99. }
  100. void CalRotation( point p1 , point p2 , int degree)
  101. {
  102. Vector v , u , n ;
  103. n.x = p2.x - p1.x ; n.y = p2.y - p1.y ; n.z = p2.z - p1.z ;
  104. float sum = sqrt( n.x * n.x + n.y * n.y + n.z * n.z );
  105. n.x = n.x / sum ; n.y = n.y / sum ; n.z = n.z / sum ;
  106. u.x = 0 ; u.y = n.z ; u.z = -n.y ;
  107. sum = sqrt( u.y * u.y + u.z * u.z );
  108. u.y = u.y / sum ; u.z = u.z / sum ;
  109. v.x = n.y * u.z - n.z * u.y ;
  110. v.y = n.z * u.x - n.x * u.z ;
  111. v.z = n.x * u.y - n.y * u.x ;
  112. float R[4][4] , A[4][4] , B[4][4] = {0};
  113. 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 );
  114. 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 );
  115. 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 );
  116. R[3][0] = R[3][1] = R[3][2] = 0 ; R[3][3] = 1;
  117. float a = ( float )cos( degree * 3.14 / 180 ) ; 
  118. float b = ( float )sin( degree * 3.14 / 180 );
  119. B[0][0] = a ; B[0][1] = -b ; B[1][0] = b ; B[1][1] = a ; B[2][2] = B[3][3] = 1 ;
  120. Mul( A , B , R );
  121. R[0][0] = v.x ; R[1][0] = v.y ; R[2][0] = v.z ; R[0][3] = p1.x ;
  122. R[0][1] = u.x ; R[1][1] = u.y ; R[2][1] = u.z ; R[1][3] = p1.y ;
  123. R[0][2] = n.x ; R[1][2] = n.y ; R[2][2] = n.z ; R[2][3] = p1.z ;
  124. R[3][0] = R[3][1] = R[3][2] = 0 ; R[3][3] = 1;
  125. Mul( Rotation , R , A ) ; 
  126. }
  127. point Rotate3D( point p)
  128. {
  129. point temp;
  130. temp.x = Rotation[0][0] * p.x + Rotation[0][1] * p.y + Rotation[0][2] * p.z ;
  131. temp.y = Rotation[1][0] * p.x + Rotation[1][1] * p.y + Rotation[1][2] * p.z ;
  132. temp.z = Rotation[2][0] * p.x + Rotation[2][1] * p.y + Rotation[2][2] * p.z ;
  133. return temp;
  134. }
  135. void Adjust( int pnum , int cx ,int cy )
  136. {
  137. float xmin , xmax , ymin , ymax , det , x , y , temp;
  138. xmin = p[0].pc.x ; xmax = p[0].pc.x ;
  139. ymin = p[0].pc.y ; ymax = p[0].pc.y ;
  140. for( int i = 1 ; i < pnum ; i ++ )
  141. {
  142. if( p[i].pc.x > xmax )
  143. xmax = p[i].pc.x ;
  144. if( p[i].pc.x < xmin )
  145. xmin = p[i].pc.x ;
  146. if( p[i].pc.y > ymax )
  147. ymax = p[i].pc.y ;
  148. if( p[i].pc.y < ymin )
  149. ymin = p[i].pc.y ;
  150. }
  151. x = - xmin + cx/4 ; y = - ymin + cy/4 ;
  152. det = ( ( xmax - xmin ) < ( ymax - ymin ) ) ? ( ymax - ymin ) : ( xmax - xmin );
  153. temp =  cx > cy ? cy : cx ;
  154. det = temp / (det * 2)  ;
  155. for( i = 0 ; i < pnum ; i ++ )
  156. {
  157. p[i].pc.x = ( p[i].pc.x  - xmin ) * det + xmin + x ;
  158. p[i].pc.y = ( p[i].pc.y  - ymin ) * det + ymin + y ;
  159. }
  160. }