atomes 1.1.15
atomes: an atomic scale modeling tool box
Loading...
Searching...
No Matches
glview.c
Go to the documentation of this file.
1/* This file is part of the 'atomes' software
2
3'atomes' is free software: you can redistribute it and/or modify it under the terms
4of the GNU Affero General Public License as published by the Free Software Foundation,
5either version 3 of the License, or (at your option) any later version.
6
7'atomes' is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
8without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
9See the GNU General Public License for more details.
10
11You should have received a copy of the GNU Affero General Public License along with 'atomes'.
12If not, see <https://www.gnu.org/licenses/>
13
14Copyright (C) 2022-2024 by CNRS and University of Strasbourg */
15
22/*
23* This file: 'glview.c'
24*
25* Contains:
26*
27
28 - The callbacks of the OpenGL window
29
30*
31* List of functions:
32
33 GLuint * allocgluint (int val);
34 GLuint ** allocdgluint (int xal, int yal);
35
36 GLfloat ** allocdGLfloat (int xal, int yal);
37
38 gboolean is_GLExtension_Supported (const char * extension);
39
40 G_MODULE_EXPORT gboolean on_motion (GtkWidget * widg, GdkEvent * event, gpointer data);
41 G_MODULE_EXPORT gboolean on_lib_pressed (GtkWidget * widg, GdkEvent * event, gpointer data);
42 G_MODULE_EXPORT gboolean on_glwin_button_event (GtkWidget * widg, GdkEvent * event, gpointer data);
43 G_MODULE_EXPORT gboolean on_scrolled (GtkWidget * widg, GdkEvent * event, gpointer data);
44 G_MODULE_EXPORT gboolean on_glwin_pointer_scoll (GtkEventControllerScroll * event, gdouble dx, gdouble dy, gpointer data);
45 G_MODULE_EXPORT gboolean on_expose (GtkGLArea * area, GdkGLContext * context, gpointer data);
46 G_MODULE_EXPORT gboolean on_expose (GtkWidget * widg, cairo_t * cr, gpointer data);
47
48 void update_bonds_ (int * bd, int * stp,
49 int * bdim, int bda[* bdim], int bdb[* bdim],
50 double * x, double * y, double * z);
51 void sort (int dim, int * tab);
52 void update_atom_neighbors_ (int * stp, int * at, int * nv);
53 void update_this_neighbor_ (int * stp, int * at, int * iv, int * nv);
54 void update (glwin * view);
55 void transform (glwin * view, double aspect);
56 void reshape (glwin * view, int width, int height, gboolean use_ratio);
57 void save_rotation_quaternion (glwin * view);
58 void edit_for_motion (glwin * view);
59 void motion (glwin * view, gint x, gint y, GdkModifierType state);
60 void render_this_gl_window (glwin * view, GtkGLArea * area);
61 void render_this_gl_window (glwin * view, GtkWidget * widg, gint button);
62 void glwin_lib_pressed (double x, double y, guint event_type, int event_button, gpointer data);
63 void glwin_button_event (double event_x, double event_y, guint event_button, guint event_type, guint32 event_time, gpointer data);
64 void zoom (glwin * view, int delta);
65 void rotate_x_y (glwin * view, double angle_x, double angle_y);
66 void init_camera (project * this_proj, int get_depth);
67 void image_init_spec_data (image * img, project * this_proj, int nsp);
68 void set_img_lights (project * this_proj, image * img);
69 void init_img (project * this_proj);
70 void init_opengl ();
71 void center_molecule (project * this_proj);
72 void center_this_molecule (glwin * view);
73 void free_glwin_spec_data (project * this_proj, int spec);
74 void glwin_init_spec_data (project * this_proj, int nspec);
75 void init_glwin (glwin * view);
76 void gtk_window_change_gdk_visual (GtkWidget * win);
77
78 G_MODULE_EXPORT void on_glwin_pointer_motion (GtkEventControllerMotion * motc, gdouble x, gdouble y, gpointer data);
79 G_MODULE_EXPORT void on_lib_button_pressed (GtkGesture * gesture, int n_press, double x, double y, gpointer data);
80 G_MODULE_EXPORT void on_lib_button_released (GtkGesture * gesture, int n_press, double x, double y, gpointer data);
81 G_MODULE_EXPORT void on_glwin_button_pressed (GtkGesture * gesture, int n_press, double x, double y, gpointer data);
82 G_MODULE_EXPORT void on_glwin_button_released (GtkGesture * gesture, int n_press, double x, double y, gpointer data);
83 G_MODULE_EXPORT void on_realize (GtkGLArea * area, gpointer data);
84 G_MODULE_EXPORT void on_realize (GtkWidget * widg, gpointer data);
85
86 GError * init_gtk_gl_area (GtkGLArea * area);
87
88 ColRGBA set_default_color (int z);
89
90*/
91
92#include "global.h"
93#include "interface.h"
94#include "initcoord.h"
95#include "bind.h"
96#include "project.h"
97#include "glview.h"
98
101extern void process_the_hits (glwin * view, gint event_button, double ptx, double pty);
102extern void arc_ball_rotation (glwin * view, int x, int y);
103extern vec3_t get_arc_ball_vector (glwin * view, int x, int y);
104extern Light init_light_source (int type, float val, float vbl);
105extern void rotate_quat (project * this_proj, vec4_t q, int status, int axis);
106extern void translate (project * this_proj, int status, int axis, vec3_t trans);
107extern vec3_t get_bary (project * this_proj, int status);
108extern void update_labels (glwin * view);
109extern void prepare_atom_edition (gpointer data, gboolean visible);
110extern atom_search * allocate_atom_search (int proj, int action, int searchid, int tsize);
111extern int action_atoms_from_project (project * this_proj, atom_search * asearch, gboolean visible);
112extern atomic_object * create_object_from_frag_mol (project * this_proj, int coord, int geo, atom_search * remove);
113
115
123GLuint * allocgluint (int val)
124{
125 GLuint * var = NULL;
126
127 var = g_malloc0 (val*sizeof*var);
128 return var;
129}
130
139GLuint ** allocdgluint (int xal, int yal)
140{
141 GLuint ** var = NULL;
142 int i;
143
144 var = g_malloc (xal*sizeof*var);
145 for ( i = 0 ; i < xal ; i ++ )
146 {
147 /* allocation d'un tableau de tableau */
148 var[i] = allocgluint(yal);
149 }
150 return var;
151}
152
161GLfloat ** allocdGLfloat (int xal, int yal)
162{
163 GLfloat ** var = NULL;
164 int i;
165
166 var = g_malloc (xal*sizeof*var);
167 for ( i = 0 ; i < xal ; i ++ )
168 {
169 /* allocation d'un tableau de tableau */
170 var[i] = g_malloc0 (yal*sizeof*var[i]);
171 }
172 return var;
173}
174
175const float light_pos[MAX_LIGHTS][4] = {{-0.1f, 0.1f, 1.0f, 0.0f},
176 { 1.0f, 2.0f, 0.5f, 0.0f},
177 {-1.0f, 2.0f, -1.0f, 0.0f},
178 {-1.0f, -1.0f, 0.0f, 0.0f}};
179
188{
189 ColRGBA col;
190 double colors[116][3]={{1.00, 1.00, 1.00},
191 {0.85, 1.00, 1.00},
192 {0.80, 0.50, 1.00},
193 {0.76, 1.00, 0.00},
194 {1.00, 0.71, 0.71},
195 {0.56, 0.56, 0.56},
196 {0.18, 0.31, 0.97},
197 {1.00, 0.05, 0.05},
198 {0.56, 0.87, 0.31},
199 {0.70, 0.89, 0.96},
200 {0.67, 0.36, 0.95},
201 {0.54, 1.00, 0.00},
202 {0.75, 0.65, 0.65},
203 {0.94, 0.78, 0.62},
204 {1.00, 0.50, 0.00},
205 {1.00, 1.00, 0.19},
206 {0.12, 0.94, 0.12},
207 {0.50, 0.82, 0.89},
208 {0.56, 0.25, 0.83},
209 {0.24, 1.00, 0.00},
210 {0.90, 0.90, 0.90},
211 {0.75, 0.76, 0.78},
212 {0.65, 0.65, 0.67},
213 {0.54, 0.60, 0.78},
214 {0.61, 0.48, 0.78},
215 {0.87, 0.40, 0.20},
216 {0.94, 0.56, 0.62},
217 {0.31, 0.81, 0.31},
218 {0.78, 0.50, 0.20},
219 {0.49, 0.50, 0.69},
220 {0.76, 0.56, 0.56},
221 {0.40, 0.56, 0.56},
222 {0.74, 0.50, 0.89},
223 {1.00, 0.63, 0.00},
224 {0.65, 0.16, 0.16},
225 {0.36, 0.72, 0.82},
226 {0.44, 0.18, 0.69},
227 {0.00, 1.00, 0.00},
228 {0.58, 1.00, 1.00},
229 {0.58, 0.88, 0.88},
230 {0.45, 0.76, 0.79},
231 {0.33, 0.71, 0.71},
232 {0.23, 0.62, 0.62},
233 {0.14, 0.56, 0.56},
234 {0.04, 0.49, 0.55},
235 {0.00, 0.41, 0.52},
236 {0.75, 0.75, 0.75},
237 {1.00, 0.85, 0.56},
238 {0.65, 0.46, 0.45},
239 {0.40, 0.50, 0.50},
240 {0.62, 0.39, 0.71},
241 {0.83, 0.48, 0.00},
242 {0.58, 0.00, 0.58},
243 {0.26, 0.62, 0.69},
244 {0.34, 0.09, 0.56},
245 {0.00, 0.79, 0.00},
246 {0.44, 0.83, 1.00},
247 {1.00, 1.00, 0.78},
248 {0.85, 1.00, 0.78},
249 {0.78, 1.00, 0.78},
250 {0.64, 1.00, 0.78},
251 {0.56, 1.00, 0.78},
252 {0.38, 1.00, 0.78},
253 {0.27, 1.00, 0.78},
254 {0.19, 1.00, 0.78},
255 {0.12, 1.00, 0.78},
256 {0.00, 1.00, 0.61},
257 {0.00, 0.90, 0.46},
258 {0.00, 0.83, 0.32},
259 {0.00, 0.75, 0.22},
260 {0.00, 0.67, 0.14},
261 {0.30, 0.76, 1.00},
262 {0.30, 0.65, 1.00},
263 {0.13, 0.58, 0.84},
264 {0.15, 0.49, 0.67},
265 {0.15, 0.40, 0.59},
266 {0.09, 0.33, 0.53},
267 {0.81, 0.81, 0.87},
268 {1.00, 0.81, 0.13},
269 {0.72, 0.72, 0.81},
270 {0.65, 0.33, 0.30},
271 {0.34, 0.35, 0.38},
272 {0.62, 0.31, 0.71},
273 {0.67, 0.36, 0.00},
274 {0.46, 0.31, 0.27},
275 {0.26, 0.51, 0.59},
276 {0.26, 0.00, 0.40},
277 {0.00, 0.49, 0.00},
278 {0.44, 0.67, 0.98},
279 {0.00, 0.73, 1.00},
280 {0.00, 0.63, 1.00},
281 {0.00, 0.56, 1.00},
282 {0.00, 0.50, 1.00},
283 {0.00, 0.42, 1.00},
284 {0.33, 0.36, 0.95},
285 {0.54, 0.31, 0.89},
286 {0.63, 0.21, 0.83},
287 {0.70, 0.12, 0.83},
288 {0.70, 0.12, 0.73},
289 {0.70, 0.05, 0.65},
290 {0.74, 0.05, 0.53},
291 {0.78, 0.00, 0.40},
292 {0.80, 0.00, 0.35},
293 {0.82, 0.00, 0.31},
294 {0.85, 0.00, 0.27},
295 {0.88, 0.00, 0.22},
296 {0.90, 0.00, 0.18},
297 {0.92, 0.00, 0.15},
298 {0.93, 0.00, 0.14},
299 {0.94, 0.00, 0.13},
300 {0.95, 0.00, 0.12},
301 {0.96, 0.00, 0.10},
302 {0.97, 0.00, 0.10},
303 {0.98, 0.00, 0.10},
304 {0.99, 0.00, 0.10}};
305 // Dumy atoms have z < 1
306 int Z = (z < 1) ? 1 : z;
307 col.red = colors[Z-1][0];
308 col.green = colors[Z-1][1];
309 col.blue = colors[Z-1][2];
310 col.alpha = 1.0;
311 return col;
312}
313
330void update_bonds_ (int * bd, int * stp,
331 int * bdim, int bda[* bdim], int bdb[* bdim],
332 double * x, double * y, double * z)
333{
334 int i, j, k;
335
336 active_glwin -> allbonds[* bd] += * bdim;
337 active_glwin -> bonds[* stp][* bd] = * bdim;
338
339 if (* bdim > 0)
340 {
341 active_glwin -> bondid[* stp][* bd] = NULL;
342 active_glwin -> bondid[* stp][* bd] = allocdint (* bdim, 2);
343 for (i=0; i< * bdim; i++)
344 {
345 j = bda[i] - 1;
346 k = bdb[i] - 1;
347 active_glwin -> bondid[* stp][* bd][i][0] = j;
348 active_glwin -> bondid[* stp][* bd][i][1] = k;
349 }
350 if (* bd)
351 {
352 if (active_glwin -> clones[* stp] != NULL)
353 {
354 g_free (active_glwin -> clones[* stp]);
355 active_glwin -> clones[* stp] = NULL;
356 }
357 active_glwin -> clones[* stp] = g_malloc0 (*bdim*sizeof*active_glwin -> clones[* stp]);
358 for (i=0; i< * bdim; i++)
359 {
360 active_glwin -> clones[* stp][i].x = x[i];
361 active_glwin -> clones[* stp][i].y = y[i];
362 active_glwin -> clones[* stp][i].z = z[i];
363 j = bda[i] - 1;
364 k = bdb[i] - 1;
365 active_project -> atoms[* stp][j].cloned = TRUE;
366 active_project -> atoms[* stp][k].cloned = TRUE;
367 }
368 }
369 }
370}
371
380void sort (int dim, int * tab)
381{
382 int i, j, val;
383 for (i=1; i<dim; i++)
384 {
385 val = tab[i];
386 for (j=i-1; j>-1; j--)
387 {
388 if (tab[j] <= val) break;
389 tab[j+1] = tab[j];
390 }
391 tab[j+1]=val;
392 }
393}
394
404void update_atom_neighbors_ (int * stp, int * at, int * nv)
405{
406 active_project -> atoms[* stp][* at].numv = * nv;
407 if (* nv)
408 {
409 active_project -> atoms[* stp][* at].vois = allocint(* nv);
410 }
411}
412
423void update_this_neighbor_ (int * stp, int * at, int * iv, int * nv)
424{
425 active_project -> atoms[* stp][* at].vois[* iv] = * nv - 1;
426 if (* iv == active_project -> atoms[* stp][* at].numv - 1)
427 {
428 sort (active_project -> atoms[* stp][* at].numv, active_project -> atoms[* stp][* at].vois);
429 }
430}
431
439void update (glwin * view)
440{
441 gtk_gl_area_queue_render ((GtkGLArea *)view -> plot);
442#ifdef G_OS_WIN32
443#ifdef GTK3
444 hide_the_widgets (view -> plot);
445 show_the_widgets (view -> plot);
446#endif
447#endif
448}
449
458void transform (glwin * view, double aspect)
459{
460 GLdouble w, h;
461 GLdouble dw, dh;
462
463 double zoom = view -> anim -> last -> img -> zoom;
464 view -> zoom_factor = zoom * 0.1 * view -> anim -> last -> img -> p_depth / (2.0 * view -> anim -> last -> img -> gfar);
465 if (view -> anim -> last -> img -> rep == ORTHOGRAPHIC)
466 {
467 zoom *= (view -> anim -> last -> img -> p_depth / view -> anim -> last -> img -> gnear);
468 }
469 dw = view -> anim -> last -> img -> c_shift[0]*2.0*zoom;
470 dh = view -> anim -> last -> img -> c_shift[1]*2.0*zoom;
471 if (aspect > 1.0)
472 {
473 w = zoom * aspect;
474 h = zoom;
475 }
476 else
477 {
478 w = zoom;
479 h = zoom / aspect;
480 }
481 view -> anim -> last -> img -> gleft = -w + dw;
482 view -> anim -> last -> img -> gright = w + dw;
483 view -> anim -> last -> img -> gbottom = -h + dh;
484 view -> anim -> last -> img -> gtop = h + dh;
485
486 if (view -> anim -> last -> img -> rep == PERSPECTIVE)
487 {
488 view -> projection_matrix = m4_frustum (view -> anim -> last -> img -> gleft,
489 view -> anim -> last -> img -> gright,
490 view -> anim -> last -> img -> gbottom,
491 view -> anim -> last -> img -> gtop,
492 view -> anim -> last -> img -> gnear,
493 view -> anim -> last -> img -> gfar);
494 }
495 else
496 {
497 view -> projection_matrix = m4_ortho (view -> anim -> last -> img -> gleft,
498 view -> anim -> last -> img -> gright,
499 view -> anim -> last -> img -> gbottom,
500 view -> anim -> last -> img -> gtop,
501 -view -> anim -> last -> img -> gfar,
502 view -> anim -> last -> img -> gfar);
503 }
504}
505
516void reshape (glwin * view, int width, int height, gboolean use_ratio)
517{
518 double aspect;
519 int scale = 1.0;
520 if (use_ratio)
521 {
522 if (view -> win)
523 {
524 if (GTK_IS_WIDGET(view -> win))
525 {
526 scale = gtk_widget_get_scale_factor (view -> win);
527 }
528 }
529 }
530 glViewport (0, 0, (GLsizei) scale * width, (GLsizei) scale * height);
531 view -> view_port = vec4 (0.0, 0.0, width, height);
532 aspect = (double) width / (double) height;
533 transform (view, aspect);
534}
535
544{
545 //int i;
546 //for (i=0; i<4; i++) old_rotation_quaternion[i] = view -> anim -> last -> img -> rotation_quaternion[i];
547 old_rotation_quaternion.w = view -> anim -> last -> img -> rotation_quaternion.w;
548 old_rotation_quaternion.x = view -> anim -> last -> img -> rotation_quaternion.x;
549 old_rotation_quaternion.y = view -> anim -> last -> img -> rotation_quaternion.y;
550 old_rotation_quaternion.z = view -> anim -> last -> img -> rotation_quaternion.z;
551}
552
561{
562 gboolean check_edit = FALSE;
563 project * this_proj = get_project_by_id(view -> proj);
564 prepare_atom_edition (& view -> colorp[0][0], FALSE);
565 atom_search * move_search = allocate_atom_search (this_proj -> id, DISPL, DISPL, this_proj -> natomes);
566 int ** frag = allocdint (this_proj -> coord -> totcoord[2], 2);
567 int i, j, k;
568 j = 0;
569 for (i=0; i<this_proj -> natomes; i++)
570 {
571 k = this_proj -> atoms[0][i].coord[2];
572 frag[k][0] ++;
573 if (this_proj -> atoms[0][i].pick[0])
574 {
575 move_search -> todo[i] = 1;
576 frag[k][1] ++;
577 j ++;
578 }
579 }
580 if (j)
581 {
582 for (i=0; i<this_proj -> coord -> totcoord[2]; i++)
583 {
584 if (frag[i][1] && frag[i][1] < frag[i][0])
585 {
586 check_edit = TRUE;
587 break;
588 }
589 }
590 if (check_edit)
591 {
592 action_atoms_from_project (this_proj, move_search,
593 (this_proj -> modelgl -> atom_win) ? this_proj -> modelgl -> atom_win -> visible : FALSE);
594 }
595 else
596 {
597 atomic_object * object;
598 for (i=0; i<this_proj -> coord -> totcoord[2]; i++)
599 {
600 if (frag[i][1])
601 {
602 object_motion = TRUE;
603 object = create_object_from_frag_mol (this_proj, 2, i, NULL);
604 object_motion = FALSE;
605 g_free (object);
606 }
607 }
608 }
609 g_free (frag);
610 }
611 view -> baryc[1] = get_bary (this_proj, 1);
612 move_search = free_this_search_data (move_search);
613 view -> prepare_motion = FALSE;
614}
615
626void motion (glwin * view, gint x, gint y, GdkModifierType state)
627{
628 view -> mouseAction = MOTION;
629 int i;
630 if (view -> mode == EDITION && view -> prepare_motion && view -> rebuild[0][0]) edit_for_motion (view);
631
632 if (state & GDK_BUTTON1_MASK)
633 {
634 arc_ball_rotation (view, x, y);
635 }
636 else if (state & GDK_BUTTON2_MASK)
637 {
638 if (view -> mode != EDITION)
639 {
640 view -> anim -> last -> img -> c_shift[0] -= (double) (x - view -> mouseX) / view -> pixels[0];
641 view -> anim -> last -> img -> c_shift[1] += (double) (y - view -> mouseY) / view -> pixels[1];
642 for (i=0; i<2; i++)
643 {
644 if (view -> camera_widg[i+5])
645 {
646 if (GTK_IS_WIDGET(view -> camera_widg[i+5]))
647 {
648 gtk_spin_button_set_value ((GtkSpinButton *)view -> camera_widg[i+5], - view -> anim -> last -> img -> c_shift[i]);
649 }
650 }
651 }
652 }
653 else
654 {
655 vec3_t pos_a = vec3(x, - y, 0.75);
656 vec3_t pos_b = vec3(view -> mouseX, - view -> mouseY, 0.75);
657 vec3_t trans_a = v3_un_project (pos_a, view -> view_port, view -> projection_matrix);
658 vec3_t trans_b = v3_un_project (pos_b, view -> view_port, view -> projection_matrix);
659 vec3_t trans;
660 trans.x = (trans_a.x - trans_b.x);
661 trans.y = (trans_b.y - trans_a.y);
662 if (view -> anim -> last -> img -> rep == PERSPECTIVE)
663 {
664 trans.x *= view -> anim -> last -> img -> p_depth;
665 trans.y *= view -> anim -> last -> img -> p_depth;
666 }
667 trans.z = 0.0;
668 translate (get_project_by_id(view -> proj), 1, 1, trans);
669 }
670 }
671 if (view -> mode == EDITION)
672 {
674#ifdef GTK3
675 // GTK3 Menu Action To Check
677#endif
678 }
679 view -> mouseX = x;
680 view -> mouseY = y;
681 update (view);
682}
683
684#ifdef GTK3
694G_MODULE_EXPORT gboolean on_motion (GtkWidget * widg, GdkEvent * event, gpointer data)
695{
696 glwin * view = (glwin *) data;
697 if (view -> mouseStatus != RELEASED)
698 {
699 GdkEventMotion * mevent = (GdkEventMotion *)event;
700 gint x, y;
701 GdkModifierType state;
702 if (mevent -> is_hint)
703 {
704 gdk_window_get_device_position (mevent -> window, mevent -> device, & x, & y, & state);
705 }
706 else
707 {
708 x = (gint) mevent -> x;
709 y = (gint) mevent -> y;
710 state = (GdkModifierType) mevent -> state;
711 }
712 motion (view, x, y, state);
713 }
714 return FALSE;
715}
716#else
727G_MODULE_EXPORT void on_glwin_pointer_motion (GtkEventControllerMotion * motc, gdouble x, gdouble y, gpointer data)
728{
729 glwin * view = (glwin *) data;
730 if (view -> mouseStatus != RELEASED)
731 {
732 motion (view, (gint)x, (gint)y, gtk_event_controller_get_current_event_state((GtkEventController*)motc));
733 }
734}
735#endif
736
737#ifdef GTKGLAREA
746void render_this_gl_window (glwin * view, GtkGLArea * area)
747#else
757void render_this_gl_window (glwin * view, GtkWidget * widg, gint button)
758#endif
759{
760#ifdef GTKGLAREA
761 view -> pixels[0] = get_widget_width (GTK_WIDGET(area));
762 view -> pixels[1] = get_widget_height (GTK_WIDGET(area));
763 gtk_gl_area_make_current (area);
764 if (gtk_gl_area_get_error (area) == NULL)
765#else
766 view -> pixels[0] = get_widget_width (widg);
767 view -> pixels[1] = get_widget_height (widg);
768 GdkWindow * win = gtk_widget_get_window (widg);
769 if (glXMakeCurrent (GDK_WINDOW_XDISPLAY (win), GDK_WINDOW_XID (win), view -> glcontext))
770#endif
771 {
772 reshape (view, view -> pixels[0], view -> pixels[1], TRUE);
773 draw (view);
774 if (view -> to_pick)
775 {
776 if (view -> mouseButton) process_the_hits (view, view -> mouseButton, view -> mouseX, view -> mouseY);
777 view -> to_pick = FALSE;
778 reshape (view, view -> pixels[0], view -> pixels[1], TRUE);
779 draw (view);
780 }
781#ifdef GTKGLAREA
782 glFlush ();
783#else
784 glXSwapBuffers (GDK_WINDOW_XDISPLAY (win), GDK_WINDOW_XID (win));
785#endif
786 }
787}
788
800void glwin_lib_pressed (double x, double y, guint event_type, guint event_button, gpointer data)
801{
802 glwin * view = (glwin *) data;
803 switch (event_type)
804 {
805 case GDK_BUTTON_PRESS:
806 view -> mouseStatus = CLICKED;
807 view -> mouseX = x;
808 view -> mouseY = y;
809 view -> mouseButton = event_button;
810 if (event_button == 1)
811 {
813 arc_ball_init = get_arc_ball_vector (view, view -> mouseX, view -> mouseY);
814 view -> to_pick = FALSE;
815 update (view);
816 }
817 break;
818 case GDK_BUTTON_RELEASE:
819 view -> mouseStatus = RELEASED;
820 break;
821 }
822}
823
824#ifdef GTK3
834G_MODULE_EXPORT gboolean on_lib_pressed (GtkWidget * widg, GdkEvent * event, gpointer data)
835{
836 GdkEventButton * bevent = (GdkEventButton*)event;
837 glwin_lib_pressed (bevent -> x, bevent -> y, bevent -> type, bevent -> button, data);
838 return FALSE;
839}
840#else
852G_MODULE_EXPORT void on_lib_button_pressed (GtkGesture * gesture, int n_press, double x, double y, gpointer data)
853{
854 glwin_lib_pressed (x, y, GDK_BUTTON_PRESS, gtk_gesture_single_get_current_button ((GtkGestureSingle * )gesture), data);
855}
856
868G_MODULE_EXPORT void on_lib_button_released (GtkGesture * gesture, int n_press, double x, double y, gpointer data)
869{
870 glwin_lib_pressed (x, y, GDK_BUTTON_RELEASE, gtk_gesture_single_get_current_button ((GtkGestureSingle * )gesture), data);
871}
872#endif // GTK3
873
875
888void glwin_button_event (double event_x, double event_y, guint event_button, guint event_type, guint32 event_time, gpointer data)
889{
890 glwin * view = (glwin *) data;
891 switch (event_type)
892 {
893 case GDK_BUTTON_PRESS:
894 view -> mouseStatus = CLICKED;
895 view -> mouseX = event_x;
896 view -> mouseY = event_y;
897 view -> mouseButton = event_button;
898 clock_gettime (CLOCK_MONOTONIC, & start_time);
899 if (event_button == 1 || event_button == 3)
900 {
902 arc_ball_init = get_arc_ball_vector (view, view -> mouseX, view -> mouseY);
903 view -> nth_copy = 0;
904 view -> insert_coords = get_insertion_coordinates (view);
905#ifdef GTKGLAREA
906 view -> to_pick = TRUE;
907 update (view);
908#else
909 render_this_gl_window (view, plot, event_button);
910#endif
911 }
912 break;
913 case GDK_BUTTON_RELEASE:
914 view -> mouseStatus = RELEASED;
915 view -> mouseButton = 0;
916 clock_gettime (CLOCK_MONOTONIC, & stop_time);
918 {
919#ifdef GTKGLAREA
920 update (view);
921#else
922 render_this_gl_window (view, plot, event_button);
923#endif
924 }
925 if (view -> mode == EDITION && view -> mouseAction == MOTION)
926 {
927 view -> baryc[1] = get_bary (get_project_by_id(view -> proj), 1);
928 view -> mouseAction = ANALYZE;
929 }
930 if (event_button == 3)
931 {
932 switch (to_pop.action)
933 {
934 case 1:
936 break;
937 case 2:
939 break;
940 }
941 to_pop.action = 0;
942 }
943 break;
944 }
945}
946
947#ifdef GTK3
957G_MODULE_EXPORT gboolean on_glwin_button_event (GtkWidget * widg, GdkEvent * event, gpointer data)
958{
959 GdkEventButton * bevent = (GdkEventButton*)event;
960 glwin_button_event (bevent -> x, bevent -> y, bevent -> button, bevent -> type, bevent -> time, data);
961 return FALSE;
962}
963#else
975G_MODULE_EXPORT void on_glwin_button_pressed (GtkGesture * gesture, int n_press, double x, double y, gpointer data)
976{
977 glwin_button_event (x, y, gtk_gesture_single_get_current_button ((GtkGestureSingle * )gesture), GDK_BUTTON_PRESS, gtk_event_controller_get_current_event_time((GtkEventController *)gesture), data);
978}
979
991G_MODULE_EXPORT void on_glwin_button_released (GtkGesture * gesture, int n_press, double x, double y, gpointer data)
992{
993 glwin_button_event (x, y, gtk_gesture_single_get_current_button ((GtkGestureSingle * )gesture), GDK_BUTTON_RELEASE, gtk_event_controller_get_current_event_time((GtkEventController *)gesture), data);
994}
995#endif // GTK3
996
997// Zoom in or out
1006void zoom (glwin * view, int delta)
1007{
1008 view -> anim -> last -> img -> zoom += delta * view -> zoom_factor;
1009 if (view -> camera_widg[0])
1010 {
1011 if (GTK_IS_WIDGET(view -> camera_widg[0]))
1012 {
1013 gtk_spin_button_set_value ((GtkSpinButton *)view -> camera_widg[0], 1.0-0.5*view -> anim -> last -> img -> zoom);
1014 }
1015 }
1016}
1017
1018#ifdef GTK3
1028G_MODULE_EXPORT gboolean on_scrolled (GtkWidget * widg, GdkEvent * event, gpointer data)
1029{
1030 glwin * view = (glwin *) data;
1031 GdkEventScroll * sevent = (GdkEventScroll *)event;
1032 if (sevent -> direction == GDK_SCROLL_UP)
1033 {
1034 zoom (view, 1);
1035 }
1036 else if (sevent -> direction == GDK_SCROLL_DOWN && view -> anim -> last -> img -> zoom > ZOOM_MAX)
1037 {
1038 zoom (view, -1);
1039 }
1040 update_labels (view);
1041 update (view);
1042 return FALSE;
1043}
1044#else
1055G_MODULE_EXPORT gboolean on_glwin_pointer_scoll (GtkEventControllerScroll * event, gdouble dx, gdouble dy, gpointer data)
1056{
1057 glwin * view = (glwin *) data;
1058 if (dy < 0)
1059 {
1060 zoom (view, 1);
1061 }
1062 else if (dy > 0 && view -> anim -> last -> img -> zoom > ZOOM_MAX)
1063 {
1064 zoom (view, -1);
1065 }
1066 update_labels (view);
1067 update (view);
1068 return TRUE;
1069}
1070#endif
1071
1081void rotate_x_y (glwin * view, double angle_x, double angle_y)
1082{
1083 vec3_t axis;
1084 vec4_t q_a, q_b, q_c;
1085 axis.x = 0.0;
1086 axis.y = 1.0;
1087 axis.z = 0.0;
1088 q_a = axis_to_quat (axis, -pi*angle_y/180.0);
1089 axis.x = 1.0;
1090 axis.y = 0.0;
1091 axis.z = 0.0;
1092 q_b = axis_to_quat (axis, -pi*angle_x/180.0);
1093 q_c = q4_mul (q_a, q_b);
1094 view -> anim -> last -> img -> c_angle[0] -= angle_x;
1095 view -> anim -> last -> img -> c_angle[1] -= angle_y;
1096 int i;
1097 for (i=0; i<2; i++)
1098 {
1099 if (fabs(view -> anim -> last -> img -> c_angle[i]) > 180.0) view -> anim -> last -> img -> c_angle[i] = 0.0;
1100 if (view -> camera_widg[i+3])
1101 {
1102 if (GTK_IS_WIDGET(view -> camera_widg[i+3]))
1103 {
1104 gtk_spin_button_set_value ((GtkSpinButton *)view -> camera_widg[i+3], view -> anim -> last -> img -> c_angle[i]);
1105 }
1106 }
1107 }
1108
1109 if (view -> mode != EDITION)
1110 {
1111 view -> anim -> last -> img -> rotation_quaternion = q4_mul (old_rotation_quaternion, q_c);
1112 }
1113 else
1114 {
1115 rotate_quat (get_project_by_id(view -> proj), q_c, 1, 1);
1116 init_default_shaders (view);
1117#ifdef GTK3
1118 // GTK3 Menu Action To Check
1120#endif
1121 }
1122}
1123
1132void init_camera (project * this_proj, int get_depth)
1133{
1134 glwin * view = this_proj -> modelgl;
1135 if (get_depth) view -> anim -> last -> img -> p_depth = (this_proj -> natomes) ? oglmax_ () : 50.0;
1136 if (view -> camera_widg[1])
1137 {
1138 if (GTK_IS_WIDGET(view -> camera_widg[1]))
1139 {
1140 gtk_spin_button_set_value ((GtkSpinButton *)view -> camera_widg[1], view -> anim -> last -> img -> p_depth);
1141 }
1142 }
1143 view -> anim -> last -> img -> gnear = 6.0;//view -> anim -> last -> img -> p_depth/15.0;
1144 if (view -> camera_widg[2])
1145 {
1146 if (GTK_IS_WIDGET(view -> camera_widg[2]))
1147 {
1148 gtk_spin_button_set_value ((GtkSpinButton *)view -> camera_widg[2], view -> anim -> last -> img -> gnear);
1149 }
1150 }
1151 view -> anim -> last -> img -> gfar = view -> anim -> last -> img -> p_depth*2.0;
1152 view -> anim -> last -> img -> rotation_quaternion.w = 0.0;
1153 view -> anim -> last -> img -> rotation_quaternion.x = 0.0;
1154 view -> anim -> last -> img -> rotation_quaternion.y = 0.0;
1155 view -> anim -> last -> img -> rotation_quaternion.z = 1.0;
1156 int i;
1157 for (i=0; i<2; i++)
1158 {
1159 view -> anim -> last -> img -> c_shift[i] = 0.0;
1160 view -> anim -> last -> img -> c_angle[i] = 0.0;
1161 if (view -> camera_widg[i+5])
1162 {
1163 if (GTK_IS_WIDGET(view -> camera_widg[i+5]))
1164 {
1165 gtk_spin_button_set_value ((GtkSpinButton *)view -> camera_widg[i+5], view -> anim -> last -> img -> c_shift[i]);
1166 }
1167 }
1168 }
1171 view -> anim -> last -> img -> zoom = ZOOM;
1172 if (view -> camera_widg[0])
1173 {
1174 if (GTK_IS_WIDGET(view -> camera_widg[0]))
1175 {
1176 gtk_spin_button_set_value ((GtkSpinButton *)view -> camera_widg[0], 1.0 - 0.5*view -> anim -> last -> img -> zoom);
1177 }
1178 }
1179}
1180
1190void image_init_spec_data (image * img, project * this_proj, int nsp)
1191{
1192 int i, j;
1193 // Chemical species related
1194 for (i = 0; i<2; i++)
1195 {
1196 img -> show_label[i] = allocbool(nsp);
1197 img -> show_atom[i] = allocbool(nsp);
1198 for (j=0; j<nsp; j++) img -> show_atom[i][j] = TRUE;
1199 }
1200 img -> at_color = g_malloc0 (2*nsp*sizeof*img -> at_color);
1201 img -> sphererad = allocdouble (2*nsp);
1202 img -> pointrad = allocdouble (2*nsp);
1203 img -> atomicrad = allocdouble (2*nsp);
1204 img -> bondrad = allocddouble (2*nsp, 2*nsp);
1205 img -> linerad = allocddouble (2*nsp, 2*nsp);
1206 for (i = 0; i < nsp; i++)
1207 {
1208 img -> sphererad[i] = img -> sphererad[i+nsp] = this_proj -> chemistry -> chem_prop[CHEM_R][i]/2.0;
1209 img -> atomicrad[i] = img -> atomicrad[i+nsp] = this_proj -> chemistry -> chem_prop[CHEM_R][i];
1210 img -> pointrad[i] = img -> pointrad[i+nsp] = DEFAULT_SIZE;
1211 img -> at_color[i] = img -> at_color[i+nsp] = set_default_color ((int)this_proj -> chemistry -> chem_prop[CHEM_Z][i]);
1212 img -> linerad[i][i] = img -> linerad[i+nsp][i+nsp] = DEFAULT_SIZE;
1213 img -> bondrad[i][i] = img -> bondrad[i+nsp][i+nsp] = min(1.0, img -> sphererad[i]/2.0);
1214 }
1215 for (i=0; i < nsp-1; i++)
1216 {
1217 for (j=i+1; j < nsp; j++)
1218 {
1219 img -> linerad[i][j] = img -> linerad[j][i]
1220 = img -> linerad[i+nsp][j+nsp]
1221 = img -> linerad[j+nsp][i+nsp] = DEFAULT_SIZE;
1222 img -> bondrad[i][j] = min(1.0, img -> sphererad[i]/2.0);
1223 img -> bondrad[i][j] = min(img -> bondrad[i][j], img -> sphererad[j]/2.0);
1224 img -> bondrad[j][i] = img -> bondrad[i+nsp][j+nsp]
1225 = img -> bondrad[j+nsp][i+nsp]
1226 = img -> bondrad[i][j];
1227 }
1228 }
1229 for (i=0; i<10; i++)
1230 {
1231 img -> spcolor[i] = NULL;
1232 if (i < 2)
1233 {
1234 img -> spcolor[i] = g_malloc (nsp*sizeof*img -> spcolor[i]);
1235 }
1236 else
1237 {
1238 img -> spcolor[i] = g_malloc (1*sizeof*img -> spcolor[i]);
1239 img -> spcolor[i][0] = NULL;
1240 }
1241 }
1242}
1243
1252void set_img_lights (project * this_proj, image * img)
1253{
1254 img -> lights = 3;
1255 if (img -> l_ght) g_free (img -> l_ght);
1256 img -> l_ght = g_malloc0 (3*sizeof*img -> l_ght);
1257 float val;
1258 if (this_proj -> cell.box)
1259 {
1260 val = (this_proj -> cell.box[0].param[0][0] == 0.0) ? img -> p_depth : this_proj -> cell.box[0].param[0][0];
1261 }
1262 else
1263 {
1264 val = img -> p_depth;
1265 }
1266 float vbl = img -> p_depth;
1267 img -> l_ght[0] = init_light_source (0, val, vbl);
1268 img -> l_ght[1] = init_light_source (1, val, vbl);
1269 img -> l_ght[2] = init_light_source (1, val, vbl);
1270}
1271
1279void init_img (project * this_proj)
1280{
1281 int i;
1282 this_proj -> modelgl -> anim -> last -> img = g_malloc0(sizeof*this_proj -> modelgl -> anim -> last -> img);
1283 image * img = this_proj -> modelgl -> anim -> last -> img;
1284 img -> backcolor.red = 0.0;
1285 img -> backcolor.green = 0.0;
1286 img -> backcolor.blue = 0.0;
1287 img -> backcolor.alpha = 1.0;
1288 img -> box_color.red = 0.0;
1289 img -> box_color.green = 1.0;
1290 img -> box_color.blue = 0.0;
1291 img -> box_color.alpha = 1.0;
1292 img -> color_map[0] = 0;
1293 img -> color_map[1] = 0;
1294 img -> box_axis_rad[BOX] = 0.05;
1295 img -> box_axis_line[BOX] = DEFAULT_SIZE;
1296 img -> axispos = BOTTOM_RIGHT;
1297 img -> box_axis_rad[AXIS] = 0.1;
1298 img -> box_axis_line[AXIS] = DEFAULT_SIZE;
1299 img -> axis_length = 2.0*DEFAULT_SIZE;
1300 img -> axis_color = NULL;
1301 img -> axis_pos[0] = 50.0;
1302 img -> axis_pos[1] = 50.0;
1303 img -> axis_pos[2] = 0.0;
1304 img -> axis_labels = 1;
1305 img -> filled_type = NONE;
1306 img -> quality = QUALITY;
1307 img -> render = FILL;
1308 img -> rep = PERSPECTIVE;
1309
1310 // Visual styles
1311 img -> style = (this_proj -> natomes <= 1000) ? BALL_AND_STICK : DEFAULT_STYLE;
1312 img -> box_axis[AXIS] = NONE; // (this_proj -> natomes <= 1000) ? CYLINDERS : DEFAULT_STYLE;
1313 if (this_proj -> cell.pbc)
1314 {
1315 img -> box_axis[BOX] = (this_proj -> natomes <= 1000) ? CYLINDERS : DEFAULT_STYLE;
1316 }
1317 else
1318 {
1319 img -> box_axis[BOX] = NONE;
1320 }
1321
1322 for (i=0; i<5; i++)
1323 {
1324 img -> labels_position[i] = 1;
1325 img -> labels_render[i] = BETTER_TEXT;
1326 if (i < 2) img -> labels_format[i] = SYMBOL_AND_NUM;
1327 img -> labels_font[i] = g_strdup_printf ("Sans Bold 12");
1328 }
1329 img -> mtilt = TRUE;
1330 img -> mfactor = 1;
1331 img -> mwidth = 1.0;
1332 for (i=0; i<2; i++)
1333 {
1334 img -> labels_font[3+i] = g_strdup_printf ("Courier New Bold 18");
1335 img -> labels_color[3+i] = g_malloc (sizeof*img -> labels_color[3]);
1336 img -> labels_color[3+i][0].red = 1.0;
1337 img -> labels_color[3+i][0].green = 1.0;
1338 img -> labels_color[3+i][0].blue = 1.0;
1339 img -> labels_color[3+i][0].alpha = 1.0;
1340 img -> selected[i] = g_malloc0 (sizeof*img -> selected[i]);
1341 }
1342 img -> axis_title[0] = g_strdup_printf ("x");
1343 img -> axis_title[1] = g_strdup_printf ("y");
1344 img -> axis_title[2] = g_strdup_printf ("z");
1345
1346 img -> radall[0] = img -> radall[1] = 0.1;
1347
1348 if (this_proj -> nspec) image_init_spec_data (img, this_proj, this_proj -> nspec);
1349 this_proj -> modelgl -> p_moy = img -> p_depth = (this_proj -> natomes) ? oglmax_ () : 50.0;
1350 set_img_lights (this_proj, img);
1351 img -> m_terial.predefine = 4;
1352 img -> m_terial.albedo = vec3(0.5, 0.5, 0.5);
1353 img -> m_terial.param[0] = DEFAULT_LIGHTNING;
1354 img -> m_terial.param[1] = DEFAULT_METALLIC;
1355 img -> m_terial.param[2] = DEFAULT_ROUGHNESS;
1356 img -> m_terial.param[3] = DEFAULT_AMBIANT_OCCLUSION;
1357 img -> m_terial.param[4] = DEFAULT_GAMMA_CORRECTION;
1358 img -> m_terial.param[5] = DEFAULT_OPACITY;
1359
1360 img -> f_g.density = 0.005;
1361 img -> f_g.depth[0] = 1.0;
1362 img -> f_g.depth[1] = 90.0;
1363 img -> f_g.color = vec3 (0.01f, 0.01f, 0.01f);
1364}
1365
1373gboolean is_GLExtension_Supported (const char * extension)
1374{
1375 int i, j;
1376 i = j = 0;
1377 glGetIntegerv (GL_NUM_EXTENSIONS, & i);
1378 for (j=0; j<i; j++)
1379 {
1380 if (g_strcmp0 (extension, (const char*)glGetStringi(GL_EXTENSIONS, j)) == 0) return TRUE;
1381 }
1382 return FALSE;
1383}
1384
1391{
1392 glEnable (GL_DEPTH_TEST);
1393 glDepthMask (GL_TRUE);
1394 glDepthFunc (GL_LEQUAL);
1395 glDepthRange (0.0f, 1.0f);
1396 glClearDepth (1.0f);
1397 glEnable (GL_NORMALIZE);
1398
1399 glShadeModel(GL_SMOOTH);
1400 glCullFace(GL_BACK);
1401 glEnable (GL_CULL_FACE); // Incompatible with polyhedra viz
1402
1403 glEnable (GL_COLOR_SUM_EXT);
1404
1405 glEnable (GL_PROGRAM_POINT_SIZE);
1406 glEnable (GL_VERTEX_PROGRAM_POINT_SIZE);
1407 glEnable (GL_POINT_SPRITE);
1408
1409 glEnable (GL_POINT_SMOOTH);
1410 glHint (GL_POINT_SMOOTH_HINT, GL_NICEST);
1411 glEnable (GL_LINE_SMOOTH); // Lines antialiasing
1412 glHint (GL_LINE_SMOOTH_HINT, GL_NICEST);
1413
1414 glDisable (GL_POLYGON_SMOOTH); // To disable ploygon antialiasing
1415 glEnable (GL_POLYGON_STIPPLE);
1416 glEnable (GL_POLYGON_OFFSET_FILL);
1417
1418 glEnable (GL_BLEND);
1419 glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1420
1421 glPolygonOffset (1.0, 1.0);
1422 glPolygonMode (GL_FRONT_AND_BACK, GL_FILL);
1423
1424 glHint (GL_FOG_HINT, GL_NICEST);
1425 glHint (GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Really Nice Perspective Calculations
1426
1427#ifdef OSX
1429#else
1430 ogl_texture = (is_GLExtension_Supported("GL_ARB_texture_rectangle")) ? GL_TEXTURE_RECTANGLE_ARB : GL_TEXTURE_2D;
1431#endif // OSX
1432}
1433
1441void center_molecule (project * this_proj)
1442{
1443 int l, i, j;
1444 double x, y, z;
1445 // We need to center the molecule at (0.0, 0.0, 0.0)
1446 // Thus (0, 0, 0) will be the center of the box
1447 mat4_t rot;
1448 vec3_t bini, bend;
1449 for (l = 0; l < this_proj -> steps; l++)
1450 {
1451 x = 0.0;
1452 y = 0.0;
1453 z = 0.0;
1454 for (i = 0; i < this_proj -> natomes; i++)
1455 {
1456 x += this_proj -> atoms[l][i].x;
1457 y += this_proj -> atoms[l][i].y;
1458 z += this_proj -> atoms[l][i].z;
1459 }
1460 x /= this_proj -> natomes;
1461 y /= this_proj -> natomes;
1462 z /= this_proj -> natomes;
1463 for (i = 0; i < this_proj -> natomes; i++)
1464 {
1465 this_proj -> atoms[l][i].x -= x;
1466 this_proj -> atoms[l][i].y -= y;
1467 this_proj -> atoms[l][i].z -= z;
1468 }
1469
1470 for (i=0; i<FILLED_STYLES; i++)
1471 {
1472 if (this_proj -> modelgl -> volume_box[i])
1473 {
1474 if (this_proj -> modelgl -> volume_box[i][l])
1475 {
1476 this_proj -> modelgl -> volume_box[i][l][6] -= x;
1477 this_proj -> modelgl -> volume_box[i][l][7] -= y;
1478 this_proj -> modelgl -> volume_box[i][l][8] -= z;
1479 rot = m4_rotation_anti_xyz (this_proj -> modelgl -> volume_box[i][l][3], this_proj -> modelgl -> volume_box[i][l][4], this_proj -> modelgl -> volume_box[i][l][5]);
1480 bini = m4_mul_coord (rot, vec3(this_proj -> modelgl -> volume_box[i][l][6], this_proj -> modelgl -> volume_box[i][l][7], this_proj -> modelgl -> volume_box[i][l][8]));
1481 bini.x -= x;
1482 bini.y -= y;
1483 bini.z -= z;
1484 rot = m4_rotation_xyz (this_proj -> modelgl -> volume_box[i][l][3], this_proj -> modelgl -> volume_box[i][l][4], this_proj -> modelgl -> volume_box[i][l][5]);
1485 bend = m4_mul_coord (rot, bini);
1486 this_proj -> modelgl -> volume_box[i][l][6] = bend.x;
1487 this_proj -> modelgl -> volume_box[i][l][7] = bend.y;
1488 this_proj -> modelgl -> volume_box[i][l][8] = bend.z;
1489 }
1490 }
1491 if (this_proj -> modelgl -> frag_box[i])
1492 {
1493 if (this_proj -> modelgl -> frag_box[i][l])
1494 {
1495 for (j=0; j<this_proj -> coord -> totcoord[2]; j++)
1496 {
1497 rot = m4_rotation_anti_xyz (this_proj -> modelgl -> frag_box[i][l][j][3], this_proj -> modelgl -> frag_box[i][l][j][4], this_proj -> modelgl -> frag_box[i][l][j][5]);
1498 bini = m4_mul_coord (rot, vec3(this_proj -> modelgl -> frag_box[i][l][j][6], this_proj -> modelgl -> frag_box[i][l][j][7], this_proj -> modelgl -> frag_box[i][l][j][8]));
1499 bini.x -= x;
1500 bini.y -= y;
1501 bini.z -= z;
1502 rot = m4_rotation_xyz (this_proj -> modelgl -> frag_box[i][l][j][3], this_proj -> modelgl -> frag_box[i][l][j][4], this_proj -> modelgl -> frag_box[i][l][j][5]);
1503 bend = m4_mul_coord (rot, bini);
1504 this_proj -> modelgl -> frag_box[i][l][j][6] = bend.x;
1505 this_proj -> modelgl -> frag_box[i][l][j][7] = bend.y;
1506 this_proj -> modelgl -> frag_box[i][l][j][8] = bend.z;
1507 }
1508 }
1509 }
1510 }
1511 }
1512 this_proj -> cell.crystal = FALSE;
1513}
1514
1523{
1525 view -> insert_coords = vec3(0.0, 0.0, 0.0);
1526 int shaders[6] = {ATOMS, BONDS, POLYS, RINGS, SELEC, VOLMS};
1528 view -> create_shaders[PICKS] = TRUE;
1529 view -> create_shaders[MDBOX] = TRUE;
1530 view -> create_shaders[LABEL] = TRUE;
1531 view -> create_shaders[MEASU] = TRUE;
1532 update (view);
1533}
1534
1543void free_glwin_spec_data (project * this_proj, int spec)
1544{
1545 int i, j, k;
1546
1547 for (i=0; i<NUM_COLORS; i++)
1548 {
1549 if (this_proj -> modelgl -> colorp[i] != NULL)
1550 {
1551 g_free (this_proj -> modelgl -> colorp[i]);
1552 this_proj -> modelgl -> colorp[i] = NULL;
1553 }
1554 }
1555 for (i=0; i<10; i++)
1556 {
1557 k = (i > 2) ? 1 : spec;
1558 if (i < 2 || i > 3)
1559 {
1560 for (j=0; j<k; j++)
1561 {
1562 if (this_proj -> coord -> geolist[i][j] != NULL)
1563 {
1564 g_free (this_proj -> coord -> geolist[i][j]);
1565 this_proj -> coord -> geolist[i][j] = NULL;
1566 }
1567 }
1568 this_proj -> coord -> geolist[i]=NULL;
1569 }
1570 }
1571 for (i=0; i<spec; i++)
1572 {
1573 if (this_proj -> coord -> partial_geo[i] != NULL) g_free (this_proj -> coord -> partial_geo[i]);
1574 this_proj -> coord -> partial_geo[i] = NULL;
1575 }
1576 g_free (this_proj -> coord -> partial_geo);
1577 this_proj -> coord -> partial_geo = NULL;
1578}
1579
1588void glwin_init_spec_data (project * this_proj, int nspec)
1589{
1590 int i, j, k;
1591 for (i=0; i<NUM_COLORS; i++)
1592 {
1593 this_proj -> modelgl -> colorp[i] = NULL;
1594 this_proj -> modelgl -> colorp[i] = g_malloc (nspec*2*sizeof*this_proj -> modelgl -> colorp[i]);
1595 for (j=0; j<nspec*2; j++)
1596 {
1597 this_proj -> modelgl -> colorp[i][j].a = this_proj -> id;
1598 this_proj -> modelgl -> colorp[i][j].b = i;
1599 this_proj -> modelgl -> colorp[i][j].c = j;
1600 }
1601 }
1602#ifdef GTK3
1603 // GTK3 Menu Action To Check
1604 int l;
1605#endif
1606 for (i=0; i<10; i++)
1607 {
1608 k = (i > 2) ? 1 : nspec;
1609#ifdef GTK3
1610 // GTK3 Menu Action To Check
1611 for (j = 0; j < 2; j++)
1612 {
1613 this_proj -> modelgl -> oglmv[j][i] = NULL;
1614 this_proj -> modelgl -> oglmv[j][i] = g_malloc0 (k*sizeof*this_proj -> modelgl -> oglmv[j][i]);
1615 if (i < 9)
1616 {
1617 this_proj -> modelgl -> oglmc[j][i] = NULL;
1618 this_proj -> modelgl -> oglmc[j][i] = g_malloc0 (k*sizeof*this_proj -> modelgl -> oglmc[j][i]);
1619 }
1620 if (i < 2 || (i > 3 && i < 9))
1621 {
1622 this_proj -> modelgl -> oglmpv[j][i] = NULL;
1623 this_proj -> modelgl -> oglmpv[j][i] = g_malloc0 (k*sizeof*this_proj -> modelgl -> oglmpv[j][i]);
1624 }
1625 for (l=0; l<k; l++)
1626 {
1627 this_proj -> modelgl -> oglmv[j][i][l] = NULL;
1628 if (i < 9) this_proj -> modelgl -> oglmc[j][i][l] = NULL;
1629 if (i < 2 || (i > 3 && i < 9)) this_proj -> modelgl -> oglmpv[j][i][l] = NULL;
1630 }
1631 }
1632#endif
1633 if (i < 2 || i > 3)
1634 {
1635 this_proj -> coord -> geolist[i] = g_malloc0 (k*sizeof*this_proj -> coord -> geolist[i]);
1636 for (j=0; j<k; j++)
1637 {
1638 this_proj -> coord -> geolist[i][j] = NULL;
1639 }
1640 }
1641 }
1642 this_proj -> coord -> partial_geo = g_malloc0 (nspec*sizeof*this_proj -> coord -> partial_geo);
1643 for (i=0; i<nspec; i++) this_proj -> coord -> partial_geo[i] = NULL;
1644}
1645
1653void init_glwin (glwin * view)
1654{
1655 project * this_proj = get_project_by_id(view -> proj); // Have to be the active project
1656 view -> anim = g_malloc0 (sizeof*view -> anim);
1657 snapshot * snap = g_malloc0 (sizeof*snap);
1658 view -> anim -> first = snap;
1659 view -> anim -> last = snap;
1660 init_img (this_proj);
1661 init_camera (this_proj, FALSE);
1662 view -> mouseStatus = RELEASED;
1663 view -> mouseAction = ANALYZE;
1664 // Warning, if not centered at start-up, dmtx could fail
1665 if (! this_proj -> cell.crystal) center_molecule (this_proj);
1666
1667 view -> bonds = allocdint (this_proj -> steps, 2);
1668 view -> bondid = g_malloc0 (this_proj -> steps*sizeof*view -> bondid);
1669 view -> clones = g_malloc0 (this_proj -> steps*sizeof*view -> clones);
1670 int i;
1671 for (i=0; i < this_proj -> steps; i++)
1672 {
1673 view -> bondid[i] = g_malloc0 (2*sizeof*view -> bondid[i]);
1674 view -> clones[i] = NULL;
1675 }
1676
1677 // Data that depends on the number of chemical species
1678 glwin_init_spec_data (this_proj, (this_proj -> nspec) ? this_proj -> nspec : 1);
1679
1680 view -> stop = TRUE;
1681 view -> speed = 100;
1682 view -> zoom_factor = ZOOM_FACTOR;
1683 view -> mode = ANALYZE;
1684 view -> selection_mode = ATOMS;
1685 // On normal motion and copy rebuild:
1686 view -> rebuild[0][0] = view -> rebuild[1][0] = (this_proj -> steps > 1) ? FALSE : TRUE;
1687 view -> init = TRUE;
1688 init_opengl ();
1689 init_shaders (view);
1690 this_proj -> initgl = TRUE;
1691#ifdef GTK4
1692 if (view -> win) update_menu_bar (view);
1693#endif
1694}
1695
1703GError * init_gtk_gl_area (GtkGLArea * area)
1704{
1705 if (area == NULL)
1706 {
1707 area = (GtkGLArea *)gtk_gl_area_new ();
1708 }
1709 else
1710 {
1711 gtk_gl_area_make_current (area);
1712 }
1713 gtk_gl_area_set_has_depth_buffer (area, TRUE);
1714 gtk_gl_area_set_has_stencil_buffer (area, TRUE);
1715 return gtk_gl_area_get_error (area);
1716}
1717
1718#ifdef GTKGLAREA
1727G_MODULE_EXPORT void on_realize (GtkGLArea * area, gpointer data)
1728#else
1737G_MODULE_EXPORT void on_realize (GtkWidget * widg, gpointer data)
1738#endif
1739{
1740 glwin * view = (glwin *)data;
1741 GError * err;
1742#ifdef GTKGLAREA
1743 err = init_gtk_gl_area (area);
1744 if (err == NULL)
1745 {
1746#else
1747 GdkWindow * xwin = gtk_widget_get_window (view -> plot);
1748 GLint attr_list[] = {GLX_DOUBLEBUFFER,
1749 GLX_RGBA,
1750 GLX_DEPTH_SIZE, 1,
1751 GLX_RED_SIZE, 1,
1752 GLX_GREEN_SIZE, 1,
1753 GLX_BLUE_SIZE, 1,
1754 None};
1755 XVisualInfo * visualinfo = glXChooseVisual (GDK_WINDOW_XDISPLAY (xwin),
1756 gdk_screen_get_number (gdk_window_get_screen (xwin)), attr_list);
1757 view -> glcontext = glXCreateContext (GDK_WINDOW_XDISPLAY (xwin), visualinfo, NULL, TRUE);
1758 g_free (visualinfo);
1759 if (glXMakeCurrent (GDK_WINDOW_XDISPLAY (xwin), GDK_WINDOW_XID (xwin), view -> glcontext))
1760 {
1761#endif
1762 init_glwin (view);
1763 }
1764 else
1765 {
1766 gchar * errm = g_strdup_printf ("Impossible to initialize the OpenGL 3D rendering ! \n %s\n", err -> message);
1767 g_error_free (err);
1768 show_error (errm, 0, MainWindow);
1769 g_free (errm);
1770 }
1771}
1772
1773#ifdef GTKGLAREA
1783G_MODULE_EXPORT gboolean on_expose (GtkGLArea * area, GdkGLContext * context, gpointer data)
1784#else
1794G_MODULE_EXPORT gboolean on_expose (GtkWidget * widg, cairo_t * cr, gpointer data)
1795#endif
1796{
1797 glwin * view = (glwin *) data;
1798#ifdef GTK3
1799 GdkEvent * event = gtk_get_current_event ();
1800 if (event && event -> type == GDK_EXPOSE && ((GdkEventExpose *)event) -> count > 0) return TRUE;
1801#endif
1802#ifdef GTKGLAREA
1803 render_this_gl_window (view, area);
1804#else
1805 render_this_gl_window (view, widg, 0);
1806#endif
1807 return TRUE;
1808}
Binding to the Fortran90 subroutines.
double oglmax_()
gchar * axis[3]
Definition w_axis.c:65
integer(kind=c_int) function chemistry()
Definition chemistry.F90:22
color colorp[64]
double scale(double axe)
find appropriate major tick spacing based on axis length
Definition curve.c:204
ColRGBA col
Definition d_measures.c:77
int atoms[NUM_STYLES][2]
int bonds[NUM_STYLES][2]
float val
Definition dlp_init.c:117
void init_default_shaders(glwin *view)
re-initialize the default OpenGL shaders
dint rep
Definition dlp_edit.c:2239
int ** allocdint(int xal, int yal)
allocate an int ** pointer
Definition global.c:330
double pi
Definition global.c:195
struct timespec start_time
Definition global.c:191
gboolean * allocbool(int val)
allocate a gboolean * pointer
Definition global.c:254
double ** allocddouble(int xal, int yal)
allocate a double ** pointer
Definition global.c:475
double * allocdouble(int val)
allocate a double * pointer
Definition global.c:459
GtkWidget * MainWindow
Definition global.c:201
int * allocint(int val)
allocate an int * pointer
Definition global.c:314
gboolean object_motion
Definition global.c:186
double get_calc_time(struct timespec start, struct timespec stop)
get calculation time in s
Definition global.c:643
struct timespec stop_time
Definition global.c:192
Global variable declarations Global convenience function declarations Global data structure defin...
glwin * active_glwin
Definition project.c:53
project * proj
int get_widget_height(GtkWidget *widg)
retrive GtkWidget height
Definition gtk-misc.c:2419
int get_widget_width(GtkWidget *widg)
retrive GtkWidget width
Definition gtk-misc.c:2403
#define CHEM_R
Definition global.h:299
#define min(a, b)
Definition global.h:81
void hide_the_widgets(GtkWidget *widg)
hide GtkWidget
Definition gtk-misc.c:189
#define CHEM_Z
Definition global.h:297
project * active_project
Definition project.c:47
void show_the_widgets(GtkWidget *widg)
show GtkWidget
Definition gtk-misc.c:173
project * get_project_by_id(int p)
get project pointer using id number
Definition project.c:120
void transform(glwin *view, double aspect)
transform the OpenGL window
Definition glview.c:458
void update_bonds_(int *bd, int *stp, int *bdim, int bda[*bdim], int bdb[*bdim], double *x, double *y, double *z)
update bonding information from Fortran90
Definition glview.c:330
int action_atoms_from_project(project *this_proj, atom_search *asearch, gboolean visible)
apply atom edition action to project (motion, remove, replace, insert, random move)
vec3_t get_arc_ball_vector(glwin *view, int x, int y)
Definition arcball.c:46
G_MODULE_EXPORT void on_realize(GtkWidget *widg, gpointer data)
realize event for the OpenGL widget
Definition glview.c:1737
atomic_object * create_object_from_frag_mol(project *this_proj, int coord, int geo, atom_search *remove)
create object from a fragment or a molecule
G_MODULE_EXPORT void on_glwin_pointer_motion(GtkEventControllerMotion *motc, gdouble x, gdouble y, gpointer data)
handle mouse motion event in the OpenGL window GTK4
Definition glview.c:727
GLfloat ** allocdGLfloat(int xal, int yal)
allocate a GLfloat ** pointer
Definition glview.c:161
void render_this_gl_window(glwin *view, GtkWidget *widg, gint button)
render the OpenGL window
Definition glview.c:757
GLuint ** allocdgluint(int xal, int yal)
allocate a GLuint ** pointer
Definition glview.c:139
void glwin_button_event(double event_x, double event_y, guint event_button, guint event_type, guint32 event_time, gpointer data)
handle mouse button event on the OpenGL window
Definition glview.c:888
vec4_t old_rotation_quaternion
Definition arcball.c:44
void update_labels(glwin *view)
update labels (on representation data update)
Definition m_rep.c:83
void arc_ball_rotation(glwin *view, int x, int y)
Perform arcball rotation.
Definition arcball.c:73
void save_rotation_quaternion(glwin *view)
save the rotation quaternion of the last image
Definition glview.c:543
void update(glwin *view)
update the rendering of the OpenGL window
Definition glview.c:439
vec3_t arc_ball_init
Definition arcball.c:42
void prepare_atom_edition(gpointer data, gboolean visible)
prepare atom edition
Definition atom_edit.c:459
GError * init_gtk_gl_area(GtkGLArea *area)
initialize a GtkGLArea, return error if any
Definition glview.c:1703
void init_img(project *this_proj)
initialize an image data structure
Definition glview.c:1279
GLenum ogl_texture
Definition glview.c:114
Light init_light_source(int type, float val, float vbl)
initialize a light source
Definition w_advance.c:324
void free_glwin_spec_data(project *this_proj, int spec)
free the memory used by the chemical species related data in a glwin data structure
Definition glview.c:1543
gl_pop_info to_pop
Definition glview.c:874
void glwin_init_spec_data(project *this_proj, int nspec)
initialize the glwin chemical species related pointers
Definition glview.c:1588
G_MODULE_EXPORT void on_lib_button_released(GtkGesture *gesture, int n_press, double x, double y, gpointer data)
handle mouse button release event on the molecular library OpenGL window (limited interaction)
Definition glview.c:868
void zoom(glwin *view, int delta)
zoom in or zoom out in the OpenGL window
Definition glview.c:1006
atom_search * allocate_atom_search(int proj, int action, int searchid, int tsize)
allocate atom search data structure
Definition atom_edit.c:392
G_MODULE_EXPORT void on_lib_button_pressed(GtkGesture *gesture, int n_press, double x, double y, gpointer data)
handle mouse button press event on the molecular library OpenGL window (limited interaction)
Definition glview.c:852
void translate(project *this_proj, int status, int axis, vec3_t trans)
translate
Definition atom_move.c:230
G_MODULE_EXPORT void on_glwin_button_released(GtkGesture *gesture, int n_press, double x, double y, gpointer data)
mouse button released signal on the OpenGL window
Definition glview.c:991
void update_atom_neighbors_(int *stp, int *at, int *nv)
update an atom number of neighbors from Fortran90
Definition glview.c:404
void glwin_lib_pressed(double x, double y, guint event_type, guint event_button, gpointer data)
handle mouse button event on the molecular library OpenGL window
Definition glview.c:800
void reshape(glwin *view, int width, int height, gboolean use_ratio)
reshape (resize) the OpenGL window
Definition glview.c:516
void init_camera(project *this_proj, int get_depth)
intialize the OpenGL camera settings
Definition glview.c:1132
void init_opengl()
initialize OpenGL rendering parameters
Definition glview.c:1390
G_MODULE_EXPORT void on_glwin_button_pressed(GtkGesture *gesture, int n_press, double x, double y, gpointer data)
mouse button pressed signal on the OpenGL window
Definition glview.c:975
void edit_for_motion(glwin *view)
if edition mode is one some steps are required
Definition glview.c:560
void rotate_quat(project *this_proj, vec4_t q, int status, int axis)
rotate using quaternion
Definition atom_move.c:269
void process_the_hits(glwin *view, gint event_button, double ptx, double pty)
process a mouse click
Definition selection.c:590
void motion(glwin *view, gint x, gint y, GdkModifierType state)
mouse motion in the OpenGL window
Definition glview.c:626
ColRGBA set_default_color(int z)
pick the default color for an atom
Definition glview.c:187
void set_img_lights(project *this_proj, image *img)
initialize lightning for an image data structure
Definition glview.c:1252
GLuint * allocgluint(int val)
allocate a GLuint * pointer
Definition glview.c:123
vec3_t get_bary(project *this_proj, int status)
get barycenter of atomic coordinates
Definition atom_move.c:170
const float light_pos[MAX_LIGHTS][4]
Definition glview.c:175
void center_this_molecule(glwin *view)
center atomic coordinates around (0,0,0) and refresh shaders
Definition glview.c:1522
void rotate_x_y(glwin *view, double angle_x, double angle_y)
rotate the OpenGL camera
Definition glview.c:1081
void init_glwin(glwin *view)
initialize a glwin pointer
Definition glview.c:1653
void center_molecule(project *this_proj)
center atomic coordinates around (0,0,0)
Definition glview.c:1441
gboolean is_GLExtension_Supported(const char *extension)
test if this GLExtension is support
Definition glview.c:1373
G_MODULE_EXPORT gboolean on_glwin_pointer_scoll(GtkEventControllerScroll *event, gdouble dx, gdouble dy, gpointer data)
handle mouse scroll event on the OpenGL window
Definition glview.c:1055
G_MODULE_EXPORT gboolean on_expose(GtkWidget *widg, cairo_t *cr, gpointer data)
OpenGL rendering widget expose event callback GTK3.
Definition glview.c:1794
void update_this_neighbor_(int *stp, int *at, int *iv, int *nv)
update atom neighbor list from Fortran90
Definition glview.c:423
void image_init_spec_data(image *img, project *this_proj, int nsp)
initialize the chemical species related pointers in an image data structure
Definition glview.c:1190
void sort(int dim, int *tab)
sort, nim to max, a table by integer value
Definition glview.c:380
Variable declarations related to the OpenGL window Function declarations related to the OpenGL wind...
image * plot
Definition ogl_draw.c:66
@ BETTER_TEXT
Definition glview.h:234
#define DEFAULT_LIGHTNING
Default OpenGL material ligthning.
Definition glview.h:49
atom_search * free_this_search_data(atom_search *this_search)
free atom search data structure
Definition popup.c:285
#define ZOOM_MAX
Maximum value for the OpenGL zoom factor.
Definition glview.h:125
#define DEFAULT_AMBIANT_OCCLUSION
Default OpenGL material ambiant occlusion.
Definition glview.h:64
@ BOTTOM_RIGHT
Definition glview.h:207
#define DEFAULT_GAMMA_CORRECTION
Default OpenGL material gamma correction.
Definition glview.h:69
#define ZOOM_FACTOR
Default value for the OpenGL zoom factor.
Definition glview.h:120
void draw(glwin *view)
main drawing subroutine for the OpenGL window
Definition ogl_draw.c:461
void re_create_md_shaders(int nshaders, int shaders[nshaders], project *this_proj)
re-initialize some MD dependent OpenGL shaders
action
Definition glview.h:189
gboolean pick
void popup_main_menu(glwin *view, double ptx, double pty)
create the OpenGL window popup menu
Definition popup.c:4131
void init_shaders(glwin *view)
initialize all the OpenGL shaders
#define CAMERA_ANGLE_X
Default value for the OpenGL camera pitch in °
Definition glview.h:100
#define MAX_LIGHTS
Maximum number of light sources.
Definition glview.h:80
@ MOTION
Definition glview.h:159
@ ANALYZE
Definition glview.h:157
@ EDITION
Definition glview.h:158
#define DEFAULT_OPACITY
Default OpenGL opacity.
Definition glview.h:74
render
Definition glview.h:182
@ FILL
Definition glview.h:183
void popup_selection(glwin *view, double ptx, double pty, int se, int pe, int ai, int bi, int ac)
create the contextual popup menu, right click on atom or bond
Definition popup.c:3666
@ PERSPECTIVE
Definition glview.h:152
@ ORTHOGRAPHIC
Definition glview.h:151
@ RELEASED
Definition glview.h:200
@ CLICKED
Definition glview.h:199
#define DEFAULT_SIZE
Definition glview.h:45
#define DEFAULT_ROUGHNESS
Default OpenGL material roughness.
Definition glview.h:59
#define DEFAULT_METALLIC
Default OpenGL material metallic.
Definition glview.h:54
@ SYMBOL_AND_NUM
Definition glview.h:217
#define ZOOM
Default value for the OpenGL zoom.
Definition glview.h:115
#define DEFAULT_STYLE
Default OpenGL style: ball and stick.
Definition glview.h:43
vec3_t get_insertion_coordinates(glwin *view)
get the insertion coordinates to insert object in the 3D window
Definition glwindow.c:794
#define QUALITY
Default OpenGL quality (number of polygons)
Definition glview.h:39
#define CAMERA_ANGLE_Y
Default value for the OpenGL camera heading in °
Definition glview.h:105
@ DISPL
Definition glview.h:224
@ BALL_AND_STICK
Definition glview.h:173
@ CYLINDERS
Definition glview.h:177
@ NONE
Definition glview.h:172
#define FILLED_STYLES
Definition glwin.h:105
#define BOX
Definition glwin.h:54
#define NUM_COLORS
Definition glwin.h:65
#define AXIS
Definition glwin.h:55
shaders
The different types of shaders in the atomes program.
Definition glwin.h:88
@ BONDS
Definition glwin.h:90
@ PICKS
Definition glwin.h:97
@ LABEL
Definition glwin.h:98
@ VOLMS
Definition glwin.h:102
@ POLYS
Definition glwin.h:92
@ SELEC
Definition glwin.h:91
@ MDBOX
Definition glwin.h:93
@ MEASU
Definition glwin.h:99
@ ATOMS
Definition glwin.h:89
@ RINGS
Definition glwin.h:96
#define GL_COLOR_SUM_EXT
Definition glwin.h:39
#define GL_TEXTURE_RECTANGLE_ARB
Definition glwin.h:51
GLXContext glcontext
GdkWindow * xwin
Function declarations to handle the atomic coordination data.
void set_advanced_bonding_menus(glwin *view)
void show_error(char *error, int val, GtkWidget *win)
show error message
Definition interface.c:293
Messaging function declarations.
double z
Definition ogl_draw.c:57
double y
Definition ogl_draw.c:57
double x
Definition ogl_draw.c:57
Function declarations for reading atomes project file Function declarations for saving atomes proje...
float blue
Definition global.h:124
float alpha
Definition global.h:125
float red
Definition global.h:122
float green
Definition global.h:123
Definition glwin.h:181
int action
Definition glview.h:369
double y
Definition glview.h:370
int pts[5]
Definition glview.h:371
double x
Definition glview.h:370
Definition glwin.h:875
Definition glwin.h:277
coord_info * coord
Definition global.h:956
float y
Definition math_3d.h:130
float x
Definition math_3d.h:130
float z
Definition math_3d.h:130
float w
Definition math_3d.h:149
float y
Definition math_3d.h:149
float x
Definition math_3d.h:149
float z
Definition math_3d.h:149
GtkWidget * axis_title
Definition tab-4.c:101
int status
Definition w_advance.c:160
GtkWidget * img
Definition workspace.c:70