D3DVIEW1.CPP

Go to the documentation of this file.
00001 /* --
00002 OpenFX version 1.0 - Modelling, Animation and Rendering Package
00003 Copyright (C) 2000 OpenFX Development Team
00004 
00005 This program is free software; you can redistribute it and/or
00006 modify it under the terms of the GNU General Public License
00007 as published by the Free Software Foundation; either version 2
00008 of the License, or (at your option) any later version.
00009 
00010 This program is distributed in the hope that it will be useful,
00011 but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 GNU General Public License for more details.
00014 
00015 You should have received a copy of the GNU General Public License
00016 along with this program; if not, write to the Free Software
00017 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00018 
00019 You may contact the OpenFX development team via elecronic mail
00020 at core@openfx.org, or visit our website at http://openfx.org for
00021 further information and support details.
00022 -- */
00023 
00024 // d3dview1.cpp
00025 
00026 #define VECSUB(a,b,c)   { c[0]=a[0]-b[0]; c[1]=a[1]-b[1]; c[2]=a[2]-b[2];}
00027 #define VECSUM(a,b,c)   { c[0]=a[0]+b[0]; c[1]=a[1]+b[1]; c[2]=a[2]+b[2];}
00028 #define VECSCALE(a,b,c) { c[0]=(a)*b[0]; c[1]=(a)*b[1]; c[2]=(a)*b[2];}
00029 #define VECCOPY(a,c)    { c[0]=a[0]; c[1]=a[1]; c[2]=a[2];}
00030 #define DOT(a,b)        ( (a[0]*b[0]) + (a[1]*b[1]) + (a[2]*b[2]) )
00031 #define CROSS(v1,v2,r)  { \
00032                           r[0] = (v1[1]*v2[2]) - (v2[1]*v1[2]);  \
00033                           r[1] = (v1[2]*v2[0]) - (v1[0]*v2[2]);  \
00034                           r[2] = (v1[0]*v2[1]) - (v2[0]*v1[1]);  \
00035                         }
00036 
00037 #define HIDDEN 2
00038 
00039 static BOOL normalise(float *v){
00040  float n,nn;
00041  n= (float)((v[0]*v[0]) + (v[1]*v[1]) + (v[2]*v[2]));
00042  if(n < 1.e-10)return FALSE;
00043  nn=sqrt(n);
00044  n = 1.0 / nn;
00045  VECSCALE(n,v,v)
00046  return TRUE;
00047 }
00048 
00049 static BOOL Normalize(point x,point y,point z,float *n){
00050  float dz[3],dy[3];
00051  VECSUB((float)y,(float)x,dy)
00052  VECSUB((float)z,(float)x,dz)
00053  CROSS(dy,dz,n)
00054  if(normalise(n))return TRUE;
00055  return FALSE;
00056 }
00057 
00058 static BOOL inview(vertex  *vp){
00059  if(vp->status == HIDDEN)return 0;
00060  if( ((vp->xyz[0] > TVpointX-TVsizeX) && (vp->xyz[0] < TVpointX+2*TVsizeX))
00061   && ((vp->xyz[1] > TVpointY-TVsizeX) && (vp->xyz[1] < TVpointY+2*TVsizeY))
00062   && ((vp->xyz[2] > TVpointZ-TVsizeX) && (vp->xyz[2] < TVpointZ+2*TVsizeZ))
00063    )return TRUE;
00064  return FALSE;
00065 }
00066 
00067 #if 0
00068 static void MakeD3dModel(AppInfo *info){
00069 // double sided by copying second side   (if comments removed)
00070  unsigned long *ff;
00071  D3DVECTOR *vv;
00072  float x,y,z,scale,n[3];
00073  face *fp;
00074  vertex *v,*v1,*v2,*v0;
00075  long i,j,kv,kn,kf;
00076  if(Nface == 0)return;
00077  scale=10.0/TVsizeX;
00078  fp=MainFp; kn=0; for(i=0;i<Nface;i++,fp++){
00079    v0=(MainVp+fp->V[0]); v1=(MainVp+fp->V[1]); v2=(MainVp+fp->V[2]);
00080    if(inview(v0) || inview(v1) || inview(v2))kn++;
00081  }
00082  if((vv = (D3DVECTOR *)X__Malloc(3*kn*sizeof(D3DVECTOR))) == NULL){
00083    return;
00084  }
00085  if((ff = (unsigned long *)X__Malloc((8*kn+1)*sizeof(long))) == NULL){
00086    X__Free(vv);
00087    return;
00088  }
00089  fp=MainFp; kv=kn=kf=0; for(i=0;i<Nface;i++,fp++){
00090    v0=(MainVp+fp->V[0]); v1=(MainVp+fp->V[1]); v2=(MainVp+fp->V[2]);
00091    if(inview(v0) || inview(v1) || inview(v2)){
00092      if((fp->matcol[1] & 0xf0) == 0){ /* Not glass */
00093        ff[kf++]=3;
00094        for(j=0;j<3;j++){
00095          v=(MainVp+fp->V[j]);
00096          x=((float)(v->xyz[0]-TVpointX-TVsizeX/2))*scale;
00097          y=((float)(v->xyz[1]-TVpointY-TVsizeY/2))*scale;
00098          z=((float)(v->xyz[2]-TVpointZ-TVsizeZ/2))*scale;
00099          vv[kv].x=D3DVAL(x);
00100          vv[kv].y=D3DVAL(z);
00101          vv[kv].z=D3DVAL(-y);
00102          ff[kf++]=kv;
00103          kv++;
00104        }
00105 //       ff[kf++]=3;     // second side
00106 //       ff[kf++]=kv-3;
00107 //       ff[kf++]=kv-1;
00108 //       ff[kf++]=kv-2;
00109        kn++;
00110      }
00111    }
00112  }
00113  ff[kf++]=0;
00114  LPDIRECT3DRMMESHBUILDER builder;
00115  LPDIRECT3DRMFRAME frame;
00116  lpD3DRM->CreateMeshBuilder(&builder);
00117  builder->AddFaces(kv,vv,0,NULL,ff,NULL);
00118  // generate normals
00119  builder->GenerateNormals();
00120 
00121  builder->SetColor(D3DRMCreateColorRGB(D3DVAL(1.0),D3DVAL(0.3),D3DVAL(0.2)));
00122  lpD3DRM->CreateFrame(info->scene,&frame);
00123  frame->AddVisual(builder);
00124  builder->Release();
00125  frame->AddRotation(D3DRMCOMBINE_REPLACE,
00126          D3DVAL(1.0f),D3DVAL(0.0f),D3DVAL(0.0f),
00127          D3DVAL(-3.1415926f*(*PerspTheta)/180.0f));
00128  frame->AddRotation(D3DRMCOMBINE_BEFORE,
00129          D3DVAL(0.0f),D3DVAL(1.0f),D3DVAL(0.0f),
00130          D3DVAL(-3.1415926f*(*PerspFi)/180.0f));
00131  frame->SetPosition(info->scene,
00132          D3DVAL(0.0), D3DVAL(0.0), D3DVAL(10.0));
00133  info->object=frame;
00134  X__Free(vv);
00135  X__Free(ff);
00136 }
00137 #endif
00138 
00139 #if 0
00140 static void MakeD3dModel(AppInfo *info){
00141 // single sided - with normals
00142  unsigned long *ff;
00143  D3DVECTOR *vv,*nn;
00144  float x,y,z,scale,n[3];
00145  face *fp;
00146  vertex *v,*v1,*v2,*v0;
00147  long i,j,kv,kn,kf;
00148  if(Nface == 0)return;
00149  scale=10.0/TVsizeX;
00150  fp=MainFp; kn=0; for(i=0;i<Nface;i++,fp++){
00151    v0=(MainVp+fp->V[0]); v1=(MainVp+fp->V[1]); v2=(MainVp+fp->V[2]);
00152    if(inview(v0) || inview(v1) || inview(v2))kn++;
00153  }
00154  if((nn = (D3DVECTOR *)X__Malloc(3*kn*sizeof(D3DVECTOR))) == NULL)
00155    return;
00156  if((vv = (D3DVECTOR *)X__Malloc(3*kn*sizeof(D3DVECTOR))) == NULL){
00157    X__Free(nn);
00158    return;
00159  }
00160  if((ff = (unsigned long *)X__Malloc((7*kn+1)*sizeof(long))) == NULL){
00161    X__Free(nn);
00162    X__Free(vv);
00163    return;
00164  }
00165  fp=MainFp; kv=kn=kf=0; for(i=0;i<Nface;i++,fp++){
00166    v0=(MainVp+fp->V[0]); v1=(MainVp+fp->V[1]); v2=(MainVp+fp->V[2]);
00167    if(inview(v0) || inview(v1) || inview(v2)){
00168      if((fp->matcol[1] & 0xf0) == 0){ /* Not glass */
00169        if(Normalize(v0->xyz,v1->xyz,v2->xyz,n)){
00170          nn[kn].x=D3DVAL(n[0]);
00171          nn[kn].y=D3DVAL(n[2]);
00172          nn[kn].z=D3DVAL(-n[1]);
00173          ff[kf++]=3;
00174          for(j=0;j<3;j++){
00175            v=(MainVp+fp->V[j]);
00176            x=((float)(v->xyz[0]-TVpointX-TVsizeX/2))*scale;
00177            y=((float)(v->xyz[1]-TVpointY-TVsizeY/2))*scale;
00178            z=((float)(v->xyz[2]-TVpointZ-TVsizeZ/2))*scale;
00179            vv[kv].x=D3DVAL(x);
00180            vv[kv].y=D3DVAL(z);
00181            vv[kv].z=D3DVAL(-y);
00182            ff[kf++]=kv;
00183            ff[kf++]=kn;
00184            kv++;
00185          }
00186          kn++;
00187        }
00188      }
00189    }
00190  }
00191  ff[kf++]=0;
00192  LPDIRECT3DRMMESHBUILDER builder;
00193  LPDIRECT3DRMFRAME frame;
00194  lpD3DRM->CreateMeshBuilder(&builder);
00195  builder->AddFaces(kv,vv,kn,nn,ff,NULL);
00196  builder->SetColor(D3DRMCreateColorRGB(D3DVAL(1.0), D3DVAL(1.0), D3DVAL(1.0)));
00197  lpD3DRM->CreateFrame(info->scene,&frame);
00198  frame->AddVisual(builder);
00199  builder->Release();
00200  frame->AddRotation(D3DRMCOMBINE_REPLACE,
00201          D3DVAL(1.0f),D3DVAL(0.0f),D3DVAL(0.0f),
00202          D3DVAL(-3.1415926f*(*PerspTheta)/180.0f));
00203  frame->AddRotation(D3DRMCOMBINE_BEFORE,
00204          D3DVAL(0.0f),D3DVAL(1.0f),D3DVAL(0.0f),
00205          D3DVAL(-3.1415926f*(*PerspFi)/180.0f));
00206  frame->SetPosition(info->scene,
00207          D3DVAL(0.0), D3DVAL(0.0), D3DVAL(10.0));
00208  info->object=frame;
00209  X__Free(vv);
00210  X__Free(nn);
00211  X__Free(ff);
00212 }
00213 #endif
00214 
00215 #undef CreateFace
00216 
00217 #if 0
00218 static void MakeD3dModel(AppInfo *info){
00219 // separate faces  - no smoothing
00220  D3DVECTOR *vv, *nn;
00221  float x,y,z,scale,n[3];
00222  face *fp;
00223  unsigned long *ff;
00224  vertex *v,*v1,*v2,*v0;
00225  long i,j,kv,kn,kf;
00226  if(Nface == 0)return;
00227  scale=10.0/TVsizeX;
00228  if((vv = (D3DVECTOR *)X__Malloc(Nvert*sizeof(D3DVECTOR))) == NULL){
00229    return;
00230  }
00231  if((ff = (unsigned long *)X__Malloc(1*sizeof(unsigned long))) == NULL){
00232    X__Free(vv);
00233    return;
00234  }
00235  if((nn = (D3DVECTOR *)X__Malloc(Nface*sizeof(D3DVECTOR))) == NULL){
00236    X__Free(vv);
00237    X__Free(ff);
00238    return;
00239  }
00240  v=MainVp; kv=kf=0; for(i=0;i<Nvert;i++,v++){
00241    x=((float)(v->xyz[0]-TVpointX-TVsizeX/2))*scale;
00242    y=((float)(v->xyz[1]-TVpointY-TVsizeY/2))*scale;
00243    z=((float)(v->xyz[2]-TVpointZ-TVsizeZ/2))*scale;
00244    vv[kv].x=D3DVAL(x);
00245    vv[kv].y=D3DVAL(z);
00246    vv[kv].z=D3DVAL(-y);
00247    kv++;
00248  }
00249  ff[kf++]=0;
00250  kf=1;
00251  fp=MainFp; kn=0; for(i=0;i<Nface;i++,fp++){
00252    v0=(MainVp+fp->V[0]); v1=(MainVp+fp->V[1]); v2=(MainVp+fp->V[2]);
00253    Normalize(v0->xyz,v1->xyz,v2->xyz,n);
00254    nn[kn].x=D3DVAL(n[0]);
00255    nn[kn].y=D3DVAL(n[2]);
00256    nn[kn].z=D3DVAL(-n[1]);
00257    kn++;
00258  }
00259  LPDIRECT3DRMMESHBUILDER builder;
00260  LPDIRECT3DRMFRAME frame;
00261  LPDIRECT3DRMFACE df;
00262  lpD3DRM->CreateMeshBuilder(&builder);
00263  builder->AddFaces(kv,vv,kn,nn,ff,NULL);
00264  builder->SetColor(D3DRMCreateColorRGB(D3DVAL(1.0),D3DVAL(1.0),D3DVAL(0.0)));
00265  fp=MainFp; for(i=0;i<Nface;i++,fp++){
00266    builder->CreateFace(&df);
00267    for(j=0;j<3;j++){
00268      df->SetColorRGB(D3DVAL(fp->color[0]/255.0f),
00269                      D3DVAL(fp->color[1]/255.0f),
00270                      D3DVAL(fp->color[2]/255.0f));
00271      df->AddVertexAndNormalIndexed((DWORD)fp->V[j],(DWORD)i);
00272    }
00273    df->Release();
00274  }
00275 
00276  lpD3DRM->CreateFrame(info->scene,&frame);
00277  frame->AddVisual(builder);
00278  builder->Release();
00279  frame->AddRotation(D3DRMCOMBINE_REPLACE,
00280          D3DVAL(1.0f),D3DVAL(0.0f),D3DVAL(0.0f),
00281          D3DVAL(-3.1415926f*(*PerspTheta)/180.0f));
00282  frame->AddRotation(D3DRMCOMBINE_BEFORE,
00283          D3DVAL(0.0f),D3DVAL(1.0f),D3DVAL(0.0f),
00284          D3DVAL(-3.1415926f*(*PerspFi)/180.0f));
00285  frame->SetPosition(info->scene,
00286          D3DVAL(0.0), D3DVAL(0.0), D3DVAL(10.0));
00287  info->object=frame;
00288  X__Free(vv);
00289  X__Free(ff);
00290  X__Free(nn);
00291 }
00292 #endif
00293 
00294 //#if 0
00295 static void MakeD3dModel(AppInfo *info){
00296 // separate faces  - smoothing
00297  D3DVECTOR *vv, *nn;
00298  float x,y,z,scale,n[3];
00299  face *fp;
00300  unsigned long *ff;
00301  vertex *v,*v1,*v2,*v0;
00302  long i,j,kv,kn,kf;
00303  if(Nface == 0)return;
00304  scale=10.0/TVsizeX;
00305  if((vv = (D3DVECTOR *)X__Malloc(Nvert*sizeof(D3DVECTOR))) == NULL){
00306    return;
00307  }
00308  if((ff = (unsigned long *)X__Malloc(1*sizeof(unsigned long))) == NULL){
00309    X__Free(vv);
00310    return;
00311  }
00312  if((nn = (D3DVECTOR *)X__Malloc(Nvert*sizeof(D3DVECTOR))) == NULL){
00313    X__Free(vv);
00314    X__Free(ff);
00315    return;
00316  }
00317  v=MainVp;  for(i=0;i<Nvert;i++,v++){
00318    x=((float)(v->xyz[0]-TVpointX-TVsizeX/2))*scale;
00319    y=((float)(v->xyz[1]-TVpointY-TVsizeY/2))*scale;
00320    z=((float)(v->xyz[2]-TVpointZ-TVsizeZ/2))*scale;
00321    vv[i].x=D3DVAL(x);
00322    vv[i].y=D3DVAL(z);
00323    vv[i].z=D3DVAL(-y);
00324    nn[i].x = nn[i].y = nn[i].z = D3DVAL(0.0f);
00325  }
00326  ff[0]=0;
00327  kf=1;
00328  fp=MainFp; kn=0; for(i=0;i<Nface;i++,fp++){
00329    long i1,i2,i3;
00330    i1=fp->V[0]; i2=fp->V[1]; i3=fp->V[2];
00331    v0=(MainVp+i1); v1=(MainVp+i2); v2=(MainVp+i3);
00332    Normalize(v0->xyz,v1->xyz,v2->xyz,n);
00333    nn[i1].x += D3DVAL(n[0]);
00334    nn[i1].y += D3DVAL(n[2]);
00335    nn[i1].z += D3DVAL(-n[1]);
00336    nn[i2].x += D3DVAL(n[0]);
00337    nn[i2].y += D3DVAL(n[2]);
00338    nn[i2].z += D3DVAL(-n[1]);
00339    nn[i3].x += D3DVAL(n[0]);
00340    nn[i3].y += D3DVAL(n[2]);
00341    nn[i3].z += D3DVAL(-n[1]);
00342 //   D3DRMVectorNormalize(&nn[i1]);
00343 //   D3DRMVectorNormalize(&nn[i2]);
00344 //   D3DRMVectorNormalize(&nn[i3]);
00345  }
00346  for(i=0;i<Nvert;i++){
00347    D3DRMVectorNormalize(&nn[i]);
00348  }
00349  LPDIRECT3DRMMESHBUILDER builder;
00350  LPDIRECT3DRMFRAME frame;
00351  LPDIRECT3DRMFACE df;
00352  lpD3DRM->CreateMeshBuilder(&builder);
00353  builder->AddFaces(Nvert,vv,Nvert,nn,ff,NULL);
00354  builder->SetColor(D3DRMCreateColorRGB(D3DVAL(1.0),D3DVAL(1.0),D3DVAL(0.0)));
00355  fp=MainFp; for(i=0;i<Nface;i++,fp++){
00356    builder->CreateFace(&df);
00357    for(j=0;j<3;j++){
00358      df->SetColorRGB(D3DVAL(fp->color[0]/255.0f),
00359                      D3DVAL(fp->color[1]/255.0f),
00360                      D3DVAL(fp->color[2]/255.0f));
00361      df->AddVertexAndNormalIndexed((DWORD)fp->V[j],(DWORD)fp->V[j]);
00362    }
00363    df->Release();
00364  }
00365 
00366  lpD3DRM->CreateFrame(info->scene,&frame);
00367  frame->AddVisual(builder);
00368  builder->Release();
00369  frame->AddRotation(D3DRMCOMBINE_REPLACE,
00370          D3DVAL(1.0f),D3DVAL(0.0f),D3DVAL(0.0f),
00371          D3DVAL(-3.1415926f*(*PerspTheta)/180.0f));
00372  frame->AddRotation(D3DRMCOMBINE_BEFORE,
00373          D3DVAL(0.0f),D3DVAL(1.0f),D3DVAL(0.0f),
00374          D3DVAL(-3.1415926f*(*PerspFi)/180.0f));
00375  frame->SetPosition(info->scene,
00376          D3DVAL(0.0), D3DVAL(0.0), D3DVAL(10.0));
00377  info->object=frame;
00378  X__Free(vv);
00379  X__Free(ff);
00380  X__Free(nn);
00381 }
00382 //#endif

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