home *** CD-ROM | disk | FTP | other *** search
/ DOS/V Power Report 1997 March / VPR9703A.ISO / VPR_DATA / DOGA / SOURCES / POLYEDIT.LZH / MODEL / VIEW4.C < prev    next >
C/C++ Source or Header  |  1996-07-09  |  12KB  |  391 lines

  1. /*
  2.  *        表示
  3.  *
  4.  *        Copyright    T.Koabayashi    1994.6.26
  5.  */
  6.  
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include <math.h>
  11. #include <assert.h>
  12.  
  13. #define    VIEW_LOCAL
  14.  
  15. #include "matrix.h"
  16. #include "poly.h"
  17. #include "view.h"
  18.  
  19. static    void    VertexConv( Vertex*, int[5][3], Vertex* );
  20. static    void    DrawBox( ViewWindow*, int, int, int, int );
  21. static    void    DrawRectangle( int, Vertex*, Vertex*, Vertex*, Vertex* );
  22. static    void    DrawCube( ViewWindow*, int *, int *, int *);
  23. static    void    DrawCube4( ViewWindow*, int *, int *, int *, int, int);
  24. static    void    DrawCube5( ViewWindow*, int *, int *, int *, int, int);
  25. static    void    DrawCube9( ViewWindow*, int *, int *, int *);
  26.  
  27. Vertex viewbox_p1, viewbox_p2;
  28. Matrix    viewbox_mat;
  29. int    viewbox_drawflag = FALSE;
  30.  
  31. int viewbox2d_x1, viewbox2d_y1, viewbox2d_x2, viewbox2d_y2;
  32. int viewbox2d_drawflag = FALSE;
  33.  
  34. /*    直方体描画    */
  35. void    ViewBox( p1, p2, mat )
  36. Vertex    *p1, *p2 ;    /*    頂点    */
  37. Matrix    mat ;
  38. {
  39.     int        i ;
  40.     Vertex    ver[8] ;
  41.     int        scr[8][3] ;
  42.     int        pos1[3], pos2[3] ;
  43.     int        imat[5][3] ;
  44.     int        vecx[4], vecy[4], vecz[4];
  45.  
  46.     if (viewbox_drawflag
  47.      && memcmp(&viewbox_p1, p1, sizeof(Vertex)) == 0
  48.      && memcmp(&viewbox_p2, p2, sizeof(Vertex)) == 0
  49.      && memcmp(&viewbox_mat, mat, sizeof(Matrix)) == 0) {
  50.         viewbox_drawflag = FALSE;
  51.     } else {
  52.         memcpy(&viewbox_p1, p1, sizeof(Vertex));
  53.         memcpy(&viewbox_p2, p2, sizeof(Vertex));
  54.         memcpy(viewbox_mat, mat, sizeof(Matrix));
  55.         viewbox_drawflag = TRUE;
  56.     }
  57.  
  58.     MatToInt( imat, mat );
  59.     /*    頂点の設定設定    */
  60.     for( i = 0 ; i < 8 ; i++ )
  61.     {
  62.         memset( &ver[i], 0, sizeof( Vertex ) );
  63.         ver[i].x = i & 1 ? p1->x : p2->x ;
  64.         ver[i].y = i & 2 ? p1->y : p2->y ;
  65.         ver[i].z = i & 4 ? p1->z : p2->z ;
  66.  
  67.         VertexConv( &ver[i], imat, &ver[i] );
  68.         ToScreen( scr[i], &ver[i] );
  69. /*printf("scr[%d]=(%d,%d,%d)\n", i, scr[i][0], scr[i][1], scr[i][2]);*/
  70.     }
  71.  
  72.     if ( ViewMode & VIEW_PERS ) {
  73.         if (p1->x == p2->x) {
  74.             DrawLineForPers(&ver[0], &ver[2], CURSOR_COLOR);
  75.             DrawLineForPers(&ver[2], &ver[6], CURSOR_COLOR);
  76.             DrawLineForPers(&ver[6], &ver[4], CURSOR_COLOR);
  77.             DrawLineForPers(&ver[4], &ver[0], CURSOR_COLOR);
  78.         } else if (p1->y == p2->y) {
  79.             DrawLineForPers(&ver[0], &ver[1], CURSOR_COLOR);
  80.             DrawLineForPers(&ver[1], &ver[5], CURSOR_COLOR);
  81.             DrawLineForPers(&ver[5], &ver[4], CURSOR_COLOR);
  82.             DrawLineForPers(&ver[4], &ver[0], CURSOR_COLOR);
  83.         } else if (p1->z == p2->z) {
  84.             DrawLineForPers(&ver[0], &ver[1], CURSOR_COLOR);
  85.             DrawLineForPers(&ver[1], &ver[3], CURSOR_COLOR);
  86.             DrawLineForPers(&ver[3], &ver[2], CURSOR_COLOR);
  87.             DrawLineForPers(&ver[2], &ver[0], CURSOR_COLOR);
  88.         } else {
  89.             DrawLineForPers(&ver[0], &ver[1], CURSOR_COLOR);
  90.             DrawLineForPers(&ver[1], &ver[3], CURSOR_COLOR);
  91.             DrawLineForPers(&ver[3], &ver[2], CURSOR_COLOR);
  92.             DrawLineForPers(&ver[2], &ver[0], CURSOR_COLOR);
  93.             DrawLineForPers(&ver[4], &ver[5], CURSOR_COLOR);
  94.             DrawLineForPers(&ver[5], &ver[7], CURSOR_COLOR);
  95.             DrawLineForPers(&ver[7], &ver[6], CURSOR_COLOR);
  96.             DrawLineForPers(&ver[6], &ver[4], CURSOR_COLOR);
  97.             DrawLineForPers(&ver[0], &ver[4], CURSOR_COLOR);
  98.             DrawLineForPers(&ver[1], &ver[5], CURSOR_COLOR);
  99.             DrawLineForPers(&ver[2], &ver[6], CURSOR_COLOR);
  100.             DrawLineForPers(&ver[3], &ver[7], CURSOR_COLOR);
  101.         }
  102.     }
  103.     vecx[0] = (scr[0][0] + scr[7][0])/2;
  104.     vecy[0] = (scr[0][1] + scr[7][1])/2;
  105.     vecz[0] = (scr[0][2] + scr[7][2])/2;
  106.     vecx[1] = (scr[1][0] - scr[0][0])/2;
  107.     vecy[1] = (scr[1][1] - scr[0][1])/2;
  108.     vecz[1] = (scr[1][2] - scr[0][2])/2;
  109.     vecx[2] = (scr[2][0] - scr[0][0])/2;
  110.     vecy[2] = (scr[2][1] - scr[0][1])/2;
  111.     vecz[2] = (scr[2][2] - scr[0][2])/2;
  112.     vecx[3] = (scr[4][0] - scr[0][0])/2;
  113.     vecy[3] = (scr[4][1] - scr[0][1])/2;
  114.     vecz[3] = (scr[4][2] - scr[0][2])/2;
  115.  
  116.     if (ViewMode & VIEW_XY) {
  117.         DrawCube(&WinXY, vecx, vecy, vecz);
  118.     }
  119.     if (ViewMode & VIEW_YZ) {
  120.         DrawCube(&WinYZ, vecy, vecz, vecx);
  121.     }
  122.     if (ViewMode & VIEW_ZX) {
  123.         DrawCube(&WinZX, vecx, vecz, vecy);
  124.     }
  125. }
  126.  
  127. static    void    DrawCube(ViewWindow *win, int *vecx, int *vecy, int *vecz)
  128. {
  129. #if 0
  130.     if (vecz[1] == 0) {
  131.         if (vecz[2] == 0) {
  132.             DrawCube4(win, vecx, vecy, vecz, 1, 2);
  133.         } else if (vecz[3] == 0) {
  134.             DrawCube4(win, vecx, vecy, vecz, 1, 3);
  135.         } else {
  136.             DrawCube5(win, vecx, vecy, vecz, 2, 3);
  137.         }
  138.     } else if (vecz[2] == 0) {
  139.         if (vecz[3] == 0) {
  140.             DrawCube4(win, vecx, vecy, vecz, 2, 3);
  141.         } else {
  142.             DrawCube5(win, vecx, vecy, vecz, 1, 3);
  143.         }
  144.     } else if (vecz[3] == 0) {
  145.         DrawCube5(win, vecx, vecy, vecz, 1, 2);
  146.     } else {
  147.         DrawCube9(win, vecx, vecy, vecz);
  148.     }
  149. #endif
  150.     if (vecx[1] == 0 && vecy[1] == 0) {
  151.         if (vecx[2] == 0 && vecy[2] == 0) {
  152.             ClipLine( win,
  153.                 win->offx + vecx[0] + vecx[3], win->offy - vecy[0] - vecy[3],
  154.                 win->offx + vecx[0] - vecx[3], win->offy - vecy[0] + vecy[3],
  155.                 CURSOR_COLOR );
  156.         } else if (vecx[3] == 0 && vecy[3] == 0) {
  157.             ClipLine( win,
  158.                 win->offx + vecx[0] + vecx[2], win->offy - vecy[0] - vecy[2],
  159.                 win->offx + vecx[0] - vecx[2], win->offy - vecy[0] + vecy[2],
  160.                 CURSOR_COLOR );
  161.         } else {
  162.             DrawCube4(win, vecx, vecy, vecz, 2, 3);
  163.         }
  164.     } else if (vecx[2] == 0 && vecy[2] == 0) {
  165.         if (vecx[3] == 0 && vecy[3] == 0) {
  166.             ClipLine( win,
  167.                 win->offx + vecx[0] + vecx[1], win->offy - vecy[0] - vecy[1],
  168.                 win->offx + vecx[0] - vecx[1], win->offy - vecy[0] + vecy[1],
  169.                 CURSOR_COLOR );
  170.         } else {
  171.             DrawCube4(win, vecx, vecy, vecz, 1, 3);
  172.         }
  173.     } else if (vecx[3] == 0 && vecy[3] == 0) {
  174.         DrawCube4(win, vecx, vecy, vecz, 1, 2);
  175.     } else if (vecz[1] == 0) {
  176.         DrawCube5(win, vecx, vecy, vecz, 2, 3);
  177.     } else if (vecz[2] == 0) {
  178.         DrawCube5(win, vecx, vecy, vecz, 1, 3);
  179.     } else if (vecz[3] == 0) {
  180.         DrawCube5(win, vecx, vecy, vecz, 1, 2);
  181.     } else {
  182.         DrawCube9(win, vecx, vecy, vecz);
  183.     }
  184. }
  185.  
  186. static    void    DrawCube4(ViewWindow *win, int *vecx, int *vecy, int *vecz, int p1, int p2)
  187. {
  188.     int x1, x2, x3, x4;
  189.     int y1, y2, y3, y4;
  190.     x1 = win->offx + (vecx[0] + vecx[p1] + vecx[p2]);
  191.     y1 = win->offy - (vecy[0] + vecy[p1] + vecy[p2]);
  192.     x2 = win->offx + (vecx[0] + vecx[p1] - vecx[p2]);
  193.     y2 = win->offy - (vecy[0] + vecy[p1] - vecy[p2]);
  194.     x3 = win->offx + (vecx[0] - vecx[p1] - vecx[p2]);
  195.     y3 = win->offy - (vecy[0] - vecy[p1] - vecy[p2]);
  196.     x4 = win->offx + (vecx[0] - vecx[p1] + vecx[p2]);
  197.     y4 = win->offy - (vecy[0] - vecy[p1] + vecy[p2]);
  198.  
  199.     ClipLine( win, x1, y1, x2, y2, CURSOR_COLOR );
  200.     ClipLine( win, x2, y2, x3, y3, CURSOR_COLOR );
  201.     ClipLine( win, x3, y3, x4, y4, CURSOR_COLOR );
  202.     ClipLine( win, x4, y4, x1, y1, CURSOR_COLOR );
  203. }
  204.  
  205. static    void    DrawCube5(ViewWindow *win, int *vecx, int *vecy, int *vecz, int p1, int p2)
  206. {
  207.     int xp1, yp1, xp2, yp2, xp3, yp3;
  208.     int x[8], y[8], i;
  209.     if (vecz[p1] > 0) {
  210.         xp1 = vecx[p1];
  211.         yp1 = vecy[p1];
  212.     } else {
  213.         xp1 = -vecx[p1];
  214.         yp1 = -vecy[p1];
  215.     }
  216.     if (vecz[p2] > 0) {
  217.         xp2 = vecx[p2];
  218.         yp2 = vecy[p2];
  219.     } else {
  220.         xp2 = -vecx[p2];
  221.         yp2 = -vecy[p2];
  222.     }
  223.  
  224.     xp3 = vecx[6-p1-p2];
  225.     yp3 = vecy[6-p1-p2];
  226.  
  227.     for (i = 0; i < 8; ++i) {
  228.         x[i] = win->offx + vecx[0]
  229.              + (i & 1 ? xp1 : -xp1)
  230.              + (i & 2 ? xp2 : -xp2)
  231.              + (i & 4 ? xp3 : -xp3);
  232.         y[i] = win->offy - vecy[0]
  233.              - (i & 1 ? yp1 : -yp1)
  234.              - (i & 2 ? yp2 : -yp2)
  235.              - (i & 4 ? yp3 : -yp3);
  236.     }
  237.     ClipLine( win, x[1], y[1], x[2], y[2], CURSOR_COLOR );
  238.     ClipLine( win, x[5], y[5], x[6], y[6], CURSOR_COLOR );
  239.  
  240.     ClipLine( win, x[0], y[0], x[4], y[4], CURSOR_COLOR );
  241.     ClipLine( win, x[1], y[1], x[5], y[5], CURSOR_COLOR );
  242.     ClipLine( win, x[3], y[3], x[7], y[7], CURSOR_COLOR );
  243.     ClipLine( win, x[2], y[2], x[6], y[6], CURSOR_COLOR );
  244.  
  245. }
  246.  
  247. static    void    DrawCube9(ViewWindow *win, int *vecx, int *vecy, int *vecz)
  248. {
  249.     int x[8], y[8], i;
  250.     for (i = 0; i < 8; ++i) {
  251.         x[i] = win->offx + vecx[0]
  252.              + (i & 1 ? vecx[1] : -vecx[1])
  253.              + (i & 2 ? vecx[2] : -vecx[2])
  254.              + (i & 4 ? vecx[3] : -vecx[3]);
  255.         y[i] = win->offy - vecy[0]
  256.              - (i & 1 ? vecy[1] : -vecy[1])
  257.              - (i & 2 ? vecy[2] : -vecy[2])
  258.              - (i & 4 ? vecy[3] : -vecy[3]);
  259.     }
  260.     ClipLine( win, x[0], y[0], x[1], y[1], CURSOR_COLOR );
  261.     ClipLine( win, x[1], y[1], x[3], y[3], CURSOR_COLOR );
  262.     ClipLine( win, x[3], y[3], x[2], y[2], CURSOR_COLOR );
  263.     ClipLine( win, x[2], y[2], x[0], y[0], CURSOR_COLOR );
  264.     ClipLine( win, x[4], y[4], x[5], y[5], CURSOR_COLOR );
  265.     ClipLine( win, x[5], y[5], x[7], y[7], CURSOR_COLOR );
  266.     ClipLine( win, x[7], y[7], x[6], y[6], CURSOR_COLOR );
  267.     ClipLine( win, x[6], y[6], x[4], y[4], CURSOR_COLOR );
  268.     ClipLine( win, x[0], y[0], x[4], y[4], CURSOR_COLOR );
  269.     ClipLine( win, x[1], y[1], x[5], y[5], CURSOR_COLOR );
  270.     ClipLine( win, x[3], y[3], x[7], y[7], CURSOR_COLOR );
  271.     ClipLine( win, x[2], y[2], x[6], y[6], CURSOR_COLOR );
  272. }
  273.  
  274.  
  275. static    void    VertexConv( ret, mat, ver )
  276. Vertex    *ret, *ver ;
  277. int        mat[5][3] ;
  278. {
  279.     int        x, y, z ;
  280. #if 0
  281.  
  282.     x = (int)ver->x * mat[0][0]
  283.       + (int)ver->y * mat[1][0]
  284.       + (int)ver->z * mat[2][0]
  285.       + mat[3][0] ;
  286.     y = (int)ver->x * mat[0][1]
  287.       + (int)ver->y * mat[1][1]
  288.       + (int)ver->z * mat[2][1]
  289.       + mat[3][1] ;
  290.     z = (int)ver->x * mat[0][2]
  291.       + (int)ver->y * mat[1][2]
  292.       + (int)ver->z * mat[2][2]
  293.       + mat[3][2] ;
  294.  
  295.     ret->x = (short)( x >> 16 );
  296.     ret->y = (short)( y >> 16 );
  297.     ret->z = (short)( z >> 16 );
  298. #else
  299.     x = (((int)ver->x * mat[0][0]
  300.       +   (int)ver->y * mat[1][0]
  301.       +   (int)ver->z * mat[2][0]) >> 16)
  302.       +                 mat[3][0] ;
  303.     y = (((int)ver->x * mat[0][1]
  304.       +   (int)ver->y * mat[1][1]
  305.       +   (int)ver->z * mat[2][1]) >> 16)
  306.       +                 mat[3][1] ;
  307.     z = (((int)ver->x * mat[0][2]
  308.       +   (int)ver->y * mat[1][2]
  309.       +   (int)ver->z * mat[2][2]) >> 16)
  310.       +                 mat[3][2] ;
  311.  
  312.     ret->x = (short)x;
  313.     ret->y = (short)y;
  314.     ret->z = (short)z;
  315. #endif
  316. }
  317.  
  318. static    void    DrawBox( win, x1, y1, x2, y2 )
  319. ViewWindow    *win ;
  320. int        x1, y1, x2, y2 ;
  321. {
  322.     x1 += win->offx ;
  323.     x2 += win->offx ;
  324.     y1 += win->offy ;
  325.     y2 += win->offy ;
  326.     ClipLine( win, x1, y1, x2, y1, CURSOR_COLOR );
  327.     ClipLine( win, x1, y1, x1, y2, CURSOR_COLOR );
  328.     ClipLine( win, x1, y2, x2, y2, CURSOR_COLOR );
  329.     ClipLine( win, x2, y1, x2, y2, CURSOR_COLOR );
  330. }
  331.  
  332. #if 0
  333. static    void    DrawRectangle( flag, p0, p1, p2, p3 )
  334. int        flag ;
  335. Vertex    *p0, *p1, *p2, *p3 ;
  336. {
  337.     int        s0[3], s1[3], s2[3], s3[3] ;
  338.  
  339.     ToScreen( s0, p0 );
  340.     ToScreen( s1, p1 );
  341.     ToScreen( s2, p2 );
  342.     ToScreen( s3, p3 );
  343.  
  344.     if ( ViewMode & VIEW_PERS )
  345.         DrawLineForPers( p0, p1, CURSOR_COLOR );
  346.     if ( ! flag )
  347.         DrawLineForWin( s0, s1, CURSOR_COLOR );
  348.  
  349.     if ( ViewMode & VIEW_PERS )
  350.         DrawLineForPers( p0, p2, CURSOR_COLOR );
  351.     if ( ! flag )
  352.         DrawLineForWin( s0, s2, CURSOR_COLOR );
  353.     if ( memcmp( p0, p1, sizeof( Vertex ) ) != 0 )
  354.     {
  355.         if ( ViewMode & VIEW_PERS )
  356.             DrawLineForPers( p1, p3, CURSOR_COLOR );
  357.         if ( ! flag )
  358.             DrawLineForWin( s1, s3, CURSOR_COLOR );
  359.     }
  360.     if ( memcmp( p0, p2, sizeof( Vertex ) ) != 0 )
  361.     {
  362.         if ( ViewMode & VIEW_PERS )
  363.             DrawLineForPers( p2, p3, CURSOR_COLOR );
  364.         if ( ! flag )
  365.             DrawLineForWin( s2, s3, CURSOR_COLOR );
  366.     }
  367. }
  368. #endif
  369.  
  370. /*    透視図に長方形を描く    */
  371. void    ViewBox2D( x1, y1, x2, y2 )
  372. int        x1, y1, x2, y2 ;
  373. {
  374.  
  375.     if (viewbox2d_drawflag
  376.      && x1 == viewbox2d_x1 && y1 == viewbox2d_y1
  377.      && x2 == viewbox2d_x2 && y2 == viewbox2d_y2) {
  378.         viewbox2d_drawflag = FALSE;
  379.     } else {
  380.         viewbox2d_x1 = x1;
  381.         viewbox2d_y1 = y1;
  382.         viewbox2d_x2 = x2;
  383.         viewbox2d_y2 = y2;
  384.         viewbox2d_drawflag = TRUE;
  385.     }
  386.     ViewLine2D( x1, y1, x1, y2, CURSOR_COLOR );
  387.     ViewLine2D( x1, y1, x2, y1, CURSOR_COLOR );
  388.     ViewLine2D( x2, y1, x2, y2, CURSOR_COLOR );
  389.     ViewLine2D( x1, y2, x2, y2, CURSOR_COLOR );
  390. }
  391.