1 /* UtilsGL.c */
2 /**********************************************************************************************************
3 Copyright (c) 2002-2013 Abdul-Rahman Allouche. All rights reserved
4 
5 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
6 documentation files (the Gabedit), to deal in the Software without restriction, including without limitation
7 the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
8 and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
9 
10   The above copyright notice and this permission notice shall be included in all copies or substantial portions
11   of the Software.
12 
13 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
14 TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
15 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
16 CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
17 DEALINGS IN THE SOFTWARE.
18 ************************************************************************************************************/
19 
20 
21 #include "../../Config.h"
22 #include <stdlib.h>
23 #include <ctype.h>
24 #include <math.h>
25 #include <time.h>
26 #include <gtk/gtk.h>
27 #include <GL/gl.h>
28 #include <GL/glu.h>
29 #include <gtk/gtkgl.h>
30 
31 #include <pango/pangoft2.h>
32 
33 /*
34 #ifndef G_OS_WIN32
35 #include <GL/glx.h>
36 #include <pango/pangox.h>
37 #include <gdk/gdkx.h>
38 #else
39 #include <pango/pangowin32.h>
40 #endif
41 */
42 
43 
44 #include "../Common/Global.h"
45 #include "../Utils/Vector3d.h"
46 #include "../Utils/Transformation.h"
47 #include "../Utils/Constants.h"
48 #include "UtilsGL.h"
49 #include "../../gl2ps/gl2ps.h"
50 /* transformation/projection matrices */
51 static GLint viewport[4];
52 static GLdouble mvmatrix[16];
53 static GLdouble projmatrix[16];
54 static gint glFontsize=10;
55 static gint fontOffset=-1;
56 static gint charWidth=0;
57 static gint charHeight=0;
58 
59 /*********************************************************************************************/
60 /* window to real space conversion primitive */
glGetWorldCoords(gint x,gint y,gint height,gdouble * w)61 void glGetWorldCoords(gint x, gint y, gint height, gdouble *w)
62 {
63 	gint i;
64 	GLdouble r[3];
65 	GLfloat winX, winY, winZ;
66 
67 	glGetIntegerv(GL_VIEWPORT, viewport);
68 	glGetDoublev(GL_MODELVIEW_MATRIX, mvmatrix);
69 	glGetDoublev(GL_PROJECTION_MATRIX, projmatrix);
70 
71 	winX = (float)x;
72 	winY = (float)viewport[3] - (float)y;
73 	winZ = 0.0;
74 	/*glReadPixels( x, (gint)(winY), 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &winZ );*/
75 
76 	gluUnProject( winX, winY, winZ, mvmatrix, projmatrix, viewport, &r[0], &r[1], &r[2]);
77 
78 	for(i=0;i<3;i++) w[i] = r[i];
79 }
80 /*********************************************************************************************/
81 /* real space to window conversion primitive */
glGetWindowCoords(gdouble * w,gint height,gint * x)82 void glGetWindowCoords(gdouble *w, gint height, gint *x)
83 {
84 	GLdouble r[3];
85 
86 	glGetIntegerv(GL_VIEWPORT, viewport);
87 	glGetDoublev(GL_MODELVIEW_MATRIX, mvmatrix);
88 	glGetDoublev(GL_PROJECTION_MATRIX, projmatrix);
89 
90 	gluProject(w[0], w[1], w[2], mvmatrix, projmatrix, viewport, &r[0], &r[1], &r[2]);
91 
92 	x[0] = r[0];
93 	x[1] = height - r[1] - 1;
94 }
95 /*********************************************************************************************/
glTextWidth(gchar * str)96 gint glTextWidth(gchar *str)
97 {
98 	return(strlen(str) * glFontsize);
99 }
100 /*********************************************************************************************/
glTextHeight()101 gint glTextHeight()
102 {
103 	if(charHeight>0) return charHeight;
104 	return(glFontsize);
105 }
106 /*********************************************************************************************/
107 /* print at a window position */
108 /*
109 void glPrintWinOld(gint x, gint y, gint height, gchar *str)
110 {
111 	gdouble w[3];
112 
113 	glGetWorldCoords(x, y, height, w);
114 	glRasterPos3f(w[0], w[1], w[2]);
115 	gl2psText(str, "Times-Roman", glFontsize);
116 
117 	glListBase(fontOffset);
118 	glCallLists(strlen(str), GL_UNSIGNED_BYTE, str);
119 }
120 */
121 /*********************************************************************************************/
122 /* print at a world position */
123 /*
124 void glPrintOld(gdouble x, gdouble y, gdouble z, gchar *str)
125 {
126 	glRasterPos3f(x,y,z);
127 	gl2psText(str, "Times-Roman", glFontsize);
128 
129 	glListBase(fontOffset);
130 	glCallLists(strlen(str), GL_UNSIGNED_BYTE, str);
131 }
132 */
133 /*********************************************************************************************/
134 /*
135 void glPrintOrthoOld(gdouble x, gdouble y, gdouble z, gchar *str, gboolean centerX, gboolean centerY)
136 {
137 	gdouble w[] = {x, y, z};
138 	gint xy[] = {0, 0};
139 	glGetIntegerv(GL_VIEWPORT, viewport);
140 	glGetWindowCoords(w, viewport[3], xy);
141 	glPushMatrix();
142 	glLoadIdentity();
143 
144 	glMatrixMode(GL_PROJECTION);
145 	glPushMatrix(); glLoadIdentity();
146 	gluOrtho2D(0, viewport[2], 0, viewport[3]);
147 
148 
149 	if(centerX) xy[0] -= charWidth*strlen(str)/2;
150 	if(centerY) xy[1] += charHeight/4;
151 	glPrintWinOld(xy[0], xy[1], viewport[3], str);
152 
153 	glPopMatrix();
154 	glMatrixMode(GL_MODELVIEW);
155 
156 	glPopMatrix();
157 }
158 */
159 /*********************************************************************************************/
glDeleteFontsList()160 void glDeleteFontsList()
161 {
162 	glDeleteLists(fontOffset,256);
163 }
164 /*********************************************************************************************/
165 /*********************************************************************************************/
166 /* pango fonts for OpenGL  */
167 /*
168 void glInitFontsOld()
169 {
170 	glInitFontsUsingOld(FontsStyleLabel.fontname);
171 }
172 */
173 /*********************************************************************************************/
174 /* pango fonts for OpenGL  */
glInitFonts(PangoContext ** pft2_context)175 void glInitFonts(PangoContext* *pft2_context)
176 {
177 	/*if (fontOffset >=0) return;*/
178 	glInitFontsUsing(FontsStyleLabel.fontname, pft2_context);
179 }
180 /*********************************************************************************************/
181 /* get a World coordinates from scene coordinates */
glGetWorldCoordsFromSceneCoords(gdouble VScene[],gdouble VWorld[])182 void glGetWorldCoordsFromSceneCoords(gdouble VScene[], gdouble VWorld[])
183 {
184 	GLdouble mvMatrix[4][4];
185 	gdouble** trMatrix;
186 	gdouble** invMatrix;
187 	gint i;
188 	gint j;
189 	glGetDoublev(GL_MODELVIEW_MATRIX, &mvMatrix[0][0]);
190 	trMatrix = g_malloc(3*sizeof(gdouble*));
191 	for(i=0;i<3;i++) trMatrix[i] = g_malloc(3*sizeof(gdouble));
192 
193 	for(i=0;i<3;i++)
194 		for(j=0;j<3;j++)
195 			trMatrix[j][i] = mvMatrix[i][j];
196 
197 	invMatrix = Inverse3(trMatrix);
198 	for(i=0;i<3;i++) g_free(trMatrix[i]);
199 	g_free(trMatrix);
200 
201 	if(invMatrix != NULL)
202 	{
203 		for(i=0;i<3;i++)
204 		{
205 			VWorld[i] = 0;
206 			for(j=0;j<3;j++)
207 			{
208 				VWorld[i] += invMatrix[i][j]*VScene[j];
209 			}
210 		}
211 		for(i=0;i<3;i++) g_free(invMatrix[i]);
212 		g_free(invMatrix);
213 	}
214 	else
215 	{
216 		for(i=0;i<3;i++) VWorld[i] = 0;
217 		VWorld[2] = 1;
218 	}
219 }
220 /*********************************************************************************************/
221 /* get a normal vector to plan of window */
getNormalPlanWindow(gdouble N[])222 void getNormalPlanWindow(gdouble N[])
223 {
224 	gdouble VScene[3]={0,0,1};
225 
226 	glGetWorldCoordsFromSceneCoords(VScene,N);
227 
228 }
229 /*********************************************************************************************/
230 /* print at a world position  after scaling of scal in scene coordinates*/
231 /*
232 void glPrintScaleOld(gdouble x, gdouble y, gdouble z, gdouble scale, gchar *str)
233 {
234 	gdouble VScene[]={0,0,0};
235 	gdouble VWorld[]={x,y,z};
236 	GLdouble mvMatrix[4][4];
237 	gdouble** trMatrix;
238 	gdouble** invMatrix;
239 	gint i;
240 	gint j;
241 	glGetDoublev(GL_MODELVIEW_MATRIX, &mvMatrix[0][0]);
242 	trMatrix = g_malloc(3*sizeof(gdouble*));
243 	for(i=0;i<3;i++) trMatrix[i] = g_malloc(3*sizeof(gdouble));
244 
245 	for(i=0;i<3;i++)
246 		for(j=0;j<3;j++)
247 			trMatrix[j][i] = mvMatrix[i][j];
248 
249 	for(i=0;i<3;i++)
250 	{
251 		VScene[i] = 0;
252 		for(j=0;j<3;j++)
253 		{
254 			VScene[i] += trMatrix[i][j]*VWorld[j];
255 		}
256 	}
257 	VScene[2] += scale;
258 
259 	invMatrix = Inverse3(trMatrix);
260 	for(i=0;i<3;i++) g_free(trMatrix[i]);
261 	g_free(trMatrix);
262 
263 	if(invMatrix != NULL)
264 	{
265 		for(i=0;i<3;i++)
266 		{
267 			VWorld[i] = 0;
268 			for(j=0;j<3;j++)
269 			{
270 				VWorld[i] += invMatrix[i][j]*VScene[j];
271 			}
272 		}
273 		for(i=0;i<3;i++) g_free(invMatrix[i]);
274 		g_free(invMatrix);
275 	}
276 	else
277 	{
278 		VWorld[0] = x;
279 		VWorld[1] = y;
280 		VWorld[2] = z;
281 	}
282 	glRasterPos3f(VWorld[0],VWorld[1],VWorld[2]);
283 	gl2psText(str, "Times-Roman", glFontsize);
284 	glListBase(fontOffset);
285 	glCallLists(strlen(str), GL_UNSIGNED_BYTE, str);
286 }
287 */
288 /*************************************************************************************/
get_opengl_options()289 OpenGLOptions get_opengl_options()
290 {
291 	return openGLOptions;
292 }
293 /**********************************************/
glMaterialdv(GLenum face,GLenum pname,const GLdouble * params)294 void glMaterialdv(GLenum face, GLenum pname, const GLdouble*  	params)
295 {
296 	GLfloat p[4] = {params[0],params[1],params[2], params[3]};
297 	glMaterialfv(face, pname, p);
298 }
299 /**********************************************/
glLightdv(GLenum face,GLenum pname,const GLdouble * params)300 void glLightdv(GLenum face, GLenum pname, const GLdouble* params)
301 {
302 	GLfloat p[4] = {params[0],params[1],params[2], params[3]};
303 	glLightfv(face, pname, p);
304 }
305 
306 /**********************************************/
glFogdv(GLenum pname,const GLdouble * params)307 void glFogdv(GLenum pname, const GLdouble* params)
308 {
309 	GLfloat p[4] = {params[0],params[1],params[2], params[3]};
310 	glFogfv(pname, p);
311 }
312 /*********************************************************************************************/
mYPerspective(GLdouble fovy,GLdouble aspect,GLdouble zNear,GLdouble zFar)313 void mYPerspective( GLdouble fovy, GLdouble aspect, GLdouble zNear, GLdouble zFar )
314 {
315 	GLdouble xmin, xmax, ymin, ymax;
316 
317 	ymax = zNear * tan( fovy * PI / 360.0 );
318 	ymin = -ymax;
319 
320 	xmin = ymin * aspect;
321 	xmax = ymax * aspect;
322 
323 	glFrustum( xmin, xmax, ymin, ymax, zNear, zFar );
324 }
325 /* Sphere.c */
326 /************************************************************************************************************/
Sphere_Draw_Precision(GLdouble radius,V3d position,GLint numberOfSubdivisions)327 void Sphere_Draw_Precision(GLdouble radius,V3d position, GLint numberOfSubdivisions)
328 {
329 	GLUquadricObj *obj;
330 
331 	glPushMatrix();
332 	glTranslated(position[0],position[1],position[2]);
333     	obj = gluNewQuadric();
334 	gluQuadricNormals(obj, GL_SMOOTH);
335 	gluQuadricDrawStyle(obj, GLU_FILL);
336 	gluSphere(obj, radius, numberOfSubdivisions*2, numberOfSubdivisions);
337 
338 	gluDeleteQuadric(obj);
339 	glTranslated(-position[0],-position[1],-position[2]);
340 	glPopMatrix();
341 }
342 /************************************************************************************************************/
Sphere_Draw_Color_Precision(GLdouble radius,V3d position,V4d Specular,V4d Diffuse,V4d Ambiant,GLint numberOfSubdivisions)343 void Sphere_Draw_Color_Precision(GLdouble radius,V3d position, V4d Specular,V4d Diffuse,V4d Ambiant, GLint numberOfSubdivisions)
344 {
345 	static GLdouble emission[] = { 0.0, 0.0, 0.0, 1.0 };
346 	glMaterialdv(GL_FRONT_AND_BACK,GL_SPECULAR,Specular);
347 	glMaterialdv(GL_FRONT_AND_BACK,GL_DIFFUSE,Diffuse);
348 	glMaterialdv(GL_FRONT_AND_BACK,GL_AMBIENT,Ambiant);
349 	glMaterialdv(GL_FRONT, GL_EMISSION, emission);
350 	glMateriali(GL_FRONT_AND_BACK,GL_SHININESS,100);
351 	Sphere_Draw_Precision(radius,position, numberOfSubdivisions);
352 }
353 /************************************************************************************************************/
Sphere_Draw(GLdouble radius,V3d position)354 void Sphere_Draw(GLdouble radius,V3d position)
355 {
356 	GLUquadricObj *obj;
357 	OpenGLOptions openGLOptions = get_opengl_options();
358 
359 	glPushMatrix();
360 	glTranslated(position[0],position[1],position[2]);
361     	obj = gluNewQuadric();
362 	gluQuadricNormals(obj, GL_SMOOTH);
363 	gluQuadricDrawStyle(obj, GLU_FILL);
364 	gluSphere(obj, radius, (GLint)openGLOptions.numberOfSubdivisionsSphere, (GLint)openGLOptions.numberOfSubdivisionsSphere);
365 
366 	gluDeleteQuadric(obj);
367 	glTranslated(-position[0],-position[1],-position[2]);
368 	glPopMatrix();
369 }
370 /************************************************************************************************************/
Sphere_Draw_Color(GLdouble radius,V3d position,V4d Specular,V4d Diffuse,V4d Ambiant)371 void Sphere_Draw_Color(GLdouble radius,V3d position, V4d Specular,V4d Diffuse,V4d Ambiant)
372 {
373 	static GLdouble emission[] = { 0.0, 0.0, 0.0, 1.0 };
374 	glMaterialdv(GL_FRONT_AND_BACK,GL_SPECULAR,Specular);
375 	glMaterialdv(GL_FRONT_AND_BACK,GL_DIFFUSE,Diffuse);
376 	glMaterialdv(GL_FRONT_AND_BACK,GL_AMBIENT,Ambiant);
377 	glMaterialdv(GL_FRONT, GL_EMISSION, emission);
378 	glMateriali(GL_FRONT_AND_BACK,GL_SHININESS,100);
379 	Sphere_Draw(radius,position);
380 
381 }
382 /************************************************************************************************************/
Sphere_Get_List(int i,GLdouble radius,V4d Specular,V4d Diffuse,V4d Ambiant)383 GLuint Sphere_Get_List(int i,GLdouble radius,V4d Specular,V4d Diffuse,V4d Ambiant)
384 {
385 
386 	GLuint sphere;
387 	V3d position={0,0,0};
388 
389 
390     sphere = glGenLists(i);
391 	glNewList(sphere, GL_COMPILE);
392 	Sphere_Draw_Color(radius,position,Specular,Diffuse,Ambiant);
393 	glEndList();
394 	return sphere;
395 }
396 /************************************************************************************************************/
Sphere_Draw_From_List(GLuint sphere,V3d position)397 void Sphere_Draw_From_List(GLuint sphere,V3d position)
398 {
399 	glPushMatrix();
400 	glTranslated(position[0],position[1],position[2]);
401 	if (glIsList(sphere) == GL_TRUE)
402 		glCallList(sphere);
403 	else
404 		printf("list error Sphere.c file\n");
405 
406 
407 	glPopMatrix();
408 }
409 /* Cylinder */
410 /************************************************************************/
rotated_vector(V3d v)411 void rotated_vector(V3d v)
412 {
413 	V3d vz={0.0,0.0,1.0};
414 	V3d	vert;
415 	gdouble angle;
416 
417 
418 	v3d_cross(vz,v,vert);
419 	angle = acos(v3d_dot(vz,v)/v3d_length(v))*RAD_TO_DEG;
420 
421 	if(fabs(angle)<1e-6)
422 		return;
423 	if(fabs(angle-180)<1e-6)
424 		glRotated(angle, 1.0, 0.0, 0.0);
425 	else
426 	glRotated(angle, vert[0],vert[1],vert[2]);
427 
428 }
429 /************************************************************************/
Cylinder_Draw(GLdouble radius,V3d Base1Pos,V3d Base2Pos)430 void Cylinder_Draw(GLdouble radius,V3d Base1Pos,V3d Base2Pos)
431 {
432 		V3d Direction;
433 		OpenGLOptions openGLOptions = get_opengl_options();
434 		GLUquadricObj *obj;
435 		glPushMatrix();
436 		glTranslated(Base1Pos[0],Base1Pos[1],Base1Pos[2]);
437 		Direction[0] = Base2Pos[0]-Base1Pos[0];
438 		Direction[1] = Base2Pos[1]-Base1Pos[1];
439 		Direction[2] = Base2Pos[2]-Base1Pos[2];
440 
441 		rotated_vector(Direction);
442 		obj = gluNewQuadric();
443 		gluQuadricNormals(obj, GL_SMOOTH);
444 		gluQuadricDrawStyle(obj, GLU_FILL);
445 		gluCylinder (obj,radius,radius,v3d_length(Direction),(GLint)openGLOptions.numberOfSubdivisionsCylindre,1);
446 		gluDeleteQuadric(obj);
447 		glPopMatrix();
448 }
449 
450 /************************************************************************/
Cylinder_Draw_Color(GLdouble radius,V3d Base1Pos,V3d Base2Pos,V4d Specular,V4d Diffuse,V4d Ambiant)451 void Cylinder_Draw_Color(GLdouble radius,V3d Base1Pos,V3d Base2Pos,
452 			 V4d Specular,V4d Diffuse,V4d Ambiant)
453 {
454 	glMaterialdv(GL_FRONT_AND_BACK,GL_SPECULAR,Specular);
455 	glMaterialdv(GL_FRONT_AND_BACK,GL_DIFFUSE,Diffuse);
456 	glMaterialdv(GL_FRONT_AND_BACK,GL_AMBIENT,Ambiant);
457 	glMateriali(GL_FRONT_AND_BACK,GL_SHININESS,50);
458 	Cylinder_Draw(radius,Base1Pos,Base2Pos);
459 }
460 /************************************************************************/
Cylinder_Draw_Color_Two(GLdouble radius,V3d Base1Pos,V3d Base2Pos,V4d Specular1,V4d Diffuse1,V4d Ambiant1,V4d Specular2,V4d Diffuse2,V4d Ambiant2,GLdouble p1,GLdouble p2)461 void Cylinder_Draw_Color_Two(GLdouble radius,V3d Base1Pos,V3d Base2Pos,
462 			 V4d Specular1,V4d Diffuse1,V4d Ambiant1,
463 			 V4d Specular2,V4d Diffuse2,V4d Ambiant2,
464 			GLdouble p1,GLdouble p2)
465 {
466 	V3d Center;
467 	GLdouble p = p1 + p2;
468 	Center[0] = (Base1Pos[0]*p2 + Base2Pos[0]*p1)/p;
469 	Center[1] = (Base1Pos[1]*p2 + Base2Pos[1]*p1)/p;
470 	Center[2] = (Base1Pos[2]*p2 + Base2Pos[2]*p1)/p;
471 	Cylinder_Draw_Color(radius,Base1Pos,Center,Specular1,Diffuse1,Ambiant1);
472 	Cylinder_Draw_Color(radius,Center,Base2Pos,Specular2,Diffuse2,Ambiant2);
473 }
474 /************************************************************************/
Prism_Draw(GLdouble radius,V3d Base1Pos,V3d Base2Pos)475 void Prism_Draw(GLdouble radius,V3d Base1Pos,V3d Base2Pos)
476 {
477 		V3d Direction;
478 		double lengt;
479 		GLUquadricObj *obj;
480 		glPushMatrix();
481 		glTranslated(Base1Pos[0],Base1Pos[1],Base1Pos[2]);
482 		Direction[0] = Base2Pos[0]-Base1Pos[0];
483 		Direction[1] = Base2Pos[1]-Base1Pos[1];
484 		Direction[2] = Base2Pos[2]-Base1Pos[2];
485 		lengt = v3d_length(Direction);
486 
487 		rotated_vector(Direction);
488 		obj = gluNewQuadric();
489 		gluQuadricNormals(obj, GL_SMOOTH);
490 		gluQuadricDrawStyle(obj, GLU_FILL);
491 		gluCylinder (obj,radius,radius/5,lengt,10,10);
492 		gluDeleteQuadric(obj);
493 		glPopMatrix();
494 }
495 
496 /************************************************************************/
Prism_Draw_Color(GLdouble radius,V3d Base1Pos,V3d Base2Pos,V4d Specular,V4d Diffuse,V4d Ambiant)497 void Prism_Draw_Color(GLdouble radius,V3d Base1Pos,V3d Base2Pos,
498 			 V4d Specular,V4d Diffuse,V4d Ambiant)
499 {
500 	glMaterialdv(GL_FRONT_AND_BACK,GL_SPECULAR,Specular);
501 	glMaterialdv(GL_FRONT_AND_BACK,GL_DIFFUSE,Diffuse);
502 	glMaterialdv(GL_FRONT_AND_BACK,GL_AMBIENT,Ambiant);
503 	glMateriali(GL_FRONT_AND_BACK,GL_SHININESS,50);
504 	Prism_Draw(radius,Base1Pos,Base2Pos);
505 }
506 /***************************************************************************************************************/
Draw_Arrow(V3d vector,GLdouble radius,V3d origin,V4d specular,V4d diffuse,V4d ambiant,gboolean negative)507 void Draw_Arrow(V3d vector, GLdouble radius,V3d origin, V4d specular,V4d diffuse,V4d ambiant, gboolean negative)
508 {
509 	V3d top;
510 	V3d bottom;
511 	V3d center;
512 	double lengt;
513 	gint i;
514 	V4d diffuseFleche;
515 	V4d ambiantFleche;
516 
517 	bottom[0] = origin[0];
518 	bottom[1] = origin[1];
519 	bottom[2] = origin[2];
520 	if(negative)
521 	{
522 		bottom[0] -= vector[0];
523 		bottom[1] -= vector[1];
524 		bottom[2] -= vector[2];
525 	}
526 
527 
528 	top[0] = origin[0] + vector[0];
529 	top[1] = origin[1] + vector[1];
530 	top[2] = origin[2] + vector[2];
531 
532 	lengt = v3d_length(vector);
533 
534 	if(radius<0.1) radius = 0.1;
535 
536 	if(negative) lengt *=2;
537 
538 	center[0] = top[0];
539 	center[1] = top[1];
540 	center[2] = top[2];
541 
542 	top[0] += (top[0]-bottom[0])/lengt*2*radius;
543 	top[1] += (top[1]-bottom[1])/lengt*2*radius;
544 	top[2] += (top[2]-bottom[2])/lengt*2*radius;
545 
546 
547 	Cylinder_Draw_Color(radius/2,bottom,center,specular,diffuse,ambiant);
548 	for(i=0;i<3;i++)
549 	{
550 		diffuseFleche[i] = diffuse[i] *0.6;
551 		ambiantFleche[i] = ambiant[i] *0.6;
552 	}
553 	diffuseFleche[3] = diffuse[3];
554 	ambiantFleche[3] = ambiant[3];
555 
556 	Prism_Draw_Color(radius/1.5,center,top,specular,diffuseFleche,ambiantFleche);
557 }
558 /*********************************************************************************************/
559 /* pango fonts for OpenGL  */
glInitFontsUsing(gchar * fontname,PangoContext ** pft2_context)560 void glInitFontsUsing(gchar* fontname, PangoContext* *pft2_context)
561 {
562 	OpenGLOptions openGlOptions = get_opengl_options();
563 	/*if (fontOffset >=0) return;*/
564 
565 	/* fprintf(stderr,"FontName = %s\n",fontname);*/
566 	fontOffset = glGenLists(256);
567 	if (fontOffset && openGlOptions.activateText==1)
568 	{
569 		//static PangoContext *ft2_context = NULL;
570 
571 		PangoFontDescription *pfd = pango_font_description_from_string(fontname);
572 		if (pfd)
573 		{
574 			glFontsize = pango_font_description_get_size(pfd) / PANGO_SCALE;
575 			/* fprintf(stderr,"PANGO_SCALE = %d\n",PANGO_SCALE);*/
576 			if(!*pft2_context) *pft2_context = pango_ft2_get_context (10*glFontsize, 10*glFontsize);
577 			pango_context_set_font_description (*pft2_context, pfd);
578 			charWidth = glFontsize / PANGO_SCALE;
579 			charHeight = glFontsize / PANGO_SCALE;
580 			pango_font_description_free(pfd);
581 		}
582 	}
583 }
584 /*********************************************************************************************/
gl_pango_ft2_render_layout(PangoLayout * layout)585 void gl_pango_ft2_render_layout (PangoLayout *layout)
586 {
587 	PangoRectangle logical_rect;
588 	FT_Bitmap bitmap;
589 	GLvoid *pixels;
590 	guint32 *p;
591 	GLfloat color[4];
592 	guint32 rgb;
593 	GLfloat a;
594 	guint8 *row, *row_end;
595 	int i;
596 
597 	pango_layout_get_extents (layout, NULL, &logical_rect);
598 	if (logical_rect.width == 0 || logical_rect.height == 0)
599   		return;
600 
601 	bitmap.rows = PANGO_PIXELS (logical_rect.height);
602 	bitmap.width = PANGO_PIXELS (logical_rect.width);
603 	bitmap.pitch = bitmap.width;
604 	bitmap.buffer = g_malloc (bitmap.rows * bitmap.width);
605 	bitmap.num_grays = 256;
606 	bitmap.pixel_mode = ft_pixel_mode_grays;
607 
608 	memset (bitmap.buffer, 0, bitmap.rows * bitmap.width);
609 	pango_ft2_render_layout (&bitmap, layout, PANGO_PIXELS (-logical_rect.x), 0);
610 
611 	pixels = g_malloc (bitmap.rows * bitmap.width * 4);
612 	p = (guint32 *) pixels;
613 
614 	glGetFloatv (GL_CURRENT_COLOR, color);
615 #if !defined(GL_VERSION_1_2) && G_BYTE_ORDER == G_LITTLE_ENDIAN
616 	  rgb =	((guint32) (color[0] * 255.0))        |
617         	(((guint32) (color[1] * 255.0)) << 8)  |
618         	(((guint32) (color[2] * 255.0)) << 16);
619 #else
620   	rgb = 	(((guint32) (color[0] * 255.0)) << 24) |
621         	(((guint32) (color[1] * 255.0)) << 16) |
622         	(((guint32) (color[2] * 255.0)) << 8);
623 #endif
624 	a = color[3];
625 
626 	row = bitmap.buffer + bitmap.rows * bitmap.width; /* past-the-end */
627 	row_end = bitmap.buffer;      /* beginning */
628 
629 	if (a == 1.0)
630 	{
631 		do
632         	{
633           		row -= bitmap.width;
634           		for (i = 0; i < bitmap.width; i++)
635 #if !defined(GL_VERSION_1_2) && G_BYTE_ORDER == G_LITTLE_ENDIAN
636             			*p++ = rgb | (((guint32) row[i]) << 24);
637 #else
638             			*p++ = rgb | ((guint32) row[i]);
639 #endif
640         	}
641       		while (row != row_end);
642     	}
643 	else
644     	{
645       		do
646         	{
647           		row -= bitmap.width;
648           		for (i = 0; i < bitmap.width; i++)
649 #if !defined(GL_VERSION_1_2) && G_BYTE_ORDER == G_LITTLE_ENDIAN
650             			*p++ = rgb | (((guint32) (a * row[i])) << 24);
651 #else
652             			*p++ = rgb | ((guint32) (a * row[i]));
653 #endif
654         	}while (row != row_end);
655 	}
656 
657 	glPixelStorei (GL_UNPACK_ALIGNMENT, 4);
658 
659 	glEnable (GL_BLEND);
660 	glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
661 
662 #if !defined(GL_VERSION_1_2)
663 	glDrawPixels (bitmap.width, bitmap.rows, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
664 #else
665 	glDrawPixels (bitmap.width, bitmap.rows, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, pixels);
666 #endif
667 	glDisable (GL_BLEND);
668 	g_free (bitmap.buffer);
669 	g_free (pixels);
670 }
671 /*********************************************************************************************/
672 /* print at a window position */
glPrintWin(gint x,gint y,gint height,gchar * str,PangoContext * ft2_context)673 void glPrintWin(gint x, gint y, gint height, gchar *str, PangoContext *ft2_context)
674 {
675 	PangoLayout *layout;
676 	PangoRectangle logical_rect;
677 
678 	/* Text layout */
679 	layout = pango_layout_new (ft2_context);
680 	pango_layout_set_width (layout, PANGO_SCALE * strlen(str)*height);
681 	pango_layout_set_alignment (layout, PANGO_ALIGN_CENTER);
682 
683 	gdouble w[3];
684 
685 	glGetWorldCoords(x, y, height, w);
686 	glRasterPos3f(w[0], w[1], w[2]);
687 	gl2psText(str, "Times-Roman", glFontsize);
688 
689 	pango_layout_set_text (layout, str, -1);
690 
691 
692 	glListBase(fontOffset);
693 	glCallLists(strlen(str), GL_UNSIGNED_BYTE, str);
694 
695 	gl_pango_ft2_render_layout (layout);
696 
697 	g_object_unref (G_OBJECT (layout));
698 }
699 /*********************************************************************************************/
glPrintOrtho(gdouble x,gdouble y,gdouble z,gchar * str,gboolean centerX,gboolean centerY,PangoContext * ft2_context)700 void glPrintOrtho(gdouble x, gdouble y, gdouble z, gchar *str, gboolean centerX, gboolean centerY, PangoContext *ft2_context)
701 {
702 	gdouble w[] = {x, y, z};
703 	gint xy[] = {0, 0};
704 	glGetIntegerv(GL_VIEWPORT, viewport);
705 	glGetWindowCoords(w, viewport[3], xy);
706 	glPushMatrix();
707 	glLoadIdentity();
708 
709 	glMatrixMode(GL_PROJECTION);
710 	glPushMatrix(); glLoadIdentity();
711 	gluOrtho2D(0, viewport[2], 0, viewport[3]);
712 
713 
714 	if(centerX) xy[0] -= charWidth*strlen(str)/2;
715 	if(centerY) xy[1] += charHeight/4;
716 	glPrintWin(xy[0], xy[1], viewport[3], str, ft2_context);
717 
718 	glPopMatrix();
719 	glMatrixMode(GL_MODELVIEW);
720 
721 	glPopMatrix();
722 }
723 /*********************************************************************************************/
724 /* print at a world position */
glPrint(gdouble x,gdouble y,gdouble z,gchar * str,PangoContext * ft2_context)725 void glPrint(gdouble x, gdouble y, gdouble z, gchar *str, PangoContext *ft2_context)
726 {
727 
728 	PangoLayout *layout;
729 	PangoRectangle logical_rect;
730 
731 	/* Text layout */
732 	layout = pango_layout_new (ft2_context);
733 	pango_layout_set_width (layout, PANGO_SCALE * strlen(str)*glFontsize);
734 	pango_layout_set_alignment (layout, PANGO_ALIGN_CENTER);
735 
736 	gdouble w[3];
737 
738 	glRasterPos3f(x,y,z);
739 	gl2psText(str, "Times-Roman", glFontsize);
740 
741 	pango_layout_set_text (layout, str, -1);
742 
743 
744 	glListBase(fontOffset);
745 	glCallLists(strlen(str), GL_UNSIGNED_BYTE, str);
746 
747 	gl_pango_ft2_render_layout (layout);
748 
749 	g_object_unref (G_OBJECT (layout));
750 }
751 /*********************************************************************************************/
752 /* print at a world position  after scaling of scal in scene coordinates*/
glPrintScale(gdouble x,gdouble y,gdouble z,gdouble scale,gchar * str,PangoContext * ft2_context)753 void glPrintScale(gdouble x, gdouble y, gdouble z, gdouble scale, gchar *str, PangoContext *ft2_context)
754 {
755 	gdouble VScene[]={0,0,0};
756 	gdouble VWorld[]={x,y,z};
757 	GLdouble mvMatrix[4][4];
758 	gdouble** trMatrix;
759 	gdouble** invMatrix;
760 	gint i;
761 	gint j;
762 	glGetDoublev(GL_MODELVIEW_MATRIX, &mvMatrix[0][0]);
763 	trMatrix = g_malloc(3*sizeof(gdouble*));
764 	for(i=0;i<3;i++) trMatrix[i] = g_malloc(3*sizeof(gdouble));
765 
766 	for(i=0;i<3;i++)
767 		for(j=0;j<3;j++)
768 			trMatrix[j][i] = mvMatrix[i][j];
769 
770 	for(i=0;i<3;i++)
771 	{
772 		VScene[i] = 0;
773 		for(j=0;j<3;j++)
774 		{
775 			VScene[i] += trMatrix[i][j]*VWorld[j];
776 		}
777 	}
778 	VScene[2] += scale;
779 
780 	invMatrix = Inverse3(trMatrix);
781 	for(i=0;i<3;i++) g_free(trMatrix[i]);
782 	g_free(trMatrix);
783 
784 	if(invMatrix != NULL)
785 	{
786 		for(i=0;i<3;i++)
787 		{
788 			VWorld[i] = 0;
789 			for(j=0;j<3;j++)
790 			{
791 				VWorld[i] += invMatrix[i][j]*VScene[j];
792 			}
793 		}
794 		for(i=0;i<3;i++) g_free(invMatrix[i]);
795 		g_free(invMatrix);
796 	}
797 	else
798 	{
799 		VWorld[0] = x;
800 		VWorld[1] = y;
801 		VWorld[2] = z;
802 	}
803 	glPrint(VWorld[0],VWorld[1],VWorld[2], str, ft2_context);
804 }
805