home *** CD-ROM | disk | FTP | other *** search
/ DOS/V Power Report 1997 March / VPR9703A.ISO / VPR_DATA / DOGA / SOURCES / POLYEDIT.LZH / MACRO / UVSHADEA.M < prev    next >
Text File  |  1996-06-24  |  11KB  |  491 lines

  1. var private const    UVmax_pos = 1;
  2. var private const    Deg_pos = 2;
  3. var private const    Houkou_pos = 3;
  4. var private    Menu_temp1;
  5. var private    Menu_temp2;
  6. var private    Menu_temp3;
  7.  
  8. function private InputUVmax()
  9. {
  10.     var    key, temp, msg[2];
  11.  
  12.     DlogOpen( "平面マッピング 設定変更", 1 );
  13.     DlogString( 0, "UV最大値   ", tostring( UVmax ), 6 );
  14.     key = DlogWait();
  15.     if( key == 0 )
  16.         return FALSE;
  17.     else
  18.     {
  19.         UVmax = atoi(DlogAnswer( 0 ));
  20.         Status_title[UVmax_pos] = "UV最大値:"+tostring( UVmax, 4 )+" ";
  21.         DrawStatus();
  22.         Menu_temp3 = ON;
  23.     }
  24.     return TRUE;
  25. }
  26.  
  27. function private InputSetup()
  28. {
  29.     var    key, temp, msg[2];
  30.  
  31.     msg = {"右回り","左回り" };
  32.     DlogOpen( "円筒マッピング 設定変更", 5 );
  33.     DlogString( 0, "UV最大値   ", tostring( UVmax ), 6 );
  34.     DlogString( 2, "角度       ", tostring( Menu_temp1 ), 6 );
  35.     DlogSelect( 4, "マッピング方向", msg, ( 1- Menu_temp2 ) );
  36.     key = DlogWait();
  37.     if( key == 0 )
  38.         return FALSE;
  39.     else
  40.     {
  41.         UVmax = atoi(DlogAnswer( 0 ));
  42.         Menu_temp1 = atoi(DlogAnswer( 2 ));
  43.         temp = DlogAnswer( 4 );
  44.         if( temp == 0 )
  45.             Menu_temp2 = ON;
  46.         else
  47.             Menu_temp2 = OFF;
  48.         Status_title[UVmax_pos] = "UV最大値:"+tostring( UVmax, 4 )+" ";
  49.         Status_title[Deg_pos] = "角度:"+ tostring( Menu_temp1 , 5)+" ";
  50.         if( Menu_temp2 == ON )
  51.             Status_title[Houkou_pos] = "マッピング方向:右回り";
  52.         else
  53.             Status_title[Houkou_pos] = "マッピング方向:左回り";
  54.         DrawStatus();
  55.         Menu_temp3 = ON;
  56.     }
  57.     return TRUE;
  58. }
  59.  
  60. //
  61. //    マッピング座標の設定
  62. //
  63. function private    DrawMap( v0, v1, v2 )
  64. {
  65.     var    pos ;
  66.     DrawLine( v1, v2 );
  67.     DrawLine( v1, v0 );
  68.     pos = v2 - v1 + v0 ;
  69.     DrawLine( pos, v2 );
  70.     DrawLine( pos, v0 );
  71. }
  72.  
  73. //
  74. //    平面マッピングUV座標生成
  75. //
  76. function _MapPlane()
  77. {
  78.     var    i, key, mat, vers;
  79.     var    org_pos[2], pos[3], temp;
  80.     var    u_vec, v_vec, mae_vec, move_vec;
  81.     var    pick, draw_flag = ON;
  82.  
  83.     if ( SelectPolygons() == 0 )
  84.     {
  85.         Warning();
  86.         if( MESSAGE == ON )
  87.             Message( error_msg3 );
  88.         return ;
  89.     }
  90.  
  91.     PushMenu();
  92.     Status_org = Status_title;
  93.     MenuPosition( Menu( " 動作設定(&S)",
  94.             "設定変更   ",        InputUVmax,
  95.             "-----------",        SEPARATE_MENU,
  96.             "マッピング中止    ESC",    MenuQuit
  97.         ), Menu_Title );
  98.     ClearStatus();
  99.     Status_title[0] = "【平面マッピング中】";
  100.     Status_title[UVmax_pos] = "UV最大値:"+tostring( UVmax, 4 )+" ";
  101.     DrawStatus();
  102.     quit_flag = FALSE;
  103.  
  104.     SelectBox( org_pos );
  105.  
  106.     cur = Cursor();
  107.     pos[0] = cur;
  108.     pick = cur;
  109.     vers = 1;
  110.     input_flag = OFF;
  111.     while( TRUE )
  112.     {
  113.         if( draw_flag == ON )
  114.         {
  115.             if( vers == 1 )
  116.                 DrawLine( pos[0], cur );
  117.             else
  118.                 DrawMap( pos[0], pos[1], cur );
  119.         }
  120.         key = WaitEvent();
  121.         if( key )
  122.             input_flag = ON;
  123.         if( (( ShiftStat() & 1 ) == 1))
  124.         {
  125.             cur = Cursor();
  126.             TrimCursor(pos[vers-1], MouseWindow());
  127.             Cursor( cur );
  128.         }
  129.         else
  130.             cur = Cursor();
  131.         key = KeyCode();
  132.         if ( input_flag | key == INPUT_KEY )
  133.         {
  134.             if( vers == 1 )
  135.             {
  136.                 if( pos[0] == cur )
  137.                 {
  138.                     Warning();
  139.                     if( MESSAGE == ON )
  140.                         Message( error_msg4 );
  141.                 }
  142.                 else
  143.                 {
  144.                     pos[1] = cur;
  145.                     vers++;
  146.                     DrawLine( pos[0], pos[1] );
  147.                 }
  148.             }
  149.             else
  150.             {
  151.                 u_vec = unit(Position( pos[1] ) - Position( pos[0] ));
  152.                 v_vec = unit(Position( pos[1] ) - Position( cur ));
  153.                 if( abs(v_vec*u_vec) < 0.00001 )
  154.                 {
  155.                     Warning();
  156.                     if( MESSAGE == ON )
  157.                         Message( "3点が一直線上にあるので\n"
  158.                             +"平面マッピングできません" );
  159.                 }
  160.                 else
  161.                 {
  162.                     pos[2] = cur;
  163.                     break;
  164.                 }
  165.             }
  166.         }
  167.         else if( key == ESC | quit_flag == TRUE )
  168.         {
  169.             PopandClear( OVERWRITE );
  170.             return;
  171.         }
  172.         else if( key == BS )
  173.         {
  174.             if( vers == 1 )
  175.             {
  176.                 PopandClear( OVERWRITE );
  177.                 return;
  178.             }
  179.             else
  180.             {
  181.                 vers--;
  182.                 DrawMap(pos[0], pos[1], pick );
  183.             }
  184.         }
  185.         else
  186.             temp = KeyProcess( key );
  187.         if( ((key!=0 ) & (key != INPUT_KEY)) | ( pick != Cursor()) )
  188.         {
  189.             draw_flag = ON;
  190.             if( vers == 1 )
  191.                 DrawLine( pos[0], pick );
  192.             else
  193.                 DrawMap( pos[0], pos[1], pick );
  194.         }
  195.         else
  196.             draw_flag = OFF;
  197.         pick = Cursor();
  198.         input_flag = OFF;
  199.     }
  200.     Clear( OVERWRITE );
  201.     move_vec = Position( pos[0] );
  202.     u_vec = ( Position( pos[1] ) - Position( pos[0] ))*(1.0/UVmax);
  203.     v_vec = ( Position( pos[2] ) - Position( pos[1] ))*(1.0/UVmax);
  204.     mae_vec = unit(u_vec*v_vec);
  205.     temp = vector( 0, 0, 0 );
  206.     mat = move(UNIT_MATRIX,move_vec*(-1)) / Matrix( u_vec, v_vec, mae_vec, temp );
  207.     PolyMap( TRUE, mat );
  208.     UpdateObject();
  209.     PopMenu();
  210.     Status_title = Status_org;
  211.     DrawStatus();
  212. }
  213.  
  214. //
  215. //    円筒マッピングUV座標の生成
  216. //
  217. function _CylinderMap()
  218. {
  219.     var    axis[2];
  220.     var    key, vers, len, ty, u, v;
  221.     var    axis_vec, mae_vec, yoko_vec;
  222.     var    temp, temp_mat, temp_pos, temp_vec;
  223.     var    rot_mat, move_mat;
  224.     var    z_mat, draw_edge;
  225.     var    box[2], b0[3], b1[3], rot_box[8], rot_box_pos[3];
  226.     var    i, j, k, theta;
  227.     var    line[5][2];
  228.     var    pick, draw_flag = ON;
  229.  
  230.     if( SelectPolygons() == 0 )
  231.     {
  232.         Warning();
  233.         if( MESSAGE == ON )
  234.             Message( error_msg3 );
  235.         return;
  236.     }
  237.  
  238.     Menu_temp1 = 360;
  239.     Menu_temp2 = OFF;    /*    生成方向 ON:右(?)    */
  240.     Menu_temp3 = OFF;
  241.  
  242.     SelectBox( box );
  243.     PushMenu();
  244.     Status_org = Status_title;
  245.     MenuPosition( Menu( " 動作設定(&S)",
  246.             "設定変更   ",        InputSetup,
  247.             "-----------",        SEPARATE_MENU,
  248.             "マッピング中止    ESC",    MenuQuit
  249.         ), Menu_Title );
  250.     ClearStatus();
  251.     Status_title[0] = "【円筒マッピング中】";
  252.     Status_title[UVmax_pos] = "UV最大値:"+tostring( UVmax, 4 )+" ";
  253.     Status_title[Deg_pos] = "角度:"+ tostring( Menu_temp1 , 5)+" ";
  254.     if( Menu_temp2 == ON )
  255.         Status_title[Houkou_pos] = "マッピング方向:右回り";
  256.     else
  257.         Status_title[Houkou_pos] = "マッピング方向:左回り";
  258.     DrawStatus();
  259.  
  260.     quit_flag = FALSE;
  261.  
  262. //    DegMenu( Menu_temp1 );
  263. //    HoukouMenu( Menu_temp2 );
  264.  
  265.     axis[0] = Cursor();
  266.     cur = Cursor();
  267.     pick = cur;
  268.     vers = 1;
  269.     input_flag = OFF;
  270.     while( TRUE )
  271.     {
  272.         if( draw_flag == ON )
  273.             DrawLine( axis[0], cur );
  274.         key = WaitEvent();
  275.         if( key )
  276.             input_flag = ON;
  277.         if( (( ShiftStat() & 1 ) == 1))
  278.         {
  279.             cur = Cursor();
  280.             TrimCursor(axis[0], MouseWindow());
  281.             Cursor( cur );
  282.         }
  283.         else
  284.             cur = Cursor();
  285.         key = KeyCode();
  286.         if( input_flag | key == INPUT_KEY )
  287.         {
  288.             axis[vers] = cur;
  289.             if( cur == axis[0] )
  290.             {
  291.                 Warning();
  292.                 if( MESSAGE == ON )
  293.                     Message( "円筒マッピングの軸を指定してください" );
  294.             }
  295.             else
  296.                 break;
  297.         }
  298.         else if( key == ESC | key == BS | quit_flag == TRUE )
  299.         {
  300.             PopandClear( OVERWRITE );
  301.             return;
  302.         }
  303.         else
  304.             temp = KeyProcess( key );
  305.         if( ((key!=0 ) & (key != INPUT_KEY)) | ( pick != Cursor()) )
  306.         {
  307.             draw_flag = ON;
  308.             DrawLine( axis[0], pick );
  309.         }
  310.         else
  311.             draw_flag = OFF;
  312.         pick = Cursor();
  313.         input_flag = OFF;
  314.     }
  315.     axis_vec = unit(Position( axis[1] )- Position( axis[0] ));
  316.     rot_mat = CalRotMatrix( axis_vec, vector( 0,0,1 ));
  317.     move_mat = move(UNIT_MATRIX, Position( axis[0] )*(-1));
  318.     z_mat = Matrix( 1,0,0, 0,1,0, 0,0,0, 0,0,0 );
  319.     temp_mat = move_mat*rot_mat*z_mat;
  320.  
  321.     Position( box[0], b0 );
  322.     Position( box[1], b1 );
  323.     rot_box[0] = Vertex( b0[0], b0[1], b0[2] )*temp_mat;
  324.     rot_box[1] = Vertex( b0[0], b0[1], b1[2] )*temp_mat;
  325.     rot_box[2] = Vertex( b0[0], b1[1], b0[2] )*temp_mat;
  326.     rot_box[3] = Vertex( b0[0], b1[1], b1[2] )*temp_mat;
  327.     rot_box[4] = Vertex( b1[0], b0[1], b0[2] )*temp_mat;
  328.     rot_box[5] = Vertex( b1[0], b0[1], b1[2] )*temp_mat;
  329.     rot_box[6] = Vertex( b1[0], b1[1], b0[2] )*temp_mat;
  330.     rot_box[7] = Vertex( b1[0], b1[1], b1[2] )*temp_mat;
  331.  
  332.     temp_pos = Vertex( 0,0,0 );
  333.     len = VertexLength2( rot_box[0],temp_pos );
  334.     for( i = 1; i< 8; i++ )
  335.     {
  336.         temp = VertexLength2( rot_box[i], temp_pos );
  337.         if( temp > len )
  338.             len = temp;
  339.     }
  340.     len = sqrt( len );
  341.  
  342.     mae_vec = vector( 1,0,0 );
  343.     yoko_vec = vector( 0, 1, 0 );
  344.  
  345.     temp_mat = scale(UNIT_MATRIX, len, len, len )/rot_mat;
  346.     draw_edge = DrawEquilateral( 30,axis[0], axis[0]+(mae_vec*temp_mat),
  347.                     axis[0]+(yoko_vec*temp_mat), OFF );
  348.     draw_edge += DrawEquilateral( 30,axis[1], axis[1]+(mae_vec*temp_mat),
  349.                     axis[1]+(yoko_vec*temp_mat), OFF );
  350.     DrawEdge( draw_edge );
  351.  
  352.     draw_flag = ON;
  353.     cur = Cursor();
  354.     pick = cur;
  355.     temp_mat = rot_mat*z_mat/rot_mat;
  356.     input_flag = OFF;
  357.     while( TRUE )
  358.     {
  359.         if( draw_flag == ON )
  360.         {
  361.             if( abs((Position(axis[0])-Position(cur))*axis_vec) > 0.00001 )
  362.             {
  363.                 temp_vec = unit((Position(cur)-Position(axis[0]))*temp_mat);
  364.                 box[0] = axis[0]+temp_vec*len;
  365.                 box[1] = axis[1]+temp_vec*len;
  366.                 temp_vec = unit(axis_vec*temp_vec);
  367.                 if( Menu_temp2 == OFF )
  368.                     temp_vec = temp_vec*(-1);
  369.                 i = ViewWidth(MouseWindow())/10;
  370.                 line[0][0] = axis[0];
  371.                 line[0][1] = box[0];
  372.                 line[1][0] = axis[1];
  373.                 line[1][1] = box[1];
  374.                 line[2][0] = box[0];
  375.                 line[2][1] = box[1];
  376.                 line[3][0] = box[0];
  377.                 line[3][1] = box[0]+temp_vec*i+axis_vec*5;
  378.                 line[4][0] = box[1];
  379.                 line[4][1] = box[1]+temp_vec*i+axis_vec*5;
  380.                 for( i = 0; i< 5;i++ )
  381.                     DrawLine( line[i][0], line[i][1] );
  382.             }
  383.         }
  384.         key = WaitEvent();
  385.         if( key )
  386.             input_flag = ON;
  387.         if(( ShiftStat() & 1 ) == 1 )
  388.         {
  389.             cur = Cursor();
  390.             TrimCursor(axis[0], MouseWindow());
  391.             Cursor( cur );
  392.         }
  393.         else
  394.             cur = Cursor();
  395.         key = KeyCode();
  396.         if ( input_flag | key == INPUT_KEY )
  397.         {
  398.             if( abs((Position(axis[0])-Position(cur))*axis_vec) > 0.00001 )
  399.                 break;
  400.         }
  401.         else if( key == ESC | key == BS | quit_flag == TRUE )
  402.         {
  403.             PopandClear( OVERWRITE );
  404.             return;
  405.         }
  406.         else
  407.             temp = KeyProcess( key );
  408.         if( ((key!=0 ) & (key != INPUT_KEY))|( pick != Cursor())|(Menu_temp3 == ON))
  409.         {
  410.             draw_flag = ON;
  411.             if( abs((Position(axis[0])-Position(pick))*axis_vec) > 0.00001 )
  412.             {
  413.                 for( i = 0; i< 5;i++ )
  414.                     DrawLine( line[i][0], line[i][1] );
  415.             }
  416.         }
  417.         else
  418.             draw_flag = OFF;
  419.         Menu_temp3 = OFF;
  420.         pick = Cursor();
  421.         input_flag = OFF;
  422.     }
  423.     Clear( OVERWRITE );
  424.  
  425.     mae_vec = unit(Position( cur )- Position( axis[0] ))*rot_mat*z_mat;
  426.     temp_mat = move_mat*rot_mat*CalRotMatrix(mae_vec, vector(1,0,0));
  427.     if( Menu_temp2 == OFF )
  428.         temp_mat = temp_mat*scale( UNIT_MATRIX, 1, -1, 1 );
  429.     len = abs( Position( axis[1] ) - Position( axis[0] ));
  430.     PolyLoadInit();
  431.     while( PolyLoad() )
  432.     {
  433.         vers = PolyVertexs();
  434.         ty = PolyType();
  435.         j = 0;
  436.         k = 0;
  437.         for( i = 0; i< vers;i++ )
  438.         {
  439.             temp_vec = Position(PolyGetVertex( i ))*temp_mat;
  440.             if( vx( temp_vec ) > 0 )
  441.             {
  442.                 if( vy(temp_vec) < 0 )
  443.                     j++;
  444.                 else
  445.                     k++;
  446.             }
  447.         }
  448.         for( i = 0; i< vers;i++ )
  449.         {
  450.             temp = PolyGetVertex( i );
  451.             temp_pos = Position( temp );
  452.             temp_vec = Vector( temp );
  453.             temp = temp*temp_mat;
  454.             Position( temp, b0 );
  455.             v = (0.0 + b0[2])*UVmax/len;
  456.             if(( b0[0] == 0 ) & ( b0[1] == 0 ))
  457.                 theta = 0;
  458.             else
  459.                 theta = atan2( b0[1], b0[0] );
  460.             if( theta < (0) )
  461.                 u = (theta*180/PI+360)*UVmax/Menu_temp1;
  462.             else
  463.                 u = (theta*180/PI)*UVmax/Menu_temp1;
  464.             if( ( j != 0 ) & ( k != 0 ))
  465.             {
  466.                 if( j>= k )
  467.                 {
  468.                     if( theta >= (0) )
  469.                         u = (theta*180/PI+360)*UVmax/Menu_temp1;
  470.                 }
  471.                 else
  472.                 {
  473.                     if( theta < (0) )
  474.                         u = (theta*180/PI)*UVmax/Menu_temp1;
  475.                 }
  476.             }
  477.             PolySetVertex( Vertex( temp_pos, temp_vec, u, v ), i );
  478.         }
  479.         if( ty == POLY_SIMPLE )
  480.             ty = POLY_UV;
  481.         else if( ty == POLY_SHADE )
  482.             ty = POLY_UVSHADE;
  483.         PolyType( ty );
  484.         PolySave();
  485.     }
  486.     UpdateObject();
  487.     PopMenu();
  488.     Status_title = Status_org;
  489.     DrawStatus();
  490. }
  491.