ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/REPOS_ERICCA/magic/app/VMM/win32/visuparametrique.cpp
Revision: 5
Committed: Tue Jun 12 20:26:34 2007 UTC (18 years, 2 months ago)
Original Path: magic/app/VMM/VMM/win32/visuparametrique.cpp
File size: 14269 byte(s)
Log Message:

File Contents

# User Rev Content
1 5 //------------------------------------------------------------
2     //------------------------------------------------------------
3     // MAGiC
4     // Jean Christophe Cuillière et Vincent FRANCOIS
5     // Département de Génie Mécanique - UQTR
6     //------------------------------------------------------------
7     // Le projet MAGIC est un projet de recherche du département
8     // de génie mécanique de l'Université du Québec à
9     // Trois Rivières
10     // Les librairies ne peuvent être utilisées sans l'accord
11     // des auteurs (contact : francois@uqtr.ca)
12     //------------------------------------------------------------
13     //------------------------------------------------------------
14     //
15     // visuparametrique.cpp
16     //
17     //------------------------------------------------------------
18     //------------------------------------------------------------
19     // COPYRIGHT 2000
20     // Version du 02/03/2006 à 11H26
21     //------------------------------------------------------------
22     //------------------------------------------------------------
23    
24    
25     #include "gestionversion.h"
26    
27     #include <vcl.h>
28     #pragma hdrstop
29    
30     #include "visuparametrique.h"
31     //---------------------------------------------------------------------------
32     #pragma package(smart_init)
33     #pragma resource "*.dfm"
34     Tvisupara *visupara;
35     //---------------------------------------------------------------------------
36     __fastcall Tvisupara::Tvisupara(TComponent* Owner)
37     : TForm(Owner),tab(NULL),nbtab(0),dx(0.0),dy(0.0),debut_trans(false)
38     {
39     }
40     //---------------------------------------------------------------------------
41     void __fastcall Tvisupara::FormCreate(TObject *Sender)
42     {
43     hdc = GetDC(Handle);
44     SetPixelFormatDescriptor();
45     hrc = wglCreateContext(hdc);
46     if (hrc == NULL)
47     ShowMessage(":-)~ hrc == NULL");
48     if(wglMakeCurrent(hdc, hrc) == false)
49     ShowMessage("Could not MakeCurrent");
50     ListBox2->Height=Panel1->ClientHeight*0.9-ListBox2->Top;
51     }
52     //---------------------------------------------------------------------------
53     void __fastcall Tvisupara::SetPixelFormatDescriptor(void)
54     {
55     PIXELFORMATDESCRIPTOR pfd = {
56     sizeof(PIXELFORMATDESCRIPTOR),
57     1,
58     PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER,
59     PFD_TYPE_RGBA,
60     24,
61     0,0,0,0,0,0,
62     0,0,
63     0,0,0,0,0,
64     32,
65     0,
66     0,
67     PFD_MAIN_PLANE,
68     0,
69     0,0,
70     };
71     int PixelFormat = ChoosePixelFormat(hdc, &pfd);
72     SetPixelFormat(hdc, PixelFormat, &pfd);
73     }
74     //---------------------------------------------------------------------------
75    
76     void __fastcall Tvisupara::recadre(double dx,double dy,int etat)
77     {
78     wglMakeCurrent(hdc,hrc);
79     GLfloat w = ClientWidth-Panel1->Width;
80     GLfloat h = ClientHeight;
81     if (etat==0) glViewport(0, 0, w, h);
82     glMatrixMode(GL_PROJECTION);
83     glLoadIdentity();
84     double d=max(xmax-xmin,ymax-ymin);
85     d=d/2*1.1;
86     double aspect=w/h;
87     double xxmin,yymin,xxmax,yymax;
88     double xcentre=0.5*(xmin+xmax);
89     double ycentre=0.5*(ymin+ymax);
90     if (w<h) xxmin=xcentre-d+dx; else xxmin=xcentre-d*aspect+dx;
91     if (w<h) yymin=ycentre-d/aspect+dy; else yymin=ycentre-d+dy;
92     if (w<h) xxmax=xxmin+2.*d; else xxmax=xxmin+2.*d*aspect;
93     if (w<h) yymax=yymin+2.*d/aspect; else yymax=yymin+2.*d;
94     glOrtho(xxmin,xxmax,yymin,yymax,-1.,1.);
95     }
96     //---------------------------------------------------------------------------
97     void __fastcall Tvisupara::FormResize(TObject *Sender)
98     {
99     ListBox2->Height=Panel1->ClientHeight*0.9-ListBox2->Top;
100     int w=ClientWidth;
101     int h=ClientHeight;
102     while(w==0)
103     {
104     Width++;
105     w=ClientWidth;
106     }
107     while(h==0)
108     {
109     Height++;
110     h=ClientHeight;
111     }
112    
113     recadre(dx,dy,0);
114     redess();
115     }
116     //---------------------------------------------------------------------------
117     void __fastcall Tvisupara::initvue(void)
118     {
119     wglMakeCurrent(hdc,hrc);
120     glMatrixMode(GL_MODELVIEW);
121     glLoadIdentity();
122     /*double xcentre=0.5*(xmin+xmax);
123     double ycentre=0.5*(ymin+ymax);
124     double zcentre=0.5*(zmin+zmax);
125     glTranslated(xcentre,ycentre,zcentre);
126     glRotatef(-90., 0.0, 1.0, 0.0);
127     glRotatef(-90., 1.0, 0.0, 0.0);
128     glRotatef(45., 0.0, 1.0, 0.0);
129     glRotatef(-45., 0.0, 0.0, 1.0);
130     glTranslated(-xcentre,-ycentre,-zcentre);*/
131     }
132     //---------------------------------------------------------------------------
133     void __fastcall Tvisupara::redess(void)
134     {
135     wglMakeCurrent(hdc,hrc);
136     glClearColor( 0.0f, 0.0f, 0.0f, 0.0f);
137     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
138     glEnable(GL_DEPTH_TEST);
139     glEnable(GL_POLYGON_OFFSET_FILL);
140     glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);
141     glBegin(GL_TRIANGLES);
142     for (int i=0;i<nbtab/6;i++)
143     {
144     glColor3f(1.0f,0.0f,0.0f);
145     glVertex3f(tab[6*i],tab[6*i+1],0.);
146     glVertex3f(tab[6*i+2],tab[6*i+3],0.);
147     glVertex3f(tab[6*i+4],tab[6*i+5],0.);
148     }
149     glEnd();
150     /*glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);
151     glPolygonOffset(1.0,1.0);
152     glBegin(GL_TRIANGLES);
153     glEdgeFlag(GL_TRUE);
154     for (int i=0;i<numtriangle;i++)
155     {
156     if (lsttriangle[i].active==0) continue;
157     glColor3f(0.0f,0.0f,1.0f);
158     glVertex3f(lstnoeud[lsttriangle[i].n1].x,lstnoeud[lsttriangle[i].n1].y,lstnoeud[lsttriangle[i].n1].z);
159     glVertex3f(lstnoeud[lsttriangle[i].n2].x,lstnoeud[lsttriangle[i].n2].y,lstnoeud[lsttriangle[i].n2].z);
160     glVertex3f(lstnoeud[lsttriangle[i].n3].x,lstnoeud[lsttriangle[i].n3].y,lstnoeud[lsttriangle[i].n3].z);
161     }
162     glEnd();
163     if (Arte1->Checked)
164     {
165     glBegin(GL_LINES);
166     for (int i=0;i<numsegment;i++)
167     {
168     glColor3f(1.0f,0.0f,0.0f);
169     glVertex3f(lstnoeud[lstsegment[i].n1].x,lstnoeud[lstsegment[i].n1].y,lstnoeud[lstsegment[i].n1].z);
170     glVertex3f(lstnoeud[lstsegment[i].n2].x,lstnoeud[lstsegment[i].n2].y,lstnoeud[lstsegment[i].n2].z);
171     }
172     glEnd();
173     glPointSize(4.0);
174     glBegin(GL_POINTS);
175     for (int i=0;i<numsegment;i++)
176     {
177     glColor3f(1.0f,0.0f,0.0f);
178     glVertex3f(lstnoeud[lstsegment[i].n1].x,lstnoeud[lstsegment[i].n1].y,lstnoeud[lstsegment[i].n1].z);
179     glVertex3f(lstnoeud[lstsegment[i].n2].x,lstnoeud[lstsegment[i].n2].y,lstnoeud[lstsegment[i].n2].z);
180     }
181     glEnd();
182     } */
183     glFlush();
184     SwapBuffers(hdc);
185     }
186     //---------------------------------------------------------------------------
187    
188     void __fastcall Tvisupara::FormPaint(TObject *Sender)
189     {
190     wglMakeCurrent(hdc,hrc);
191     glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
192     redess();
193     }
194     //---------------------------------------------------------------------------
195    
196     void __fastcall Tvisupara::FormClose(TObject *Sender, TCloseAction &Action)
197     {
198     Action=caFree;
199     if (nbtab!=0) delete [] tab;
200     }
201     //---------------------------------------------------------------------------
202    
203     void __fastcall Tvisupara::ListBox1Click(TObject *Sender)
204     {
205     if (ListBox1->ItemIndex==-1) return;
206     char mess[255];
207     sprintf(mess,"Maillage %s",ListBox1->Items->Strings[ListBox1->ItemIndex].c_str());
208     Label1->Caption=mess;
209     Panel1->SetFocus();
210     if (ListBox2->ItemIndex==-1) return;
211     init();
212     dx=0.;dy=0.;
213     glMatrixMode(GL_MODELVIEW);
214     glLoadIdentity();
215     recadre(dx,dy,0);
216     redess();
217     }
218     //---------------------------------------------------------------------------
219    
220     void __fastcall Tvisupara::ListBox2Click(TObject *Sender)
221     {
222     if (ListBox2->ItemIndex==-1) return;
223     char mess[255];
224     sprintf(mess,"Face %s",ListBox2->Items->Strings[ListBox2->ItemIndex].c_str());
225     Label2->Caption=mess;
226     Panel1->SetFocus();
227     if (ListBox1->ItemIndex==-1) return;
228     init();
229     dx=0.;dy=0.;
230     glMatrixMode(GL_MODELVIEW);
231     glLoadIdentity();
232     recadre(dx,dy,0);
233     redess();
234     }
235     //---------------------------------------------------------------------------
236     void __fastcall Tvisupara::init(void)
237     {
238     if (nbtab!=0)
239     {
240     nbtab=0;
241     delete [] tab;
242     }
243     int nummai=ListBox1->ItemIndex;
244     int numface=ListBox2->ItemIndex;
245     int numgeo=ListBox3->ItemIndex;
246     MG_GEOMETRIE* geo=gest->get_mg_geometrie(numgeo);
247     MG_FACE* face=geo->get_mg_face(numface);
248     MG_MAILLAGE* mai=gest->get_mg_maillage(nummai);
249     int nbtri=face->get_lien_maillage()->get_nb();
250     int nbtriactive=0;
251     for (int i=0;i<nbtri;i++)
252     {
253     MG_TRIANGLE* tri=(MG_TRIANGLE*)face->get_lien_maillage()->get(i);
254     MG_TRIANGLE* tri2=mai->get_mg_triangleid(tri->get_id());
255     if (tri2==tri) nbtriactive++;
256     }
257     nbtab=nbtriactive*6;
258     tab=new double[nbtab];
259     xmin=1e308;xmax=-1e308;
260     ymin=1e308;ymax=-1e308;
261     for (int i=0;i<nbtri;i++)
262     {
263     MG_TRIANGLE* tri=(MG_TRIANGLE*)face->get_lien_maillage()->get(i);
264     MG_TRIANGLE* tri2=mai->get_mg_triangleid(tri->get_id());
265     if (tri2==tri)
266     {
267     double *xyz,uv[2];
268     MG_NOEUD* noeud1=tri->get_noeud1();
269     xyz=noeud1->get_coord();
270     face->inverser(uv,xyz);
271     tab[6*i]=uv[0];
272     tab[6*i+1]=uv[1];
273     if (xmin>uv[0]) xmin=uv[0];
274     if (xmax<uv[0]) xmax=uv[0];
275     if (ymin>uv[1]) ymin=uv[1];
276     if (ymax<uv[1]) ymax=uv[1];
277     MG_NOEUD* noeud2=tri->get_noeud2();
278     xyz=noeud2->get_coord();
279     face->inverser(uv,xyz);
280     tab[6*i+2]=uv[0];
281     tab[6*i+3]=uv[1];
282     if (xmin>uv[0]) xmin=uv[0];
283     if (xmax<uv[0]) xmax=uv[0];
284     if (ymin>uv[1]) ymin=uv[1];
285     if (ymax<uv[1]) ymax=uv[1];
286     MG_NOEUD* noeud3=tri->get_noeud3();
287     xyz=noeud3->get_coord();
288     face->inverser(uv,xyz);
289     tab[6*i+4]=uv[0];
290     tab[6*i+5]=uv[1];
291     if (xmin>uv[0]) xmin=uv[0];
292     if (xmax<uv[0]) xmax=uv[0];
293     if (ymin>uv[1]) ymin=uv[1];
294     if (ymax<uv[1]) ymax=uv[1];
295     }
296     }
297    
298     }
299    
300    
301     void __fastcall Tvisupara::ListBox3Click(TObject *Sender)
302     {
303     ListBox2->Clear();
304     int numgeo=ListBox3->ItemIndex;
305     MG_GEOMETRIE* geo=gest->get_mg_geometrie(numgeo);
306     int nbface=geo->get_nb_mg_face();
307     for (int i=0;i<nbface;i++)
308     {
309     MG_FACE* face=geo->get_mg_face(i);
310     long id=face->get_id();
311     ListBox2->Items->Add(id);
312     }
313     char mess[255];
314     sprintf(mess,"Geometrie %s",ListBox3->Items->Strings[ListBox3->ItemIndex].c_str());
315     Label3->Caption=mess;
316     Panel1->SetFocus();
317     }
318     //---------------------------------------------------------------------------
319    
320     void __fastcall Tvisupara::FormMouseWheelDown(TObject *Sender,
321     TShiftState Shift, TPoint &MousePos, bool &Handled)
322     {
323     wglMakeCurrent(hdc,hrc);
324     glMatrixMode(GL_MODELVIEW);
325     //zoom++;
326     double xcentre=0.5*(xmin+xmax);
327     double ycentre=0.5*(ymin+ymax);
328     glTranslated(xcentre,ycentre,0.);
329     glScalef(1.1,1.1,1.1);
330     glTranslated(-xcentre,-ycentre,0.);
331     redess();
332     }
333     //---------------------------------------------------------------------------
334    
335     void __fastcall Tvisupara::FormMouseWheelUp(TObject *Sender,
336     TShiftState Shift, TPoint &MousePos, bool &Handled)
337     {
338     wglMakeCurrent(hdc,hrc);
339     glMatrixMode(GL_MODELVIEW);
340     //zoom++;
341     double xcentre=0.5*(xmin+xmax);
342     double ycentre=0.5*(ymin+ymax);
343     glTranslated(xcentre,ycentre,0.);
344     glScalef(1./1.1,1./1.1,1./1.1);
345     glTranslated(-xcentre,-ycentre,0.);
346     redess();
347     }
348     //---------------------------------------------------------------------------
349    
350     void __fastcall Tvisupara::FormMouseDown(TObject *Sender,
351     TMouseButton Button, TShiftState Shift, int X, int Y)
352     {
353     if (Button==mbRight)
354     {
355     if (debut_trans==false)
356     {
357     debut_trans=true;
358     xdepart=X;
359     ydepart=Y;
360     }
361     }
362     }
363     //---------------------------------------------------------------------------
364    
365     void __fastcall Tvisupara::FormMouseMove(TObject *Sender,
366     TShiftState Shift, int X, int Y)
367     {
368     if (debut_trans==true)
369     {
370     GLfloat w = ClientWidth-Panel1->Width;
371     GLfloat h = ClientHeight;
372     double aspect=w/h;
373     double d=max(xmax-xmin,ymax-ymin);
374     d=d*1.1;
375     double xxmin,yymin,xxmax,yymax;
376     double xcentre=0.5*(xmin+xmax);
377     double ycentre=0.5*(ymin+ymax);
378     if (w<h) xxmin=xcentre-d+dx; else xxmin=xcentre-d*aspect+dx;
379     if (w<h) yymin=ycentre-d/aspect+dy; else yymin=ycentre-d+dy;
380     if (w<h) xxmax=xxmin+2.*d; else xxmax=xxmin+2.*d*aspect;
381     if (w<h) yymax=yymin+2.*d/aspect; else yymax=yymin+2.*d;
382     dx=dx+(xdepart-X)/2./w*(xxmax-xxmin);
383     dy=dy-(ydepart-Y)/2./h*(yymax-yymin);
384     recadre(dx,dy,1);
385     redess();
386     xdepart=X;
387     ydepart=Y;
388     }
389     }
390     //---------------------------------------------------------------------------
391    
392     void __fastcall Tvisupara::FormMouseUp(TObject *Sender,
393     TMouseButton Button, TShiftState Shift, int X, int Y)
394     {
395     if (Button==mbRight)
396     if (debut_trans==true)
397     {
398     debut_trans=false;
399     GLfloat w = ClientWidth-Panel1->Width;
400     GLfloat h = ClientHeight;
401     double aspect=w/h;
402     double d=max(xmax-xmin,ymax-ymin);
403     d=d*1.1;
404     double xxmin,yymin,xxmax,yymax;
405     double xcentre=0.5*(xmin+xmax);
406     double ycentre=0.5*(ymin+ymax);
407     if (w<h) xxmin=xcentre-d+dx; else xxmin=xcentre-d*aspect+dx;
408     if (w<h) yymin=ycentre-d/aspect+dy; else yymin=ycentre-d+dy;
409     if (w<h) xxmax=xxmin+2.*d; else xxmax=xxmin+2.*d*aspect;
410     if (w<h) yymax=yymin+2.*d/aspect; else yymax=yymin+2.*d;
411     dx=dx+(xdepart-X)/2./w*(xxmax-xxmin);
412     dy=dy-(ydepart-Y)/2./h*(yymax-yymin);
413     recadre(dx,dy,1);
414     redess();
415     xdepart=X;
416     ydepart=Y;
417     }
418     }
419     //---------------------------------------------------------------------------
420    
421