Debian Codegraph @

vcg/wrap/gl/addons.h

raw symbols SCIP
1 /****************************************************************************
2 * VCGLib o o *
3 * Visual and Computer Graphics Library o o *
4 * _ O _ *
5 * Copyright(C) 2004 \/)\/ *
6 * Visual Computing Lab /\/| *
7 * ISTI - Italian National Research Council | *
8 * \ *
9 * All rights reserved. *
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 * This program is distributed in the hope that it will be useful, *
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
19 * GNU General Public License (http://www.gnu.org/licenses/gpl.txt) *
20 * for more details. *
21 * *
22 ****************************************************************************/
23 /****************************************************************************
24 History
25 $Log: addons.h,v $
26 Revision 1.13 2006/07/03 13:46:34 spinelli
27 remove warning C4244
28
29 Revision 1.12 2006/05/25 09:22:58 cignoni
30 Removed all GLUT dependencies!
31
32 Revision 1.11 2006/03/29 07:54:03 cignoni
33 Wrong matrix type in cone (thx Maarten)
34
35
36 ****************************************************************************/
37 #ifndef __VCG_GLADDONS
38 #define __VCG_GLADDONS
39
40 #include <wrap/gl/math.h>
41 #include <wrap/gl/space.h>
42 #include <vcg/space/point3.h>
43 #include <map>
44
45 namespace vcg
46 {
47 #include "gl_geometry.h"
48 /** Class Add_Ons.
49 This is class draw 3d icons on the screen
50 */
51 class Add_Ons
52 {
53 public:
54 enum DrawMode {DMUser,DMWire,DMSolid} ;
55 private:
56
57 ///used to find right trasformation in case of rotation
58 static void XAxis( vcg::Point3f zero, vcg::Point3f uno, Matrix44f & tr){
59 tr.SetZero();
60 *((vcg::Point3f*)&tr[0][0]) = uno-zero;
61 GetUV(*((vcg::Point3f*)tr[0]),*((vcg::Point3f*)tr[1]),*((vcg::Point3f*)tr[2]));
62 tr[3][3] = 1.0;
63 *((vcg::Point3f*)&tr[3][0]) = zero;
64 }
65
66 //set drawingmode parameters
67 static void SetGLParameters(DrawMode DM)
68 {
69 switch(DM)
70 {
71 case DMUser :
72 break;
73 case DMWire :
74 glDisable(GL_CULL_FACE);
75 glDisable(GL_LIGHTING);
76 glDisable(GL_NORMALIZE);
77 glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);
78 break;
79 case DMSolid :
80 glDisable(GL_CULL_FACE);
81 glPolygonMode(GL_FRONT,GL_FILL);
82 break;
83 default :
84 break;
85 }
86 }
87
88 ///draw a cylinder
89 static void Cylinder(int slices,float lenght,float width,bool useDisplList)
90 {
91 static std::map<int,GLint> Disp_listMap;
92 GLint cyl_List=-1;
93 std::map<int,GLint>::const_iterator it=Disp_listMap.find(slices);
94 ///if the diplay list is createdtake the Glint that identify it
95 bool to_insert=false;
96 if (useDisplList)
97 {
98 if (it!=Disp_listMap.end())///the list exist
99 cyl_List=it->second;
100 else to_insert=true;
101 }
102
103 glScaled(lenght,width,width);
104
105 if (((!glIsList(cyl_List))&&(useDisplList))||(!useDisplList))
106 {
107 if (useDisplList)
108 {
109 cyl_List = glGenLists(1);
110 glNewList(cyl_List, GL_COMPILE);
111 }
112 int b;
113 vcg::Point3f p0;
114 vcg::Point3f p1;
115
116 float step=6.28f/(float)slices;
117 float angle=0;
118 glBegin(GL_TRIANGLE_STRIP);
119 for(b = 0; b <= slices-1; ++b){
120 p0 = vcg::Point3f( 0, sin(angle),cos(angle));
121 p1 = p0; p1[0] = 1.f;
122 glNormal3f(p0[0],p0[1],p0[2]);
123 glVertex3d(p0[0],p0[1],p0[2]);
124 glVertex3d(p1[0],p1[1],p1[2]);
125 angle+=step;
126 }
127 ///re-conjunction with first point of cylinder
128 glNormal3f(0,0,1);
129 glVertex3d(0,0,1);
130 glVertex3d(1,0,1);
131
132 glEnd();
133
134 ///fill the cylinder down
135 angle=0;
136 p0=vcg::Point3f(0,0,0);
137 glBegin(GL_TRIANGLE_FAN);
138 glNormal3f(-1,0,0);
139 glVertex3d(p0[0],p0[1],p0[2]);
140 for(b = 0; b <= slices-1; ++b){
141 glNormal3f(-1,0,0);
142 p1 = Point3f( 0, sin(angle),cos(angle));
143 glVertex3d(p1[0],p1[1],p1[2]);
144 angle+=step;
145 }
146 glNormal3f(-1,0,0);
147 glVertex3d(0,0,1);
148 glEnd();
149
150 angle=0;
151 p0=vcg::Point3f(1,0,0);
152 glBegin(GL_TRIANGLE_FAN);
153 glNormal3f(1,0,0);
154 glVertex3d(p0[0],p0[1],p0[2]);
155 for(b = 0; b <= slices-1; ++b){
156 glNormal3f(1,0,0);
157 p1 = Point3f( 1, sin(angle),cos(angle));
158 glVertex3d(p1[0],p1[1],p1[2]);
159 angle+=step;
160 }
161 glNormal3f(1,0,0);
162 glVertex3d(1,0,1);
163 glEnd();
164
165
166 if (useDisplList)
167 glEndList();
168 }
169 if (useDisplList)
170 {
171 glCallList(cyl_List);
172 ///I insert key and value in the map if I need
173 if (to_insert)
174 Disp_listMap.insert(std::pair<int,GLint>(slices,cyl_List));
175 }
176 }
177 public:
178 static void Diamond (float radius,bool useDisplList)
179 {
180 static GLint diam_List=-1;
181
182 glScaled(radius,radius,radius);
183 if (((!glIsList(diam_List))&&(useDisplList))||(!useDisplList))
184 {
185 if (useDisplList)
186 {
187 diam_List = glGenLists(1);
188 glNewList(diam_List, GL_COMPILE);
189 }
190
191 glBegin(GL_TRIANGLE_FAN);
192 glNormal3f( 0.0, 1, 0.0);
193 glVertex3f(0.0,1,0.0);
194
195 glNormal3f( 1, 0.0, 0.0);
196 glVertex3f( 1, 0.0, 0.0);
197
198 glNormal3f( 0.0, 0.0, -1);
199 glVertex3f( 0.0, 0.0,-1);
200
201 glNormal3f(-1, 0.0 , 0.0);
202 glVertex3f(-1, 0.0, 0.0);
203
204 glNormal3f( 0.0, 0.0, 1);
205 glVertex3f( 0.0, 0.0, 1);
206
207 glNormal3f( 1, 0.0, 0.0);
208 glVertex3f( 1, 0.0, 0.0);
209
210 glEnd();
211
212 glBegin(GL_TRIANGLE_FAN);
213 glNormal3f( 0.0, 1, 0.0);
214 glVertex3f( 0.0,-1, 0.0);
215
216 glNormal3f( 1, 0.0, 0.0);
217 glVertex3f( 1, 0.0, 0.0);
218
219 glNormal3f( 0.0, 0.0, 1);
220 glVertex3f( 0.0, 0.0, 1);
221
222 glNormal3f(-1,0.0 , 0.0);
223 glVertex3f(-1, 0.0, 0.0);
224
225 glNormal3f( 0.0,0.0, -1);
226 glVertex3f( 0.0, 0.0,-1);
227
228 glNormal3f( 1, 0.0, 0.0);
229 glVertex3f( 1, 0.0, 0.0);
230 glEnd();
231 if (useDisplList)
232 glEndList();
233 }
234 if (useDisplList)
235 glCallList(diam_List);
236 }
237
238 ///draw a cone
239 static void Cone(int slices,float lenght,float width,bool useDisplList)
240 {
241 assert(!glGetError());
242 static std::map<int,GLint> Disp_listMap;
243 GLint cone_List=-1;
244 std::map<int,GLint>::const_iterator it=Disp_listMap.find(slices);
245 ///if the diplay list is createdtake the Glint that identify it
246 bool to_insert=false;
247
248 if (useDisplList)
249 {
250 if (it!=Disp_listMap.end())///the list exist
251 cone_List=it->second;
252 else to_insert=true;
253 }
254
255 glScaled(lenght,width,width);
256 assert(!glGetError());
257 if (((!glIsList(cone_List))&&(useDisplList))||(!useDisplList))
258 {
259 int h=1;
260 vcg::Point3f p0;
261 vcg::Point3f P[2];
262 vcg::Point3f N[2];
263 assert(!glGetError());
264 glScaled(lenght,width,width);
265 if (useDisplList)
266 {
267 cone_List = glGenLists(1);
268 glNewList(cone_List, GL_COMPILE);
269 }
270 for(h=0; h < 2; ++h)
271 {
272 assert(!glGetError());
273 //glBegin(GL_TRIANGLE_FAN);
274 p0 = Point3f(0,0,0);
275 if(h==0) p0[0]+=1.f;
276 //glNormal3f(1,0.0,0.0);
277 //glVertex3d(p0[0],p0[1],p0[2]);
278 N[0]= Point3f( 1.f,sinf(0),cosf(0) );
279 P[0]= Point3f( 0,sinf(0),cosf(0));
280 int b;
281 for(b = 1; b <= slices; ++b)
282 {
283 float angle = -6.28f*(float)b/(float)slices;
284 if (b==slices) angle=0;
285 N[1] = Point3f( 1.f, sinf(angle), cosf(angle) );
286 P[1] = Point3f( 0, sinf(angle), cosf(angle));
287 assert(!glGetError());
288 glBegin(GL_TRIANGLES);
289 Point3f n = ( (P[0]-p0) ^ (P[2]-p0) ).Normalize();
290 glNormal3f(n[0],n[1],n[2]);
291 glVertex3f(p0[0],p0[1],p0[2]);
292 glNormal3f(N[0][0],N[0][1],N[0][2]);
293 glVertex3f(P[0][0],P[0][1],P[0][2]);
294 glNormal3f(N[1][0],N[1][1],N[1][2]);
295 glVertex3f(P[1][0],P[1][1],P[1][2]);
296 glEnd();
297 assert(!glGetError());
298 N[0] = N[1];
299 P[0] = P[1];
300 }
301 //glEnd();
302 }
303 if (useDisplList)
304 glEndList();
305 }
306 if (useDisplList)
307 {
308 glCallList(cone_List);
309 ///I insert key and value in the map if I need
310 if (to_insert)
311 Disp_listMap.insert(std::pair<int,GLint>(slices,cone_List));
312 }
313 }
314
315 public:
316
317 /// draw an arrow from tail to head
318 /// body_width = width of the body of arrow
319 /// head_lenght = lenght of the head of arrow
320 /// head_width = width of the head of arrow
321 /// body_slice = number of slices on the body
322 /// head_slice = number of slices on the head
323 template <DrawMode dm>
324 static void glArrow(Point3f tail, Point3f head,float body_width,float head_lenght,
325 float head_width,int body_slice=10,int head_slice=10,bool useDisplList=true)
326 {
327 if (tail!=head)
328 {
329 //assert(!glGetError());
330 Matrix44f tr;
331 XAxis(tail,head,tr);
332 glPushAttrib(GL_ALL_ATTRIB_BITS);
333 SetGLParameters(dm);
334 glPushMatrix();
335 glMultMatrixf(&tr[0][0]);
336 vcg::Point3f Direct=(head-tail);
337 float l_body=Direct.Norm()-head_lenght;
338 glPushMatrix();
339 glTranslate(vcg::Point3f(tail.Norm(),0,0));
340 Cylinder(body_slice,l_body,body_width,useDisplList);
341 glPopMatrix();
342 glTranslate(vcg::Point3f(l_body,0,0));
343 Cone(head_slice,head_lenght,head_width,useDisplList);
344 glPopMatrix();
345 //assert(!glGetError());
346 glPopAttrib();
347 //assert(!glGetError());
348 }
349 }
350
351 /// draw a cone from tail to head
352 /// width = width of the base of the cone
353 /// slice = number of slices on the cone
354 template <DrawMode dm>
355 static void glCone(Point3f tail, Point3f head,float width,int slice=10,bool useDisplList=true)
356 {
357 if (tail!=head)
358 {
359 Matrix44f tr;
360 XAxis(tail,head,tr);
361 glPushAttrib(GL_ALL_ATTRIB_BITS);
362 SetGLParameters(dm);
363 glPushMatrix();
364 glMultMatrixf(&tr[0][0]);
365 vcg::Point3f Direct=(head-tail);
366 float l_body=Direct.Norm();
367 glTranslate(vcg::Point3f(tail.Norm(),0,0));
368 Cone(slice,l_body,width,useDisplList);
369 glPopMatrix();
370 glPopAttrib();
371 }
372 }
373
374 /// draw a cylinder from tail to head
375 /// width = width of the base of the cylinder
376 /// slice = number of slices on the cylinder
377 template <DrawMode dm>
378 static void glCylinder(Point3f tail, Point3f head,float width,int slice=10,bool useDisplList=true)
379 {
380 if (tail!=head)
381 {
382 Matrix44f tr;
383 XAxis(tail,head,tr);
384 glPushAttrib(GL_ALL_ATTRIB_BITS);
385 SetGLParameters(dm);
386 glPushMatrix();
387 glMultMatrixf(&tr[0][0]);
388 vcg::Point3f Direct=(head-tail);
389 float l_body=Direct.Norm();
390 glTranslate(vcg::Point3f(tail.Norm(),0,0));
391 Cylinder(slice,l_body,width,useDisplList);
392 glPopMatrix();
393 glPopAttrib();
394 }
395
396 }
397
398 /// draw a point in Center
399 /// size = Radius of the point
400 /// slices = The number of subdivisions around the Z axis (similar to lines of longitude).
401 /// stacks = The number of subdivisions along the Z axis (similar to lines of latitude).
402 template <DrawMode dm>
403 static void glPoint(vcg::Point3f Center,float size,int slices =16,int stacks =16)
404 {
405 if (size!=0){
406 glPushMatrix();
407 glTranslate(Center);
408 if (dm==DMWire)
409 glutWireSphere(size,slices,stacks);
410 else
411 if (dm==DMSolid)
412 glutSolidSphere(size,slices,stacks);
413 else
414 glutSolidSphere(size,slices,stacks);
415 glPopMatrix();
416 }
417 }
418
419 /// draw a point in Center
420 /// size = Radius of the point
421 /// slices = The number of subdivisions around the Z axis (similar to lines of longitude).
422 /// stacks = The number of subdivisions along the Z axis (similar to lines of latitude).
423 template <DrawMode dm>
424 static void glDiamond (Point3f Center, float size,bool useDisplList=true)
425 {
426 if (size!=0){
427 glPushAttrib(GL_ALL_ATTRIB_BITS);
428 SetGLParameters(dm);
429 glPushMatrix();
430 glTranslated(Center[0],Center[1],Center[2]);
431 Diamond(size,useDisplList);
432 glPopMatrix();
433 glPopAttrib();
434 }
435 }
436 };
437 }
438 #endif