Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | File Members | Related Pages

curve.cpp

Go to the documentation of this file.
00001 /* *************************************************************************************
00002         Writer:         Sebastien Bloc
00003         Copyright:      2003-2004
00004         eMail:          sebastien.bloc@free.fr
00005         URL:            http://mignonsoft.free.fr
00006 
00007         This program is free software; you can redistribute it and/or
00008         modify it under the terms of the GNU General Public License
00009         as published by the Free Software Foundation; either version 2
00010         of the License, or (at your option) any later version.
00011 
00012         This program is distributed in the hope that it will be useful,
00013         but WITHOUT ANY WARRANTY; without even the implied warranty of
00014         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015         GNU General Public License for more details.
00016         http://www.gnu.org/copyleft/gpl.html
00017 ************************************************************************************* */
00018 
00019 #include "curve.h"
00020 
00021 Curve::Curve()
00022 {
00023         tabQuads = NULL;
00024         tabSommets = NULL;
00025         tabCoorTextures = NULL;
00026         Zero();
00027 }
00028 
00029 Curve::~Curve()
00030 {
00031         UnAlloc();
00032 }
00033 void Curve::Zero()
00034 {
00035         UnAlloc();
00036         nbCut.Set(1,1);
00037         center.Set(0.5,0.5);
00038         numList=0;
00039 }
00040 
00041 void Curve::operator=(Curve &source)
00042 {
00043         nbCut = source.nbCut;
00044         center = source.center;
00045         oldCenter = source.oldCenter;
00046         oldNbCut = source.oldNbCut;
00047 }
00048 
00049 BOOL Curve::Set(GLdouble zCenter,int nbCutXY) 
00050 {
00051         if (nbCutXY<0) return FALSE;
00052         nbCut.Set(nbCutXY,nbCutXY);
00053         center.Set(0.5,0.5,zCenter);
00054         return TRUE;
00055 }
00056 
00057 BOOL Curve::Set(GLdouble zCenter,int nbCutX,int nbCutY) 
00058 {
00059         if ((nbCutX<0)||(nbCutY<0)) return FALSE;
00060         nbCut.Set(nbCutX,nbCutY);
00061         center.Set(0.5,0.5,zCenter);
00062         return TRUE;
00063 }
00064 
00065 BOOL Curve::IsActive()
00066 {
00067         return ((nbCut.x>1)||(nbCut.y>1))?TRUE:FALSE;
00068 }
00069 
00076 BOOL Curve::Draw(Point2D<double> size,Texture *texture)
00077 {
00078         if (!IsActive()) return FALSE;
00079         glBegin(GL_QUADS);
00080         GLdouble cdx = 1.0/(GLdouble)nbCut.x;
00081         GLdouble cdy = 1.0/(GLdouble)nbCut.y;
00082         BOOL noCutX = nbCut.x==1?TRUE:FALSE;
00083         BOOL noCutY = nbCut.y==1?TRUE:FALSE;
00084         GLdouble px = 0;
00085         GLdouble py = 0;
00086         GLdouble z1,z2,z3,z4;
00087         for (int x=0;x<nbCut.x;x++)
00088         {
00089                 for (int y=0;y<nbCut.y;y++)                                             
00090                 {
00091 #define f(value) sin(PI*(value))
00092 #define fxy(value,center) ((value<center)?sin((HALF_PI/center)*(value)):cos((HALF_PI/(1-center))*((value)-center)))
00093 #define fx(value) fxy(value,center.x)
00094 #define fy(value) fxy(value,center.y)
00095                         if (noCutX)
00096                         {
00097                                 z1 = fy(py)*center.z;
00098                                 z2 = fy(py)*center.z;
00099                                 z3 = fy(py+cdy)*center.z;
00100                                 z4 = fy(py+cdy)*center.z;
00101                         }
00102                         else if (noCutY)
00103                         {
00104                                 z1 = fx(px)*center.z;
00105                                 z2 = fx(px+cdx)*center.z;
00106                                 z3 = fx(px+cdx)*center.z;
00107                                 z4 = fx(px)*center.z;
00108                         }
00109                         else
00110                         {
00111                                 z1 = (fx(px)*fy(py))*center.z;
00112                                 z2 = (fx(px+cdx)*fy(py))*center.z;
00113                                 z3 = (fx(px+cdx)*fy(py+cdy))*center.z;
00114                                 z4 = (fx(px)*fy(py+cdy))*center.z;
00115                         }
00116                         GLdouble x1 = px*size.x;
00117                         GLdouble y1 = py*size.y-size.y;
00118                         GLdouble x2 = (px+cdx)*size.x;
00119                         GLdouble y2 = (py+cdy)*size.y-size.y;
00120 
00121                         if (texture)
00122                         {       
00123                                 texture->SetCoord(px,py); 
00124                                 glVertex3d(x1,y1,z1);
00125                                 texture->SetCoord(px+cdx,py); 
00126                                 glVertex3d(x2,y1,z2);
00127                                 texture->SetCoord(px+cdx,py+cdy); 
00128                                 glVertex3d(x2,y2,z3);
00129                                 texture->SetCoord(px,py+cdy); 
00130                                 glVertex3d(x1,y2,z4);
00131                         }
00132                         else
00133                         {
00134                                 glVertex3d(x1,y1,z1);
00135                                 glVertex3d(x2,y1,z2);
00136                                 glVertex3d(x2,y2,z3);
00137                                 glVertex3d(x1,y2,z4);
00138                         }
00139                         py+=cdy;
00140                 }
00141                 px+=cdx;
00142                 py=0;
00143         }
00144         glEnd();
00145         return TRUE;
00146 }
00147 
00148 // le fonction ci-apres son pour utilisé l'optimisation (qui n'est pas mailleur d'ailleur)
00149 
00156 void Curve::Refresh()
00157 {
00158         nbCut.Set(-1,-1);
00159 }
00160 
00161 void Curve::UnAlloc()
00162 {
00163         if (tabQuads) delete tabQuads;
00164         if (tabSommets) delete tabSommets;
00165         if (tabCoorTextures) delete tabCoorTextures;
00166         tabQuads = NULL;
00167         tabSommets = NULL;
00168         tabCoorTextures = NULL;
00169 }
00170 
00179 /*
00180 BOOL Curve::Draw(Point2D<double> size,Texture *texture)
00181 {
00182         if (!IsActive()) return FALSE;
00183         int nbFaces = nbCut.x*nbCut.y;
00184 
00185         BOOL isResize = oldNbCut!=nbCut;
00186         BOOL isRepositioning;
00187         GLdouble *ptrCoorTexture;
00188         if (isResize)
00189         {       // y a t'il eut des changement depuis le dernier appel ?
00190                 UnAlloc();
00191                 oldNbCut = nbCut;
00192                 nbFaces = nbCut.x*nbCut.y;
00193                 int nbSommets = (nbCut.x+1)*(nbCut.y+1);
00194                 tabSommets = new GLdouble[nbSommets*3];
00195                 tabCoorTextures = new GLdouble[nbSommets*2];
00196                 tabQuads = new GLuint[nbFaces*4];
00197                 isRepositioning = TRUE;
00198         }
00199         else isRepositioning = oldCenter!=center;
00200 
00201         if (texture) 
00202         {
00203                 ptrCoorTexture = tabCoorTextures;
00204                 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
00205                 glTexCoordPointer(2,GL_DOUBLE,0,tabCoorTextures);
00206         }
00207         else ptrCoorTexture = NULL;
00208 
00209         GLdouble *ptrSommet = tabSommets;
00210         GLuint *ptrQuad = tabQuads;
00211         glEnableClientState(GL_VERTEX_ARRAY);
00212         glVertexPointer(3,GL_DOUBLE,0,tabSommets);
00213 
00214         if (isRepositioning) 
00215         {
00216                 oldCenter=center;
00217                 GLdouble cdx = 1.0/(GLdouble)nbCut.x;
00218                 GLdouble cdy = 1.0/(GLdouble)nbCut.y;
00219                 BOOL noCutX = nbCut.x==1?TRUE:FALSE;
00220                 BOOL noCutY = nbCut.y==1?TRUE:FALSE;
00221                 GLdouble px = 0;
00222                 GLdouble py = 0;
00223                 GLdouble z;
00224                 for (int x=0;x<=nbCut.x;x++)
00225                 {
00226                         for (int y=0;y<=nbCut.y;y++)                                            
00227                         {
00228                         #define fxy(value,center) ((value<center)?sin((HALF_PI/center)*(value)):cos((HALF_PI/(1-center))*((value)-center)))
00229                         #define fx(value) fxy(value,center.x)
00230                         #define fy(value) fxy(value,center.y)
00231                         if (noCutX) z = fy(py);
00232                         else if (noCutY) z = fx(px);
00233                         else z = fx(px)*fy(py);
00234                         ptrSommet[0]=px*size.x;
00235                         ptrSommet[1]=py*size.y-size.y;
00236                         ptrSommet[2]=z*center.z;
00237                         ptrSommet+=3;
00238                         if (ptrCoorTexture)
00239                         {
00240                                 ptrCoorTexture[0]=texture->GetXCoord(px);
00241                                 ptrCoorTexture[1]=texture->GetYCoord(py);
00242                                 ptrCoorTexture+=2;
00243                         }
00244                         py+=cdy;
00245                 }
00246                 px+=cdx;
00247                 py=0;
00248         }
00249 
00250         int numSommet=0;
00251         for (int x=0;x<nbCut.x;x++)
00252         {
00253         for (int y=0;y<nbCut.y;y++)                                             
00254         {
00255         ptrQuad[1]=numSommet;
00256         ptrQuad[0]=numSommet+1;
00257         ptrQuad[3]=numSommet+nbCut.y+2;
00258         ptrQuad[2]=numSommet+nbCut.y+1;
00259         ptrQuad+=4;
00260         numSommet++;
00261         }
00262         numSommet++;
00263         }
00264         }
00265 
00266 glDrawElements(GL_QUADS,nbFaces*4,GL_UNSIGNED_INT,tabQuads);
00267 glDisableClientState(GL_VERTEX_ARRAY);
00268 if (tabCoorTextures) glDisableClientState(GL_TEXTURE_COORD_ARRAY);
00269 
00270 return TRUE;
00271 }
00272 */

Generated on Fri Aug 20 19:19:44 2004 for 3d Controls by doxygen 1.3.6