atomes 1.1.15
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 x = y = z = 0.0;
105 float from_edge = 50.0;
106 vec3_t win_coord;
107 GLfloat sx = (GLfloat)wingl -> pixels[0];
108 GLfloat sy = (GLfloat)wingl -> pixels[1];
109 mat4_t axis_projection_matrix;
110 mat4_t axis_model_matrix;
111 mat4_t axis_view_matrix;
112 mat4_t axis_model_view_matrix;
113 if (plot -> axispos == CENTER)
114 {
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 win_coord = v3_un_project (vec3(x, y, z), wingl -> view_port, axis_projection_matrix);
153 win_coord.z = 1.0;
154 //print_axis_matrices (win_coord);
155 if (type == 0)
156 {
157 axis_view_matrix = wingl -> view_matrix;
158 }
159 else
160 {
161 axis_view_matrix = wingl -> model_matrix;
162 }
163 axis_model_matrix = m4_translation (win_coord);
164 axis_model_view_matrix = m4_mul (axis_model_matrix, axis_view_matrix);
165 return m4_mul (axis_projection_matrix, axis_model_view_matrix);
166}
167
180void setup_arrow (float * vert, vec3_t a, vec3_t b, vec3_t c, vec3_t d, vec3_t e)
181{
182 setup_triangles (vert, a, b, c);
183 setup_triangles (vert, a, c, d);
184 setup_triangles (vert, a, d, e);
185 setup_triangles (vert, a, e, b);
186 setup_triangles (vert, b, c, d);
187 setup_triangles (vert, b, d, e);
188}
189
196{
197 arrow_length = 0.5;
198 axis_size = plot -> axis_length;
199 axis_radius = plot -> box_axis_rad[AXIS];
200 arrow_base = 0.1;
201 label_pos = 0.2;
202 if (plot -> rep == PERSPECTIVE && plot -> axispos != CENTER)
203 {
204 arrow_base /= (plot -> p_depth / plot -> gnear);
205 arrow_length /= (plot -> p_depth / plot -> gnear);
206 axis_radius /= (plot -> p_depth / plot -> gnear);
207 axis_size /= (plot -> p_depth / plot -> gnear);
208 label_pos /= (plot -> p_depth / plot -> gnear);
209 }
210}
211
221void prepare_axis_data (float * vert_a, float * vert_b, float * vert_c)
222{
224 poly_alpha = 1.0;
225
226 vec3_t a, b, c, d, e;
227 float sa;
228
229 nbs = nbl = nba = 0;
230 int i;
231 for (i=0; i<3; i++)
232 {
233 pcol = color_axis (i);
234 a = vec3(0.0, 0.0, 0.0);
235 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);
236 if (plot -> box_axis[AXIS] == WIREFRAME)
237 {
238 setup_line_vertice (vert_a, a, pcol, 1.0);
239 setup_line_vertice (vert_a, b, pcol, 1.0);
240 a = vec3((i==0)? axis_size : 0.0, (i==1)? axis_size : 0.0, (i==2)? axis_size : 0.0);
241 sa = (i == 2) ? -1.0 : 1.0;
246 centroid = v3_add (a, v3_add(b, v3_add(c, v3_add(d, e))));
247 centroid = v3_divs (centroid, 5.0);
248 setup_arrow (vert_b, a, b, c, d, e);
249 }
250 else
251 {
252 setup_cylinder_vertice (vert_a, a, b, pcol, axis_radius, 1.0, 0.0);
253 c = vec3((i==0)? axis_size : 0.0, (i==1)? axis_size : 0.0, (i==2)? axis_size : 0.0);
254 nbs --;
255 setup_cylinder_vertice (vert_b, c, b, pcol, axis_radius+arrow_base, 1.0, 0.0);
256 nbs --;
258 }
259 }
260}
261
268{
269 vec3_t pos;
270 float shift[3]={0.0, 0.0, 0.0};
271 int nshaders = 0;
272 object_3d * axis_a, * axis_b, * axis_c, * axis_d;
273
275 wingl -> create_shaders[MAXIS] = FALSE;
276 if (plot -> box_axis[AXIS] == NONE) return nshaders;
277
278 if (plot -> box_axis[AXIS] == WIREFRAME)
279 {
280 axis_a = g_malloc0 (sizeof*axis_a);
281 axis_a -> vert_buffer_size = LINE_BUFF_SIZE;
282 axis_a -> num_vertices = 3*2;
283 axis_a -> vertices = allocfloat (axis_a -> vert_buffer_size*axis_a -> num_vertices);
284 axis_b = g_malloc0 (sizeof*axis_b);
285 axis_b -> vert_buffer_size = POLY_BUFF_SIZE;
286 axis_b -> num_vertices = 3*6*9;
287 axis_b -> vertices = allocfloat (axis_b -> vert_buffer_size*axis_b -> num_vertices);
288 }
289 else
290 {
291 axis_a = draw_cylinder (plot -> quality, 1.0, 1.0);
292 axis_a -> num_instances = 3;
293 axis_a -> inst_buffer_size = CYLI_BUFF_SIZE;
294 axis_a -> instances = allocfloat (axis_a -> num_instances*CYLI_BUFF_SIZE);
295 axis_b = draw_cylinder (plot -> quality, 0.0, 1.0);
296 axis_b -> num_instances = 3;
297 axis_b -> inst_buffer_size = CYLI_BUFF_SIZE;
298 axis_b -> instances = allocfloat (axis_b -> num_instances*CYLI_BUFF_SIZE);
299 axis_c = draw_cylinder_cap (plot -> quality, 1.0, FALSE);
300 axis_c -> num_instances = 3;
301 axis_c -> inst_buffer_size = CAPS_BUFF_SIZE;
302 axis_c -> instances = allocfloat (CAPS_BUFF_SIZE*axis_c -> num_instances);
303 axis_d = draw_sphere (plot -> quality);
304 axis_d -> num_instances = 1;
305 axis_d -> inst_buffer_size = ATOM_BUFF_SIZE;
306 axis_d -> instances = allocfloat (ATOM_BUFF_SIZE);
307 }
308 nshaders = (plot -> box_axis[AXIS] == WIREFRAME) ? 2 : 4;
309 prepare_axis_data ((plot -> box_axis[AXIS] == WIREFRAME) ? axis_a -> vertices : axis_a -> instances,
310 (plot -> box_axis[AXIS] == WIREFRAME) ? axis_b -> vertices : axis_b -> instances,
311 (plot -> box_axis[AXIS] == WIREFRAME) ? NULL : axis_c -> instances);
312 clean_labels (2);
313 if (plot -> axis_labels)
314 {
315 int i;
316 for (i=0; i<3; i++)
317 {
318 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);
319 prepare_string (plot -> axis_title[i], 2, color_axis (i), pos, shift, NULL, NULL, NULL);
320 }
321 nshaders += (plot -> labels_render[2]+1) * (plot -> labels_list[2] -> last -> id + 1);
322 wingl -> ogl_glsl[MAXIS][0] = g_malloc0 (nshaders*sizeof*wingl -> ogl_glsl[MAXIS][0]);
324 }
325 else
326 {
327 wingl -> ogl_glsl[MAXIS][0] = g_malloc0 (nshaders*sizeof*wingl -> ogl_glsl[MAXIS][0]);
328 }
329 if (plot -> box_axis[AXIS] == WIREFRAME)
330 {
331 // Lines
332 wingl -> ogl_glsl[MAXIS][0][0] = init_shader_program (MAXIS, GLSL_LINES, line_vertex, NULL, line_color, GL_LINES, 2, 1, FALSE, axis_a);
333 wingl -> ogl_glsl[MAXIS][0][0] -> line_width = plot -> box_axis_line[AXIS];
334 // Arrows
335 wingl -> ogl_glsl[MAXIS][0][1] = init_shader_program (MAXIS, GLSL_POLYEDRA, full_vertex, NULL, full_color, GL_TRIANGLES, 3, 1, TRUE, axis_b);
336 }
337 else
338 {
339 // Yellow sphere at (0.0, 0.0, 0.0)
340 pcol.red = pcol.green = 1.0;
341 pcol.blue = 0.0;
342 pcol.alpha = 1.0;
343 setup_sphere_vertice (axis_d -> instances, vec3(0.0,0.0,0.0), pcol, axis_radius, 1.0);
344 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);
345 // Cylinders
346 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);
347 // Cones
348 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);
349 // Cones Caps
350 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);
351 g_free (axis_c);
352 g_free (axis_d);
353 }
354 g_free (axis_a);
355 g_free (axis_b);
356 return nshaders;
357}
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:180
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:195
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:267
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:221
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:398
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:124
float alpha
Definition global.h:125
float red
Definition global.h:122
float green
Definition global.h:123
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