atomes 1.1.14
atomes: an atomic scale modeling tool box
Loading...
Searching...
No Matches
d_axis.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: 'd_axis.c'
24*
25* Contains:
26*
27
28 - The functions to prepare the OpenGL rendering for the axis
29
30*
31* List of functions:
32
33 int create_axis_lists ();
34
35 void setup_arrow (float * vert, vec3_t a, vec3_t b, vec3_t c, vec3_t d, vec3_t e);
36 void init_axis_param ();
37 void prepare_axis_data (float * vert_a, float * vert_b, float * vert_c);
38
39 ColRGBA color_axis (int id);
40
41 mat4_t create_axis_matrices (int type);
42
43*/
44
45#include "global.h"
46#include "glview.h"
47
48extern object_3d * draw_sphere (int quality);
49extern object_3d * draw_cylinder (int quality, float ra, float rb);
50extern object_3d * draw_cylinder_cap (int quality, float rad, gboolean picked);
51extern void setup_line_vertice (float * vertices, vec3_t pos, ColRGBA col, float alpha);
52extern void setup_sphere_vertice (float * vertices, vec3_t pos, ColRGBA col, float rad, float alpha);
53extern void setup_cylinder_vertice (float * vertices, vec3_t pos_a, vec3_t pos_b, ColRGBA col, float rad, float alpha, float delta);
54extern void setup_cap_vertice (float * vertices, vec3_t pos_a, vec3_t pos_b, ColRGBA col, float rad, float alpha);
55extern void setup_triangles (float * vertices, vec3_t sa, vec3_t sb, vec3_t sc);
56extern float poly_alpha;
57extern vec3_t centroid;
58
59extern void clean_labels (int id);
60
61extern ColRGBA pcol;
62extern float extra;
63
69
78{
79 if (plot -> axis_color != NULL)
80 {
81 return plot -> axis_color[id];
82 }
83 else
84 {
86 col.red = (id == 2) ? 1.0: 0.0;
87 col.green = (id == 1) ? 1.0: 0.0;
88 col.blue = (id == 0) ? 1.0: 0.0;
89 col.alpha = 1.0;
90 return col;
91 }
92}
93
102{
103 GLfloat x, y, z;
104 float from_edge = 50.0;
105 vec3_t win_coord;
106 GLfloat sx = (GLfloat)wingl -> pixels[0];
107 GLfloat sy = (GLfloat)wingl -> pixels[1];
108 mat4_t axis_projection_matrix;
109 mat4_t axis_model_matrix;
110 mat4_t axis_view_matrix;
111 mat4_t axis_model_view_matrix;
112 if (plot -> axispos == CENTER)
113 {
114 x = y = 0.0;
115 if (plot -> rep == ORTHOGRAPHIC)
116 {
117 x = 0.5 * sx;
118 y = 0.5 * sy;
119 }
120 axis_projection_matrix = wingl -> proj_model_matrix;
121 }
122 else
123 {
124 axis_projection_matrix = m4_ortho (plot -> gleft, plot -> gright, plot -> gbottom, plot -> gtop, -plot -> gfar, plot -> gfar);
125 if (plot -> axispos == TOP_RIGHT)
126 {
127 x = sx - from_edge;
128 y = from_edge;
129 }
130 else if (plot -> axispos == TOP_LEFT)
131 {
132 x = from_edge;
133 y = from_edge;
134 }
135 else if (plot -> axispos == BOTTOM_RIGHT)
136 {
137 x = sx - from_edge;
138 y = sy - from_edge;
139 }
140 else if (plot -> axispos == BOTTOM_LEFT)
141 {
142 x = from_edge;
143 y = sy - from_edge;
144 }
145 else if (plot -> axispos == CUSTOM)
146 {
147 // Custom position
148 x = plot -> axis_pos[0] * sx / 100.0;
149 y = plot -> axis_pos[1] * sy / 100.0;
150 }
151 }
152 z = 0.0;
153 win_coord = v3_un_project (vec3(x, y, z), wingl -> view_port, axis_projection_matrix);
154 win_coord.z = 1.0;
155 //print_axis_matrices (win_coord);
156 if (type == 0)
157 {
158 axis_view_matrix = wingl -> view_matrix;
159 }
160 else
161 {
162 axis_view_matrix = wingl -> model_matrix;
163 }
164 axis_model_matrix = m4_translation (win_coord);
165 axis_model_view_matrix = m4_mul (axis_model_matrix, axis_view_matrix);
166 return m4_mul (axis_projection_matrix, axis_model_view_matrix);
167}
168
181void setup_arrow (float * vert, vec3_t a, vec3_t b, vec3_t c, vec3_t d, vec3_t e)
182{
183 setup_triangles (vert, a, b, c);
184 setup_triangles (vert, a, c, d);
185 setup_triangles (vert, a, d, e);
186 setup_triangles (vert, a, e, b);
187 setup_triangles (vert, b, c, d);
188 setup_triangles (vert, b, d, e);
189}
190
197{
198 arrow_length = 0.5;
199 axis_size = plot -> axis_length;
200 axis_radius = plot -> box_axis_rad[AXIS];
201 arrow_base = 0.1;
202 label_pos = 0.2;
203 if (plot -> rep == PERSPECTIVE && plot -> axispos != CENTER)
204 {
205 arrow_base /= (plot -> p_depth / plot -> gnear);
206 arrow_length /= (plot -> p_depth / plot -> gnear);
207 axis_radius /= (plot -> p_depth / plot -> gnear);
208 axis_size /= (plot -> p_depth / plot -> gnear);
209 label_pos /= (plot -> p_depth / plot -> gnear);
210 }
211}
212
222void prepare_axis_data (float * vert_a, float * vert_b, float * vert_c)
223{
225 poly_alpha = 1.0;
226
227 vec3_t a, b, c, d, e;
228 float sa;
229
230 nbs = nbl = nba = 0;
231 int i;
232 for (i=0; i<3; i++)
233 {
234 pcol = color_axis (i);
235 a = vec3(0.0, 0.0, 0.0);
236 b = vec3((i==0)? axis_size-arrow_length : 0.0, (i==1)? axis_size-arrow_length : 0.0, (i==2)? axis_size-arrow_length : 0.0);
237 if (plot -> box_axis[AXIS] == WIREFRAME)
238 {
239 setup_line_vertice (vert_a, a, pcol, 1.0);
240 setup_line_vertice (vert_a, b, pcol, 1.0);
241 a = vec3((i==0)? axis_size : 0.0, (i==1)? axis_size : 0.0, (i==2)? axis_size : 0.0);
242 sa = (i == 2) ? -1.0 : 1.0;
247 centroid = v3_add (a, v3_add(b, v3_add(c, v3_add(d, e))));
248 centroid = v3_divs (centroid, 5.0);
249 setup_arrow (vert_b, a, b, c, d, e);
250 }
251 else
252 {
253 setup_cylinder_vertice (vert_a, a, b, pcol, axis_radius, 1.0, 0.0);
254 c = vec3((i==0)? axis_size : 0.0, (i==1)? axis_size : 0.0, (i==2)? axis_size : 0.0);
255 nbs --;
256 setup_cylinder_vertice (vert_b, c, b, pcol, axis_radius+arrow_base, 1.0, 0.0);
257 nbs --;
259 }
260 }
261}
262
269{
270 vec3_t pos;
271 float shift[3]={0.0, 0.0, 0.0};
272 int nshaders = 0;
273 object_3d * axis_a, * axis_b, * axis_c, * axis_d;
274
276 wingl -> create_shaders[MAXIS] = FALSE;
277 if (plot -> box_axis[AXIS] == NONE) return nshaders;
278
279 if (plot -> box_axis[AXIS] == WIREFRAME)
280 {
281 axis_a = g_malloc0 (sizeof*axis_a);
282 axis_a -> vert_buffer_size = LINE_BUFF_SIZE;
283 axis_a -> num_vertices = 3*2;
284 axis_a -> vertices = allocfloat (axis_a -> vert_buffer_size*axis_a -> num_vertices);
285 axis_b = g_malloc0 (sizeof*axis_b);
286 axis_b -> vert_buffer_size = POLY_BUFF_SIZE;
287 axis_b -> num_vertices = 3*6*9;
288 axis_b -> vertices = allocfloat (axis_b -> vert_buffer_size*axis_b -> num_vertices);
289 }
290 else
291 {
292 axis_a = draw_cylinder (plot -> quality, 1.0, 1.0);
293 axis_a -> num_instances = 3;
294 axis_a -> inst_buffer_size = CYLI_BUFF_SIZE;
295 axis_a -> instances = allocfloat (axis_a -> num_instances*CYLI_BUFF_SIZE);
296 axis_b = draw_cylinder (plot -> quality, 0.0, 1.0);
297 axis_b -> num_instances = 3;
298 axis_b -> inst_buffer_size = CYLI_BUFF_SIZE;
299 axis_b -> instances = allocfloat (axis_b -> num_instances*CYLI_BUFF_SIZE);
300 axis_c = draw_cylinder_cap (plot -> quality, 1.0, FALSE);
301 axis_c -> num_instances = 3;
302 axis_c -> inst_buffer_size = CAPS_BUFF_SIZE;
303 axis_c -> instances = allocfloat (CAPS_BUFF_SIZE*axis_c -> num_instances);
304 axis_d = draw_sphere (plot -> quality);
305 axis_d -> num_instances = 1;
306 axis_d -> inst_buffer_size = ATOM_BUFF_SIZE;
307 axis_d -> instances = allocfloat (ATOM_BUFF_SIZE);
308 }
309 nshaders = (plot -> box_axis[AXIS] == WIREFRAME) ? 2 : 4;
310 prepare_axis_data ((plot -> box_axis[AXIS] == WIREFRAME) ? axis_a -> vertices : axis_a -> instances,
311 (plot -> box_axis[AXIS] == WIREFRAME) ? axis_b -> vertices : axis_b -> instances,
312 (plot -> box_axis[AXIS] == WIREFRAME) ? NULL : axis_c -> instances);
313 clean_labels (2);
314 if (plot -> axis_labels)
315 {
316 int i;
317 for (i=0; i<3; i++)
318 {
319 pos = vec3 ((i==0) ? axis_size+label_pos : 0.0, (i==1) ? axis_size+label_pos : 0.0, (i==2) ? axis_size+label_pos : 0.0);
320 prepare_string (plot -> axis_title[i], 2, color_axis (i), pos, shift, NULL, NULL, NULL);
321 }
322 nshaders += (plot -> labels_render[2]+1) * (plot -> labels_list[2] -> last -> id + 1);
323 wingl -> ogl_glsl[MAXIS][0] = g_malloc0 (nshaders*sizeof*wingl -> ogl_glsl[MAXIS][0]);
325 }
326 else
327 {
328 wingl -> ogl_glsl[MAXIS][0] = g_malloc0 (nshaders*sizeof*wingl -> ogl_glsl[MAXIS][0]);
329 }
330 if (plot -> box_axis[AXIS] == WIREFRAME)
331 {
332 // Lines
333 wingl -> ogl_glsl[MAXIS][0][0] = init_shader_program (MAXIS, GLSL_LINES, line_vertex, NULL, line_color, GL_LINES, 2, 1, FALSE, axis_a);
334 wingl -> ogl_glsl[MAXIS][0][0] -> line_width = plot -> box_axis_line[AXIS];
335 // Arrows
336 wingl -> ogl_glsl[MAXIS][0][1] = init_shader_program (MAXIS, GLSL_POLYEDRA, full_vertex, NULL, full_color, GL_TRIANGLES, 3, 1, TRUE, axis_b);
337 }
338 else
339 {
340 // Yellow sphere at (0.0, 0.0, 0.0)
341 pcol.red = pcol.green = 1.0;
342 pcol.blue = 0.0;
343 pcol.alpha = 1.0;
344 setup_sphere_vertice (axis_d -> instances, vec3(0.0,0.0,0.0), pcol, axis_radius, 1.0);
345 wingl -> ogl_glsl[MAXIS][0][0] = init_shader_program (MAXIS, GLSL_SPHERES, sphere_vertex, NULL, full_color, GL_TRIANGLE_STRIP, 4, 1, TRUE, axis_d);
346 // Cylinders
347 wingl -> ogl_glsl[MAXIS][0][1] = init_shader_program (MAXIS, GLSL_CYLINDERS, cylinder_vertex, NULL, full_color, GL_TRIANGLE_STRIP, 6, 1, TRUE, axis_a);
348 // Cones
349 wingl -> ogl_glsl[MAXIS][0][2] = init_shader_program (MAXIS, GLSL_CYLINDERS, cone_vertex, NULL, full_color, GL_TRIANGLE_STRIP, 6, 1, TRUE, axis_b);
350 // Cones Caps
351 wingl -> ogl_glsl[MAXIS][0][3] = init_shader_program (MAXIS, GLSL_CAPS, cap_vertex, NULL, full_color, GL_TRIANGLE_FAN, 5, 1, TRUE, axis_c);
352 g_free (axis_c);
353 g_free (axis_d);
354 }
355 g_free (axis_a);
356 g_free (axis_b);
357 return nshaders;
358}
int nba
Definition d_atoms.c:176
int nbs
Definition d_atoms.c:176
int nbl
Definition d_atoms.c:176
float axis_radius
Definition d_axis.c:66
void clean_labels(int id)
clean atomic label shaders
Definition d_label.c:143
ColRGBA color_axis(int id)
get axis color
Definition d_axis.c:77
float axis_size
Definition d_axis.c:65
void setup_arrow(float *vert, vec3_t a, vec3_t b, vec3_t c, vec3_t d, vec3_t e)
setup axis 3D arrow rendering data
Definition d_axis.c:181
mat4_t create_axis_matrices(int type)
create axis OpenGL rendering matrices
Definition d_axis.c:101
void init_axis_param()
initialize axis rendering parameters
Definition d_axis.c:196
object_3d * draw_sphere(int quality)
OpenGL 3D sphere object rendering.
Definition d_atoms.c:209
ColRGBA pcol
Definition d_poly.c:60
float poly_alpha
Definition d_poly.c:65
vec3_t centroid
Definition d_poly.c:59
void setup_sphere_vertice(float *vertices, vec3_t pos, ColRGBA col, float rad, float alpha)
fill the OpenGL data buffer for a atom (or clone) to render
Definition d_atoms.c:312
float label_pos
Definition d_axis.c:68
int create_axis_lists()
prepare axis OpenGL rendering
Definition d_axis.c:268
void setup_cylinder_vertice(float *vertices, vec3_t pos_a, vec3_t pos_b, ColRGBA col, float rad, float alpha, float delta)
fill the OpenGL data buffer for a cylinder bond (or clone bond) to render
Definition d_bonds.c:291
void setup_line_vertice(float *vertices, vec3_t pos, ColRGBA col, float alpha)
fill the OpenGL data buffer for a line bond (or clone bond) to render
Definition d_bonds.c:265
object_3d * draw_cylinder_cap(int quality, float rad, gboolean picked)
OpenGL 3D cylinder cap object rendering.
Definition d_bonds.c:169
float arrow_base
Definition d_axis.c:67
void prepare_axis_data(float *vert_a, float *vert_b, float *vert_c)
prepare axis OpenGL rendering data buffer
Definition d_axis.c:222
object_3d * draw_cylinder(int quality, float ra, float rb)
OpenGL 3D cylinder object rendering.
Definition d_bonds.c:98
float extra
void setup_cap_vertice(float *vertices, vec3_t pos_a, vec3_t pos_b, ColRGBA col, float rad, float alpha)
fill the OpenGL data buffer for a cylinder cap bond (or clone bond) to render
Definition d_bonds.c:323
void setup_triangles(float *vertices, vec3_t sa, vec3_t sb, vec3_t sc)
setup triangle veertices
Definition d_poly.c:126
float arrow_length
Definition d_axis.c:64
ColRGBA col
Definition d_measures.c:77
int * shift
Definition d_measures.c:72
dint rep
Definition dlp_edit.c:2239
float * allocfloat(int val)
allocate a float * pointer
Definition global.c:410
Global variable declarations Global convenience function declarations Global data structure defin...
Variable declarations related to the OpenGL window Function declarations related to the OpenGL wind...
image * plot
Definition ogl_draw.c:66
@ CENTER
Definition glview.h:209
@ BOTTOM_RIGHT
Definition glview.h:207
@ TOP_LEFT
Definition glview.h:206
@ BOTTOM_LEFT
Definition glview.h:208
@ CUSTOM
Definition glview.h:210
@ TOP_RIGHT
Definition glview.h:205
glwin * wingl
Definition ogl_draw.c:59
@ PERSPECTIVE
Definition glview.h:152
@ ORTHOGRAPHIC
Definition glview.h:151
void cleaning_shaders(glwin *view, int shader)
re-initialize an OpenGL shader
glsl_program * init_shader_program(int object, int object_id, const GLchar *vertex, const GLchar *geometry, const GLchar *fragment, GLenum type_of_vertices, int narray, int nunif, gboolean lightning, object_3d *obj)
create an OpenGL shader program
@ WIREFRAME
Definition glview.h:174
@ NONE
Definition glview.h:172
#define AXIS
Definition glwin.h:55
@ MAXIS
Definition glwin.h:94
double z
Definition ogl_draw.c:57
double y
Definition ogl_draw.c:57
double x
Definition ogl_draw.c:57
const GLchar * full_color
const GLchar * cylinder_vertex
const GLchar * full_vertex
const GLchar * line_color
const GLchar * cap_vertex
const GLchar * line_vertex
Definition ogl_shaders.c:80
const GLchar * sphere_vertex
const GLchar * cone_vertex
#define LINE_BUFF_SIZE
Definition ogl_shading.h:49
#define ATOM_BUFF_SIZE
Definition ogl_shading.h:52
#define CAPS_BUFF_SIZE
Definition ogl_shading.h:51
#define POLY_BUFF_SIZE
Definition ogl_shading.h:48
#define CYLI_BUFF_SIZE
Definition ogl_shading.h:50
@ GLSL_CAPS
Definition ogl_shading.h:42
@ GLSL_LINES
Definition ogl_shading.h:40
@ GLSL_POLYEDRA
Definition ogl_shading.h:43
@ GLSL_SPHERES
Definition ogl_shading.h:39
@ GLSL_CYLINDERS
Definition ogl_shading.h:41
void prepare_string(char *text, int id, ColRGBA col, vec3_t pos, float lshift[3], atom *at, atom *bt, atom *ct)
prepare a screen string to be rendered
Definition ogl_text.c:691
void render_all_strings(int glsl, int id)
render all string to be rendered for a label list
Definition ogl_text.c:549
float blue
Definition global.h:118
float alpha
Definition global.h:119
float red
Definition global.h:116
float green
Definition global.h:117
float z
Definition math_3d.h:130
int b
Definition tab-1.c:95
int c
Definition tab-1.c:95
int d
Definition tab-1.c:95
int a
Definition tab-1.c:95
GtkWidget * axis_title
Definition tab-4.c:101