gldesign_tools.c

Go to the documentation of this file.
00001 /* file GLDESIGN_TOOLS.C  */
00002 
00003 // This file contains copies of some functions that are needed in the DLL but
00004 // cannot be accessed from the main Designer program because it is part of the GLDESIGN DLL
00005 // The funcions here also allow the modeller's tools to be used in the non-rectangular
00006 // windows of the OpenGL based shanded view (in Otho-projection mode)
00007 
00008 #define _DSCRUCT_SUB 1
00009 
00010 #include "gldesign1.h"
00011 
00012 extern SelXs,SelYs,SelXc,SelYc;
00013 extern GLfloat vertex_scale,ortho_width;
00014 extern GLfloat up_angle,round_angle;
00015 extern int *Tool;
00016 extern RECT oldrect;
00017 
00018 extern void Ortho2Dto3D(int x, int y, GLfloat *xx, GLfloat *yy);
00019 extern void Ortho3Dto2D(int *x, int *y, GLfloat xx, GLfloat yy);
00020 extern BOOL inview(vertex  *vp);
00021 
00022 static void m4by4(GLfloat t1[4][4], GLfloat t2[4][4], GLfloat tr[4][4]){
00023  short n=4,i,j,k;
00024  for(i=0;i<4;i++)
00025  for(j=0;j<4;j++)
00026  {
00027   tr[i][j]=0.0;
00028   for(k=0;k<4;k++)tr[i][j]=tr[i][j]+t1[i][k]*t2[k][j];
00029  }
00030  return;
00031 }
00032 
00033 static m4by1(GLfloat t4[4][4], GLfloat x, GLfloat y, GLfloat z,
00034            GLfloat *xx, GLfloat *yy, GLfloat *zz){
00035  *xx=t4[0][0]*x+t4[0][1]*y+t4[0][2]*z+t4[0][3];
00036  *yy=t4[1][0]*x+t4[1][1]*y+t4[1][2]*z+t4[1][3];
00037  *zz=t4[2][0]*x+t4[2][1]*y+t4[2][2]*z+t4[2][3];
00038  return;
00039 }
00040 
00041 static void rotz(GLfloat tr[4][4], GLfloat ang){
00042  short i,j;
00043  for(i=0;i<4;i++)
00044  for(j=0;j<4;j++)
00045  {
00046   tr[i][j]=0.0;
00047   if(i == j)tr[i][j]=1.0;
00048  }
00049   tr[0][0]= cos(ang);
00050   tr[0][1]=-sin(ang);
00051   tr[1][0]= sin(ang);
00052   tr[1][1]= cos(ang);
00053   return;
00054 }
00055 
00056 static void rotx(GLfloat tr[4][4], GLfloat ang){
00057  long i,j;
00058  for(i=0;i<4;i++)
00059  for(j=0;j<4;j++)
00060  {
00061   tr[i][j]=0.0;
00062   if(i == j)tr[i][j]=1.0;
00063  }
00064   tr[1][1]= cos(ang);
00065   tr[1][2]=-sin(ang);
00066   tr[2][1]= sin(ang);
00067   tr[2][2]= cos(ang);
00068   return;
00069 }
00070 
00071 static void roty(GLfloat tr[4][4], GLfloat ang){
00072  short i,j;
00073  for(i=0;i<4;i++)
00074  for(j=0;j<4;j++)
00075  {
00076   tr[i][j]=0.0;
00077   if(i == j)tr[i][j]=1.0;
00078  }
00079   tr[0][0]= cos(ang);
00080   tr[0][2]=-sin(ang);
00081   tr[2][0]= sin(ang);
00082   tr[2][2]= cos(ang);
00083   return;
00084 }
00085 
00086 static void rotate_round_vector(double angle, vector v, double t[4][4]){
00087   double dx,dy,dz,phi,theta,dxy;
00088   double t1[4][4],t2[4][4],t3[4][4];
00089   angle *= 3.14159/180.0;
00090   dx=v[0]; dy=v[1]; dz=v[2];
00091   dxy=dx*dx+dy*dy;
00092   if(dxy < 0.00001){ /* vertical so just rotate about z  and return */
00093     if(dz > 0.0)rotz(t, angle);
00094     else        rotz(t,-angle);
00095     return;
00096   }
00097   dxy=sqrt(dxy);
00098   if     (dx == 0.0 && dy > 0.0)phi =  3.14159/2.0;
00099   else if(dx == 0.0 && dy < 0.0)phi = -3.14159/2.0;
00100   else phi = atan2(dy,dx);
00101   theta = atan2(dz,dxy);
00102   rotz(t3, -phi);
00103   roty(t2, -theta);
00104   m4by4(t2,t3,t1);
00105   rotx(t2, angle);
00106   m4by4(t2,t1,t3);
00107   roty(t2,theta);
00108   m4by4(t2,t3,t1);
00109   rotz(t2,phi);
00110   m4by4(t2,t1,t);
00111 }
00112 
00113 void SelectVertexInRectGL(void){
00114  int i;
00115  vertex *vp;
00116  GLfloat x,y,z,xx,yy,zz,scale,m[16],t[4][4];
00117  GLfloat xs,ys,xe,ye,xmin,xmax,ymin,ymax;
00118  scale=vertex_scale/TVsizeX;
00119  // Get the OpenGL transformation 
00120  glPushMatrix();
00121  glLoadIdentity();
00122  glTranslatef(0.0, 0.0, -5.0);
00123  glRotatef(up_angle,1.0,0.0,0.0);
00124  glRotatef(round_angle,0.0,1.0,0.0);
00125  glGetFloatv(GL_MODELVIEW_MATRIX,m);
00126  glPopMatrix();
00127  t[0][0]=m[0]; t[0][1]=m[4]; t[0][2]=m[ 8]; t[0][3]=m[12]; // convert to "C" matrix order
00128  t[1][0]=m[1]; t[1][1]=m[5]; t[1][2]=m[ 9]; t[1][3]=m[13];
00129  t[2][0]=m[2]; t[2][1]=m[6]; t[2][2]=m[10]; t[2][3]=m[14];
00130  t[3][0]=m[3]; t[3][1]=m[7]; t[3][2]=m[11]; t[3][3]=m[15]; 
00131  Ortho2Dto3D(SelXs,SelYs,&xs,&ys);   // get the Open GL coordinates of the selection
00132  Ortho2Dto3D(SelXc,SelYc,&xe,&ye);
00133  xmin=min(xs,xe); xmax=max(xs,xe);
00134  ymin=min(ys,ye); ymax=max(ys,ye);
00135  if(Nvert > 0){
00136    for(i=0,vp=MainVp;i<Nvert;i++,vp++){
00137      if(inview(vp)){
00138        x=((GLfloat)(vp->xyz[0]-TVpointX-TVsizeX/2))*scale;
00139        y=((GLfloat)(vp->xyz[1]-TVpointY-TVsizeY/2))*scale;
00140        z=((GLfloat)(vp->xyz[2]-TVpointZ-TVsizeZ/2))*scale;
00141        m4by1(t,x,z,-y,&xx,&yy,&zz);  // in OpenGL frame of reference
00142        x=xx; y=yy;  // extract the coordinates perpendicular to Ortho Window
00143        if(*Tool == SELECTOR){
00144          if(x > xmin && x < xmax && y > ymin && y < ymax && vp->status == DESELECTED){
00145            vp->status=SELECTED;
00146            NvertSelect++; NvertDeselect--;
00147          }
00148        }
00149        else if(*Tool == DESELECTOR){
00150          if(x > xmin && x < xmax && y > ymin && y < ymax && vp->status == SELECTED){
00151            vp->status=DESELECTED;
00152            NvertSelect--; NvertDeselect++;
00153          }
00154        }
00155      }
00156    }
00157    DrawModel();
00158    UpdateCounters();
00159  }
00160 }
00161 
00162 void MoveInPerspectiveGL(double dx, double dy){
00163  vertex *vp;
00164  GLfloat x1,y1,z1,dr,aspect;
00165  GLfloat scale,tt1[4][4],tt2[4][4],rrt[4][4];
00166  int i;
00167  dr=3.14159/180.0;
00168  rotz(tt2,-round_angle*dr);
00169  rotx(tt1,-up_angle*dr);
00170  m4by4(tt2,tt1,rrt);
00171  scale=1.0/TVsizeX;
00172  aspect = (GLfloat) oldrect.right / (GLfloat)oldrect.bottom;;
00173  dx = aspect*dx/(GLfloat)oldrect.right;
00174  dy = dy/(GLfloat)oldrect.bottom;
00175  m4by1(rrt,(GLfloat)dx*(GLfloat)TVsizeX,0.0,-(GLfloat)dy*TVsizeX,&x1,&y1,&z1);
00176  vp=MainVp; if(MainVp != NULL && Nvert > 0)for(i=0;i<Nvert;i++){
00177    if(vp->status == SELECTED){
00178      vp->xyz[0] += (long)x1;
00179      vp->xyz[1] += (long)y1;
00180      vp->xyz[2] += (long)z1;
00181    }
00182    vp++;
00183  }
00184  DrawModel();
00185 }
00186 
00187 void RotateInPerspectiveGL(double dx, double dy){
00188  int i;
00189  vertex *vp;
00190  GLfloat x,y,z,xx,yy,zz,dr,xf,yf,aspect,rt;
00191  GLfloat scale,tt1[4][4],tt2[4][4],rrt[4][4],urrt[4][4],r[4][4];
00192  dr=3.14159/180.0;
00193  rt=-(GLfloat)dx/10.0;
00194  roty(r,rt*dr);
00195  rotz(tt2,round_angle*dr);
00196  rotx(tt1,up_angle*dr);
00197  m4by4(tt1,tt2,rrt);
00198  rotx(tt1,-up_angle*dr);
00199  rotz(tt2,-round_angle*dr);
00200  m4by4(tt2,tt1,urrt);
00201  scale=vertex_scale/TVsizeX;
00202  aspect = (GLfloat) oldrect.right / (GLfloat)oldrect.bottom;;
00203  xf=(GLfloat)SelXs/(GLfloat)oldrect.right*vertex_scale-0.5*vertex_scale;
00204  yf=(GLfloat)SelYs/(GLfloat)oldrect.bottom*vertex_scale-0.5*vertex_scale;
00205  yf=yf/aspect; 
00206  vp=MainVp; if(MainVp != NULL && Nvert > 0)for(i=0;i<Nvert;i++){
00207    if(vp->status == SELECTED){
00208      x=((GLfloat)(vp->xyz[0]-TVpointX-TVsizeX/2))*scale;
00209      y=((GLfloat)(vp->xyz[1]-TVpointY-TVsizeY/2))*scale;
00210      z=((GLfloat)(vp->xyz[2]-TVpointZ-TVsizeZ/2))*scale;
00211      m4by1(rrt,x,y,z,&xx,&yy,&zz);
00212      xx -= xf; 
00213      zz += yf;
00214      m4by1(r,xx,yy,zz,&x,&y,&z);
00215      x += xf; 
00216      z -= yf;
00217      m4by1(urrt,x,y,z,&xx,&yy,&zz); 
00218      vp->xyz[0]= (long)(xx/scale+TVpointX+TVsizeX/2);
00219      vp->xyz[1]= (long)(yy/scale+TVpointY+TVsizeY/2);
00220      vp->xyz[2]= (long)(zz/scale+TVpointZ+TVsizeZ/2);
00221    }
00222    vp++;
00223  }
00224  DrawModel();
00225 }
00226 
00227 void ShapeInPerspectiveGL(double dx, double dy){
00228  vertex *vp;
00229  GLfloat x1,y1,z1,dr,aspect;
00230  GLfloat scale,tt1[4][4],tt2[4][4],rrt[4][4];
00231  int i;
00232  dr=3.14159/180.0;
00233  rotz(tt2,-round_angle*dr);
00234  rotx(tt1,-up_angle*dr);
00235  m4by4(tt2,tt1,rrt);
00236  scale=1.0/TVsizeX;
00237  aspect = (GLfloat) oldrect.right / (GLfloat)oldrect.bottom;;
00238  dx = aspect*dx/(GLfloat)oldrect.right;
00239  dy = dy/(GLfloat)oldrect.bottom;
00240  m4by1(rrt,(GLfloat)dx*(GLfloat)TVsizeX,0.0,-(GLfloat)dy*TVsizeX,&x1,&y1,&z1);
00241  vp=MainVp; if(MainVp != NULL && Nvert > 0)for(i=0;i<Nvert;i++){
00242    if(vp->status == SELECTED){
00243 //     vp->xyz[0] += (long)x1;
00244 //     vp->xyz[1] += (long)y1;
00245 //     vp->xyz[2] += (long)z1;
00246    }
00247    vp++;
00248  }
00249  DrawModel();
00250 }

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