MATERIAL.C

Go to the documentation of this file.
00001 /* --
00002 OpenFX - Modelling, Animation and Rendering Package
00003 -- */
00004 
00005 /* file MATERIAL.C */
00006 
00007 // Handle surface attributes shaders and materials
00008 
00009 #define MODULE_MATERIAL 1
00010 
00011 #include "design.h"
00012 #include "faceattr.h"
00013 
00014 static int GetFaceAttribs(FACEATTRIBS *fa, int defaults);
00015 static void UpdateAttribsDialog(HWND, int, FACEATTRIBS *, FACEATTRIBS *);
00016 static void PutFaceAttribute(int what, FACEATTRIBS *f);
00017 static BOOL CALLBACK SetExternalShaderDlgProc(HWND hdlg,UINT msg,
00018                                     WPARAM wparam,LPARAM lparam);
00019 static void UpdateShaderLoadDialog(int id,HWND hdlg);
00020 static void UpdateShaderSetDialog(int sa, int id, HWND hdlg);
00021 static void LoadExternalShader(int id, HWND hdlg);
00022 static int  EditExternalShader(int id, HWND hdlg);
00023 static void DropExternalShader(int id, HWND hdlg);
00024 static void DrawShaderPreview(HDC hdc,RECT rc);
00025 static BOOL CALLBACK MaterialDlgProc(HWND hdlg,UINT msg,
00026                                     WPARAM wparam,LPARAM lparam);
00027 
00028 
00029 static FACEATTRIBS FaceAttribs,DefAttribs;
00030 static int nf;
00031 static int LastUsedMatID=0;
00032 static int IdentifiedShaderPoint=-1;
00033 
00034 
00035 static void UpdateGLview(void){
00036  if(ghwndOpenGLview != NULL)PostMessage(ghwndOpenGLview,(WM_USER+2),0,0);
00037 }
00038 
00039 static int GetFaceAttribs(FACEATTRIBS *fa, int defaults){
00040  unsigned char lfc[3],lsc[3];
00041  int ff,lma,lsm,lbr,lgl,ltr,lrf,lsh,lsa,lbu,lba,lbt,lbs,lbc,lpa,lgt,lst;
00042  int ma;
00043  face *fp;
00044  vertex *V0,*V1,*V2;
00045  int i,j,nff=0;
00046  fa->flag_fc = 1;
00047  fa->flag_sm = 1;                    /* SMOOTHING    */
00048  fa->flag_sa = 1;                    /* material ID  */
00049  fa->flag_bu = 1;                    /* map ID       */
00050  fa->flag_ba = 1;                    /* map active   */
00051  fa->flag_ma = 1;                    // face mapping coords
00052  for(i=0;i<3;i++){
00053    fa->fc[i]=LastColour[i];
00054  }
00055  fa->sm=  DefaultSmoothing;
00056  fa->sh=  0;   /* No Material applied  */
00057  fa->sa=  0;   /* material ID  */
00058  fa->bu=  0;   /* map ID       */
00059  fa->ba=  0;   /* map active   */
00060  fa->ma=  0;   // mapping coords 
00061  if(defaults)return 0;
00062  ff=1;   // first found
00063  if((fp=MainFp) != NULL)for(j=0;j<Nface;j++){
00064    V0=(MainVp+fp->V[0]); V1=(MainVp+fp->V[1]); V2=(MainVp+fp->V[2]);
00065    if(V0->status == SELECTED &&
00066       V1->status == SELECTED &&
00067       V2->status == SELECTED){
00068      nff++; /* make sure that at least one face is selected */
00069      if(fp->bSmooth)fa->sm=1; else fa->sm=0; /* smoothing on  */
00070      if(fp->gp)ma=1; else ma=0;    // face has mapping
00071      if(fp->gp)fa->ma=1;
00072      if(fp->imagemap < 0)fa->ba=0; else fa->ba=1;
00073      fa->bu=fp->imagemap;
00074      if(fp->material >= 0)fa->sh=1;  // some material on at least one face
00075      fa->sa=fp->material;
00076      fa->fc[0]=fp->color[0];
00077      fa->fc[1]=fp->color[1];
00078      fa->fc[2]=fp->color[2];
00079      if(ff){
00080        for(i=0;i<3;i++){lfc[i]=fa->fc[i];}
00081        lsm=fa->sm; lma=fa->ma;  // smoothing and mapping coords
00082        lsh=fa->sh; lsa=fa->sa;
00083        lbu=fa->bu; lba=fa->ba;
00084        ff=0;
00085      }
00086      else{
00087        for(i=0;i<3;i++){
00088          if(lfc[i] != fa->fc[i])fa->flag_fc = -1;
00089        }
00090        if(lsm != fa->sm)fa->flag_sm = -1;  // some faces smoothed - some not
00091        if(lma != ma)fa->flag_ma = -1;  // some faces mapped some not !
00092        if(lsa != fa->sa)fa->flag_sa = -1;
00093        if(lbu != fa->bu)fa->flag_bu = -1;
00094        if(lba != fa->ba)fa->flag_ba = -1;
00095      }
00096    }
00097    fp++;
00098  }
00099  return nff;
00100 }
00101 
00102 static void PutFaceAttribute(int what, FACEATTRIBS *f){
00103 /* what   0 = face colour
00104           1 = shader colour
00105           2 = gloss
00106           3 = trans
00107           4 = reflectance
00108           5 = smoothing
00109           6 = brilliance switch
00110           7 = shader ID
00111           8 = dump map
00112           9 = shader axis
00113          10 = set brush ID;
00114          11 = set tiled
00115          12 = set shaded
00116          13 = set clear
00117          14 = set parameter
00118          15 = set glass type
00119          16 = remove mapping coords
00120          17 = apply mapping coords.
00121 */
00122  face *fp;
00123  vertex *vp,*V0,*V1,*V2;
00124  int i,j;
00125  EDIT_ACTION=YES;
00126  if(what < 17 && (fp=MainFp) != NULL)for(i=0;i<Nface;i++){
00127    V0=(MainVp+fp->V[0]); V1=(MainVp+fp->V[1]); V2=(MainVp+fp->V[2]);
00128    if(  (V0->status == SELECTED)
00129      && (V1->status == SELECTED)
00130      && (V2->status == SELECTED)){
00131      if(what == 0){
00132        VECCOPY(f->fc,fp->color)
00133      }
00134      else if(what == 5){ /* smoothing */
00135        if(f->sm == 1)fp->bSmooth=TRUE; /* Set Smoothing bit #7            */
00136        else          fp->bSmooth=FALSE; /* Clear Smoothing bit in faces #7 */
00137      }
00138      else if(what == 8){ /* dump map - clear bit 6 */
00139        fp->imagemap = -1;
00140      }
00141      else if(what == 9){ /* set material    (could be -1) */
00142        fp->material  = (short)f->sa;
00143      }
00144      else if(what == 10){ /* set map */
00145        fp->imagemap    = (short)f->bu;
00146      }
00147      else if(what == 16){ /* drop mapping coords */
00148        fp->gp=FALSE; for(j=0;j<3;j++){fp->x[j]=0.0; fp->y[j]=0.0;}
00149      }
00150    }
00151    fp++;
00152  }
00153  PutNurbsAttribute(what,f);
00154  if(what == 16){ // need to drop vertex coords too.
00155    if(Nvert > 0)for(i=0,vp=MainVp;i<Nvert;i++,vp++)if(vp->status == SELECTED){
00156      vp->gp=0; vp->x=vp->y=0.0;
00157    }
00158  }
00159  if(what == 17){ // Assign Mapping Coordinates to selected
00160    long id,mapt,type = -1;
00161    if(nImaps == 1)id=0;
00162    else id=RequestMapID(ghwnd_main,"Choose Map",0);
00163    if(id >= 0 && id < nImaps){
00164      mapt=iMap[id].Map;
00165      if     (mapt == PLANE)type=0;
00166      else if(mapt == CYLINDER)type=1;         
00167      else if(mapt == PLANE_MOZIAC)type=0;
00168      else if(mapt == CYLINDER_MOZIAC)type=1;
00169      else if(mapt == MAP_SPHERICAL)type=2;
00170      // don't re-assign fixed mapping
00171      if(type >= 0){
00172        MapVerticesByImageMap(id,type); 
00173        MapFacesByImageMap(id,type,FALSE);
00174      }
00175      else MessageBox(NULL,"Fixed mapping not reassigned",NULL,MB_OK);
00176    }
00177  }
00178 }
00179 
00180 static void UpdateMapAttribsSection(HWND hdlg, int nff,
00181                                 FACEATTRIBS *fa, FACEATTRIBS *da){
00182  int active;
00183  char *map_type_list[]={"Still","Animate"};
00184  char *map_attr_list[]={"Plane","Cylinder","Moziac-P","Moziac-C","Fixed To","Spherical"};
00185  char str[64];
00186 
00187  if(nff != 0 && fa->ma)active=SW_SHOW; else active=SW_HIDE; // some mapping coords exists
00188  ShowWindow(GetDlgItem(hdlg,DLG_ATTRIBS_WARN_FMAP),active);
00189  if(nff != 0 && fa->flag_ma < 0)active=SW_SHOW; else active=SW_HIDE; // some mappcoords exist and some dont
00190  ShowWindow(GetDlgItem(hdlg,DLG_ATTRIBS_WARN_FMAP1),active);
00191  if(nff == 0){
00192    EnableWindow(GetDlgItem(hdlg,DLG_ATTRIBS_FMAP_ON),FALSE);
00193    EnableWindow(GetDlgItem(hdlg,DLG_ATTRIBS_FMAP_OFF),FALSE);
00194  }
00195  else if(nImaps == 0){
00196   if(fa->ma)EnableWindow(GetDlgItem(hdlg,DLG_ATTRIBS_FMAP_OFF),TRUE);
00197   else      EnableWindow(GetDlgItem(hdlg,DLG_ATTRIBS_FMAP_OFF),FALSE);
00198   EnableWindow(GetDlgItem(hdlg,DLG_ATTRIBS_FMAP_ON),FALSE);
00199  }
00200  else {
00201   if(fa->ma)EnableWindow(GetDlgItem(hdlg,DLG_ATTRIBS_FMAP_OFF),TRUE);
00202   else      EnableWindow(GetDlgItem(hdlg,DLG_ATTRIBS_FMAP_OFF),FALSE);
00203   EnableWindow(GetDlgItem(hdlg,DLG_ATTRIBS_FMAP_ON),TRUE);
00204  }
00205  if(nff == 0){
00206    EnableWindow(GetDlgItem(hdlg,DLG_ATTRIBS_MAP_SET),FALSE);
00207    EnableWindow(GetDlgItem(hdlg,DLG_ATTRIBS_MAP_DROP),FALSE);
00208    EnableWindow(GetDlgItem(hdlg,DLG_ATTRIBS_MAP_MOVE),FALSE);
00209    EnableWindow(GetDlgItem(hdlg,DLG_ATTRIBS_MAP_EDIT),FALSE);
00210    EnableWindow(GetDlgItem(hdlg,DLG_ATTRIBS_MAP_DELETE),FALSE);
00211    SetDlgItemText(hdlg,DLG_ATTRIBS_MAP_NAME,(LPCTSTR)"<No faces>");
00212    SetDlgItemText(hdlg,DLG_ATTRIBS_MAP_TYPE,(LPCTSTR)"  ");
00213    return;
00214  }
00215  EnableWindow(GetDlgItem(hdlg,DLG_ATTRIBS_MAP_SET),TRUE);
00216  if(fa->flag_ba < 0 || fa->ba == 0 || fa->flag_bu < 0 || nImaps == 0)active=FALSE;
00217  else                                                                active=TRUE;
00218  if(fa->flag_ba < 0){ /* brush activity is not consistant */
00219    SetDlgItemText(hdlg,DLG_ATTRIBS_MAP_NAME,(LPCTSTR)"<Some faces have map>");
00220    SetDlgItemText(hdlg,DLG_ATTRIBS_MAP_TYPE,(LPCTSTR)"  ");
00221    EnableWindow(GetDlgItem(hdlg,DLG_ATTRIBS_MAP_DROP),TRUE);
00222    EnableWindow(GetDlgItem(hdlg,DLG_ATTRIBS_MAP_MOVE),FALSE);
00223    EnableWindow(GetDlgItem(hdlg,DLG_ATTRIBS_MAP_EDIT),FALSE);
00224    EnableWindow(GetDlgItem(hdlg,DLG_ATTRIBS_MAP_DELETE),FALSE);
00225  }
00226  else{
00227    if(fa->ba == 0 || nImaps == 0){
00228      SetDlgItemText(hdlg,DLG_ATTRIBS_MAP_NAME,(LPCTSTR)"<No map>");
00229      SetDlgItemText(hdlg,DLG_ATTRIBS_MAP_TYPE,(LPCTSTR)"  ");
00230      EnableWindow(GetDlgItem(hdlg,DLG_ATTRIBS_MAP_DROP),FALSE);
00231      EnableWindow(GetDlgItem(hdlg,DLG_ATTRIBS_MAP_MOVE),FALSE);
00232      EnableWindow(GetDlgItem(hdlg,DLG_ATTRIBS_MAP_EDIT),FALSE);
00233      EnableWindow(GetDlgItem(hdlg,DLG_ATTRIBS_MAP_DELETE),FALSE);
00234    }
00235    else if(fa->flag_bu < 0){
00236      SetDlgItemText(hdlg,DLG_ATTRIBS_MAP_NAME,(LPCTSTR)"<Not all same map>");
00237      SetDlgItemText(hdlg,DLG_ATTRIBS_MAP_TYPE,(LPCTSTR)"  ");
00238      EnableWindow(GetDlgItem(hdlg,DLG_ATTRIBS_MAP_DROP),TRUE);
00239      EnableWindow(GetDlgItem(hdlg,DLG_ATTRIBS_MAP_MOVE),FALSE);
00240      EnableWindow(GetDlgItem(hdlg,DLG_ATTRIBS_MAP_EDIT),FALSE);
00241      EnableWindow(GetDlgItem(hdlg,DLG_ATTRIBS_MAP_DELETE),FALSE);
00242    }
00243    else{
00244      SetDlgItemText(hdlg,DLG_ATTRIBS_MAP_NAME,(LPCTSTR)iMap[fa->bu].N);
00245      sprintf(str,"%s and %s",map_type_list[min((int)iMap[fa->bu].Type,1)],
00246                                   map_attr_list[(int)(iMap[fa->bu].Map)] );
00247      SetDlgItemText(hdlg,DLG_ATTRIBS_MAP_TYPE,(LPCTSTR)str);
00248      EnableWindow(GetDlgItem(hdlg,DLG_ATTRIBS_MAP_DROP),TRUE);
00249      EnableWindow(GetDlgItem(hdlg,DLG_ATTRIBS_MAP_MOVE),TRUE);
00250      EnableWindow(GetDlgItem(hdlg,DLG_ATTRIBS_MAP_EDIT),TRUE);
00251      EnableWindow(GetDlgItem(hdlg,DLG_ATTRIBS_MAP_DELETE),TRUE);
00252    }
00253  }
00254 }
00255 
00256 static void UpdateShaderAttribsSection(HWND hdlg, int nff,
00257                                 FACEATTRIBS *fa, FACEATTRIBS *da){
00258  BOOL active;
00259  if(nff != 0 && (fa->flag_sa < 0))active=SW_SHOW; else active=SW_HIDE;
00260  ShowWindow(GetDlgItem(hdlg,DLG_ATTRIBS_WARN_MATL),active);
00261  if(nff == 0){
00262    EnableWindow(GetDlgItem(hdlg,DLG_ATTRIBS_MATL_SET),FALSE);
00263    EnableWindow(GetDlgItem(hdlg,DLG_ATTRIBS_MATL_DROP),FALSE);
00264    EnableWindow(GetDlgItem(hdlg,DLG_ATTRIBS_SHA_XMOVE),FALSE);
00265    EnableWindow(GetDlgItem(hdlg,DLG_ATTRIBS_MATL_EDIT),FALSE);
00266    SetDlgItemText(hdlg,DLG_ATTRIBS_MATL_NAME,(LPCTSTR)"<No faces>");
00267  }
00268  else{
00269    if(fa->sh == 0){
00270      EnableWindow(GetDlgItem(hdlg,DLG_ATTRIBS_MATL_SET),TRUE);
00271      EnableWindow(GetDlgItem(hdlg,DLG_ATTRIBS_MATL_DROP),FALSE);
00272      EnableWindow(GetDlgItem(hdlg,DLG_ATTRIBS_SHA_XMOVE),FALSE);
00273      EnableWindow(GetDlgItem(hdlg,DLG_ATTRIBS_MATL_EDIT),FALSE);
00274      SetDlgItemText(hdlg,DLG_ATTRIBS_MATL_NAME,(LPCTSTR)"<No Materials Applied>");
00275    }
00276    else{
00277      if(fa->flag_sa < 0){
00278        EnableWindow(GetDlgItem(hdlg,DLG_ATTRIBS_MATL_SET),TRUE);
00279        EnableWindow(GetDlgItem(hdlg,DLG_ATTRIBS_MATL_DROP),TRUE);
00280        EnableWindow(GetDlgItem(hdlg,DLG_ATTRIBS_SHA_XMOVE),FALSE);
00281        EnableWindow(GetDlgItem(hdlg,DLG_ATTRIBS_MATL_EDIT),FALSE);
00282        SetDlgItemText(hdlg,DLG_ATTRIBS_MATL_NAME,(LPCTSTR)"<Not same>");
00283      }
00284      else{
00285        EnableWindow(GetDlgItem(hdlg,DLG_ATTRIBS_MATL_SET),TRUE);
00286        EnableWindow(GetDlgItem(hdlg,DLG_ATTRIBS_MATL_DROP),TRUE);
00287        EnableWindow(GetDlgItem(hdlg,DLG_ATTRIBS_SHA_XMOVE),TRUE);
00288        EnableWindow(GetDlgItem(hdlg,DLG_ATTRIBS_MATL_EDIT),TRUE);
00289        SetDlgItemText(hdlg,DLG_ATTRIBS_MATL_NAME,(LPCTSTR)iMat[fa->sa].name);
00290      }
00291    }
00292  }
00293 }
00294 
00295 static void UpdateSmoothAttribsSection(HWND hdlg, int nff,
00296                                 FACEATTRIBS *fa, FACEATTRIBS *da){
00297  BOOL active;
00298  if(nff != 0 && fa->flag_sm < 0)active=SW_SHOW; else active=SW_HIDE;
00299  ShowWindow(GetDlgItem(hdlg,DLG_ATTRIBS_WARN_SMOOTH),active);
00300  if(nff == 0){
00301    EnableWindow(GetDlgItem(hdlg,DLG_ATTRIBS_SMOOTH_ON),FALSE);
00302    SendDlgItemMessage(hdlg,DLG_ATTRIBS_SMOOTH_ON, BM_SETCHECK,BST_INDETERMINATE,0);
00303  }
00304  else {
00305    EnableWindow(GetDlgItem(hdlg,DLG_ATTRIBS_SMOOTH_ON),TRUE);
00306    if( fa->flag_sm < 0){
00307      SendDlgItemMessage(hdlg,DLG_ATTRIBS_SMOOTH_ON, BM_SETCHECK,BST_INDETERMINATE,0);
00308    }
00309    else{
00310      SendDlgItemMessage(hdlg,DLG_ATTRIBS_SMOOTH_ON, BM_SETCHECK,fa->sm,0);
00311    }
00312  }
00313 }
00314 
00315 static void UpdateColourAttribsSection(HWND hdlg, int nff,
00316                                 FACEATTRIBS *fa, FACEATTRIBS *da){
00317  int active;
00318  if(nff != 0 && fa->flag_fc < 0)active=SW_SHOW; else active=SW_HIDE;
00319  ShowWindow(GetDlgItem(hdlg,DLG_ATTRIBS_WARN_FC),active);
00320  if(nff != 0)active=TRUE; else active=FALSE;
00321  EnableWindow(GetDlgItem(hdlg,DLG_ATTRIBS_FC_EDIT),active);
00322  if(nff != 0){
00323    VECCOPY(FaceAttribs.fc,DefAttribs.fc)
00324    VECCOPY(DefAttribs.fc,LastColour)
00325  }
00326  InvalidateRect(GetDlgItem(hdlg,DLG_ATTRIBS_FC_EDIT),NULL,FALSE);
00327 
00328  if(nff != 0 && (fa->sh == 1))active=SW_SHOW; else active=SW_HIDE;
00329  ShowWindow(GetDlgItem(hdlg,DLG_ATTRIBS_WARN_MESSAGE),active);
00330  ShowWindow(GetDlgItem(hdlg,DLG_ATTRIBS_WARN_TRIANGL),active);
00331 
00332 }
00333 
00334 
00335 static void UpdateTitleDialog(HWND hdlg,int nff){
00336  char str[128];
00337  char ltext[256];
00338  LoadString(ghinst_main,IDX_MISC_SETATTRIBS,ltext,256);
00339  sprintf(str,ltext,nff);
00340  SendMessage(hdlg,WM_SETTEXT,0,(LPARAM)str);
00341 }
00342 
00343 static void UpdateAttribsDialog(HWND hdlg, int nff,
00344                                 FACEATTRIBS *fa, FACEATTRIBS *da){
00345  UpdateTitleDialog(hdlg,nff);
00346  UpdateColourAttribsSection(hdlg,nff,fa,da);
00347  UpdateSmoothAttribsSection(hdlg,nff,fa,da);
00348  UpdateShaderAttribsSection(hdlg,nff,fa,da);
00349  UpdateMapAttribsSection(hdlg,nff,fa,da);
00350 }
00351 
00352 
00353 static BOOL APIENTRY PropColourDlgProc(HWND hDlg,UINT msg,
00354                                     WPARAM wParam, LPARAM lParam){
00355  HWND hctl;
00356  int i,iMin,iMax;
00357  LPDRAWITEMSTRUCT lpdis;
00358  FACEATTRIBS *fa,*da;
00359  fa=&FaceAttribs; da=&DefAttribs;
00360  switch(msg){
00361    case WM_PAINT:
00362      PaintDialogBackground(hDlg,ghinst_main);
00363      break;
00364    case WM_INITDIALOG:
00365 //     image_edit_flag= -1;                     //  Put these lines in which
00366 //     nf=GetFaceAttribs(da,1);                 //  ever is the default dialog tab
00367 //     nf=GetFaceAttribs(fa,0);                 //  
00368 //     UpdateTitleDialog(GetParent(hDlg),nf);   //
00369      UpdateColourAttribsSection(hDlg,nf,fa,da);
00370      UpdateSmoothAttribsSection(hDlg,nf,fa,da);
00371      return TRUE;
00372    case WM_DRAWITEM:{
00373        HBRUSH   hbr,hbrold;
00374        BYTE r,g,b;
00375        lpdis=(LPDRAWITEMSTRUCT)lParam;
00376        if(lpdis->CtlID == DLG_ATTRIBS_FC_EDIT){
00377          r=da->fc[0]; g=da->fc[1]; b=da->fc[2];
00378          if(lpdis->itemState & ODS_DISABLED){
00379            r=128; g=128; b=128;
00380          }
00381          if(lpdis->itemState & ODS_SELECTED)
00382            InvertRect(lpdis->hDC,&(lpdis->rcItem));
00383          else{
00384            if(lpdis->itemState & ODS_DISABLED)
00385                 hbr=CreateHatchBrush(HS_DIAGCROSS,RGB(128,128,128));
00386            else hbr=CreateSolidBrush(RGB(r,g,b));
00387            hbrold=SelectObject(lpdis->hDC,hbr);
00388            Rectangle(lpdis->hDC,lpdis->rcItem.left,lpdis->rcItem.top,
00389                          lpdis->rcItem.right,lpdis->rcItem.bottom);
00390            SelectObject(lpdis->hDC,hbrold);
00391            DeleteObject(hbr);
00392          }
00393        }
00394      }
00395      return TRUE;
00396    case WM_HSCROLL:
00397      break;
00398    case WM_COMMAND:
00399      switch(LOWORD(wParam)){
00400        case DLG_ATTRIBS_UPDATE:
00401          nf=GetFaceAttribs(fa,0);
00402          UpdateTitleDialog(GetParent(hDlg),nf);
00403          UpdateColourAttribsSection(hDlg,nf,fa,da);
00404          UpdateSmoothAttribsSection(hDlg,nf,fa,da);
00405          break;
00406        case DLG_ATTRIBS_FC_EDIT:
00407          if(SetSfxColour(da->fc,IDX_MISC_FACE_COLOUR,hDlg)){
00408            VECCOPY(da->fc,fa->fc) fa->flag_fc=1;
00409            VECCOPY(da->fc,LastColour)
00410            UpdateColourAttribsSection(hDlg,nf,fa,da);
00411            PutFaceAttribute(0,fa);
00412            UpdateGLview();
00413          }
00414          InvalidateRect(GetDlgItem(hDlg,DLG_ATTRIBS_FC_EDIT),NULL,FALSE);
00415 
00416          break;
00417        case DLG_ATTRIBS_SMOOTH_ON:
00418          i=SendDlgItemMessage(hDlg,DLG_ATTRIBS_SMOOTH_ON,BM_GETCHECK,0,0);
00419          if(i == BST_CHECKED)fa->sm=da->sm=0;
00420          else                fa->sm=da->sm=1;
00421          fa->flag_sm=1;
00422          UpdateSmoothAttribsSection(hDlg,nf,fa,da);
00423          PutFaceAttribute(5,fa);
00424          UpdateGLview();
00425          break;
00426        default:
00427          break;
00428      }
00429    default:   break;
00430  }
00431  return FALSE;
00432 }
00433 
00434 static BOOL APIENTRY PropTextureDlgProc(HWND hDlg,UINT msg, // NOT USED 
00435                                        WPARAM wParam, LPARAM lParam){
00436  HWND hctl;
00437  int i,iMin,iMax;
00438  FACEATTRIBS *fa,*da;
00439  fa=&FaceAttribs; da=&DefAttribs;
00440  switch(msg){
00441    case WM_PAINT:
00442      PaintDialogBackground(hDlg,ghinst_main);
00443      break;
00444    case WM_INITDIALOG:
00445      return TRUE;
00446      break;
00447    case WM_COMMAND:
00448      switch(LOWORD(wParam)){
00449        case DLG_ATTRIBS_UPDATE:
00450          nf=GetFaceAttribs(fa,0);
00451          UpdateTitleDialog(GetParent(hDlg),nf);
00452          break;
00453        default:
00454          break;
00455      }
00456    default:   break;
00457  }
00458  return FALSE;
00459 }
00460 
00461 static BOOL APIENTRY PropShaderDlgProc(HWND hDlg,UINT msg,
00462                                        WPARAM wParam, LPARAM lParam){
00463  HWND hctl;
00464  int i,iMin,iMax;
00465  LPDRAWITEMSTRUCT lpdis;
00466  FACEATTRIBS *fa,*da;
00467  fa=&FaceAttribs; da=&DefAttribs;
00468  switch(msg){
00469    case WM_PAINT:
00470      PaintDialogBackground(hDlg,ghinst_main);
00471      break;
00472    case WM_INITDIALOG:
00473      image_edit_flag= -1;                     //  Put these lines in which
00474      nf=GetFaceAttribs(da,1);                 //  ever is the default dialog tab
00475      nf=GetFaceAttribs(fa,0);                 //  
00476      UpdateTitleDialog(GetParent(hDlg),nf);   //
00477      UpdateShaderAttribsSection(hDlg,nf,fa,da);
00478      return TRUE;
00479      break;
00480    case WM_COMMAND:
00481      switch(LOWORD(wParam)){
00482        case DLG_ATTRIBS_UPDATE:
00483          nf=GetFaceAttribs(fa,0);
00484          UpdateTitleDialog(GetParent(hDlg),nf);
00485          UpdateShaderAttribsSection(hDlg,nf,fa,da);
00486          break;
00487        case DLG_ATTRIBS_MATL_DROP:
00488          fa->flag_sa=1; fa->sa = -1;
00489          fa->sh=0;
00490          PutFaceAttribute(9,fa);
00491          UpdateShaderAttribsSection(hDlg,nf,fa,da);
00492          UpdateGLview();
00493          break;
00494        case DLG_ATTRIBS_MATL_SET:
00495          if(nMats <= 0){
00496            CreateMaterial(TRUE);
00497            if(nMats <= 0){
00498              SendPrgmQuery(IDQ_NOSHADERAXES,0);
00499              break;
00500            }
00501            fa->flag_sa=1; fa->sa=0; fa->sh=1;
00502            PutFaceAttribute(9,fa);
00503            UpdateShaderAttribsSection(hDlg,nf,fa,da);
00504            UpdateGLview();
00505            break;
00506          }
00507          else {
00508            int sa;
00509            if((sa=RequestMaterialID(hDlg,"Choose Material to Use")) >= 0){
00510              fa->flag_sa=1; fa->sa=sa; fa->sh=1;
00511              PutFaceAttribute(9,fa);
00512              UpdateShaderAttribsSection(hDlg,nf,fa,da);
00513              UpdateGLview();
00514            }
00515          }
00516          break;
00517        case DLG_ATTRIBS_MATL_EDIT:
00518          if(fa->flag_sa > 0 && fa->sa >= 0 && fa->sh == 1){
00519            image_edit_flag=1;   // so we return to editing faces attributes
00520            axis_move_flag=fa->sa;
00521            DestroyWindow(ghwndDlgAttribs);
00522            ghwndDlgAttribs=NULL;
00523            if(sktool == YES)DisengageSkeletonTool();
00524            PostMessage(ghwnd_main,WM_COMMAND,IDM_MATERIAL_EDIT,0);
00525          }
00526          break; 
00527        case DLG_ATTRIBS_SHA_XMOVE:
00528          if(fa->sa >= 0){
00529            if(tool != NOTOOL)DisengageDesignerTool();
00530            if(sktool == YES)DisengageSkeletonTool();
00531            PositionMaterialOnOff(1,fa->sa);
00532          }
00533          break;
00534       default:
00535         break;
00536      }
00537    default:   break;
00538  }
00539  return FALSE;
00540 }
00541 
00542 static BOOL APIENTRY PropMappingDlgProc(HWND hDlg,UINT msg,
00543                                        WPARAM wParam, LPARAM lParam){
00544  FACEATTRIBS *fa,*da;
00545  fa=&FaceAttribs; da=&DefAttribs;
00546  switch(msg){
00547    case WM_PAINT:
00548      PaintDialogBackground(hDlg,ghinst_main);
00549      break;
00550    case WM_INITDIALOG:
00551      UpdateMapAttribsSection(hDlg,nf,fa,da);
00552      return TRUE;
00553      break;
00554    case WM_COMMAND:
00555      switch(LOWORD(wParam)){
00556        case DLG_ATTRIBS_UPDATE:
00557          nf=GetFaceAttribs(fa,0);
00558          UpdateTitleDialog(GetParent(hDlg),nf);
00559          UpdateMapAttribsSection(hDlg,nf,fa,da);
00560          break;
00561        case DLG_ATTRIBS_MAP_SET:{
00562          int id;
00563            if((id=RequestMapID(hDlg,"Choose map",1)) < 0)break;
00564            if(id == nImaps){
00565              if(CreateImageMap() == FAIL){
00566                SendPrgmQuery(IDQ_NOMOREMAPS,0);
00567                break;
00568              }
00569            }
00570            fa->flag_ba=1; fa->ba=1;  /* brush    */
00571            fa->flag_bu=1; fa->bu=id; /* brush ID */
00572            PutFaceAttribute(10,fa);
00573            UpdateMapAttribsSection(hDlg,nf,fa,da);
00574            UpdateGLview();
00575          }
00576          break;
00577        case DLG_ATTRIBS_MAP_DROP:
00578          fa->flag_ba=1; fa->ba=0;
00579          fa->flag_bu=1; fa->bu &=0xe0;
00580          PutFaceAttribute(8,fa);
00581          UpdateMapAttribsSection(hDlg,nf,fa,da);
00582          UpdateGLview();
00583          break;
00584        case DLG_ATTRIBS_FMAP_ON:
00585          fa->ma=1; 
00586          fa->flag_ma=1;
00587          PutFaceAttribute(17,fa);
00588          UpdateMapAttribsSection(hDlg,nf,fa,da);
00589          UpdateGLview();
00590          break;
00591        case DLG_ATTRIBS_FMAP_OFF:
00592          fa->ma=0; 
00593          fa->flag_ma=1;
00594          PutFaceAttribute(16,fa);
00595          UpdateMapAttribsSection(hDlg,nf,fa,da);
00596          UpdateGLview();
00597          break;
00598        case DLG_ATTRIBS_MAP_MOVE:
00599          if(tool != NOTOOL)DisengageDesignerTool();
00600          if(sktool == YES)DisengageSkeletonTool();
00601          PositionBrushOnOff(1,fa->bu);
00602          break;
00603        case DLG_ATTRIBS_MAP_EDIT:
00604          image_edit_flag=1;    // so that we return to editing the face attributes
00605          axis_move_flag=fa->bu;
00606          DestroyWindow(ghwndDlgAttribs);
00607          ghwndDlgAttribs=NULL;
00608          if(sktool == YES)DisengageSkeletonTool();
00609          PostMessage(ghwnd_main,WM_COMMAND,IDM_ATTRIBUTES_MAP,0);
00610          break;
00611        case DLG_ATTRIBS_MAP_DELETE:   /* not used yet */
00612          break;
00613        default:
00614          break;
00615      }
00616    default:   break;
00617  }
00618  return FALSE;
00619 }
00620 
00621 #define D_BORDER 4
00622 #define N_PAGES  3
00623 
00624 
00625 typedef struct tag_dlghdr1 {
00626   HWND hwndDlg[N_PAGES];
00627   RECT rcTab;
00628 } DLGHDR1;
00629 
00630 static DLGHDR1 DlgHdrAttribs,*lpDlgHdrAttribs;
00631 static HWND    hwndTabAttribs;
00632 
00633 BOOL CALLBACK AttribsDlgProc(HWND hdlg,UINT msg,
00634                              WPARAM wparam,LPARAM lparam){
00635  static BOOL bRollUp=FALSE;
00636  static RECT RollUpRect;
00637  static int xpos= -1,ypos= -1;
00638  int i;
00639  RECT rc,rd;
00640  char str[32];
00641  switch( msg ){
00642 //   case WM_PAINT: // Include this if the tabbed page bacground is to be textured
00643 //     PaintDialogBackground(hdlg,ghinst_main);
00644 //     break;
00645    case WM_INITDIALOG:{
00646        TC_ITEM tie;
00647        int h;
00648        DWORD dwDlgBase=GetDialogBaseUnits();
00649        if(xpos == -1 && ypos == -1)CentreDialogOnScreen(hdlg);
00650        else SetWindowPos(hdlg,HWND_TOP,xpos,ypos,0,0,SWP_NOSIZE);
00651        lpDlgHdrAttribs=&DlgHdrAttribs;
00652        GetWindowRect(hdlg,&rc);
00653        GetWindowRect(GetDlgItem(hdlg,DLG_ATTRIBS_PREVIEW),&rd);
00654        h=rd.top-rc.top-GetSystemMetrics(SM_CYCAPTION);
00655        GetClientRect(hdlg,&rc);
00656        hwndTabAttribs=CreateWindow(WC_TABCONTROL," ",
00657                             WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS,
00658                             (D_BORDER*LOWORD(dwDlgBase))/4,
00659                             (D_BORDER*HIWORD(dwDlgBase))/8,
00660                             rc.right-(2*D_BORDER*LOWORD(dwDlgBase))/4,
00661                             h-(2*D_BORDER*HIWORD(dwDlgBase))/8,
00662                             hdlg,NULL,ghinst_main,NULL);
00663        {char text_string[255];
00664          LoadString(ghinst_main,IDX_MISC_LANGUAGE,
00665                     text_string,sizeof(text_string));
00666          if(strcmp(text_string,"ENGLISH") == 0)
00667            SendMessage(hwndTabAttribs,WM_SETFONT, /* for text in tab */
00668                   (WPARAM)GetStockObject(ANSI_VAR_FONT),0);
00669        }
00670        memset(&tie,0,sizeof(TC_ITEM));
00671        tie.mask = TCIF_TEXT | TCIF_IMAGE;
00672        tie.iImage = -1;
00673        tie.pszText=str;
00674 //       LoadString(ghinst_main,IDX_MISC_ATT_SETTINGS,str,32);
00675 //       TabCtrl_InsertItem(hwndTabAttribs,3,&tie);
00676        LoadString(ghinst_main,IDX_MISC_ATT_SHADER,str,32);
00677        TabCtrl_InsertItem(hwndTabAttribs,0,&tie);
00678        LoadString(ghinst_main,IDX_MISC_ATT_MAPS,str,32);
00679        TabCtrl_InsertItem(hwndTabAttribs,1,&tie);
00680        LoadString(ghinst_main,IDX_MISC_ATT_COLOUR,str,32);
00681        TabCtrl_InsertItem(hwndTabAttribs,2,&tie);
00682        SetRectEmpty(&rc);
00683        TabCtrl_AdjustRect(hwndTabAttribs,FALSE,&rc);
00684        rc.left += (D_BORDER*LOWORD(dwDlgBase))/4;
00685        rc.top += (D_BORDER*HIWORD(dwDlgBase))/8;
00686        CopyRect(&(lpDlgHdrAttribs->rcTab),&rc);
00687        for(i=0;i<N_PAGES;i++)lpDlgHdrAttribs->hwndDlg[i]=NULL;
00688        lpDlgHdrAttribs->hwndDlg[0]=CreateDialog(ghinst_main,
00689                                    "ATTRIBSPAGE3",
00690                                    hdlg,(DLGPROC)PropShaderDlgProc);
00691 //       lpDlgHdrAttribs->hwndDlg[0]=CreateDialog(ghinst_main,
00692 //                                   "ATTRIBSPAGE1",
00693 //                                   hdlg,(DLGPROC)PropColourDlgProc);
00694        SetWindowPos(lpDlgHdrAttribs->hwndDlg[0],NULL,
00695                     lpDlgHdrAttribs->rcTab.left,
00696                     lpDlgHdrAttribs->rcTab.top,0,0,
00697                     SWP_NOSIZE | SWP_NOZORDER);
00698        ShowWindow(lpDlgHdrAttribs->hwndDlg[0],SW_SHOW);
00699      }
00700      return TRUE;
00701    case WM_NOTIFY:
00702      if( ((LPNMHDR)lparam)->code == TCN_SELCHANGING){
00703        int id=TabCtrl_GetCurSel(hwndTabAttribs);
00704      }
00705      if( ((LPNMHDR)lparam)->code == TCN_SELCHANGE){
00706        int id=TabCtrl_GetCurSel(hwndTabAttribs);
00707        for(i=0;i<N_PAGES;i++){
00708          if(lpDlgHdrAttribs->hwndDlg[i] != NULL){
00709            DestroyWindow(lpDlgHdrAttribs->hwndDlg[i]);
00710            lpDlgHdrAttribs->hwndDlg[i]=NULL;
00711          }
00712        }
00713        if(id == 2)lpDlgHdrAttribs->hwndDlg[2]=CreateDialog(ghinst_main,
00714                                    "ATTRIBSPAGE1",
00715                                    hdlg,(DLGPROC)PropColourDlgProc);
00716 //       if(id == 3)lpDlgHdrAttribs->hwndDlg[3]=CreateDialog(ghinst_main,
00717 //                                   "ATTRIBSPAGE2",
00718 //                                   hdlg,(DLGPROC)PropTextureDlgProc);
00719        if(id == 0)lpDlgHdrAttribs->hwndDlg[0]=CreateDialog(ghinst_main,
00720                                    "ATTRIBSPAGE3",
00721                                    hdlg,(DLGPROC)PropShaderDlgProc);
00722        if(id == 1)lpDlgHdrAttribs->hwndDlg[1]=CreateDialog(ghinst_main,
00723                                    "ATTRIBSPAGE4",
00724                                    hdlg,(DLGPROC)PropMappingDlgProc);
00725        if(lpDlgHdrAttribs->hwndDlg[id] != NULL){
00726          SetWindowPos(lpDlgHdrAttribs->hwndDlg[id],NULL,
00727                       lpDlgHdrAttribs->rcTab.left,
00728                       lpDlgHdrAttribs->rcTab.top,0,0,
00729                       SWP_NOSIZE | SWP_NOZORDER);
00730          ShowWindow(lpDlgHdrAttribs->hwndDlg[id],SW_SHOW);
00731        }
00732      }
00733      break;
00734    case WM_SYSCOMMAND:
00735      switch(LOWORD(wparam & 0xfff0)){
00736        default:
00737          break;
00738        case SC_MINIMIZE:
00739          if(bRollUp){
00740            SetWindowPos(hdlg,NULL,0,0,RollUpRect.right-RollUpRect.left,
00741                                       RollUpRect.bottom-RollUpRect.top,
00742                                       SWP_NOMOVE | SWP_NOZORDER);
00743          }
00744          else{
00745            GetWindowRect(hdlg,&RollUpRect);
00746            SetWindowPos(hdlg,NULL,0,0,RollUpRect.right-RollUpRect.left,
00747                                       GetSystemMetrics(SM_CYCAPTION)+
00748                                       2*GetSystemMetrics(SM_CYFIXEDFRAME),
00749                                       SWP_NOMOVE | SWP_NOZORDER);
00750          }
00751          bRollUp ^= 1;
00752          return TRUE;
00753          break;
00754        case SC_RESTORE:
00755          return TRUE;
00756          break;
00757        case SC_CLOSE:
00758          PostMessage(ghwnd_main,WM_COMMAND,IDM_ATTRIBUTES_FACE,0);
00759          break;
00760      }
00761      break;
00762    case WM_COMMAND:
00763      switch( LOWORD(wparam) ){
00764        case DLG_ATTRIBS_UPDATE:
00765          for(i=0;i<N_PAGES;i++){
00766            if(lpDlgHdrAttribs->hwndDlg[i] != NULL)
00767              PostMessage(lpDlgHdrAttribs->hwndDlg[i],
00768              WM_COMMAND,DLG_ATTRIBS_UPDATE,0);
00769          }
00770          break;
00771        case DLG_ATTRIBS_PREVIEW:
00772          PostMessage(ghwnd_main,WM_COMMAND,IDM_VIEW_RENDERINWINDOW,0);
00773          break;
00774        case DLG_ATTRIBS_CLOSE:
00775        case IDOK:
00776        case IDCANCEL:
00777          PostMessage(ghwnd_main,WM_COMMAND,IDM_ATTRIBUTES_FACE,0);
00778          break;
00779        default:
00780          break;
00781      }
00782      break;
00783    case WM_NCHITTEST:
00784      if(current_message >= 0){
00785        current_message = -1;
00786        SendPrgmMessage(IDS_ZERO,1);
00787      }
00788      break;
00789    case WM_DESTROY:
00790      for(i=0;i<N_PAGES;i++){
00791        if(lpDlgHdrAttribs->hwndDlg[i] != NULL)
00792          DestroyWindow(lpDlgHdrAttribs->hwndDlg[i]);
00793        lpDlgHdrAttribs->hwndDlg[i]=NULL;
00794      }
00795      GetWindowRect(hdlg,&rc);
00796      bRollUp=FALSE;
00797      xpos=rc.left; ypos=rc.top;
00798      break;
00799    default: break;
00800  }
00801  return( FALSE );
00802 }
00803 
00804 
00806 
00807 BOOL IsAttribDlgMessage(HWND hWnd, LPMSG lpMsg){
00808  int i;
00809  DLGHDR1 *lpDlgHdr;
00810  lpDlgHdr=&DlgHdrAttribs;
00811  for(i=0;i<N_PAGES;i++){
00812    if(lpDlgHdr->hwndDlg[i] != NULL){
00813      if(IsDialogMessage(lpDlgHdr->hwndDlg[i],lpMsg))return TRUE;
00814    }
00815  }
00816  return IsDialogMessage(hWnd,lpMsg);
00817 }
00818 
00819 
00821 
00822 BOOL CALLBACK DefaultAttribsDlgProc(HWND hdlg,UINT msg,
00823                              WPARAM wparam,LPARAM lparam){
00824  HWND hctl;
00825  BOOL err;
00826  int i,iMin,iMax;
00827  LPDRAWITEMSTRUCT lpdis;
00828  static FACEATTRIBS DefAttribs;
00829  static int nf;
00830  FACEATTRIBS *da;
00831  char str[32];
00832  da=&DefAttribs;
00833  switch( msg ) {
00834    case WM_PAINT:
00835      PaintDialogBackground(hdlg,ghinst_main);
00836      break;
00837    case WM_INITDIALOG:
00838      GetFaceAttribs(da,1);
00839      for(i=0;i<3;i++){
00840        da->fc[i]=FaceColour[i];
00841      }
00842      SendDlgItemMessage(hdlg,DLG_DEFAULT_SMOOTH_OFF,BM_SETCHECK,da->sm^1,0);
00843      SendDlgItemMessage(hdlg,DLG_DEFAULT_SMOOTH_ON, BM_SETCHECK,da->sm,  0);
00844      CentreDialogOnScreen(hdlg);
00845      return (TRUE);
00846    case WM_DRAWITEM:{
00847        HBRUSH   hbr,hbrold;
00848        BYTE r,g,b;
00849        lpdis=(LPDRAWITEMSTRUCT)lparam;
00850        if(lpdis->CtlID == DLG_DEFAULT_FC_EDIT ||
00851           lpdis->CtlID == DLG_DEFAULT_SC_EDIT){
00852          if(lpdis->CtlID == DLG_DEFAULT_FC_EDIT){
00853            r=da->fc[0]; g=da->fc[1]; b=da->fc[2];
00854          }
00855          if(lpdis->itemState & ODS_SELECTED)
00856            InvertRect(lpdis->hDC,&(lpdis->rcItem));
00857          else{
00858            hbr=CreateSolidBrush(RGB(r,g,b));
00859            hbrold=SelectObject(lpdis->hDC,hbr);
00860            Rectangle(lpdis->hDC,lpdis->rcItem.left,lpdis->rcItem.top,
00861                          lpdis->rcItem.right,lpdis->rcItem.bottom);
00862            SelectObject(lpdis->hDC,hbrold);
00863            DeleteObject(hbr);
00864          }
00865        }
00866      }
00867      return TRUE;
00868    case WM_HSCROLL:
00869      break;
00870    case WM_COMMAND:
00871       switch(LOWORD(wparam)){
00872         case DLG_DEFAULT_FC_EDIT:
00873           SetSfxColour(da->fc,IDX_MISC_FACE_COLOUR,hdlg);
00874           InvalidateRect(GetDlgItem(hdlg,DLG_DEFAULT_FC_EDIT),NULL,FALSE);
00875           break;
00876         case DLG_DEFAULT_SMOOTH_OFF:
00877           da->sm=0;
00878           break;
00879         case DLG_DEFAULT_SMOOTH_ON:
00880           da->sm=1;
00881           break;
00882         case IDCANCEL:
00883         case DLG_DEFAULT_CANCEL:
00884           EndDialog(hdlg,FAIL);
00885           return(TRUE);
00886         case IDOK:
00887         case DLG_DEFAULT_OK:
00888           for(i=0;i<3;i++){
00889             FaceColour[i]=da->fc[i];
00890           }
00891           DefaultSmoothing=da->sm;
00892           EndDialog(hdlg,OK);
00893           return(TRUE);
00894         default:
00895           break;
00896       }
00897       break;
00898     default: break;
00899  }
00900  return(FALSE);
00901 }
00902 
00904 
00905 static int EditExternalMaterial(HWND hdlg, int mapid, int id){
00906  HMODULE hLib;
00907  FARPROC fpFun;
00908  X__MEMORY_MANAGER *Evs;
00909  char *(*fpFun1)(char *, HWND, X__MEMORY_MANAGER *);
00910  Evs=&EviMemoryManager;
00911  EDIT_ACTION=YES;
00912  if(iMat[mapid].Shader[id].in_use ==  0)return FAIL;
00913  if(iMat[mapid].Shader[id].xname == NULL)return FAIL;
00914  hLib=LoadLibrary(iMat[mapid].Shader[id].xname);
00915  if(hLib == NULL){ /* try the default directory */
00916    char *sfname,tfname[256];
00917    sfname=short_form(iMat[mapid].Shader[id].xname);
00918    strcpy(tfname,gszHomeDir);  strcat(tfname,"textures\\");
00919    strcat(tfname,sfname);
00920    hLib=LoadLibrary(tfname);
00921  }
00922  if(hLib != NULL){
00923    if((fpFun=GetProcAddress(hLib,"_SetExternalParameters")) != NULL){
00924      fpFun1 = (void *)fpFun;
00925      if(iMat[mapid].Shader[id].parameters != NULL)iMat[mapid].Shader[id].parameters[0]='#';
00926      iMat[mapid].Shader[id].parameters=(*fpFun1)(iMat[mapid].Shader[id].parameters,hdlg,Evs);
00927    }
00928    else SendPrgmQuery(IDQ_FAILEDFUNCTION,3);
00929    FreeLibrary(hLib);
00930  }
00931  else SendPrgmQuery(IDQ_FAILEDLIBRARY,3);
00932  if(iMat[mapid].Shader[id].parameters == NULL)return FAIL;
00933  return OK;
00934 }
00935 
00936 
00937 static void DropExternalMaterial(HWND hdlg, int mapid, int id){
00938  EDIT_ACTION=YES;
00939  iMat[mapid].Shader[id].in_use=0;
00940  if(iMat[mapid].Shader[id].parameters != NULL)X__Free(iMat[mapid].Shader[id].parameters);
00941  if(iMat[mapid].Shader[id].xname != NULL)X__Free(iMat[mapid].Shader[id].xname);
00942  iMat[mapid].Shader[id].parameters=NULL;
00943  iMat[mapid].Shader[id].xname=NULL;
00944  return;
00945 }
00946 
00947 static void LoadExternalMaterial(HWND hdlg, int mapid, int id ){
00948  long l;
00949  unsigned char *np;
00950  l=strlen(gszDLLfile);
00951  strcpy((gszDLLfile+l-3),"sfx");
00952  if(SelectSfxFileName(0,gszDLLfile,gszDLLdir,IDX_MISC_LOADXSHADER,
00953    "(*.TFX)|*.tfx|",hdlg) == TRUE){
00954    l=strlen(gszDLLfile);
00955    strcpy((gszDLLfile+l-3),"dll");
00956    if(iMat[mapid].Shader[id].xname != NULL)X__Free(iMat[mapid].Shader[id].xname);
00957    iMat[mapid].Shader[id].xname=NULL;
00958 
00959    if((np=(unsigned char *)X__Malloc(strlen(gszDLLfile)+2)) == NULL)return;
00960    iMat[mapid].Shader[id].xname=np;
00961    strcpy(iMat[mapid].Shader[id].xname,gszDLLfile);
00962 
00963    iMat[mapid].Shader[id].in_use=1;
00964    if(iMat[mapid].Shader[id].parameters != NULL)X__Free(iMat[mapid].Shader[id].parameters);
00965    iMat[mapid].Shader[id].parameters=NULL;
00966    if(EditExternalMaterial(hdlg,mapid,id) == FAIL ||
00967       iMat[mapid].Shader[id].parameters == NULL  || 
00968       iMat[mapid].Shader[id].xname == NULL)DropExternalMaterial(hdlg,mapid,id);
00969  }
00970  return;
00971 }
00972 
00973 int CreateMaterial(BOOL ask){ 
00974  long i,j;
00975  char tempstr[32];
00976  if((i=nMats) >= NMATERIALMAX)return FAIL;
00977  sprintf(tempstr,"Material%ld",i);
00978  if(ask)if(RequestCharString(30,tempstr,"New Material Name",ghwnd_main) == 0)return FAIL;
00979  if(strlen(tempstr) > 0){
00980    iMat[i].version=1;
00981    strcpy(iMat[i].name,tempstr);
00982    for(j=0;j<3;j++){
00983     iMat[i].mcolour1[j]=128; iMat[i].mcolour2[j]=64;
00984     iMat[i].acolour[j]=64;   iMat[i].scolour[j]=255; // ambient and spec colours
00985     iMat[i].xcolour[j]=0;  // renamed as appropriate for other uses
00986     iMat[i].ycolour[j]=0;   
00987     iMat[i].zcolour[j]=0; 
00988    }
00989    iMat[i].bClear=iMat[i].bGlassEdge=FALSE;
00990    iMat[i].bShiny=iMat[i].bSmooth=TRUE;
00991    iMat[i].bUseSurfaceMapping=FALSE;
00992    iMat[i].gloss=128;
00993    iMat[i].refractive_index=1000;  // this is  1 (which is assumed to just be onesided class)
00994    iMat[i].refl=0;
00995    iMat[i].transp=0;
00996    iMat[i].bInternalShader=TRUE;
00997    iMat[i].internalShaderID=0;
00998    iMat[i].ax.P[0]=0;          iMat[i].ax.P[1]=0;         iMat[i].ax.P[2]=TVpointZ;
00999    iMat[i].ax.X[0]=TVsizeX/2;  iMat[i].ax.X[1]=0;         iMat[i].ax.X[2]=TVpointZ;
01000    iMat[i].ax.Y[0]=0;          iMat[i].ax.Y[1]=TVsizeZ/2; iMat[i].ax.Y[2]=TVpointZ;
01001    for(j=0;j<4;j++){
01002      iMat[i].Shader[j].bActive=FALSE;
01003      iMat[i].Shader[j].version=1;
01004      iMat[i].Shader[j].in_use=0;
01005      iMat[i].Shader[j].mixx=0;
01006      iMat[i].Shader[j].parameters=NULL;
01007      iMat[i].Shader[j].xname=NULL;
01008    } 
01009    for(j=0;j<16;j++) iMat[i].params[j]=0;
01010    for(j=0;j<16;j++) iMat[i].mmw[j]=(double)0.0;
01011    iMat[i].mmw[0]=(double)1.0;   // Emission
01012    iMat[i].mmw[1]=(double)0.0;   // Reflection
01013    iMat[i].mmw[2]=(double)0.0;   // transmission
01014    iMat[i].mmw[3]=(double)1.0;   // Refractive Index
01015    iMat[i].mmw[4]=(double)300.0; // Temerature
01016    iMat[i].mmw[5]=(double)0.0;   // Model (0/1/2)
01017    iMat[i].mmw[6]=(double)0.75;  // thickness (mm)
01018    iMat[i].mmw[7]=(double)0.5;   // Refr  Im
01019    // 8 is polarisation
01020    iMat[i].mmw[9]=(double)0.0001;   // APL
01021    iMat[i].mmw[10]=(double)0.015;   // BPL
01022    iMat[i].mmw[11]=(double)2.0;     // GPL
01023    iMat[i].mmw[12]=(double)0.0001;  // APD
01024    iMat[i].mmw[13]=(double)0.015;   // BPD
01025    iMat[i].mmw[14]=(double)2.0;     // GPD
01026    strcpy(iMat[i].mmw_brdf,"<none>");
01027    nMats++;
01028  }
01029  return OK;
01030 }
01031 
01032 void DeleteMaterial(int idd){
01033   int bid,id,i,j,k;
01034   face *f;
01035   if(nMats == 0){
01036     SendPrgmQuery(IDQ_NOSHADERAXES,0);
01037     return;
01038   }
01039   if(idd < 0){
01040     id=RequestMaterialID(ghwnd_main,"Choose Material to Delete");
01041     if(id < 0)return;  // cancel pressed
01042   }
01043   else id=idd;
01044   EDIT_ACTION=YES;
01045   if((f=MainFp) != NULL && Nface > 0)for(k=0;k<Nface;k++){ /* drop shader */
01046     if((bid = (int)f->material) == id){
01047       f->material = -1;            /* goto default plain material  */
01048     }
01049     else if(bid > id){      /* slide all the axes down one */
01050       bid--;
01051       f->material=bid;
01052     }
01053     f++;
01054   }
01055   for(k=0;k<4;k++){
01056     if(iMat[id].Shader[k].in_use && iMat[id].Shader[k].parameters != NULL)X__Free(iMat[id].Shader[k].parameters);
01057     if(iMat[id].Shader[k].in_use && iMat[id].Shader[k].xname != NULL)X__Free(iMat[id].Shader[k].xname);
01058   }
01059   if(id < nMats-1)for(i=id;i<nMats-1;i++){
01060     memcpy(&iMat[i],&iMat[i+1],sizeof(SURFACEMATERIAL));
01062   }
01063   nMats--;
01064   return;
01065 }
01066 
01067 void PurgeUnusedMaterials(void){
01068  int i,k,id;
01069  BOOL found;
01070  face *f;
01071  if(nMats == 0){
01072    SendPrgmQuery(IDQ_NOSHADERAXES,0);
01073    return;
01074  }
01075  LOOP:
01076  for(i=0;i<nMats;i++){
01077    found=FALSE;
01078    if((f=MainFp) != NULL && Nface > 0)for(k=0;k<Nface;k++){ /* drop shader */
01079      if((id=(unsigned char)f->material) == i)found=TRUE;
01080      if(id >= nMats) f->material = -1; // bad axis - reset to none axis
01081      f++;
01082    }
01083    if(!found){
01084      DeleteMaterial(i);
01085      goto LOOP;  // go back and repeat until all materials have been done
01086    }
01087  }
01088  return;
01089 }
01090 
01091 void PurgeDuplicateMaterials(long startat){
01092  long i,j,k;
01093  face *f;
01094  startagain:
01095  if(nMats < 2)return;
01096  if(startat >= nMats)return;
01097  for(i=0;i<startat;i++){
01098   for(j=startat;j<nMats;j++){
01099     if(stricmp(iMat[i].name,iMat[j].name) == 0){
01100       if((f=MainFp) != NULL && Nface > 0){for(k=0;k<Nface;k++,f++){if((f->material) == j)f->material=i;}}
01101       DeleteMaterial(j);
01102       goto startagain;
01103     }
01104   }
01105  }
01106  return;
01107 }
01108 
01109 void  CopyMaterial(int fromID, int toID){  
01110  int k;
01111  long ln;
01112  char name[32];
01113  for(k=0;k<4;k++){
01114    if(iMat[toID].Shader[k].in_use && iMat[toID].Shader[k].parameters != NULL)X__Free(iMat[toID].Shader[k].parameters);
01115    if(iMat[toID].Shader[k].in_use && iMat[toID].Shader[k].xname != NULL)X__Free(iMat[toID].Shader[k].xname);
01116  }
01117  memcpy(name,iMat[toID].name,32);
01118  memcpy(&iMat[toID],&iMat[fromID],sizeof(SURFACEMATERIAL));
01119  memcpy(iMat[toID].name,name,32);
01120  for(k=0;k<4;k++){
01121    iMat[toID].Shader[k].in_use=0; iMat[toID].Shader[k].xname=NULL; iMat[toID].Shader[k].parameters=NULL; 
01122    if(iMat[fromID].Shader[k].in_use){
01123      if(iMat[fromID].Shader[k].xname != NULL){
01124        ln=strlen(iMat[fromID].Shader[k].xname);  
01125        if((iMat[toID].Shader[k].xname=(char *)X__Malloc(ln+1)) != NULL){
01126          memcpy(iMat[toID].Shader[k].xname,iMat[fromID].Shader[k].xname,ln+1);
01127          if(iMat[fromID].Shader[k].parameters != NULL){
01128            ln=strlen(iMat[fromID].Shader[k].parameters);  
01129            if((iMat[toID].Shader[k].parameters=(char *)X__Malloc(ln+1)) != NULL){
01130              memcpy(iMat[toID].Shader[k].parameters,iMat[fromID].Shader[k].parameters,ln+1);
01131              iMat[toID].Shader[k].in_use=1;
01132            }
01133            else{
01134              X__Free(iMat[toID].Shader[k].xname); iMat[toID].Shader[k].xname=NULL;
01135            }
01136          }  
01137        }
01138        else iMat[k].Shader[toID].in_use=0;
01139      }
01140    }
01141  }
01142 }
01143 
01144 void DrawShaderInOne(HDC hDC,int view,int j){
01145  HPEN holdPen;
01146  HBRUSH holdBrush;
01147  int  oldROP;
01148  int i,k;
01149  int sho,svo,shx,svx,shy,svy;
01150  long x,y,z;
01151  if(j < 0 || j >= nMats)return; 
01152  holdPen=SelectObject(hDC,ghInvertPen);
01153  holdBrush=SelectObject(hDC,GetStockObject(HOLLOW_BRUSH));
01154  oldROP=SetROP2(hDC,R2_XORPEN);
01155  SelectPalette(hDC,ghpaletteScreen,FALSE);
01156  RealizePalette(hDC);
01157  GetWindowCoords(view,iMat[j].ax.P[0],iMat[j].ax.P[1],iMat[j].ax.P[2],&sho,&svo);
01158  GetWindowCoords(view,iMat[j].ax.X[0],iMat[j].ax.X[1],iMat[j].ax.X[2],&shx,&svx);
01159  GetWindowCoords(view,iMat[j].ax.Y[0],iMat[j].ax.Y[1],iMat[j].ax.Y[2],&shy,&svy);
01160  Rectangle(hDC,sho-5,svo-5,sho+5,svo+5);
01161  Rectangle(hDC,shx-2,svx-2,shx+2,svx+2);
01162  MoveToEx(hDC,shy-6,svy-3,NULL); LineTo(hDC,shy+5,svy-3);
01163  MoveToEx(hDC,shy+6,svy-3,NULL); LineTo(hDC,shy,svy+5);
01164  MoveToEx(hDC,shy-1,svy+4,NULL); LineTo(hDC,shy-6,svy-2);
01165  MoveToEx(hDC,shx,svx,NULL);  LineTo(hDC,sho,svo); LineTo(hDC,shy,svy);
01166  SelectObject(hDC,holdPen);
01167  SelectObject(hDC,holdBrush);
01168  SetROP2(hDC,oldROP);
01169  return;
01170 }
01171 
01172 
01173 int RequestMaterialID(HWND parent,char *message){
01174  int N,i,id;
01175  char **ItemList;
01176  N=nMats;
01177  if((ItemList = (char **)X__Malloc(N*sizeof(char *))) == NULL){
01178    SendPrgmQuery(IDQ_NOMEM2,0);
01179    return -1;
01180  }
01181  for(i=0;i<N;i++)ItemList[i] = iMat[i].name;
01182  id=SelectScrolledItemList(N,ItemList,message,parent);
01183  X__Free(ItemList);
01184  return id;
01185 }
01186 
01187 void EditMaterial(int id){
01188  int id1;
01189  if(id < 0){
01190   if(nMats == 0){ // create a new material;
01191     id1=0;
01192     CreateMaterial(TRUE);
01193     if(nMats == 0)return;
01194   }
01195   else{
01196     if(LastUsedMatID < 0){
01197       if(nMats == 1)id1=0;
01198       else id1=RequestMaterialID(ghwnd_main,"Choose Material");
01199     }
01200     id1=LastUsedMatID;
01201     if(id1 < 0)return;
01202     if(id1 >= nMats)id1=0; // In case one has been deleted.
01203   }
01204   id=id1;
01205  }
01206  if(nMats == 0)return; // MOVE later
01207  if(id >= nMats)return;
01208  EnableToolPannels(SELECT_PANNEL|COMMAND_PANNEL|TOOL_PANNEL|
01209                    ACTION_PANNEL|COORD_PANNEL|LOCK_PANNEL,FALSE);
01210  ActivateAllMenus(ghwnd_main,MF_GRAYED);
01211  id1=(int)DialogBoxParam(ghinst_main,MAKEINTRESOURCE(DLG_MATS),ghwnd_main,
01212             (DLGPROC)MaterialDlgProc,(LPARAM)id);
01213  ActivateAllMenus(ghwnd_main,MF_ENABLED);
01214  EnableToolPannels(SELECT_PANNEL|COMMAND_PANNEL|TOOL_PANNEL|
01215                    ACTION_PANNEL|COORD_PANNEL|LOCK_PANNEL,TRUE);
01216  if(id1 >= 0){
01217    axis_move_flag=id1;
01218    PositionMaterialOnOff(1,id1);
01219  }
01220  else if(image_edit_flag == 1) /* go back to the faces dialog */
01221    PostMessage(ghwnd_main,WM_COMMAND,IDM_ATTRIBUTES_FACE,0);
01222 }
01223 
01224 static int Nplist=5;
01225 static char *plist[]={"None","Horizontal","Vertical","Circular+","Circular-"};
01226 
01227 static UpdateMaterialDialogMMW(HWND hdlg, int matid){
01228  char str[32];
01229  int type;
01230  sprintf(str,"%.3lf",(float)iMat[matid].mmw[0]);  // EMISSION 
01231  SendDlgItemMessage(hdlg,DLG_MMW_EMISSION,WM_SETTEXT,0,(LPARAM)str);
01232  sprintf(str,"%.3lf",(float)iMat[matid].mmw[1]);  //reflection
01233  SendDlgItemMessage(hdlg,DLG_MMW_REFLECTION,WM_SETTEXT,0,(LPARAM)str);
01234  sprintf(str,"%.3lf",(float)iMat[matid].mmw[2]);  // transmission
01235  SendDlgItemMessage(hdlg,DLG_MMW_TRANSIMSSION,WM_SETTEXT,0,(LPARAM)str);
01236  sprintf(str,"%.3lf",(float)iMat[matid].mmw[3]); // Refractive Index (real)
01237  SendDlgItemMessage(hdlg,DLG_MMW_RI_RL,WM_SETTEXT,0,(LPARAM)str);
01238  sprintf(str,"%.3lf",(float)iMat[matid].mmw[4]); // Temperature
01239  SendDlgItemMessage(hdlg,DLG_MMW_TEMPERATURE,WM_SETTEXT,0,(LPARAM)str);
01240  type=(int)iMat[matid].mmw[5];
01241  if(type == 0)SendDlgItemMessage(hdlg,DLG_MMW_MODEL0,BM_SETCHECK,1,0); // Spec
01242  else         SendDlgItemMessage(hdlg,DLG_MMW_MODEL0,BM_SETCHECK,0,0); 
01243  if(type == 1)SendDlgItemMessage(hdlg,DLG_MMW_MODEL1,BM_SETCHECK,1,0); // ABg
01244  else         SendDlgItemMessage(hdlg,DLG_MMW_MODEL1,BM_SETCHECK,0,0); 
01245  if(type == 2)SendDlgItemMessage(hdlg,DLG_MMW_MODEL2,BM_SETCHECK,1,0); // lookup
01246  else         SendDlgItemMessage(hdlg,DLG_MMW_MODEL2,BM_SETCHECK,0,0); 
01247  sprintf(str,"%.3lf",(float)iMat[matid].mmw[6]); // Thickness (mm)
01248  SendDlgItemMessage(hdlg,DLG_MMW_THICKNESS,WM_SETTEXT,0,(LPARAM)str);
01249  sprintf(str,"%.3lf",(float)iMat[matid].mmw[7]); // Refractive Index Imag
01250  SendDlgItemMessage(hdlg,DLG_MMW_RI_IM,WM_SETTEXT,0,(LPARAM)str);
01251  {
01252    int i;
01253    HWND hctl;
01254    hctl=GetDlgItem(hdlg,DLG_MMW_POLARISATION);
01255    SendMessage(hctl,CB_RESETCONTENT,0,0);
01256    for(i=0;i<Nplist;i++)SendMessage(hctl,CB_ADDSTRING,0,(LPARAM)plist[i]);
01257    SendMessage(hctl,CB_SELECTSTRING,0,(LPARAM)plist[(int)(iMat[matid].mmw[8])]);
01258  } 
01259  sprintf(str,"%.5lf",(float)iMat[matid].mmw[9]); // APL 
01260  SendDlgItemMessage(hdlg,DLG_MMW_BRDF_APL,WM_SETTEXT,0,(LPARAM)str);
01261  sprintf(str,"%.5lf",(float)iMat[matid].mmw[10]); // BPL 
01262  SendDlgItemMessage(hdlg,DLG_MMW_BRDF_BPL,WM_SETTEXT,0,(LPARAM)str);
01263  sprintf(str,"%.2lf",(float)iMat[matid].mmw[11]); // GPL 
01264  SendDlgItemMessage(hdlg,DLG_MMW_BRDF_GPL,WM_SETTEXT,0,(LPARAM)str);
01265  sprintf(str,"%.5lf",(float)iMat[matid].mmw[12]); // APD 
01266  SendDlgItemMessage(hdlg,DLG_MMW_BRDF_APD,WM_SETTEXT,0,(LPARAM)str);
01267  sprintf(str,"%.5lf",(float)iMat[matid].mmw[13]); // BPD 
01268  SendDlgItemMessage(hdlg,DLG_MMW_BRDF_BPD,WM_SETTEXT,0,(LPARAM)str);
01269  sprintf(str,"%.2lf",(float)iMat[matid].mmw[14]); // GPD 
01270  SendDlgItemMessage(hdlg,DLG_MMW_BRDF_GPD,WM_SETTEXT,0,(LPARAM)str);
01271  if(type == 2){
01272    SendDlgItemMessage(hdlg,DLG_MMW_BRDF,WM_SETTEXT,0,(LPARAM)iMat[matid].mmw_brdf);
01273  }
01274  else {
01275    SendDlgItemMessage(hdlg,DLG_MMW_BRDF,WM_SETTEXT,0,(LPARAM)"  ");
01276  }
01277 }
01278 
01279 static void ReadDialogMMWvalues(HWND hdlg, int matid){
01280  char str[32];
01281  int i;
01282  // Non MMW specific
01283  if(GetDlgItemText(hdlg,DLG_MATS_REFINDEX,str,12) != 0)iMat[matid].refractive_index=(long)(atof(str)*1000.0);
01284  //
01285  if(GetDlgItemText(hdlg,DLG_MMW_EMISSION,str,12) != 0)iMat[matid].mmw[0]=atof(str);
01286  if(GetDlgItemText(hdlg,DLG_MMW_REFLECTION,str,12) != 0)iMat[matid].mmw[1]=atof(str);
01287  if(GetDlgItemText(hdlg,DLG_MMW_TRANSIMSSION,str,12) != 0)iMat[matid].mmw[2]=atof(str);
01288  if(GetDlgItemText(hdlg,DLG_MMW_RI_RL,str,12) != 0)iMat[matid].mmw[3]=atof(str); // Ref index
01289  if(GetDlgItemText(hdlg,DLG_MMW_TEMPERATURE,str,12) != 0)iMat[matid].mmw[4]=atof(str);  //Temperature
01290  if(GetDlgItemText(hdlg,DLG_MMW_THICKNESS,str,12) != 0)iMat[matid].mmw[6]=atof(str);  //Thickness (mm)
01291  if(GetDlgItemText(hdlg,DLG_MMW_RI_IM,str,12) != 0)iMat[matid].mmw[7]=atof(str); // Ref index (Imag)
01292  // RI tick and BRDF name set by buttons - no need to update
01293  // get the polarisation 
01294  SendDlgItemMessage(hdlg,DLG_MMW_POLARISATION,WM_GETTEXT,(WPARAM)31,(LPARAM)str);
01295  for(i=0;i<Nplist;i++){ 
01296    if(strcmp(str,plist[i]) == 0){
01297      iMat[matid].mmw[8]=(double)i;
01298      break;
01299    }
01300  }
01301  if(GetDlgItemText(hdlg,DLG_MMW_BRDF_APL,str,12) != 0)iMat[matid].mmw[9]=atof(str);
01302  if(GetDlgItemText(hdlg,DLG_MMW_BRDF_BPL,str,12) != 0)iMat[matid].mmw[10]=atof(str);
01303  if(GetDlgItemText(hdlg,DLG_MMW_BRDF_GPL,str,12) != 0)iMat[matid].mmw[11]=atof(str);
01304  if(GetDlgItemText(hdlg,DLG_MMW_BRDF_APD,str,12) != 0)iMat[matid].mmw[12]=atof(str);
01305  if(GetDlgItemText(hdlg,DLG_MMW_BRDF_BPD,str,12) != 0)iMat[matid].mmw[13]=atof(str);
01306  if(GetDlgItemText(hdlg,DLG_MMW_BRDF_GPD,str,12) != 0)iMat[matid].mmw[14]=atof(str);
01307 }
01308 
01309 static UpdateMaterialDialog(HWND hdlg,int matid){
01310  char str[32];
01311  SendDlgItemMessage(hdlg,DLG_ATTRIBS_TRN_SCROLL,TBM_SETPOS,TRUE,(LPARAM)iMat[matid].transp);
01312  sprintf(str,"%.1lf",(float)iMat[matid].transp/255.0);
01313  SendDlgItemMessage(hdlg,DLG_ATTRIBS_TRN_EDIT,WM_SETTEXT,0,(LPARAM)str);
01314  SendDlgItemMessage(hdlg,DLG_ATTRIBS_GLS_SCROLL,TBM_SETPOS,TRUE,(LPARAM)iMat[matid].gloss);
01315  sprintf(str,"%.1lf",(float)iMat[matid].gloss/255.0);
01316  SendDlgItemMessage(hdlg,DLG_ATTRIBS_GLS_EDIT,WM_SETTEXT,0,(LPARAM)str);
01317  SendDlgItemMessage(hdlg,DLG_ATTRIBS_REF_SCROLL,TBM_SETPOS,TRUE,(LPARAM)iMat[matid].refl);
01318  sprintf(str,"%.1lf",(float)iMat[matid].refl/255.0);
01319  SendDlgItemMessage(hdlg,DLG_ATTRIBS_REF_EDIT,WM_SETTEXT,0,(LPARAM)str);
01320  SendDlgItemMessage(hdlg,DLG_ATTRIBS_PRM_SCROLL,TBM_SETPOS,TRUE,(LPARAM)iMat[matid].params[0]);
01321  sprintf(str,"%.1lf",(float)iMat[matid].params[0]/255.0);
01322  SendDlgItemMessage(hdlg,DLG_ATTRIBS_PRM_EDIT,WM_SETTEXT,0,(LPARAM)str);
01323  SendDlgItemMessage(hdlg,DLG_MATS_LIST,CB_SELECTSTRING,0,
01324                            (LPARAM)iMat[matid].name);
01325  EnableWindow(GetDlgItem(hdlg,DLG_ATTRIBS_GLS_ON),TRUE);
01326  EnableWindow(GetDlgItem(hdlg,DLG_ATTRIBS_SMOOTH_ON),TRUE);
01327  EnableWindow(GetDlgItem(hdlg,DLG_ATTRIBS_TRN_TYPE1),TRUE);
01328  SendDlgItemMessage(hdlg,DLG_ATTRIBS_GLS_ON, BM_SETCHECK,iMat[matid].bShiny,0);
01329  SendDlgItemMessage(hdlg,DLG_ATTRIBS_TRN_TYPE1, BM_SETCHECK,iMat[matid].bGlassEdge,0);
01330  SendDlgItemMessage(hdlg,DLG_ATTRIBS_SMOOTH_ON, BM_SETCHECK,iMat[matid].bSmooth,0);
01331  SendDlgItemMessage(hdlg,DLG_MATS_MAPCOORDS, BM_SETCHECK,iMat[matid].bUseSurfaceMapping,0);
01332  
01333  SendDlgItemMessage(hdlg,DLG_ATTRIBS_SHA_INT, BM_SETCHECK,iMat[matid].bInternalShader,0);
01334  SendDlgItemMessage(hdlg,DLG_ATTRIBS_SHA_EXT, BM_SETCHECK,!iMat[matid].bInternalShader,0);
01335  SetDlgItemText(hdlg,DLG_ATTRIBS_SHA_NAME,AlTextList[iMat[matid].internalShaderID]);
01336  SendDlgItemMessage(hdlg,DLG_SHADERSET_ON1, BM_SETCHECK,iMat[matid].Shader[0].bActive,0);
01337  SendDlgItemMessage(hdlg,DLG_SHADERSET_ON2, BM_SETCHECK,iMat[matid].Shader[1].bActive,0);
01338  SendDlgItemMessage(hdlg,DLG_SHADERSET_ON3, BM_SETCHECK,iMat[matid].Shader[2].bActive,0);
01339  SendDlgItemMessage(hdlg,DLG_SHADERSET_ON4, BM_SETCHECK,iMat[matid].Shader[3].bActive,0);
01340  if(iMat[matid].Shader[0].in_use == 1 && iMat[matid].Shader[0].xname != NULL)
01341    SetDlgItemText(hdlg,DLG_SHADERSET_NAME1 ,(LPCTSTR)FileInPath(iMat[matid].Shader[0].xname));
01342  else 
01343    SetDlgItemText(hdlg,DLG_SHADERSET_NAME1 ," ");
01344  if(iMat[matid].Shader[1].in_use == 1 && iMat[matid].Shader[1].xname != NULL)
01345    SetDlgItemText(hdlg,DLG_SHADERSET_NAME2 ,(LPCTSTR)FileInPath(iMat[matid].Shader[1].xname));
01346  else 
01347    SetDlgItemText(hdlg,DLG_SHADERSET_NAME2 ," ");
01348  if(iMat[matid].Shader[2].in_use == 1 && iMat[matid].Shader[2].xname != NULL)
01349    SetDlgItemText(hdlg,DLG_SHADERSET_NAME3 ,(LPCTSTR)FileInPath(iMat[matid].Shader[2].xname));
01350   else 
01351    SetDlgItemText(hdlg,DLG_SHADERSET_NAME3 ," ");
01352 if(iMat[matid].Shader[3].in_use == 1 && iMat[matid].Shader[3].xname != NULL)
01353    SetDlgItemText(hdlg,DLG_SHADERSET_NAME4 ,(LPCTSTR)FileInPath(iMat[matid].Shader[3].xname));
01354  else 
01355    SetDlgItemText(hdlg,DLG_SHADERSET_NAME4 ," ");
01356  InvalidateRect(GetDlgItem(hdlg,DLG_ATTRIBS_FC_EDIT),NULL,FALSE);
01357  InvalidateRect(GetDlgItem(hdlg,DLG_ATTRIBS_SC_EDIT),NULL,FALSE);
01358  sprintf(str,"%.2lf",((float)iMat[matid].refractive_index)/1000.0);
01359  SendDlgItemMessage(hdlg,DLG_MATS_REFINDEX,WM_SETTEXT,0,(LPARAM)str);
01360  UpdateMaterialDialogMMW(hdlg,matid);
01361 }
01362 
01363 static BOOL CALLBACK MaterialDlgProc(HWND hdlg,UINT msg,
01364                                     WPARAM wparam,LPARAM lparam){
01365  static int MatID,sv;
01366  HWND hctl;
01367  BOOL err;
01368  int i,iMin,iMax;
01369  unsigned char fcc[3];
01370  LPDRAWITEMSTRUCT lpdis;
01371  switch( msg ) {
01372    case WM_PAINT:
01373      PaintDialogBackground(hdlg,ghinst_main);
01374      break;
01375    case WM_INITDIALOG:
01376      EDIT_ACTION=YES;
01377      EnableWindow(ghwnd_main,TRUE);
01378      MatID=(int)lparam;
01379      SendDlgItemMessage(hdlg,DLG_MAPS_LIST,CB_LIMITTEXT,30,0);
01380      for(i=0;i<nMats;i++){
01381        SendDlgItemMessage(hdlg,DLG_MATS_LIST,CB_ADDSTRING,0,
01382                                (LPARAM)iMat[i].name);
01383      }
01384      //
01385      SendDlgItemMessage(hdlg,DLG_ATTRIBS_GLS_SCROLL,TBM_SETRANGE,
01386                                 (WPARAM)TRUE,(LPARAM)MAKELONG(0,255));
01387      SendDlgItemMessage(hdlg,DLG_ATTRIBS_GLS_SCROLL,TBM_SETLINESIZE,0,1);
01388      SendDlgItemMessage(hdlg,DLG_ATTRIBS_GLS_SCROLL,TBM_SETPAGESIZE,0,5);
01389      SendDlgItemMessage(hdlg,DLG_ATTRIBS_GLS_SCROLL,TBM_SETTICFREQ,16,(LPARAM)0);
01390      //
01391      SendDlgItemMessage(hdlg,DLG_ATTRIBS_TRN_SCROLL,TBM_SETRANGE,
01392                                 (WPARAM)TRUE,(LPARAM)MAKELONG(0,255));
01393      SendDlgItemMessage(hdlg,DLG_ATTRIBS_TRN_SCROLL,TBM_SETLINESIZE,0,1);
01394      SendDlgItemMessage(hdlg,DLG_ATTRIBS_TRN_SCROLL,TBM_SETPAGESIZE,0,5);
01395      SendDlgItemMessage(hdlg,DLG_ATTRIBS_TRN_SCROLL,TBM_SETTICFREQ,16,(LPARAM)0);
01396      //
01397      SendDlgItemMessage(hdlg,DLG_ATTRIBS_REF_SCROLL,TBM_SETRANGE,
01398                                 (WPARAM)TRUE,(LPARAM)MAKELONG(0,255));
01399      SendDlgItemMessage(hdlg,DLG_ATTRIBS_REF_SCROLL,TBM_SETLINESIZE,0,1);
01400      SendDlgItemMessage(hdlg,DLG_ATTRIBS_REF_SCROLL,TBM_SETPAGESIZE,0,5);
01401      SendDlgItemMessage(hdlg,DLG_ATTRIBS_REF_SCROLL,TBM_SETTICFREQ,16,(LPARAM)0);
01402      //
01403      SendDlgItemMessage(hdlg,DLG_ATTRIBS_PRM_SCROLL,TBM_SETRANGE,
01404                                 (WPARAM)TRUE,(LPARAM)MAKELONG(0,255));
01405      SendDlgItemMessage(hdlg,DLG_ATTRIBS_PRM_SCROLL,TBM_SETLINESIZE,0,1);
01406      SendDlgItemMessage(hdlg,DLG_ATTRIBS_PRM_SCROLL,TBM_SETPAGESIZE,0,5);
01407      SendDlgItemMessage(hdlg,DLG_ATTRIBS_PRM_SCROLL,TBM_SETTICFREQ,16,(LPARAM)0);
01408      //
01409      CentreDialogOnScreen(hdlg);
01410      UpdateMaterialDialog(hdlg,MatID);
01411      return (TRUE);
01412    case WM_DESTROY:
01413      break;
01414    case WM_DRAWITEM:{
01415        HBRUSH   hbr,hbrold;
01416        BYTE r,g,b;
01417        lpdis=(LPDRAWITEMSTRUCT)lparam;
01418        if(lpdis->CtlID == DLG_ATTRIBS_FC_EDIT){
01419          r=iMat[MatID].mcolour1[0];
01420          g=iMat[MatID].mcolour1[1];
01421          b=iMat[MatID].mcolour1[2];
01422          if(lpdis->itemState & ODS_DISABLED){
01423            r=128; g=128; b=128;
01424          }
01425          if(lpdis->itemState & ODS_SELECTED)
01426            InvertRect(lpdis->hDC,&(lpdis->rcItem));
01427          else{
01428            if(lpdis->itemState & ODS_DISABLED)
01429                 hbr=CreateHatchBrush(HS_DIAGCROSS,RGB(128,128,128));
01430            else hbr=CreateSolidBrush(RGB(r,g,b));
01431            hbrold=SelectObject(lpdis->hDC,hbr);
01432            Rectangle(lpdis->hDC,lpdis->rcItem.left,lpdis->rcItem.top,
01433                          lpdis->rcItem.right,lpdis->rcItem.bottom);
01434            SelectObject(lpdis->hDC,hbrold);
01435            DeleteObject(hbr);
01436          }
01437        }
01438        else if(lpdis->CtlID == DLG_ATTRIBS_SC_EDIT){
01439          r=iMat[MatID].mcolour2[0];
01440          g=iMat[MatID].mcolour2[1];
01441          b=iMat[MatID].mcolour2[2];
01442          if(lpdis->itemState & ODS_DISABLED){
01443            r=128; g=128; b=128;
01444          }
01445          if(lpdis->itemState & ODS_SELECTED)
01446            InvertRect(lpdis->hDC,&(lpdis->rcItem));
01447          else{
01448            if(lpdis->itemState & ODS_DISABLED)
01449                 hbr=CreateHatchBrush(HS_DIAGCROSS,RGB(128,128,128));
01450            else hbr=CreateSolidBrush(RGB(r,g,b));
01451            hbrold=SelectObject(lpdis->hDC,hbr);
01452            Rectangle(lpdis->hDC,lpdis->rcItem.left,lpdis->rcItem.top,
01453                          lpdis->rcItem.right,lpdis->rcItem.bottom);
01454            SelectObject(lpdis->hDC,hbrold);
01455            DeleteObject(hbr);
01456          }
01457        }
01458      }
01459      return TRUE;
01460    case WM_HSCROLL:
01461      i=0;
01462      if((HWND)lparam == (hctl=GetDlgItem(hdlg,DLG_ATTRIBS_PRM_SCROLL)))i=1;
01463      if((HWND)lparam == (hctl=GetDlgItem(hdlg,DLG_ATTRIBS_REF_SCROLL)))i=2;
01464      if((HWND)lparam == (hctl=GetDlgItem(hdlg,DLG_ATTRIBS_TRN_SCROLL)))i=3;
01465      if((HWND)lparam == (hctl=GetDlgItem(hdlg,DLG_ATTRIBS_GLS_SCROLL)))i=4;
01466      if(i > 0){
01467        iMin=SendMessage(hctl,TBM_GETRANGEMIN,0,0);
01468        iMax=SendMessage(hctl,TBM_GETRANGEMAX,0,0);
01469        switch (LOWORD(wparam)) {
01470          case TB_PAGEDOWN:
01471          case TB_LINEDOWN: 
01472            if(i == 1)sv=iMat[MatID].params[0]=min(iMax,iMat[MatID].params[0]+16);
01473            if(i == 2)sv=iMat[MatID].refl=min(iMax,iMat[MatID].refl+16);
01474            if(i == 3)sv=iMat[MatID].transp=min(iMax,iMat[MatID].transp+16);
01475            if(i == 4)sv=iMat[MatID].gloss=min(iMax,iMat[MatID].gloss+16);
01476            UpdateMaterialDialog(hdlg,MatID);
01477            break;
01478          case TB_PAGEUP:
01479          case TB_LINEUP:
01480            if(i == 1)sv=iMat[MatID].params[0]=max(iMin,iMat[MatID].params[0]-16);
01481            if(i == 2)sv=iMat[MatID].refl=max(iMin,iMat[MatID].refl-16);
01482            if(i == 3)sv=iMat[MatID].transp=max(iMin,iMat[MatID].transp-16);
01483            if(i == 4)sv=iMat[MatID].gloss=max(iMin,iMat[MatID].gloss-16);
01484            UpdateMaterialDialog(hdlg,MatID);
01485            break;
01486          case TB_THUMBTRACK:
01487          case TB_THUMBPOSITION:
01488            sv=HIWORD(wparam);  
01489            if(i == 1)iMat[MatID].params[0]=sv;
01490            if(i == 2)sv=iMat[MatID].refl=sv;
01491            if(i == 3)sv=iMat[MatID].transp=sv;
01492            if(i == 4)sv=iMat[MatID].gloss=sv;
01493            UpdateMaterialDialog(hdlg,MatID);
01494            break;
01495          case TB_ENDTRACK:
01496            if(i == 1)iMat[MatID].params[0]=sv;
01497            if(i == 1)iMat[MatID].params[0]=sv;
01498            if(i == 2)sv=iMat[MatID].refl=sv;
01499            if(i == 3)sv=iMat[MatID].transp=sv;
01500            if(i == 4)sv=iMat[MatID].gloss=sv;
01501            UpdateMaterialDialog(hdlg,MatID);
01502            UpdateGLview();
01503            break;
01504          default: break;
01505        }
01506      }
01507      break;
01508    case WM_COMMAND:
01509      switch(HIWORD(wparam)){
01510        case CBN_EDITCHANGE:
01511          SendDlgItemMessage(hdlg,DLG_MATS_LIST,WM_GETTEXT,(WPARAM)30,
01512                             (LPARAM)iMat[MatID].name);
01513          if(strlen(iMat[MatID].name) == 0)strcpy(iMat[MatID].name,"$");
01514          SendDlgItemMessage(hdlg,DLG_MATS_LIST,CB_DELETESTRING,
01515                                 (WPARAM)MatID,0);
01516          SendDlgItemMessage(hdlg,DLG_MATS_LIST,CB_INSERTSTRING,
01517                                 (WPARAM)MatID,(LPARAM)iMat[MatID].name);
01518          break;
01519        case CBN_SELENDOK:
01520          if(LOWORD(wparam) == DLG_MATS_LIST){
01521            i=(int)SendDlgItemMessage(hdlg,DLG_MATS_LIST,CB_GETCURSEL,0,0);
01522            if(i != CB_ERR){
01523              ReadDialogMMWvalues(hdlg,MatID);   // update the structure with the user's settings
01524              MatID=(long)i;
01525              LastUsedMatID=MatID; // save global
01526              UpdateMaterialDialog(hdlg,MatID);
01527            }
01528          }
01529          break;
01530        default:
01531          break;
01532      }
01533      switch(LOWORD(wparam)){
01534        case DLG_MMW_MODEL0:
01535          i=SendDlgItemMessage(hdlg,DLG_MMW_MODEL0,BM_GETCHECK,0,0);
01536          if(i == BST_CHECKED)iMat[MatID].mmw[5]=0.0;
01537          UpdateMaterialDialog(hdlg,MatID);
01538          break;
01539        case DLG_MMW_MODEL1:
01540          i=SendDlgItemMessage(hdlg,DLG_MMW_MODEL1,BM_GETCHECK,0,0);
01541          if(i == BST_CHECKED)iMat[MatID].mmw[5]=1.0;
01542          UpdateMaterialDialog(hdlg,MatID);
01543          break;
01544        case DLG_MMW_MODEL2:
01545          i=SendDlgItemMessage(hdlg,DLG_MMW_MODEL2,BM_GETCHECK,0,0);
01546          if(i == BST_CHECKED)iMat[MatID].mmw[5]=2.0;
01547          UpdateMaterialDialog(hdlg,MatID);
01548          break;
01549        case DLG_MATS_BRDF_FILE:
01550          if(SelectSfxFileName(1,gszBRDfile,gszBRDdir,IDX_MISC_USEBRDF,
01551                            "(*.BRDF)|*.brdf|",hdlg) == TRUE){
01552            AppendFileExtension(gszBRDfile,".brdf");
01553            strcpy(iMat[MatID].mmw_brdf,gszBRDfile);  
01554            UpdateMaterialDialog(hdlg,MatID);
01555          }
01556          break;
01557        case DLG_ATTRIBS_FC_EDIT:
01558          VECCOPY(iMat[MatID].mcolour1,fcc)
01559          if(SetSfxColour(fcc,IDX_MISC_FACE_COLOUR,hdlg)){
01560            VECCOPY(fcc,iMat[MatID].mcolour1)
01561            VECCOPY(fcc,LastColour)
01562            UpdateGLview();
01563            InvalidateRect(GetDlgItem(hdlg,DLG_ATTRIBS_FC_EDIT),NULL,FALSE);
01564          }
01565          break;
01566        case DLG_ATTRIBS_SC_EDIT:
01567          VECCOPY(iMat[MatID].mcolour2,fcc)
01568          if(SetSfxColour(fcc,IDX_MISC_FACE_COLOUR,hdlg)){
01569            VECCOPY(fcc,iMat[MatID].mcolour2)
01570            VECCOPY(fcc,LastColour)
01571            UpdateGLview();
01572            InvalidateRect(GetDlgItem(hdlg,DLG_ATTRIBS_SC_EDIT),NULL,FALSE);
01573          }
01574          break;
01575        case DLG_MATS_MAPCOORDS:
01576          i=SendDlgItemMessage(hdlg,DLG_MATS_MAPCOORDS,BM_GETCHECK,0,0);
01577          if(i == BST_CHECKED)iMat[MatID].bUseSurfaceMapping=TRUE;
01578          else                iMat[MatID].bUseSurfaceMapping=FALSE;
01579          UpdateMaterialDialog(hdlg,MatID);
01580          break;
01581        case DLG_ATTRIBS_SMOOTH_ON:
01582          i=SendDlgItemMessage(hdlg,DLG_ATTRIBS_SMOOTH_ON,BM_GETCHECK,0,0);
01583          if(i == BST_CHECKED)iMat[MatID].bSmooth=TRUE;
01584          else                iMat[MatID].bSmooth=FALSE;
01585          UpdateMaterialDialog(hdlg,MatID);
01586          UpdateGLview();
01587          break;
01588        case DLG_ATTRIBS_GLS_ON:
01589          i=SendDlgItemMessage(hdlg,DLG_ATTRIBS_GLS_ON,BM_GETCHECK,0,0);
01590          if(i == BST_CHECKED)iMat[MatID].bShiny=TRUE;
01591          else                iMat[MatID].bShiny=FALSE;
01592          UpdateMaterialDialog(hdlg,MatID);
01593          UpdateGLview();
01594          break;
01595        case DLG_ATTRIBS_TRN_TYPE1:
01596          i=SendDlgItemMessage(hdlg,DLG_ATTRIBS_TRN_TYPE1,BM_GETCHECK,0,0);
01597          if(i == BST_CHECKED)iMat[MatID].bGlassEdge=TRUE;
01598          else                iMat[MatID].bGlassEdge=FALSE;
01599          UpdateMaterialDialog(hdlg,MatID);
01600          break;
01601        case DLG_ATTRIBS_SHA_SET:
01602          if((i=SelectScrolledItemList(NoAlTexts,AlTextList,
01603                  "Texture",hdlg)) >= 0){
01604            iMat[MatID].internalShaderID=i; 
01605          }
01606          UpdateMaterialDialog(hdlg,MatID);
01607          UpdateGLview(); 
01608          break;
01609        case DLG_ATTRIBS_SHA_INT:
01610          iMat[MatID].bInternalShader=TRUE; 
01611          UpdateMaterialDialog(hdlg,MatID);
01612          UpdateGLview();
01613          break;
01614        case DLG_ATTRIBS_SHA_EXT:
01615          iMat[MatID].bInternalShader=FALSE; 
01616          UpdateMaterialDialog(hdlg,MatID);
01617          UpdateGLview();
01618          break;
01619 
01620        case DLG_SHADERSET_ON1:
01621          iMat[MatID].Shader[0].bActive ^= 1;
01622          UpdateMaterialDialog(hdlg,MatID);
01623          break;
01624        case DLG_SHADERSET_ON2:
01625          iMat[MatID].Shader[1].bActive ^= 1;
01626          UpdateMaterialDialog(hdlg,MatID);
01627          break;
01628        case DLG_SHADERSET_ON3:
01629          iMat[MatID].Shader[2].bActive ^= 1;
01630          UpdateMaterialDialog(hdlg,MatID);
01631          break;
01632        case DLG_SHADERSET_ON4:
01633          iMat[MatID].Shader[3].bActive ^= 1;
01634          UpdateMaterialDialog(hdlg,MatID);
01635          break;
01636        case DLG_SHADERSET_LOAD1:
01637          LoadExternalMaterial(hdlg,MatID,0);
01638          UpdateMaterialDialog(hdlg,MatID);
01639          break;
01640        case DLG_SHADERLOAD_DROP1:
01641          DropExternalMaterial(hdlg,MatID,0);
01642          UpdateMaterialDialog(hdlg,MatID);
01643          break;
01644        case DLG_SHADERSET_EDIT1:
01645          EditExternalMaterial(hdlg,MatID,0);
01646          UpdateMaterialDialog(hdlg,MatID);
01647          break;
01648        case DLG_SHADERSET_LOAD2:
01649          LoadExternalMaterial(hdlg,MatID,1);
01650          UpdateMaterialDialog(hdlg,MatID);
01651          break;
01652        case DLG_SHADERLOAD_DROP2:
01653          DropExternalMaterial(hdlg,MatID,1);
01654          UpdateMaterialDialog(hdlg,MatID);
01655          break;
01656        case DLG_SHADERSET_EDIT2:
01657          EditExternalMaterial(hdlg,MatID,1);
01658          UpdateMaterialDialog(hdlg,MatID);
01659          break;
01660        case DLG_SHADERSET_LOAD3:
01661          LoadExternalMaterial(hdlg,MatID,2);
01662          UpdateMaterialDialog(hdlg,MatID);
01663          break;
01664        case DLG_SHADERLOAD_DROP3:
01665          DropExternalMaterial(hdlg,MatID,2);
01666          UpdateMaterialDialog(hdlg,MatID);
01667          break;
01668        case DLG_SHADERSET_EDIT3:
01669          EditExternalMaterial(hdlg,MatID,2);
01670          UpdateMaterialDialog(hdlg,MatID);
01671          break;
01672        case DLG_SHADERSET_LOAD4:
01673          LoadExternalMaterial(hdlg,MatID,3);
01674          UpdateMaterialDialog(hdlg,MatID);
01675          break;
01676        case DLG_SHADERLOAD_DROP4:
01677          DropExternalMaterial(hdlg,MatID,3);
01678          UpdateMaterialDialog(hdlg,MatID);
01679          break;
01680        case DLG_SHADERSET_EDIT4:
01681          EditExternalMaterial(hdlg,MatID,3);
01682          UpdateMaterialDialog(hdlg,MatID);
01683          break;
01684        case DLG_MATS_MOVE:
01685          // if openGL then update the OpenGL maps
01686          if(ghwndOpenGLview != NULL)PostMessage(ghwndOpenGLview,(WM_USER+4),0,0);
01687          EndDialog(hdlg,MatID);
01688          return(TRUE);
01689        case DLG_MATS_NEW:
01690          ReadDialogMMWvalues(hdlg,MatID);   // update the structure with the user's settings
01691          CreateMaterial(TRUE);
01692          MatID=nMats-1;
01693          LastUsedMatID=MatID; // save global
01694          SendDlgItemMessage(hdlg,DLG_MATS_LIST,CB_ADDSTRING,0,
01695                                (LPARAM)iMat[MatID].name);
01696          UpdateMaterialDialog(hdlg,MatID);
01697          break;
01698        case DLG_MATS_DELETE:
01699          SendDlgItemMessage(hdlg,DLG_MATS_LIST,CB_DELETESTRING,
01700                                 (WPARAM)MatID,0);
01701          DeleteMaterial(MatID);
01702          // if openGL then update the OpenGL maps
01703          if(ghwndOpenGLview != NULL)PostMessage(ghwndOpenGLview,
01704                                                 (WM_USER+4),0,0);
01705          if(nMats == 0){
01706            EndDialog(hdlg,-1);
01707            return(TRUE);
01708          }
01709          if(MatID >= nMats)MatID=nMats-1;
01710          LastUsedMatID=MatID; // save global
01711          UpdateMaterialDialog(hdlg,MatID);
01712          break;
01713        case DLG_MATS_COPY: 
01714          i=RequestMaterialID(hdlg,"Copy From ?");
01715          if(i >= 0 && i < nMats && i != MatID){
01716            CopyMaterial(i,MatID);  
01717            UpdateMaterialDialog(hdlg,MatID);
01718          } 
01719          break;
01720        case DLG_MATS_PREVIEW:
01721          SendMessage(ghwnd_main,WM_COMMAND,(WPARAM)IDM_VIEW_RENDERINWINDOW,0);
01722          break;
01723        case IDCANCEL:
01724        case DLG_MATS_DONE:
01725          // if openGL then update the OpenGL maps
01726          ReadDialogMMWvalues(hdlg,MatID);   // update the structure with the user's settings
01727          if(ghwndOpenGLview != NULL)PostMessage(ghwndOpenGLview,
01728                                                 (WM_USER+4),0,0);
01729          EndDialog(hdlg,-1);
01730          return(TRUE);
01731        default:
01732          break;
01733      }
01734      break;
01735    default: break;
01736  }
01737  return(FALSE);
01738 }
01739 
01740 void PositionMaterialOnOff(int OnOff,int id){
01741  int i;
01742  static int LastShaderUsed = -1;
01743  if(!OnOff){ // i.e. ON/OFF = 0 
01744    EnableToolPannels(COMMAND_PANNEL|SELECT_PANNEL|TOOL_PANNEL|ACTION_PANNEL,TRUE);
01745    ActivateAllMenus(ghwnd_main,MF_ENABLED);
01746    EnableMenuItem(GetMenu(ghwnd_main),IDM_STOP,MF_GRAYED);
01747    DrawMenuBar(ghwnd_main);
01748    lock_visible=NO;
01749    if(ghwndLock)ShowWindow(ghwndLock,SW_HIDE);
01750    controls_visible=YES;
01751    DrawShader(SelectedShader);
01752    SelectedShader = -1;
01753    if(IsWindow(ghwndDlgAttribs))ShowWindow(ghwndDlgAttribs,SW_SHOW);
01754    if(axis_move_flag >= 0){
01755      PostMessage(ghwnd_main,WM_COMMAND,IDM_MATERIAL_EDIT,0);
01756    }
01757    return;
01758  }
01759  if(SelectedShader >= 0)return;
01760  if(id < 0){
01761    if(nMats == 0){
01762      MessageBox(NULL,"NEED MATERIAL",NULL,MB_OK);// need to create material
01763      return;
01764    }
01765    else if((id=RequestMaterialID(ghwnd_main,"Choose Material")) < 0)return;
01766  }
01767  EDIT_ACTION=YES;
01768  SelectedShader = id;
01769  LastShaderUsed=SelectedShader;
01770  EnableToolPannels(COMMAND_PANNEL|SELECT_PANNEL|TOOL_PANNEL|ACTION_PANNEL,FALSE);
01771  ActivateAllMenus(ghwnd_main,MF_GRAYED);
01772  EnableMenuItem(GetMenu(ghwnd_main),IDM_STOP,MF_ENABLED);
01773  DrawMenuBar(ghwnd_main);
01774  controls_visible=NO;
01775  lock_visible=YES;
01776  if(ghwndLock)ShowWindow(ghwndLock,SW_SHOW);
01777  if(IsWindow(ghwndDlgAttribs))ShowWindow(ghwndDlgAttribs,SW_HIDE);
01778  DrawShader(SelectedShader);
01779  return;
01780 }
01781 
01782 void GrabMaterialAxisPoint(int x,int y){
01783  int id,x0,y0,x1,y1,x2,y2;
01784  double d,dmin;
01785  if(SelectedShader < 0)return;
01786  GetWindowCoords(ActiveView,iMat[SelectedShader].ax.P[0],
01787            iMat[SelectedShader].ax.P[1],iMat[SelectedShader].ax.P[2],&x0,&y0);
01788  GetWindowCoords(ActiveView,iMat[SelectedShader].ax.X[0],
01789            iMat[SelectedShader].ax.X[1],iMat[SelectedShader].ax.X[2],&x1,&y1);
01790  GetWindowCoords(ActiveView,iMat[SelectedShader].ax.Y[0],
01791            iMat[SelectedShader].ax.Y[1],iMat[SelectedShader].ax.Y[2],&x2,&y2);
01792  id = -1; dmin=(double)TVsizeX*(double)TVsizeY*2.0;
01793  if(abs(x-x0) < 4 && abs(y-y0) < 4){
01794    d=DIS2(iMat[SelectedShader].ax.P); if(d < dmin){id = 0; dmin=d;}
01795  }
01796  if(abs(x-x1) < 4 && abs(y-y1) < 4){
01797    d=DIS2(iMat[SelectedShader].ax.X); if(d < dmin){id = 1; dmin=d;}
01798  }
01799  if(abs(x-x2) < 4 && abs(y-y2) < 4){
01800    d=DIS2(iMat[SelectedShader].ax.Y); if(d < dmin){id = 2; dmin=d;}
01801  }
01802  if(id >= 0){
01803    IdentifiedShaderPoint=id;
01804  }
01805 }
01806 
01807 void UnGrabMaterialAxisPoint(void){
01808   if(ghwndOpenGLview != NULL)PostMessage(ghwndOpenGLview,(WM_USER+4),0,0); 
01809   IdentifiedShaderPoint=-1;
01810 }
01811 
01812 void PullShaderPoint(int x,int y){
01813  int p,sb,ix,iy,iz;
01814  long x1,y1,z1,dx,dy,dz;
01815  if((p=IdentifiedShaderPoint) >= 0){
01816    sb=SelectedShader;
01817    DrawShader(sb);
01818    GetWorldCoords(ActiveView,&x1,&y1,&z1,x,y);
01819    ix=iy=iz=1;
01820    if     (ActiveView == TRITOP  )iz=0;
01821    else if(ActiveView == TRIFRONT)iy=0;
01822    else if(ActiveView == TRIRIGHT)ix=0;
01823    if(p == 0){
01824      dx=iMat[sb].ax.X[0]-iMat[sb].ax.P[0];
01825      dy=iMat[sb].ax.X[1]-iMat[sb].ax.P[1];
01826      dz=iMat[sb].ax.X[2]-iMat[sb].ax.P[2];
01827      if(ix)iMat[sb].ax.X[0]=x1+dx;
01828      if(iy)iMat[sb].ax.X[1]=y1+dy;
01829      if(iz)iMat[sb].ax.X[2]=z1+dz;
01830      dx=iMat[sb].ax.Y[0]-iMat[sb].ax.P[0];
01831      dy=iMat[sb].ax.Y[1]-iMat[sb].ax.P[1];
01832      dz=iMat[sb].ax.Y[2]-iMat[sb].ax.P[2];
01833      if(ix){iMat[sb].ax.Y[0]=x1+dx; iMat[sb].ax.P[0]=x1;}
01834      if(iy){iMat[sb].ax.Y[1]=y1+dy; iMat[sb].ax.P[1]=y1;}
01835      if(iz){iMat[sb].ax.Y[2]=z1+dz; iMat[sb].ax.P[2]=z1;}
01836    }
01837    else if(p == 1){
01838      if(ix)iMat[sb].ax.X[0]=x1;
01839      if(iy)iMat[sb].ax.X[1]=y1;
01840      if(iz)iMat[sb].ax.X[2]=z1;
01841    }
01842    else if(p == 2){
01843      if(ix)iMat[sb].ax.Y[0]=x1;
01844      if(iy)iMat[sb].ax.Y[1]=y1;
01845      if(iz)iMat[sb].ax.Y[2]=z1;
01846    }
01847    DrawShader(SelectedShader);
01848  }
01849 }
01850 
01851 void ExportMaterialsToFile(HWND parent){ //RSFx
01852  int i;
01853  FILE *fp;
01854  if(SelectSfxFileName(1,gszMTLfile,gszMTLdir,IDX_MISC_USEMATERIAL,
01855                         "(*.MTL)|*.mtl|",parent) == TRUE){
01856    AppendFileExtension(gszMTLfile,".mtl");
01857    if(nMats > 0 && (fp=fopen(gszMTLfile,"wb")) != NULL){
01858      fwrite(&nMats,sizeof(int),1,fp);
01859      for(i=0;i<nMats;i++){
01860        fwrite(&(iMat[i].name),sizeof(char),32,fp);
01861        fwrite(&(iMat[i]),sizeof(SURFACEMATERIAL),1,fp);
01862      }
01863      fclose(fp);
01864    }
01865  } 
01866 }
01867  
01868 void ImportMaterialsFromFile(HWND parent){
01869  int i,j,nlMats,c=0;
01870  SURFACEMATERIAL lMat;
01871  char name[32],txt[128];
01872  FILE *fp;
01873  if(nMats < 1)return;
01874  if(SelectSfxFileName(0,gszMTLfile,gszMTLdir,IDX_MISC_USEMATERIAL,
01875                         "(*.MTL)|*.mtl|",parent) == TRUE){
01876    AppendFileExtension(gszMTLfile,".mtl");
01877    if(nMats > 0 && (fp=fopen(gszMTLfile,"rb")) != NULL){
01878      fread(&nlMats,sizeof(int),1,fp);     
01879      if(nlMats > 0)for(i=0;i<nlMats;i++){
01880        fread(&name,sizeof(char),32,fp);
01881        fread(&lMat,sizeof(SURFACEMATERIAL),1,fp);
01882        // now see if this material is in use and replace
01883        for(j=0;j<nMats;j++){
01884          if(strncmp(name,iMat[j].name,32) == 0){ // this material has been found
01885            memcpy(&(iMat[j]),&lMat,sizeof(SURFACEMATERIAL));
01886            c++;
01887          }
01888        }
01889      }
01890      fclose(fp);
01891    }
01892  }
01893  sprintf(txt,"%d Materials Replaced",c); MessageBox(parent,txt,"OpenFx",MB_OK); 
01894 }

Generated on Sun Apr 27 14:20:11 2014 for OpenFX by  doxygen 1.5.6