Board outline polygon generation
[geda-pcb/pcjc2.git] / src / hid / gtk / gtkhid-gl.c
blobc661c19ae877720451d91ba292cabf97adf6768b
1 #ifdef HAVE_CONFIG_H
2 #include "config.h"
3 #endif
5 #include <stdio.h>
7 #include "crosshair.h"
8 #include "clip.h"
9 #include "../hidint.h"
10 #include "gui.h"
11 #include "draw.h"
12 #include "draw_funcs.h"
13 #include "rtree.h"
14 #include "polygon.h"
15 #include "gui-pinout-preview.h"
17 /* The Linux OpenGL ABI 1.0 spec requires that we define
18 * GL_GLEXT_PROTOTYPES before including gl.h or glx.h for extensions
19 * in order to get prototypes:
20 * http://www.opengl.org/registry/ABI/
23 #define GL_GLEXT_PROTOTYPES 1
24 #ifdef HAVE_OPENGL_GL_H
25 # include <OpenGL/gl.h>
26 #else
27 # include <GL/gl.h>
28 #endif
30 #include <gtk/gtkgl.h>
31 #include "hid/common/hidgl.h"
33 #include "hid/common/draw_helpers.h"
34 #include "hid/common/trackball.h"
36 #ifdef HAVE_LIBDMALLOC
37 #include <dmalloc.h>
38 #endif
40 extern HID ghid_hid;
41 extern HID_DRAW ghid_graphics;
42 extern HID_DRAW_CLASS ghid_graphics_class;
44 static hidGC current_gc = NULL;
46 /* Sets gport->u_gc to the "right" GC to use (wrt mask or window)
48 #define USE_GC(gc) if (!use_gc(gc)) return
50 static enum mask_mode cur_mask = HID_MASK_OFF;
51 static GLfloat view_matrix[4][4] = {{1.0, 0.0, 0.0, 0.0},
52 {0.0, 1.0, 0.0, 0.0},
53 {0.0, 0.0, 1.0, 0.0},
54 {0.0, 0.0, 0.0, 1.0}};
55 static GLfloat last_modelview_matrix[4][4] = {{1.0, 0.0, 0.0, 0.0},
56 {0.0, 1.0, 0.0, 0.0},
57 {0.0, 0.0, 1.0, 0.0},
58 {0.0, 0.0, 0.0, 1.0}};
59 static int global_view_2d = 1;
61 typedef struct render_priv {
62 GdkGLConfig *glconfig;
63 bool trans_lines;
64 bool in_context;
65 int subcomposite_stencil_bit;
66 char *current_colorname;
67 double current_alpha_mult;
68 GTimer *time_since_expose;
70 /* Feature for leading the user to a particular location */
71 guint lead_user_timeout;
72 GTimer *lead_user_timer;
73 bool lead_user;
74 Coord lead_user_radius;
75 Coord lead_user_x;
76 Coord lead_user_y;
78 hidgl_instance *hidgl;
79 GList *active_gc_list;
80 double edit_depth;
82 } render_priv;
84 typedef struct gtk_gc_struct
86 struct hidgl_gc_struct hidgl_gc; /* Parent */
88 const char *colorname;
89 double alpha_mult;
90 Coord width;
91 gint cap, join;
92 } *gtkGC;
94 static void draw_lead_user (hidGC gc, render_priv *priv);
95 static void ghid_unproject_to_z_plane (int ex, int ey, Coord pcb_z, Coord *pcb_x, Coord *pcb_y);
98 #define BOARD_THICKNESS MM_TO_COORD(1.60)
99 #define MASK_COPPER_SPACING MM_TO_COORD(0.05)
100 #define SILK_MASK_SPACING MM_TO_COORD(0.01)
101 static int
102 compute_depth (int group)
104 static int last_depth_computed = 0;
106 int top_group;
107 int bottom_group;
108 int min_copper_group;
109 int max_copper_group;
110 int num_copper_groups;
111 int middle_copper_group;
112 int depth;
114 top_group = GetLayerGroupNumberBySide (TOP_SIDE);
115 bottom_group = GetLayerGroupNumberBySide (BOTTOM_SIDE);
117 min_copper_group = MIN (bottom_group, top_group);
118 max_copper_group = MAX (bottom_group, top_group);
119 num_copper_groups = max_copper_group - min_copper_group + 1;
120 middle_copper_group = min_copper_group + num_copper_groups / 2;
122 if (group >= 0 && group < max_group) {
123 if (group >= min_copper_group && group <= max_copper_group) {
124 /* XXX: IS THIS INCORRECT FOR REVERSED GROUP ORDERINGS? */
125 depth = -(group - middle_copper_group) * BOARD_THICKNESS / num_copper_groups;
126 } else {
127 depth = 0;
130 } else if (SL_TYPE (group) == SL_MASK) {
131 if (SL_SIDE (group) == SL_TOP_SIDE) {
132 depth = -((min_copper_group - middle_copper_group) * BOARD_THICKNESS / num_copper_groups - MASK_COPPER_SPACING);
133 } else {
134 depth = -((max_copper_group - middle_copper_group) * BOARD_THICKNESS / num_copper_groups + MASK_COPPER_SPACING);
136 } else if (SL_TYPE (group) == SL_SILK) {
137 if (SL_SIDE (group) == SL_TOP_SIDE) {
138 depth = -((min_copper_group - middle_copper_group) * BOARD_THICKNESS / num_copper_groups - MASK_COPPER_SPACING - SILK_MASK_SPACING);
139 } else {
140 depth = -((max_copper_group - middle_copper_group) * BOARD_THICKNESS / num_copper_groups + MASK_COPPER_SPACING + SILK_MASK_SPACING);
143 } else if (SL_TYPE (group) == SL_INVISIBLE) {
144 /* Same as silk, but for the back-side layer */
145 if (Settings.ShowBottomSide) {
146 depth = -((min_copper_group - middle_copper_group) * BOARD_THICKNESS / num_copper_groups - MASK_COPPER_SPACING - SILK_MASK_SPACING);
147 } else {
148 depth = -((max_copper_group - middle_copper_group) * BOARD_THICKNESS / num_copper_groups + MASK_COPPER_SPACING + SILK_MASK_SPACING);
150 } else if (SL_TYPE (group) == SL_RATS ||
151 SL_TYPE (group) == SL_PDRILL ||
152 SL_TYPE (group) == SL_UDRILL) {
153 /* Draw these at the depth we last rendered at */
154 depth = last_depth_computed;
155 } else if (SL_TYPE (group) == SL_PASTE ||
156 SL_TYPE (group) == SL_FAB ||
157 SL_TYPE (group) == SL_ASSY) {
158 /* Layer types we don't use, which draw.c asks us about, so
159 * we just return _something_ to avoid the warnign below. */
160 depth = last_depth_computed;
161 } else {
162 /* DEFAULT CASE */
163 printf ("Unknown layer group to set depth for: %i\n", group);
164 depth = last_depth_computed;
167 last_depth_computed = depth;
168 return depth;
171 static void
172 start_subcomposite (hidgl_instance *hidgl)
174 render_priv *priv = gport->render_priv;
175 int stencil_bit;
177 /* Flush out any existing geoemtry to be rendered */
178 hidgl_flush_triangles (hidgl);
180 glEnable (GL_STENCIL_TEST); /* Enable Stencil test */
181 glStencilOp (GL_KEEP, GL_KEEP, GL_REPLACE); /* Stencil pass => replace stencil value (with 1) */
183 stencil_bit = hidgl_assign_clear_stencil_bit (hidgl); /* Get a new (clean) bitplane to stencil with */
184 glStencilMask (stencil_bit); /* Only write to our subcompositing stencil bitplane */
185 glStencilFunc (GL_GREATER, stencil_bit, stencil_bit); /* Pass stencil test if our assigned bit is clear */
187 priv->subcomposite_stencil_bit = stencil_bit;
190 static void
191 end_subcomposite (hidgl_instance *hidgl)
193 render_priv *priv = gport->render_priv;
195 /* Flush out any existing geoemtry to be rendered */
196 hidgl_flush_triangles (hidgl);
198 hidgl_return_stencil_bit (hidgl, priv->subcomposite_stencil_bit); /* Relinquish any bitplane we previously used */
200 glStencilMask (0);
201 glStencilFunc (GL_ALWAYS, 0, 0); /* Always pass stencil test */
202 glDisable (GL_STENCIL_TEST); /* Disable Stencil test */
204 priv->subcomposite_stencil_bit = 0;
207 static void
208 set_depth_on_all_active_gc (render_priv *priv, float depth)
210 GList *iter;
212 for (iter = priv->active_gc_list;
213 iter != NULL;
214 iter = g_list_next (iter))
216 hidGC gc = iter->data;
218 hidgl_set_depth (gc, depth);
222 /* Compute group visibility based upon on copper layers only */
223 static bool
224 is_layer_group_visible (int group)
226 int entry;
227 for (entry = 0; entry < PCB->LayerGroups.Number[group]; entry++)
229 int layer_idx = PCB->LayerGroups.Entries[group][entry];
230 if (layer_idx >= 0 && layer_idx < max_copper_layer &&
231 LAYER_PTR (layer_idx)->On)
232 return true;
234 return false;
238 ghid_set_layer (const char *name, int group, int empty)
240 render_priv *priv = gport->render_priv;
241 hidgl_instance *hidgl = priv->hidgl;
242 bool group_visible = false;
243 bool subcomposite = true;
245 if (group >= 0 && group < max_group)
247 priv->trans_lines = true;
248 subcomposite = true;
249 group_visible = is_layer_group_visible (group);
251 else
253 switch (SL_TYPE (group))
255 case SL_INVISIBLE:
256 priv->trans_lines = false;
257 subcomposite = false;
258 group_visible = PCB->InvisibleObjectsOn;
259 break;
260 case SL_MASK:
261 priv->trans_lines = true;
262 subcomposite = false;
263 group_visible = TEST_FLAG (SHOWMASKFLAG, PCB);
264 break;
265 case SL_SILK:
266 priv->trans_lines = true;
267 subcomposite = true;
268 group_visible = PCB->ElementOn;
269 break;
270 case SL_ASSY:
271 break;
272 case SL_PDRILL:
273 case SL_UDRILL:
274 priv->trans_lines = true;
275 subcomposite = true;
276 group_visible = true;
277 break;
278 case SL_RATS:
279 priv->trans_lines = true;
280 subcomposite = false;
281 group_visible = PCB->RatOn;
282 break;
286 end_subcomposite (hidgl);
288 if (group_visible && subcomposite)
289 start_subcomposite (hidgl);
291 /* Drawing is already flushed by {start,end}_subcomposite */
292 set_depth_on_all_active_gc (priv, compute_depth (group));
294 return group_visible;
297 static void
298 ghid_end_layer ()
300 render_priv *priv = gport->render_priv;
301 hidgl_instance *hidgl = priv->hidgl;
303 end_subcomposite (hidgl);
306 void
307 ghid_destroy_gc (hidGC gc)
309 render_priv *priv = gport->render_priv;
311 priv->active_gc_list = g_list_remove (priv->active_gc_list, gc);
313 hidgl_finish_gc (gc);
314 g_free (gc);
317 hidGC
318 ghid_make_gc (void)
320 render_priv *priv = gport->render_priv;
321 hidGC gc = (hidGC) g_new0 (struct gtk_gc_struct, 1);
322 gtkGC gtk_gc = (gtkGC)gc;
324 gc->hid = &ghid_hid;
325 gc->hid_draw = &ghid_graphics;
327 hidgl_init_gc (priv->hidgl, gc);
329 gtk_gc->colorname = Settings.BackgroundColor;
330 gtk_gc->alpha_mult = 1.0;
332 priv->active_gc_list = g_list_prepend (priv->active_gc_list, gc);
334 return gc;
337 static void
338 ghid_draw_grid (hidGC gc, BoxType *drawn_area)
340 if (Vz (PCB->Grid) < MIN_GRID_DISTANCE)
341 return;
343 if (gdk_color_parse (Settings.GridColor, &gport->grid_color))
345 gport->grid_color.red ^= gport->bg_color.red;
346 gport->grid_color.green ^= gport->bg_color.green;
347 gport->grid_color.blue ^= gport->bg_color.blue;
350 glDisable (GL_STENCIL_TEST);
351 glEnable (GL_COLOR_LOGIC_OP);
352 glLogicOp (GL_XOR);
354 glColor3f (gport->grid_color.red / 65535.,
355 gport->grid_color.green / 65535.,
356 gport->grid_color.blue / 65535.);
358 hidgl_draw_grid (gc, drawn_area);
360 glDisable (GL_COLOR_LOGIC_OP);
361 glEnable (GL_STENCIL_TEST);
364 static void
365 ghid_draw_bg_image (void)
367 static GLuint texture_handle = 0;
369 if (!ghidgui->bg_pixbuf)
370 return;
372 if (texture_handle == 0)
374 int width = gdk_pixbuf_get_width (ghidgui->bg_pixbuf);
375 int height = gdk_pixbuf_get_height (ghidgui->bg_pixbuf);
376 int rowstride = gdk_pixbuf_get_rowstride (ghidgui->bg_pixbuf);
377 int bits_per_sample = gdk_pixbuf_get_bits_per_sample (ghidgui->bg_pixbuf);
378 int n_channels = gdk_pixbuf_get_n_channels (ghidgui->bg_pixbuf);
379 unsigned char *pixels = gdk_pixbuf_get_pixels (ghidgui->bg_pixbuf);
381 g_warn_if_fail (bits_per_sample == 8);
382 g_warn_if_fail (rowstride == width * n_channels);
384 glGenTextures (1, &texture_handle);
385 glBindTexture (GL_TEXTURE_2D, texture_handle);
387 /* XXX: We should proabbly determine what the maxmimum texture supported is,
388 * and if our image is larger, shrink it down using GDK pixbuf routines
389 * rather than having it fail below.
392 glTexImage2D (GL_TEXTURE_2D, 0, GL_RGB, width, height, 0,
393 (n_channels == 4) ? GL_RGBA : GL_RGB, GL_UNSIGNED_BYTE, pixels);
396 glBindTexture (GL_TEXTURE_2D, texture_handle);
398 glTexEnvf (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
399 glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
400 glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
401 glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
402 glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
403 glEnable (GL_TEXTURE_2D);
405 /* Render a quad with the background as a texture */
407 glBegin (GL_QUADS);
408 glTexCoord2d (0., 0.);
409 glVertex3i (0, 0, 0);
410 glTexCoord2d (1., 0.);
411 glVertex3i (PCB->MaxWidth, 0, 0);
412 glTexCoord2d (1., 1.);
413 glVertex3i (PCB->MaxWidth, PCB->MaxHeight, 0);
414 glTexCoord2d (0., 1.);
415 glVertex3i (0, PCB->MaxHeight, 0);
416 glEnd ();
418 glDisable (GL_TEXTURE_2D);
421 void
422 ghid_use_mask (enum mask_mode mode)
424 render_priv *priv = gport->render_priv;
425 hidgl_instance *hidgl = priv->hidgl;
426 static int stencil_bit = 0;
428 if (mode == cur_mask)
429 return;
431 /* Flush out any existing geoemtry to be rendered */
432 hidgl_flush_triangles (hidgl);
434 switch (mode)
436 case HID_MASK_BEFORE:
437 /* The HID asks not to receive this mask type, so warn if we get it */
438 g_return_if_reached ();
440 case HID_MASK_CLEAR:
441 /* Write '1' to the stencil buffer where the solder-mask should not be drawn. */
442 glColorMask (0, 0, 0, 0); /* Disable writting in color buffer */
443 glEnable (GL_STENCIL_TEST); /* Enable Stencil test */
444 stencil_bit = hidgl_assign_clear_stencil_bit (hidgl); /* Get a new (clean) bitplane to stencil with */
445 glStencilFunc (GL_ALWAYS, stencil_bit, stencil_bit); /* Always pass stencil test, write stencil_bit */
446 glStencilMask (stencil_bit); /* Only write to our subcompositing stencil bitplane */
447 glStencilOp (GL_KEEP, GL_KEEP, GL_REPLACE); /* Stencil pass => replace stencil value (with 1) */
448 break;
450 case HID_MASK_AFTER:
451 /* Drawing operations as masked to areas where the stencil buffer is '0' */
452 glColorMask (1, 1, 1, 1); /* Enable drawing of r, g, b & a */
453 glStencilFunc (GL_GEQUAL, 0, stencil_bit); /* Draw only where our bit of the stencil buffer is clear */
454 glStencilOp (GL_KEEP, GL_KEEP, GL_KEEP); /* Stencil buffer read only */
455 break;
457 case HID_MASK_OFF:
458 /* Disable stenciling */
459 hidgl_return_stencil_bit (hidgl, stencil_bit); /* Relinquish any bitplane we previously used */
460 glDisable (GL_STENCIL_TEST); /* Disable Stencil test */
461 break;
463 cur_mask = mode;
467 /* Config helper functions for when the user changes color preferences.
468 | set_special colors used in the gtkhid.
470 static void
471 set_special_grid_color (void)
473 if (!gport->colormap)
474 return;
475 gport->grid_color.red ^= gport->bg_color.red;
476 gport->grid_color.green ^= gport->bg_color.green;
477 gport->grid_color.blue ^= gport->bg_color.blue;
480 void
481 ghid_set_special_colors (HID_Attribute * ha)
483 if (!ha->name || !ha->value)
484 return;
485 if (!strcmp (ha->name, "background-color"))
487 ghid_map_color_string (*(char **) ha->value, &gport->bg_color);
488 set_special_grid_color ();
490 else if (!strcmp (ha->name, "off-limit-color"))
492 ghid_map_color_string (*(char **) ha->value, &gport->offlimits_color);
494 else if (!strcmp (ha->name, "grid-color"))
496 ghid_map_color_string (*(char **) ha->value, &gport->grid_color);
497 set_special_grid_color ();
501 typedef struct
503 int color_set;
504 GdkColor color;
505 double red;
506 double green;
507 double blue;
508 } ColorCache;
510 static void
511 set_gl_color_for_gc (hidGC gc)
513 gtkGC gtk_gc = (gtkGC)gc;
514 render_priv *priv = gport->render_priv;
515 static void *cache = NULL;
516 hidval cval;
517 ColorCache *cc;
518 double r, g, b, a;
520 if (priv->current_colorname != NULL &&
521 strcmp (priv->current_colorname, gtk_gc->colorname) == 0 &&
522 priv->current_alpha_mult == gtk_gc->alpha_mult)
523 return;
525 free (priv->current_colorname);
526 priv->current_colorname = strdup (gtk_gc->colorname);
527 priv->current_alpha_mult = gtk_gc->alpha_mult;
529 if (gport->colormap == NULL)
530 gport->colormap = gtk_widget_get_colormap (gport->top_window);
531 if (strcmp (gtk_gc->colorname, "erase") == 0)
533 r = gport->bg_color.red / 65535.;
534 g = gport->bg_color.green / 65535.;
535 b = gport->bg_color.blue / 65535.;
536 a = 1.0;
538 else if (strcmp (gtk_gc->colorname, "drill") == 0)
540 r = gport->offlimits_color.red / 65535.;
541 g = gport->offlimits_color.green / 65535.;
542 b = gport->offlimits_color.blue / 65535.;
543 a = 0.85;
545 else
547 if (hid_cache_color (0, gtk_gc->colorname, &cval, &cache))
548 cc = (ColorCache *) cval.ptr;
549 else
551 cc = (ColorCache *) malloc (sizeof (ColorCache));
552 memset (cc, 0, sizeof (*cc));
553 cval.ptr = cc;
554 hid_cache_color (1, gtk_gc->colorname, &cval, &cache);
557 if (!cc->color_set)
559 if (gdk_color_parse (gtk_gc->colorname, &cc->color))
560 gdk_color_alloc (gport->colormap, &cc->color);
561 else
562 gdk_color_white (gport->colormap, &cc->color);
563 cc->red = cc->color.red / 65535.;
564 cc->green = cc->color.green / 65535.;
565 cc->blue = cc->color.blue / 65535.;
566 cc->color_set = 1;
568 r = cc->red;
569 g = cc->green;
570 b = cc->blue;
571 a = 0.7;
573 if (1) {
574 double maxi, mult;
575 a *= gtk_gc->alpha_mult;
576 if (!priv->trans_lines)
577 a = 1.0;
578 maxi = r;
579 if (g > maxi) maxi = g;
580 if (b > maxi) maxi = b;
581 mult = MIN (1 / a, 1 / maxi);
582 #if 1
583 r = r * mult;
584 g = g * mult;
585 b = b * mult;
586 #endif
589 if(!priv->in_context)
590 return;
592 hidgl_flush_triangles (gtk_gc->hidgl_gc.hidgl);
593 glColor4d (r, g, b, a);
596 void
597 ghid_set_color (hidGC gc, const char *name)
599 gtkGC gtk_gc = (gtkGC)gc;
601 gtk_gc->colorname = name;
602 set_gl_color_for_gc (gc);
605 void
606 ghid_set_alpha_mult (hidGC gc, double alpha_mult)
608 gtkGC gtk_gc = (gtkGC)gc;
610 gtk_gc->alpha_mult = alpha_mult;
611 set_gl_color_for_gc (gc);
614 void
615 ghid_set_line_cap (hidGC gc, EndCapStyle style)
617 gtkGC gtk_gc = (gtkGC)gc;
619 gtk_gc->cap = style;
622 void
623 ghid_set_line_width (hidGC gc, Coord width)
625 gtkGC gtk_gc = (gtkGC)gc;
627 gtk_gc->width = width;
631 void
632 ghid_set_draw_xor (hidGC gc, int xor)
634 /* NOT IMPLEMENTED */
636 /* Only presently called when setting up a crosshair GC.
637 * We manage our own drawing model for that anyway. */
640 void
641 ghid_set_draw_faded (hidGC gc, int faded)
643 printf ("ghid_set_draw_faded(%p,%d) -- not implemented\n", gc, faded);
646 void
647 ghid_set_line_cap_angle (hidGC gc, Coord x1, Coord y1, Coord x2, Coord y2)
649 printf ("ghid_set_line_cap_angle() -- not implemented\n");
652 static void
653 ghid_invalidate_current_gc (void)
655 current_gc = NULL;
658 static int
659 use_gc (hidGC gc)
661 if (gc->hid != &ghid_hid)
663 fprintf (stderr, "Fatal: GC from another HID passed to GTK HID\n");
664 abort ();
667 if (current_gc == gc)
668 return 1;
670 current_gc = gc;
672 set_gl_color_for_gc (gc);
673 return 1;
676 void
677 ghid_draw_line (hidGC gc, Coord x1, Coord y1, Coord x2, Coord y2)
679 gtkGC gtk_gc = (gtkGC)gc;
680 USE_GC (gc);
682 hidgl_draw_line (gc, gtk_gc->cap, gtk_gc->width, x1, y1, x2, y2, gport->view.coord_per_px);
685 void
686 ghid_draw_arc (hidGC gc, Coord cx, Coord cy, Coord xradius, Coord yradius,
687 Angle start_angle, Angle delta_angle)
689 gtkGC gtk_gc = (gtkGC)gc;
690 USE_GC (gc);
692 hidgl_draw_arc (gc, gtk_gc->width, cx, cy, xradius, yradius,
693 start_angle, delta_angle, gport->view.coord_per_px);
696 void
697 ghid_draw_rect (hidGC gc, Coord x1, Coord y1, Coord x2, Coord y2)
699 USE_GC (gc);
701 hidgl_draw_rect (gc, x1, y1, x2, y2);
705 void
706 ghid_fill_circle (hidGC gc, Coord cx, Coord cy, Coord radius)
708 USE_GC (gc);
710 hidgl_fill_circle (gc, cx, cy, radius, gport->view.coord_per_px);
714 void
715 ghid_fill_polygon (hidGC gc, int n_coords, Coord *x, Coord *y)
717 USE_GC (gc);
719 hidgl_fill_polygon (gc, n_coords, x, y);
722 void
723 ghid_fill_pcb_polygon (hidGC gc, PolygonType *poly, const BoxType *clip_box)
725 USE_GC (gc);
727 hidgl_fill_pcb_polygon (gc, poly, clip_box, gport->view.coord_per_px);
730 void
731 ghid_thindraw_pcb_polygon (hidGC gc, PolygonType *poly, const BoxType *clip_box)
733 gtkGC gtk_gc = (gtkGC)gc;
735 double old_alpha_mult = gtk_gc->alpha_mult;
736 common_thindraw_pcb_polygon (gc, poly, clip_box);
737 ghid_set_alpha_mult (gc, gtk_gc->alpha_mult * 0.25);
738 hid_draw_fill_pcb_polygon (gc, poly, clip_box);
739 ghid_set_alpha_mult (gc, old_alpha_mult);
742 void
743 ghid_fill_rect (hidGC gc, Coord x1, Coord y1, Coord x2, Coord y2)
745 USE_GC (gc);
747 hidgl_fill_rect (gc, x1, y1, x2, y2);
750 void
751 ghid_invalidate_lr (int left, int right, int top, int bottom)
753 ghid_invalidate_all ();
756 #define MAX_ELAPSED (50. / 1000.) /* 50ms */
757 void
758 ghid_invalidate_all ()
760 render_priv *priv = gport->render_priv;
761 double elapsed = g_timer_elapsed (priv->time_since_expose, NULL);
763 ghid_draw_area_update (gport, NULL);
765 if (elapsed > MAX_ELAPSED)
766 gdk_window_process_all_updates ();
769 void
770 ghid_notify_crosshair_change (bool changes_complete)
772 /* We sometimes get called before the GUI is up */
773 if (gport->drawing_area == NULL)
774 return;
776 /* FIXME: We could just invalidate the bounds of the crosshair attached objects? */
777 ghid_invalidate_all ();
780 void
781 ghid_notify_mark_change (bool changes_complete)
783 /* We sometimes get called before the GUI is up */
784 if (gport->drawing_area == NULL)
785 return;
787 /* FIXME: We could just invalidate the bounds of the mark? */
788 ghid_invalidate_all ();
791 static void
792 draw_right_cross (gint x, gint y, gint z)
794 glVertex3i (x, 0, z);
795 glVertex3i (x, PCB->MaxHeight, z);
796 glVertex3i (0, y, z);
797 glVertex3i (PCB->MaxWidth, y, z);
800 static void
801 draw_slanted_cross (gint x, gint y, gint z)
803 gint x0, y0, x1, y1;
805 x0 = x + (PCB->MaxHeight - y);
806 x0 = MAX(0, MIN (x0, PCB->MaxWidth));
807 x1 = x - y;
808 x1 = MAX(0, MIN (x1, PCB->MaxWidth));
809 y0 = y + (PCB->MaxWidth - x);
810 y0 = MAX(0, MIN (y0, PCB->MaxHeight));
811 y1 = y - x;
812 y1 = MAX(0, MIN (y1, PCB->MaxHeight));
813 glVertex3i (x0, y0, z);
814 glVertex3i (x1, y1, z);
816 x0 = x - (PCB->MaxHeight - y);
817 x0 = MAX(0, MIN (x0, PCB->MaxWidth));
818 x1 = x + y;
819 x1 = MAX(0, MIN (x1, PCB->MaxWidth));
820 y0 = y + x;
821 y0 = MAX(0, MIN (y0, PCB->MaxHeight));
822 y1 = y - (PCB->MaxWidth - x);
823 y1 = MAX(0, MIN (y1, PCB->MaxHeight));
824 glVertex3i (x0, y0, z);
825 glVertex3i (x1, y1, z);
828 static void
829 draw_dozen_cross (gint x, gint y, gint z)
831 gint x0, y0, x1, y1;
832 gdouble tan60 = sqrt (3);
834 x0 = x + (PCB->MaxHeight - y) / tan60;
835 x0 = MAX(0, MIN (x0, PCB->MaxWidth));
836 x1 = x - y / tan60;
837 x1 = MAX(0, MIN (x1, PCB->MaxWidth));
838 y0 = y + (PCB->MaxWidth - x) * tan60;
839 y0 = MAX(0, MIN (y0, PCB->MaxHeight));
840 y1 = y - x * tan60;
841 y1 = MAX(0, MIN (y1, PCB->MaxHeight));
842 glVertex3i (x0, y0, z);
843 glVertex3i (x1, y1, z);
845 x0 = x + (PCB->MaxHeight - y) * tan60;
846 x0 = MAX(0, MIN (x0, PCB->MaxWidth));
847 x1 = x - y * tan60;
848 x1 = MAX(0, MIN (x1, PCB->MaxWidth));
849 y0 = y + (PCB->MaxWidth - x) / tan60;
850 y0 = MAX(0, MIN (y0, PCB->MaxHeight));
851 y1 = y - x / tan60;
852 y1 = MAX(0, MIN (y1, PCB->MaxHeight));
853 glVertex3i (x0, y0, z);
854 glVertex3i (x1, y1, z);
856 x0 = x - (PCB->MaxHeight - y) / tan60;
857 x0 = MAX(0, MIN (x0, PCB->MaxWidth));
858 x1 = x + y / tan60;
859 x1 = MAX(0, MIN (x1, PCB->MaxWidth));
860 y0 = y + x * tan60;
861 y0 = MAX(0, MIN (y0, PCB->MaxHeight));
862 y1 = y - (PCB->MaxWidth - x) * tan60;
863 y1 = MAX(0, MIN (y1, PCB->MaxHeight));
864 glVertex3i (x0, y0, z);
865 glVertex3i (x1, y1, z);
867 x0 = x - (PCB->MaxHeight - y) * tan60;
868 x0 = MAX(0, MIN (x0, PCB->MaxWidth));
869 x1 = x + y * tan60;
870 x1 = MAX(0, MIN (x1, PCB->MaxWidth));
871 y0 = y + x / tan60;
872 y0 = MAX(0, MIN (y0, PCB->MaxHeight));
873 y1 = y - (PCB->MaxWidth - x) / tan60;
874 y1 = MAX(0, MIN (y1, PCB->MaxHeight));
875 glVertex3i (x0, y0, z);
876 glVertex3i (x1, y1, z);
879 static void
880 draw_crosshair (hidGC gc, render_priv *priv)
882 gtkGC gtk_gc = (gtkGC)gc;
883 gint x, y, z;
884 static int done_once = 0;
885 static GdkColor cross_color;
887 if (!done_once)
889 done_once = 1;
890 /* FIXME: when CrossColor changed from config */
891 ghid_map_color_string (Settings.CrossColor, &cross_color);
894 x = gport->crosshair_x;
895 y = gport->crosshair_y;
896 z = gtk_gc->hidgl_gc.depth;
898 glEnable (GL_COLOR_LOGIC_OP);
899 glLogicOp (GL_XOR);
901 glColor3f (cross_color.red / 65535.,
902 cross_color.green / 65535.,
903 cross_color.blue / 65535.);
905 glBegin (GL_LINES);
907 draw_right_cross (x, y, z);
908 if (Crosshair.shape == Union_Jack_Crosshair_Shape)
909 draw_slanted_cross (x, y, z);
910 if (Crosshair.shape == Dozen_Crosshair_Shape)
911 draw_dozen_cross (x, y, z);
913 glEnd ();
915 glDisable (GL_COLOR_LOGIC_OP);
918 void
919 ghid_init_renderer (int *argc, char ***argv, GHidPort *port)
921 render_priv *priv;
923 port->render_priv = priv = g_new0 (render_priv, 1);
925 priv->time_since_expose = g_timer_new ();
927 gtk_gl_init(argc, argv);
929 /* setup GL-context */
930 priv->glconfig = gdk_gl_config_new_by_mode (GDK_GL_MODE_RGBA |
931 GDK_GL_MODE_STENCIL |
932 GDK_GL_MODE_DOUBLE);
933 if (!priv->glconfig)
935 printf ("Could not setup GL-context!\n");
936 return; /* Should we abort? */
939 hidgl_init ();
940 priv->hidgl = hidgl_new_instance ();
942 /* Setup HID function pointers specific to the GL renderer*/
943 ghid_graphics_class.end_layer = ghid_end_layer;
944 ghid_graphics_class.fill_pcb_polygon = ghid_fill_pcb_polygon;
945 ghid_graphics_class.thindraw_pcb_polygon = ghid_thindraw_pcb_polygon;
948 void
949 ghid_shutdown_renderer (GHidPort *port)
951 render_priv *priv = port->render_priv;
953 hidgl_free_instance (priv->hidgl);
955 ghid_cancel_lead_user ();
956 g_free (port->render_priv);
957 port->render_priv = NULL;
960 void
961 ghid_init_drawing_widget (GtkWidget *widget, GHidPort *port)
963 render_priv *priv = port->render_priv;
965 gtk_widget_set_gl_capability (widget,
966 priv->glconfig,
967 NULL,
968 TRUE,
969 GDK_GL_RGBA_TYPE);
972 void
973 ghid_drawing_area_configure_hook (GHidPort *port)
977 gboolean
978 ghid_start_drawing (GHidPort *port, GtkWidget *widget)
980 GdkGLContext *pGlContext = gtk_widget_get_gl_context (widget);
981 GdkGLDrawable *pGlDrawable = gtk_widget_get_gl_drawable (widget);
983 /* make GL-context "current" */
984 if (!gdk_gl_drawable_gl_begin (pGlDrawable, pGlContext))
985 return FALSE;
987 port->render_priv->in_context = true;
989 hidgl_start_render (port->render_priv->hidgl);
991 return TRUE;
994 void
995 ghid_end_drawing (GHidPort *port, GtkWidget *widget)
997 GdkGLDrawable *pGlDrawable = gtk_widget_get_gl_drawable (widget);
999 hidgl_finish_render (port->render_priv->hidgl);
1001 if (gdk_gl_drawable_is_double_buffered (pGlDrawable))
1002 gdk_gl_drawable_swap_buffers (pGlDrawable);
1003 else
1004 glFlush ();
1006 port->render_priv->in_context = false;
1008 /* end drawing to current GL-context */
1009 gdk_gl_drawable_gl_end (pGlDrawable);
1012 void
1013 ghid_screen_update (void)
1017 static int
1018 EMark_callback (const BoxType * b, void *cl)
1020 ElementType *element = (ElementType *) b;
1022 DrawEMark (element, element->MarkX, element->MarkY, !FRONT (element));
1023 return 1;
1026 static void
1027 set_object_color (AnyObjectType *obj, char *warn_color, char *selected_color,
1028 char *connected_color, char *found_color, char *normal_color)
1030 char *color;
1032 if (warn_color != NULL && TEST_FLAG (WARNFLAG, obj)) color = warn_color;
1033 else if (selected_color != NULL && TEST_FLAG (SELECTEDFLAG, obj)) color = selected_color;
1034 else if (connected_color != NULL && TEST_FLAG (CONNECTEDFLAG, obj)) color = connected_color;
1035 else if (found_color != NULL && TEST_FLAG (FOUNDFLAG, obj)) color = found_color;
1036 else color = normal_color;
1038 hid_draw_set_color (Output.fgGC, color);
1041 static void
1042 set_layer_object_color (LayerType *layer, AnyObjectType *obj)
1044 set_object_color (obj, NULL, layer->SelectedColor, PCB->ConnectedColor, PCB->FoundColor, layer->Color);
1047 static void
1048 set_pv_inlayer_color (PinType *pv, LayerType *layer, int type)
1050 if (TEST_FLAG (WARNFLAG, pv)) hid_draw_set_color (Output.fgGC, PCB->WarnColor);
1051 else if (TEST_FLAG (SELECTEDFLAG, pv)) hid_draw_set_color (Output.fgGC, (type == VIA_TYPE) ? PCB->ViaSelectedColor
1052 : PCB->PinSelectedColor);
1053 else if (TEST_FLAG (CONNECTEDFLAG, pv)) hid_draw_set_color (Output.fgGC, PCB->ConnectedColor);
1054 else if (TEST_FLAG (FOUNDFLAG, pv)) hid_draw_set_color (Output.fgGC, PCB->FoundColor);
1055 else
1057 int top_group = GetLayerGroupNumberBySide (TOP_SIDE);
1058 int bottom_group = GetLayerGroupNumberBySide (BOTTOM_SIDE);
1059 int this_group = GetLayerGroupNumberByPointer (layer);
1061 if (this_group == top_group || this_group == bottom_group)
1062 hid_draw_set_color (Output.fgGC, (SWAP_IDENT == (this_group == bottom_group)) ?
1063 PCB->ViaColor : PCB->InvisibleObjectsColor);
1064 else
1065 hid_draw_set_color (Output.fgGC, layer->Color);
1069 static void
1070 _draw_pv_name (PinType *pv)
1072 BoxType box;
1073 bool vert;
1074 TextType text;
1076 if (!pv->Name || !pv->Name[0])
1077 text.TextString = EMPTY (pv->Number);
1078 else
1079 text.TextString = EMPTY (TEST_FLAG (SHOWNUMBERFLAG, PCB) ? pv->Number : pv->Name);
1081 vert = TEST_FLAG (EDGE2FLAG, pv);
1083 if (vert)
1085 box.X1 = pv->X - pv->Thickness / 2 + Settings.PinoutTextOffsetY;
1086 box.Y1 = pv->Y - pv->DrillingHole / 2 - Settings.PinoutTextOffsetX;
1088 else
1090 box.X1 = pv->X + pv->DrillingHole / 2 + Settings.PinoutTextOffsetX;
1091 box.Y1 = pv->Y - pv->Thickness / 2 + Settings.PinoutTextOffsetY;
1094 hid_draw_set_color (Output.fgGC, PCB->PinNameColor);
1096 text.Flags = NoFlags ();
1097 /* Set font height to approx 56% of pin thickness */
1098 text.Scale = 56 * pv->Thickness / FONT_CAPHEIGHT;
1099 text.X = box.X1;
1100 text.Y = box.Y1;
1101 text.Direction = vert ? 1 : 0;
1103 hid_draw_pcb_text (Output.fgGC, &text, 0);
1106 static void
1107 _draw_pv (PinType *pv, bool draw_hole)
1109 if (TEST_FLAG (THINDRAWFLAG, PCB))
1110 hid_draw_thin_pcb_pv (Output.fgGC, Output.fgGC, pv, draw_hole, false);
1111 else
1112 hid_draw_fill_pcb_pv (Output.fgGC, Output.bgGC, pv, draw_hole, false);
1114 if (!TEST_FLAG (HOLEFLAG, pv) && TEST_FLAG (DISPLAYNAMEFLAG, pv))
1115 _draw_pv_name (pv);
1118 static void
1119 draw_pin (PinType *pin, bool draw_hole)
1121 set_object_color ((AnyObjectType *) pin, PCB->WarnColor, PCB->PinSelectedColor,
1122 PCB->ConnectedColor, PCB->FoundColor, PCB->PinColor);
1124 _draw_pv (pin, draw_hole);
1127 static int
1128 pin_callback (const BoxType * b, void *cl)
1130 PinType *pin = (PinType *) b;
1132 if (!TEST_FLAG (HOLEFLAG, pin) && TEST_FLAG (DISPLAYNAMEFLAG, pin))
1133 _draw_pv_name (pin);
1134 draw_pin (pin, TEST_FLAG (THINDRAWFLAG, PCB));
1135 return 1;
1138 static int
1139 pin_name_callback (const BoxType * b, void *cl)
1141 PinType *pin = (PinType *) b;
1143 if (!TEST_FLAG (HOLEFLAG, pin) && TEST_FLAG (DISPLAYNAMEFLAG, pin))
1144 _draw_pv_name (pin);
1145 return 1;
1148 static int
1149 pin_inlayer_callback (const BoxType * b, void *cl)
1151 set_pv_inlayer_color ((PinType *) b, cl, PIN_TYPE);
1152 _draw_pv ((PinType *) b, false);
1153 return 1;
1156 static void
1157 draw_via (PinType *via, bool draw_hole)
1159 set_object_color ((AnyObjectType *) via, PCB->WarnColor, PCB->ViaSelectedColor,
1160 PCB->ConnectedColor, PCB->FoundColor, PCB->ViaColor);
1162 _draw_pv (via, draw_hole);
1165 static int
1166 via_callback (const BoxType * b, void *cl)
1168 draw_via ((PinType *)b, TEST_FLAG (THINDRAWFLAG, PCB));
1169 return 1;
1172 static int
1173 via_inlayer_callback (const BoxType * b, void *cl)
1175 set_pv_inlayer_color ((PinType *) b, cl, VIA_TYPE);
1176 _draw_pv ((PinType *) b, TEST_FLAG (THINDRAWFLAG, PCB));
1177 return 1;
1180 static void
1181 draw_pad_name (PadType *pad)
1183 BoxType box;
1184 bool vert;
1185 TextType text;
1187 if (!pad->Name || !pad->Name[0])
1188 text.TextString = EMPTY (pad->Number);
1189 else
1190 text.TextString = EMPTY (TEST_FLAG (SHOWNUMBERFLAG, PCB) ? pad->Number : pad->Name);
1192 /* should text be vertical ? */
1193 vert = (pad->Point1.X == pad->Point2.X);
1195 if (vert)
1197 box.X1 = pad->Point1.X - pad->Thickness / 2;
1198 box.Y1 = MAX (pad->Point1.Y, pad->Point2.Y) + pad->Thickness / 2;
1199 box.X1 += Settings.PinoutTextOffsetY;
1200 box.Y1 -= Settings.PinoutTextOffsetX;
1202 else
1204 box.X1 = MIN (pad->Point1.X, pad->Point2.X) - pad->Thickness / 2;
1205 box.Y1 = pad->Point1.Y - pad->Thickness / 2;
1206 box.X1 += Settings.PinoutTextOffsetX;
1207 box.Y1 += Settings.PinoutTextOffsetY;
1210 hid_draw_set_color (Output.fgGC, PCB->PinNameColor);
1212 text.Flags = NoFlags ();
1213 /* Set font height to approx 90% of pad thickness */
1214 text.Scale = 90 * pad->Thickness / FONT_CAPHEIGHT;
1215 text.X = box.X1;
1216 text.Y = box.Y1;
1217 text.Direction = vert ? 1 : 0;
1219 hid_draw_pcb_text (Output.fgGC, &text, 0);
1222 static void
1223 _draw_pad (hidGC gc, PadType *pad, bool clear, bool mask)
1225 if (clear && !mask && pad->Clearance <= 0)
1226 return;
1228 if (TEST_FLAG (THINDRAWFLAG, PCB) ||
1229 (clear && TEST_FLAG (THINDRAWPOLYFLAG, PCB)))
1230 hid_draw_thin_pcb_pad (gc, pad, clear, mask);
1231 else
1232 hid_draw_fill_pcb_pad (gc, pad, clear, mask);
1235 static void
1236 draw_pad (PadType *pad)
1238 set_object_color ((AnyObjectType *)pad, PCB->WarnColor,
1239 PCB->PinSelectedColor, PCB->ConnectedColor, PCB->FoundColor,
1240 FRONT (pad) ? PCB->PinColor : PCB->InvisibleObjectsColor);
1242 _draw_pad (Output.fgGC, pad, false, false);
1244 if (TEST_FLAG (DISPLAYNAMEFLAG, pad))
1245 draw_pad_name (pad);
1248 static int
1249 pad_callback (const BoxType * b, void *cl)
1251 PadType *pad = (PadType *) b;
1252 int *side = cl;
1254 if (ON_SIDE (pad, *side)) {
1255 if (TEST_FLAG (DISPLAYNAMEFLAG, pad))
1256 draw_pad_name (pad);
1257 draw_pad (pad);
1259 return 1;
1263 static int
1264 hole_callback (const BoxType * b, void *cl)
1266 PinType *pv = (PinType *) b;
1267 int plated = cl ? *(int *) cl : -1;
1269 if ((plated == 0 && !TEST_FLAG (HOLEFLAG, pv)) ||
1270 (plated == 1 && TEST_FLAG (HOLEFLAG, pv)))
1271 return 1;
1273 if (TEST_FLAG (THINDRAWFLAG, PCB))
1275 if (!TEST_FLAG (HOLEFLAG, pv))
1277 hid_draw_set_line_cap (Output.fgGC, Round_Cap);
1278 hid_draw_set_line_width (Output.fgGC, 0);
1279 hid_draw_arc (Output.fgGC, pv->X, pv->Y,
1280 pv->DrillingHole / 2, pv->DrillingHole / 2, 0, 360);
1283 else
1284 hid_draw_fill_circle (Output.bgGC, pv->X, pv->Y, pv->DrillingHole / 2);
1286 if (TEST_FLAG (HOLEFLAG, pv))
1288 set_object_color ((AnyObjectType *) pv, PCB->WarnColor,
1289 PCB->ViaSelectedColor, NULL, NULL, Settings.BlackColor);
1291 hid_draw_set_line_cap (Output.fgGC, Round_Cap);
1292 hid_draw_set_line_width (Output.fgGC, 0);
1293 hid_draw_arc (Output.fgGC, pv->X, pv->Y,
1294 pv->DrillingHole / 2, pv->DrillingHole / 2, 0, 360);
1296 return 1;
1299 static int
1300 line_callback (const BoxType * b, void *cl)
1302 LayerType *layer = cl;
1303 LineType *line = (LineType *)b;
1305 set_layer_object_color (layer, (AnyObjectType *) line);
1306 hid_draw_pcb_line (Output.fgGC, line);
1307 return 1;
1310 static int
1311 arc_callback (const BoxType * b, void *cl)
1313 LayerType *layer = cl;
1314 ArcType *arc = (ArcType *)b;
1316 set_layer_object_color (layer, (AnyObjectType *) arc);
1317 hid_draw_pcb_arc (Output.fgGC, arc);
1318 return 1;
1321 static int
1322 text_callback (const BoxType * b, void *cl)
1324 LayerType *layer = cl;
1325 TextType *text = (TextType *)b;
1326 int min_silk_line;
1328 if (TEST_FLAG (SELECTEDFLAG, text))
1329 hid_draw_set_color (Output.fgGC, layer->SelectedColor);
1330 else
1331 hid_draw_set_color (Output.fgGC, layer->Color);
1332 if (layer == &PCB->Data->SILKLAYER ||
1333 layer == &PCB->Data->BACKSILKLAYER)
1334 min_silk_line = PCB->minSlk;
1335 else
1336 min_silk_line = PCB->minWid;
1337 hid_draw_pcb_text (Output.fgGC, text, min_silk_line);
1338 return 1;
1341 struct poly_info
1343 LayerType *layer;
1344 const BoxType *drawn_area;
1347 static int
1348 poly_callback (const BoxType * b, void *cl)
1350 struct poly_info *i = (struct poly_info *) cl;
1351 PolygonType *polygon = (PolygonType *)b;
1353 set_layer_object_color (i->layer, (AnyObjectType *) polygon);
1354 hid_draw_pcb_polygon (Output.fgGC, polygon, i->drawn_area);
1355 return 1;
1358 static int
1359 clearPin_callback (const BoxType * b, void *cl)
1361 PinType *pin = (PinType *) b;
1362 if (TEST_FLAG (THINDRAWFLAG, PCB) || TEST_FLAG (THINDRAWPOLYFLAG, PCB))
1363 hid_draw_thin_pcb_pv (Output.pmGC, Output.pmGC, pin, false, true);
1364 else
1365 hid_draw_fill_pcb_pv (Output.pmGC, Output.pmGC, pin, false, true);
1366 return 1;
1369 static int
1370 clearPad_callback (const BoxType * b, void *cl)
1372 PadType *pad = (PadType *) b;
1373 int *side = cl;
1374 if (ON_SIDE (pad, *side) && pad->Mask)
1375 _draw_pad (Output.pmGC, pad, true, true);
1376 return 1;
1379 static int
1380 clearPin_callback_solid (const BoxType * b, void *cl)
1382 PinType *pin = (PinType *) b;
1383 hid_draw_fill_pcb_pv (Output.pmGC, Output.pmGC, pin, false, true);
1384 return 1;
1387 static int
1388 clearPad_callback_solid (const BoxType * b, void *cl)
1390 PadType *pad = (PadType *) b;
1391 int *side = cl;
1392 if (ON_SIDE (pad, *side) && pad->Mask)
1393 hid_draw_fill_pcb_pad (Output.pmGC, pad, true, true);
1394 return 1;
1397 static void
1398 GhidDrawMask (int side, BoxType * screen)
1400 int thin = TEST_FLAG(THINDRAWFLAG, PCB) || TEST_FLAG(THINDRAWPOLYFLAG, PCB);
1401 LayerType *Layer = LAYER_PTR (side == TOP_SIDE ? top_soldermask_layer : bottom_soldermask_layer);
1402 struct poly_info info;
1403 PolygonType polygon;
1405 OutputType *out = &Output;
1407 if (thin)
1409 hid_draw_set_line_width (Output.pmGC, 0);
1410 hid_draw_set_color (Output.pmGC, PCB->MaskColor);
1411 r_search (PCB->Data->pin_tree, screen, NULL, clearPin_callback, NULL);
1412 r_search (PCB->Data->via_tree, screen, NULL, clearPin_callback, NULL);
1413 r_search (PCB->Data->pad_tree, screen, NULL, clearPad_callback, &side);
1414 hid_draw_set_color (Output.pmGC, "erase");
1417 hid_draw_use_mask (&ghid_graphics, HID_MASK_CLEAR);
1419 info.layer = Layer;
1420 info.drawn_area = screen;
1421 r_search (Layer->polygon_tree, screen, NULL, poly_callback, &info);
1422 r_search (Layer->line_tree, screen, NULL, line_callback, Layer);
1423 r_search (Layer->arc_tree, screen, NULL, arc_callback, Layer);
1424 r_search (Layer->text_tree, screen, NULL, text_callback, Layer);
1426 r_search (PCB->Data->pin_tree, screen, NULL, clearPin_callback_solid, NULL);
1427 r_search (PCB->Data->via_tree, screen, NULL, clearPin_callback_solid, NULL);
1428 r_search (PCB->Data->pad_tree, screen, NULL, clearPad_callback_solid, &side);
1430 hid_draw_use_mask (&ghid_graphics, HID_MASK_AFTER);
1431 hid_draw_set_color (out->fgGC, PCB->MaskColor);
1432 ghid_set_alpha_mult (out->fgGC, thin ? 0.35 : 1.0);
1434 memset (&polygon, 0, sizeof (polygon));
1435 polygon.Clipped = board_outline_poly ();
1436 polygon.BoundingBox = *screen;
1437 polygon.Flags = NoFlags ();
1438 SET_FLAG (FULLPOLYFLAG, &polygon);
1439 common_fill_pcb_polygon (out->fgGC, &polygon, screen);
1440 poly_Free (&polygon.Clipped);
1441 poly_FreeContours (&polygon.NoHoles);
1442 /* THE GL fill_pcb_polygon doesn't work whilst masking */
1443 // hid_draw_fill_pcb_polygon (out->fgGC, &polygon, screen);
1444 // hid_draw_fill_rect (out->fgGC, 0, 0, PCB->MaxWidth, PCB->MaxHeight);
1445 ghid_set_alpha_mult (out->fgGC, 1.0);
1447 hid_draw_use_mask (&ghid_graphics, HID_MASK_OFF);
1450 static int
1451 GhidDrawLayerGroup (int group, const BoxType * screen)
1453 render_priv *priv = gport->render_priv;
1454 int i;
1455 int layernum;
1456 int side;
1457 struct poly_info info;
1458 LayerType *Layer;
1459 int n_entries = PCB->LayerGroups.Number[group];
1460 Cardinal *layers = PCB->LayerGroups.Entries[group];
1461 int first_run = 1;
1462 int top_group = GetLayerGroupNumberBySide (TOP_SIDE);
1463 int bottom_group = GetLayerGroupNumberBySide (BOTTOM_SIDE);
1464 bool is_outline;
1466 if (!hid_draw_set_layer (&ghid_graphics, 0, group, 0))
1467 return 0;
1469 /* HACK: Subcomposite each layer in a layer group separately */
1470 for (i = n_entries - 1; i >= 0; i--) {
1471 layernum = layers[i];
1472 Layer = PCB->Data->Layer + layers[i];
1474 is_outline = strcmp (Layer->Name, "outline") == 0 ||
1475 strcmp (Layer->Name, "route") == 0;
1477 if (layernum < max_copper_layer && Layer->On) {
1479 if (!first_run)
1480 hid_draw_set_layer (&ghid_graphics, 0, group, 0);
1482 first_run = 0;
1484 if (!is_outline && !TEST_FLAG (THINDRAWFLAG, PCB)) {
1485 /* Mask out drilled holes on this layer */
1486 hidgl_flush_triangles (priv->hidgl);
1487 glPushAttrib (GL_COLOR_BUFFER_BIT);
1488 glColorMask (0, 0, 0, 0);
1489 hid_draw_set_color (Output.bgGC, PCB->MaskColor);
1490 if (PCB->PinOn) r_search (PCB->Data->pin_tree, screen, NULL, hole_callback, NULL);
1491 if (PCB->ViaOn) r_search (PCB->Data->via_tree, screen, NULL, hole_callback, NULL);
1492 hidgl_flush_triangles (priv->hidgl);
1493 glPopAttrib ();
1496 /* draw all polygons on this layer */
1497 if (Layer->PolygonN) {
1498 info.layer = Layer;
1499 info.drawn_area = screen;
1500 r_search (Layer->polygon_tree, screen, NULL, poly_callback, &info);
1502 /* HACK: Subcomposite polygons separately from other layer primitives */
1503 /* Reset the compositing */
1504 hid_draw_end_layer (&ghid_graphics);
1505 hid_draw_set_layer (&ghid_graphics, 0, group, 0);
1507 if (!is_outline && !TEST_FLAG (THINDRAWFLAG, PCB)) {
1508 hidgl_flush_triangles (priv->hidgl);
1509 glPushAttrib (GL_COLOR_BUFFER_BIT);
1510 glColorMask (0, 0, 0, 0);
1511 /* Mask out drilled holes on this layer */
1512 if (PCB->PinOn) r_search (PCB->Data->pin_tree, screen, NULL, hole_callback, NULL);
1513 if (PCB->ViaOn) r_search (PCB->Data->via_tree, screen, NULL, hole_callback, NULL);
1514 hidgl_flush_triangles (priv->hidgl);
1515 glPopAttrib ();
1519 /* Draw pins, vias and pads on this layer */
1520 if (!global_view_2d && !is_outline) {
1521 if (PCB->PinOn &&
1522 (group == bottom_group || group == top_group))
1523 r_search (PCB->Data->pin_tree, screen, NULL, pin_name_callback, Layer);
1524 if (PCB->PinOn) r_search (PCB->Data->pin_tree, screen, NULL, pin_inlayer_callback, Layer);
1525 if (PCB->ViaOn) r_search (PCB->Data->via_tree, screen, NULL, via_inlayer_callback, Layer);
1526 if (PCB->PinOn && group == top_group)
1528 side = TOP_SIDE;
1529 r_search (PCB->Data->pad_tree, screen, NULL, pad_callback, &side);
1531 if (PCB->PinOn && group == bottom_group)
1533 side = BOTTOM_SIDE;
1534 r_search (PCB->Data->pad_tree, screen, NULL, pad_callback, &side);
1538 if (TEST_FLAG (CHECKPLANESFLAG, PCB))
1539 continue;
1541 r_search (Layer->line_tree, screen, NULL, line_callback, Layer);
1542 r_search (Layer->arc_tree, screen, NULL, arc_callback, Layer);
1543 r_search (Layer->text_tree, screen, NULL, text_callback, Layer);
1547 hid_draw_end_layer (&ghid_graphics);
1549 return (n_entries > 1);
1552 static void
1553 DrawDrillChannel (hidGC gc, int vx, int vy, int vr, int from_layer, int to_layer, double scale)
1555 #define PIXELS_PER_CIRCLINE 5.
1556 #define MIN_FACES_PER_CYL 6
1557 #define MAX_FACES_PER_CYL 360
1558 float radius = vr;
1559 float x1, y1;
1560 float x2, y2;
1561 float z1, z2;
1562 int i;
1563 int slices;
1565 slices = M_PI * 2 * vr / scale / PIXELS_PER_CIRCLINE;
1567 if (slices < MIN_FACES_PER_CYL)
1568 slices = MIN_FACES_PER_CYL;
1570 if (slices > MAX_FACES_PER_CYL)
1571 slices = MAX_FACES_PER_CYL;
1573 z1 = compute_depth (from_layer);
1574 z2 = compute_depth (to_layer);
1576 x1 = vx + vr;
1577 y1 = vy;
1579 hidgl_ensure_triangle_space (gc, 2 * slices);
1580 for (i = 0; i < slices; i++)
1582 x2 = radius * cosf (((float)(i + 1)) * 2. * M_PI / (float)slices) + vx;
1583 y2 = radius * sinf (((float)(i + 1)) * 2. * M_PI / (float)slices) + vy;
1584 hidgl_add_triangle_3D (gc, x1, y1, z1, x2, y2, z1, x1, y1, z2);
1585 hidgl_add_triangle_3D (gc, x2, y2, z1, x1, y1, z2, x2, y2, z2);
1586 x1 = x2;
1587 y1 = y2;
1591 struct cyl_info {
1592 int from_layer;
1593 int to_layer;
1594 double scale;
1597 static int
1598 draw_hole_cyl (PinType *Pin, struct cyl_info *info, int Type)
1600 char *color;
1602 if (TEST_FLAG (WARNFLAG, Pin))
1603 color = PCB->WarnColor;
1604 else if (TEST_FLAG (SELECTEDFLAG, Pin))
1605 color = (Type == VIA_TYPE) ? PCB->ViaSelectedColor : PCB->PinSelectedColor;
1606 else if (TEST_FLAG (CONNECTEDFLAG, Pin))
1607 color = PCB->ConnectedColor;
1608 else if (TEST_FLAG (FOUNDFLAG, Pin))
1609 color = PCB->FoundColor;
1610 else
1611 color = "drill";
1613 hid_draw_set_color (Output.fgGC, color);
1614 DrawDrillChannel (Output.fgGC, Pin->X, Pin->Y, Pin->DrillingHole / 2, info->from_layer, info->to_layer, info->scale);
1615 return 0;
1618 static int
1619 pin_hole_cyl_callback (const BoxType * b, void *cl)
1621 return draw_hole_cyl ((PinType *)b, (struct cyl_info *)cl, PIN_TYPE);
1624 static int
1625 via_hole_cyl_callback (const BoxType * b, void *cl)
1627 return draw_hole_cyl ((PinType *)b, (struct cyl_info *)cl, VIA_TYPE);
1630 void
1631 ghid_draw_everything (BoxType *drawn_area)
1633 render_priv *priv = gport->render_priv;
1634 int i, ngroups;
1635 int number_phys_on_top;
1636 int side;
1637 /* This is the list of layer groups we will draw. */
1638 int do_group[MAX_LAYER];
1639 /* This is the reverse of the order in which we draw them. */
1640 int drawn_groups[MAX_LAYER];
1641 struct cyl_info cyl_info;
1642 int reverse_layers;
1643 int save_show_solder;
1644 int top_group;
1645 int bottom_group;
1646 int min_phys_group;
1647 int max_phys_group;
1649 priv->current_colorname = NULL;
1651 /* Test direction of rendering */
1652 /* Look at sign of eye coordinate system z-coord when projecting a
1653 world vector along +ve Z axis, (0, 0, 1). */
1654 /* XXX: This isn't strictly correct, as I've ignored the matrix
1655 elements for homogeneous coordinates. */
1656 /* NB: last_modelview_matrix is transposed in memory! */
1657 reverse_layers = (last_modelview_matrix[2][2] < 0);
1659 save_show_solder = Settings.ShowBottomSide;
1660 Settings.ShowBottomSide = reverse_layers;
1662 PCB->Data->SILKLAYER.Color = PCB->ElementColor;
1663 PCB->Data->BACKSILKLAYER.Color = PCB->InvisibleObjectsColor;
1665 top_group = GetLayerGroupNumberBySide (TOP_SIDE);
1666 bottom_group = GetLayerGroupNumberBySide (BOTTOM_SIDE);
1668 min_phys_group = MIN (bottom_group, top_group);
1669 max_phys_group = MAX (bottom_group, top_group);
1671 memset (do_group, 0, sizeof (do_group));
1672 if (global_view_2d) {
1673 /* Draw in layer stack order when in 2D view */
1674 for (ngroups = 0, i = 0; i < max_copper_layer; i++) {
1675 int group = GetLayerGroupNumberByNumber (LayerStack[i]);
1677 if (!do_group[group]) {
1678 do_group[group] = 1;
1679 drawn_groups[ngroups++] = group;
1682 } else {
1683 /* Draw in group order when in 3D view */
1684 for (ngroups = 0, i = 0; i < max_group; i++) {
1685 int group = reverse_layers ? max_group - 1 - i : i;
1687 if (!do_group[group]) {
1688 do_group[group] = 1;
1689 drawn_groups[ngroups++] = group;
1695 * first draw all 'invisible' stuff
1697 side = SWAP_IDENT ? TOP_SIDE : BOTTOM_SIDE;
1699 if (!TEST_FLAG (CHECKPLANESFLAG, PCB) &&
1700 hid_draw_set_layer (&ghid_graphics, "invisible", SL (INVISIBLE, 0), 0)) {
1701 DrawSilk (&ghid_graphics, side, drawn_area);
1703 if (global_view_2d)
1704 r_search (PCB->Data->pad_tree, drawn_area, NULL, pad_callback, &side);
1706 hid_draw_end_layer (&ghid_graphics);
1708 /* Draw the reverse-side solder mask if turned on */
1709 if (!global_view_2d &&
1710 hid_draw_set_layer (&ghid_graphics, SWAP_IDENT ? "componentmask" : "soldermask",
1711 SWAP_IDENT ? SL (MASK, TOP) : SL (MASK, BOTTOM), 0)) {
1712 GhidDrawMask (side, drawn_area);
1713 hid_draw_end_layer (&ghid_graphics);
1717 /* draw all layers in layerstack order */
1718 #define FADE_FACTOR 0.8
1719 number_phys_on_top = max_phys_group - min_phys_group;
1720 for (i = ngroups - 1; i >= 0; i--) {
1721 bool is_this_physical = drawn_groups[i] >= min_phys_group &&
1722 drawn_groups[i] <= max_phys_group;
1723 bool is_next_physical = i > 0 &&
1724 drawn_groups[i - 1] >= min_phys_group &&
1725 drawn_groups[i - 1] <= max_phys_group;
1727 double alpha_mult = global_view_2d ? pow (FADE_FACTOR, i) :
1728 (is_this_physical ? pow (FADE_FACTOR, number_phys_on_top) : 1.);
1730 if (is_this_physical)
1731 number_phys_on_top --;
1733 ghid_set_alpha_mult (Output.fgGC, alpha_mult);
1734 GhidDrawLayerGroup (drawn_groups [i], drawn_area);
1736 #if 1
1737 if (!global_view_2d && is_this_physical && is_next_physical) {
1738 cyl_info.from_layer = drawn_groups[i];
1739 cyl_info.to_layer = drawn_groups[i - 1];
1740 cyl_info.scale = gport->view.coord_per_px;
1741 hid_draw_set_color (Output.fgGC, "drill");
1742 ghid_set_alpha_mult (Output.fgGC, alpha_mult * 0.75);
1743 if (PCB->PinOn) r_search (PCB->Data->pin_tree, drawn_area, NULL, pin_hole_cyl_callback, &cyl_info);
1744 if (PCB->ViaOn) r_search (PCB->Data->via_tree, drawn_area, NULL, via_hole_cyl_callback, &cyl_info);
1746 #endif
1748 #undef FADE_FACTOR
1750 ghid_set_alpha_mult (Output.fgGC, 1.0);
1752 if (TEST_FLAG (CHECKPLANESFLAG, PCB))
1753 return;
1755 side = SWAP_IDENT ? BOTTOM_SIDE : TOP_SIDE;
1757 /* Draw pins, pads, vias below silk */
1758 if (global_view_2d) {
1759 start_subcomposite (priv->hidgl);
1761 if (!TEST_FLAG (THINDRAWFLAG, PCB)) {
1762 /* Mask out drilled holes */
1763 hidgl_flush_triangles (priv->hidgl);
1764 glPushAttrib (GL_COLOR_BUFFER_BIT);
1765 glColorMask (0, 0, 0, 0);
1766 if (PCB->PinOn) r_search (PCB->Data->pin_tree, drawn_area, NULL, hole_callback, NULL);
1767 if (PCB->ViaOn) r_search (PCB->Data->via_tree, drawn_area, NULL, hole_callback, NULL);
1768 hidgl_flush_triangles (priv->hidgl);
1769 glPopAttrib ();
1772 if (PCB->PinOn) r_search (PCB->Data->pad_tree, drawn_area, NULL, pad_callback, &side);
1773 if (PCB->PinOn) r_search (PCB->Data->pin_tree, drawn_area, NULL, pin_callback, NULL);
1774 if (PCB->ViaOn) r_search (PCB->Data->via_tree, drawn_area, NULL, via_callback, NULL);
1776 end_subcomposite (priv->hidgl);
1779 /* Draw the solder mask if turned on */
1780 if (hid_draw_set_layer (&ghid_graphics, SWAP_IDENT ? "soldermask" : "componentmask",
1781 SWAP_IDENT ? SL (MASK, BOTTOM) : SL (MASK, TOP), 0)) {
1782 GhidDrawMask (side, drawn_area);
1783 hid_draw_end_layer (&ghid_graphics);
1786 if (hid_draw_set_layer (&ghid_graphics, SWAP_IDENT ? "bottomsilk" : "topsilk",
1787 SWAP_IDENT ? SL (SILK, BOTTOM) : SL (SILK, TOP), 0)) {
1788 DrawSilk (&ghid_graphics, side, drawn_area);
1789 hid_draw_end_layer (&ghid_graphics);
1792 /* Draw element Marks */
1793 if (PCB->PinOn)
1794 r_search (PCB->Data->element_tree, drawn_area, NULL, EMark_callback, NULL);
1796 /* Draw rat lines on top */
1797 if (PCB->RatOn && hid_draw_set_layer (&ghid_graphics, "rats", SL (RATS, 0), 0)) {
1798 DrawRats (&ghid_graphics, drawn_area);
1799 hid_draw_end_layer (&ghid_graphics);
1802 Settings.ShowBottomSide = save_show_solder;
1805 #define Z_NEAR 3.0
1806 gboolean
1807 ghid_drawing_area_expose_cb (GtkWidget *widget,
1808 GdkEventExpose *ev,
1809 GHidPort *port)
1811 render_priv *priv = port->render_priv;
1812 GtkAllocation allocation;
1813 BoxType region;
1814 Coord min_x, min_y;
1815 Coord max_x, max_y;
1816 Coord new_x, new_y;
1817 Coord min_depth;
1818 Coord max_depth;
1820 gtk_widget_get_allocation (widget, &allocation);
1822 ghid_start_drawing (port, widget);
1824 Output.fgGC = hid_draw_make_gc (&ghid_graphics);
1825 Output.bgGC = hid_draw_make_gc (&ghid_graphics);
1826 Output.pmGC = hid_draw_make_gc (&ghid_graphics);
1828 /* If we don't have any stencil bits available,
1829 we can't use the hidgl polygon drawing routine */
1830 /* TODO: We could use the GLU tessellator though */
1831 if (hidgl_stencil_bits (priv->hidgl) == 0)
1832 ghid_graphics_class.fill_pcb_polygon = common_fill_pcb_polygon;
1834 glEnable (GL_BLEND);
1835 glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1837 glViewport (0, 0, allocation.width, allocation.height);
1839 glEnable (GL_SCISSOR_TEST);
1840 glScissor (ev->area.x,
1841 allocation.height - ev->area.height - ev->area.y,
1842 ev->area.width, ev->area.height);
1844 glMatrixMode (GL_PROJECTION);
1845 glLoadIdentity ();
1846 glOrtho (0, allocation.width, allocation.height, 0, -100000, 100000);
1847 glMatrixMode (GL_MODELVIEW);
1848 glLoadIdentity ();
1849 glTranslatef (widget->allocation.width / 2., widget->allocation.height / 2., 0);
1850 glMultMatrixf ((GLfloat *)view_matrix);
1851 glTranslatef (-widget->allocation.width / 2., -widget->allocation.height / 2., 0);
1852 glScalef ((port->view.flip_x ? -1. : 1.) / port->view.coord_per_px,
1853 (port->view.flip_y ? -1. : 1.) / port->view.coord_per_px,
1854 ((port->view.flip_x == port->view.flip_y) ? 1. : -1.) / port->view.coord_per_px);
1855 glTranslatef (port->view.flip_x ? port->view.x0 - PCB->MaxWidth :
1856 -port->view.x0,
1857 port->view.flip_y ? port->view.y0 - PCB->MaxHeight :
1858 -port->view.y0, 0);
1859 glGetFloatv (GL_MODELVIEW_MATRIX, (GLfloat *)last_modelview_matrix);
1861 glEnable (GL_STENCIL_TEST);
1862 glClearColor (port->offlimits_color.red / 65535.,
1863 port->offlimits_color.green / 65535.,
1864 port->offlimits_color.blue / 65535.,
1865 1.);
1866 glStencilMask (~0);
1867 glClearStencil (0);
1868 glClear (GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
1869 hidgl_reset_stencil_usage (priv->hidgl);
1871 /* Disable the stencil test until we need it - otherwise it gets dirty */
1872 glDisable (GL_STENCIL_TEST);
1873 glStencilMask (0);
1874 glStencilFunc (GL_ALWAYS, 0, 0);
1876 /* Test the 8 corners of a cube spanning the event */
1877 min_depth = -50 + compute_depth (0); /* FIXME: NEED TO USE PHYSICAL GROUPS */
1878 max_depth = 50 + compute_depth (max_copper_layer - 1); /* FIXME: NEED TO USE PHYSICAL GROUPS */
1880 ghid_unproject_to_z_plane (ev->area.x,
1881 ev->area.y,
1882 min_depth, &new_x, &new_y);
1883 max_x = min_x = new_x;
1884 max_y = min_y = new_y;
1886 ghid_unproject_to_z_plane (ev->area.x,
1887 ev->area.y,
1888 max_depth, &new_x, &new_y);
1889 min_x = MIN (min_x, new_x); max_x = MAX (max_x, new_x);
1890 min_y = MIN (min_y, new_y); max_y = MAX (max_y, new_y);
1892 /* */
1893 ghid_unproject_to_z_plane (ev->area.x + ev->area.width,
1894 ev->area.y,
1895 min_depth, &new_x, &new_y);
1896 min_x = MIN (min_x, new_x); max_x = MAX (max_x, new_x);
1897 min_y = MIN (min_y, new_y); max_y = MAX (max_y, new_y);
1899 ghid_unproject_to_z_plane (ev->area.x + ev->area.width, ev->area.y,
1900 max_depth, &new_x, &new_y);
1901 min_x = MIN (min_x, new_x); max_x = MAX (max_x, new_x);
1902 min_y = MIN (min_y, new_y); max_y = MAX (max_y, new_y);
1904 /* */
1905 ghid_unproject_to_z_plane (ev->area.x + ev->area.width,
1906 ev->area.y + ev->area.height,
1907 min_depth, &new_x, &new_y);
1908 min_x = MIN (min_x, new_x); max_x = MAX (max_x, new_x);
1909 min_y = MIN (min_y, new_y); max_y = MAX (max_y, new_y);
1911 ghid_unproject_to_z_plane (ev->area.x + ev->area.width,
1912 ev->area.y + ev->area.height,
1913 max_depth, &new_x, &new_y);
1914 min_x = MIN (min_x, new_x); max_x = MAX (max_x, new_x);
1915 min_y = MIN (min_y, new_y); max_y = MAX (max_y, new_y);
1917 /* */
1918 ghid_unproject_to_z_plane (ev->area.x,
1919 ev->area.y + ev->area.height,
1920 min_depth,
1921 &new_x, &new_y);
1922 min_x = MIN (min_x, new_x); max_x = MAX (max_x, new_x);
1923 min_y = MIN (min_y, new_y); max_y = MAX (max_y, new_y);
1925 ghid_unproject_to_z_plane (ev->area.x,
1926 ev->area.y + ev->area.height,
1927 max_depth,
1928 &new_x, &new_y);
1929 min_x = MIN (min_x, new_x); max_x = MAX (max_x, new_x);
1930 min_y = MIN (min_y, new_y); max_y = MAX (max_y, new_y);
1932 region.X1 = min_x; region.X2 = max_x + 1;
1933 region.Y1 = min_y; region.Y2 = max_y + 1;
1935 region.X1 = MAX (0, MIN (PCB->MaxWidth, region.X1));
1936 region.X2 = MAX (0, MIN (PCB->MaxWidth, region.X2));
1937 region.Y1 = MAX (0, MIN (PCB->MaxHeight, region.Y1));
1938 region.Y2 = MAX (0, MIN (PCB->MaxHeight, region.Y2));
1940 glColor3f (port->bg_color.red / 65535.,
1941 port->bg_color.green / 65535.,
1942 port->bg_color.blue / 65535.);
1944 ghid_invalidate_current_gc ();
1946 /* Setup stenciling */
1947 /* Drawing operations set the stencil buffer to '1' */
1948 glStencilOp (GL_KEEP, GL_KEEP, GL_REPLACE); /* Stencil pass => replace stencil value (with 1) */
1949 /* Drawing operations as masked to areas where the stencil buffer is '0' */
1950 /* glStencilFunc (GL_GREATER, 1, 1); */ /* Draw only where stencil buffer is 0 */
1952 if (global_view_2d) {
1953 glBegin (GL_QUADS);
1954 glVertex3i (0, 0, 0);
1955 glVertex3i (PCB->MaxWidth, 0, 0);
1956 glVertex3i (PCB->MaxWidth, PCB->MaxHeight, 0);
1957 glVertex3i (0, PCB->MaxHeight, 0);
1958 glEnd ();
1959 } else {
1960 int top_group;
1961 int bottom_group;
1962 int min_phys_group;
1963 int max_phys_group;
1964 int i;
1966 top_group = GetLayerGroupNumberBySide (TOP_SIDE);
1967 bottom_group = GetLayerGroupNumberBySide (BOTTOM_SIDE);
1969 min_phys_group = MIN (bottom_group, top_group);
1970 max_phys_group = MAX (bottom_group, top_group);
1972 glBegin (GL_QUADS);
1973 for (i = min_phys_group; i <= max_phys_group; i++) {
1974 int depth = compute_depth (i);
1975 glVertex3i (0, 0, depth);
1976 glVertex3i (PCB->MaxWidth, 0, depth);
1977 glVertex3i (PCB->MaxWidth, PCB->MaxHeight, depth);
1978 glVertex3i (0, PCB->MaxHeight, depth);
1980 glEnd ();
1983 ghid_draw_bg_image ();
1985 /* hid_expose_callback (&ghid_graphics, &region, 0); */
1986 ghid_draw_everything (&region);
1987 hidgl_flush_triangles (priv->hidgl);
1989 /* Set the current depth to the right value for the layer we are editing */
1990 priv->edit_depth = compute_depth (GetLayerGroupNumberByNumber (INDEXOFCURRENT));
1991 hidgl_set_depth (Output.fgGC, priv->edit_depth);
1993 ghid_draw_grid (Output.fgGC, &region);
1995 ghid_invalidate_current_gc ();
1997 DrawAttached (Output.fgGC);
1998 DrawMark (Output.fgGC);
1999 hidgl_flush_triangles (priv->hidgl);
2001 draw_crosshair (Output.fgGC, priv);
2002 hidgl_flush_triangles (priv->hidgl);
2004 draw_lead_user (Output.fgGC, priv);
2006 ghid_end_drawing (port, widget);
2008 hid_draw_destroy_gc (Output.fgGC);
2009 hid_draw_destroy_gc (Output.bgGC);
2010 hid_draw_destroy_gc (Output.pmGC);
2012 Output.fgGC = NULL;
2013 Output.bgGC = NULL;
2014 Output.pmGC = NULL;
2015 g_timer_start (priv->time_since_expose);
2017 return FALSE;
2020 /* This realize callback is used to work around a crash bug in some mesa
2021 * versions (observed on a machine running the intel i965 driver. It isn't
2022 * obvious why it helps, but somehow fiddling with the GL context here solves
2023 * the issue. The problem appears to have been fixed in recent mesa versions.
2025 void
2026 ghid_port_drawing_realize_cb (GtkWidget *widget, gpointer data)
2028 GdkGLContext *glcontext = gtk_widget_get_gl_context (widget);
2029 GdkGLDrawable *gldrawable = gtk_widget_get_gl_drawable (widget);
2031 if (!gdk_gl_drawable_gl_begin (gldrawable, glcontext))
2032 return;
2034 gdk_gl_drawable_gl_end (gldrawable);
2035 return;
2038 gboolean
2039 ghid_pinout_preview_expose (GtkWidget *widget,
2040 GdkEventExpose *ev)
2042 GhidPinoutPreview *pinout = GHID_PINOUT_PREVIEW (widget);
2043 render_priv *priv = gport->render_priv;
2044 GtkAllocation allocation;
2045 view_data save_view;
2046 int save_width, save_height;
2047 Coord save_max_width;
2048 Coord save_max_height;
2049 double xz, yz;
2051 save_view = gport->view;
2052 save_width = gport->width;
2053 save_height = gport->height;
2054 save_max_width = PCB->MaxWidth;
2055 save_max_height = PCB->MaxHeight;
2057 /* Setup zoom factor for drawing routines */
2059 gtk_widget_get_allocation (widget, &allocation);
2060 xz = (double) pinout->x_max / allocation.width;
2061 yz = (double) pinout->y_max / allocation.height;
2062 if (xz > yz)
2063 gport->view.coord_per_px = xz;
2064 else
2065 gport->view.coord_per_px = yz;
2067 gport->width = allocation.width;
2068 gport->height = allocation.height;
2069 gport->view.width = allocation.width * gport->view.coord_per_px;
2070 gport->view.height = allocation.height * gport->view.coord_per_px;
2071 gport->view.x0 = (pinout->x_max - gport->view.width) / 2;
2072 gport->view.y0 = (pinout->y_max - gport->view.height) / 2;
2073 PCB->MaxWidth = pinout->x_max;
2074 PCB->MaxHeight = pinout->y_max;
2076 ghid_start_drawing (gport, widget);
2078 #if 0 /* We disable alpha blending here, as hid_expose_callback() does not
2079 * call set_layer() as appropriate for us to sub-composite rendering
2080 * from each layer when drawing a single element. If we leave alpha-
2081 * blending on, it means text and overlapping pads are rendered ugly.
2084 glEnable (GL_BLEND);
2085 glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
2086 #endif
2088 glViewport (0, 0, allocation.width, allocation.height);
2090 #if 0 /* We disable the scissor test here, as it is interacting badly with
2091 * being handed expose events which don't cover the whole window.
2092 * As we have a double-buffered GL window, we end up with unintialised
2093 * contents remaining in the unpainted areas (outside the scissor
2094 * region), and these are being flipped onto the screen.
2096 * The debugging code below shows multiple expose events when the
2097 * window is shown the first time, some of which are very small.
2099 * XXX: There is clearly a perforamnce issue here, in that we may
2100 * be rendering the preview more times, and over a larger area
2101 * than is really required.
2104 glEnable (GL_SCISSOR_TEST);
2105 glScissor (ev->area.x,
2106 allocation.height - ev->area.height - ev->area.y,
2107 ev->area.width, ev->area.height);
2108 #endif
2110 #ifdef DEBUG
2111 printf ("EVT: %i, %i, w=%i, h=%i, Scissor setup: glScissor (%f, %f, %f, %f);\n",
2112 ev->area.x, ev->area.y, ev->area.width, ev->area.height,
2113 (double)ev->area.x,
2114 (double)(allocation.height - ev->area.height - ev->area.y),
2115 (double)ev->area.width,
2116 (double)ev->area.height);
2117 #endif
2119 glMatrixMode (GL_PROJECTION);
2120 glLoadIdentity ();
2121 glOrtho (0, allocation.width, allocation.height, 0, -100000, 100000);
2122 glMatrixMode (GL_MODELVIEW);
2123 glLoadIdentity ();
2124 glTranslatef (0.0f, 0.0f, -Z_NEAR);
2126 glClearColor (gport->bg_color.red / 65535.,
2127 gport->bg_color.green / 65535.,
2128 gport->bg_color.blue / 65535.,
2129 1.);
2130 glStencilMask (~0);
2131 glClearStencil (0);
2132 glClear (GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
2133 hidgl_reset_stencil_usage (priv->hidgl);
2135 /* Disable the stencil test until we need it - otherwise it gets dirty */
2136 glDisable (GL_STENCIL_TEST);
2137 glStencilMask (0);
2138 glStencilFunc (GL_ALWAYS, 0, 0);
2140 /* call the drawing routine */
2141 ghid_invalidate_current_gc ();
2142 glPushMatrix ();
2143 glScalef ((gport->view.flip_x ? -1. : 1.) / gport->view.coord_per_px,
2144 (gport->view.flip_y ? -1. : 1.) / gport->view.coord_per_px,
2145 ((gport->view.flip_x == gport->view.flip_y) ? 1. : -1.) / gport->view.coord_per_px);
2146 glTranslatef (gport->view.flip_x ? gport->view.x0 - PCB->MaxWidth :
2147 -gport->view.x0,
2148 gport->view.flip_y ? gport->view.y0 - PCB->MaxHeight :
2149 -gport->view.y0, 0);
2151 hid_expose_callback (&ghid_graphics, NULL, pinout->element);
2152 hidgl_flush_triangles (priv->hidgl);
2153 glPopMatrix ();
2155 ghid_end_drawing (gport, widget);
2157 gport->view = save_view;
2158 gport->width = save_width;
2159 gport->height = save_height;
2160 PCB->MaxWidth = save_max_width;
2161 PCB->MaxHeight = save_max_height;
2163 return FALSE;
2167 GdkPixmap *
2168 ghid_render_pixmap (int cx, int cy, double zoom, int width, int height, int depth)
2170 render_priv *priv = gport->render_priv;
2171 GdkGLConfig *glconfig;
2172 GdkPixmap *pixmap;
2173 GdkGLPixmap *glpixmap;
2174 GdkGLContext* glcontext;
2175 GdkGLDrawable* gldrawable;
2176 view_data save_view;
2177 int save_width, save_height;
2178 BoxType region;
2180 save_view = gport->view;
2181 save_width = gport->width;
2182 save_height = gport->height;
2184 /* Setup rendering context for drawing routines
2187 glconfig = gdk_gl_config_new_by_mode (GDK_GL_MODE_RGB |
2188 GDK_GL_MODE_STENCIL |
2189 GDK_GL_MODE_SINGLE);
2191 pixmap = gdk_pixmap_new (NULL, width, height, depth);
2192 glpixmap = gdk_pixmap_set_gl_capability (pixmap, glconfig, NULL);
2193 gldrawable = GDK_GL_DRAWABLE (glpixmap);
2194 glcontext = gdk_gl_context_new (gldrawable, NULL, TRUE, GDK_GL_RGBA_TYPE);
2196 /* Setup zoom factor for drawing routines */
2198 gport->view.coord_per_px = zoom;
2199 gport->width = width;
2200 gport->height = height;
2201 gport->view.width = width * gport->view.coord_per_px;
2202 gport->view.height = height * gport->view.coord_per_px;
2203 gport->view.x0 = gport->view.flip_x ? PCB->MaxWidth - cx : cx;
2204 gport->view.x0 -= gport->view.height / 2;
2205 gport->view.y0 = gport->view.flip_y ? PCB->MaxHeight - cy : cy;
2206 gport->view.y0 -= gport->view.width / 2;
2208 /* make GL-context "current" */
2209 if (!gdk_gl_drawable_gl_begin (gldrawable, glcontext)) {
2210 return NULL;
2212 hidgl_start_render (priv->hidgl);
2213 gport->render_priv->in_context = true;
2215 glEnable (GL_BLEND);
2216 glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
2218 glViewport (0, 0, width, height);
2220 glEnable (GL_SCISSOR_TEST);
2221 glScissor (0, 0, width, height);
2223 glMatrixMode (GL_PROJECTION);
2224 glLoadIdentity ();
2225 glOrtho (0, width, height, 0, -100000, 100000);
2226 glMatrixMode (GL_MODELVIEW);
2227 glLoadIdentity ();
2228 glTranslatef (0.0f, 0.0f, -Z_NEAR);
2230 glClearColor (gport->bg_color.red / 65535.,
2231 gport->bg_color.green / 65535.,
2232 gport->bg_color.blue / 65535.,
2233 1.);
2234 glStencilMask (~0);
2235 glClearStencil (0);
2236 glClear (GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
2237 hidgl_reset_stencil_usage (priv->hidgl);
2239 /* Disable the stencil test until we need it - otherwise it gets dirty */
2240 glDisable (GL_STENCIL_TEST);
2241 glStencilMask (0);
2242 glStencilFunc (GL_ALWAYS, 0, 0);
2244 /* call the drawing routine */
2245 ghid_invalidate_current_gc ();
2246 glPushMatrix ();
2247 glScalef ((gport->view.flip_x ? -1. : 1.) / gport->view.coord_per_px,
2248 (gport->view.flip_y ? -1. : 1.) / gport->view.coord_per_px,
2249 ((gport->view.flip_x == gport->view.flip_y) ? 1. : -1.) / gport->view.coord_per_px);
2250 glTranslatef (gport->view.flip_x ? gport->view.x0 - PCB->MaxWidth :
2251 -gport->view.x0,
2252 gport->view.flip_y ? gport->view.y0 - PCB->MaxHeight :
2253 -gport->view.y0, 0);
2255 region.X1 = MIN(Px(0), Px(gport->width + 1));
2256 region.Y1 = MIN(Py(0), Py(gport->height + 1));
2257 region.X2 = MAX(Px(0), Px(gport->width + 1));
2258 region.Y2 = MAX(Py(0), Py(gport->height + 1));
2260 region.X1 = MAX (0, MIN (PCB->MaxWidth, region.X1));
2261 region.X2 = MAX (0, MIN (PCB->MaxWidth, region.X2));
2262 region.Y1 = MAX (0, MIN (PCB->MaxHeight, region.Y1));
2263 region.Y2 = MAX (0, MIN (PCB->MaxHeight, region.Y2));
2265 hid_expose_callback (&ghid_graphics, &region, NULL);
2266 hidgl_flush_triangles (priv->hidgl);
2267 glPopMatrix ();
2269 glFlush ();
2271 hidgl_finish_render (priv->hidgl);
2273 /* end drawing to current GL-context */
2274 gport->render_priv->in_context = false;
2275 gdk_gl_drawable_gl_end (gldrawable);
2277 gdk_pixmap_unset_gl_capability (pixmap);
2279 g_object_unref (glconfig);
2280 g_object_unref (glcontext);
2282 gport->view = save_view;
2283 gport->width = save_width;
2284 gport->height = save_height;
2286 return pixmap;
2289 HID_DRAW *
2290 ghid_request_debug_draw (void)
2292 GHidPort *port = gport;
2293 GtkWidget *widget = port->drawing_area;
2294 GtkAllocation allocation;
2296 gtk_widget_get_allocation (widget, &allocation);
2298 ghid_start_drawing (port, widget);
2300 glViewport (0, 0, allocation.width, allocation.height);
2302 glMatrixMode (GL_PROJECTION);
2303 glLoadIdentity ();
2304 glOrtho (0, allocation.width, allocation.height, 0, 0, 100);
2305 glMatrixMode (GL_MODELVIEW);
2306 glLoadIdentity ();
2307 glTranslatef (0.0f, 0.0f, -Z_NEAR);
2309 ghid_invalidate_current_gc ();
2311 /* Setup stenciling */
2312 glDisable (GL_STENCIL_TEST);
2314 glPushMatrix ();
2315 glScalef ((port->view.flip_x ? -1. : 1.) / port->view.coord_per_px,
2316 (port->view.flip_y ? -1. : 1.) / port->view.coord_per_px,
2317 ((gport->view.flip_x == port->view.flip_y) ? 1. : -1.) / gport->view.coord_per_px);
2318 glTranslatef (port->view.flip_x ? port->view.x0 - PCB->MaxWidth :
2319 -port->view.x0,
2320 port->view.flip_y ? port->view.y0 - PCB->MaxHeight :
2321 -port->view.y0, 0);
2323 return &ghid_graphics;
2326 void
2327 ghid_flush_debug_draw (void)
2329 render_priv *priv = gport->render_priv;
2330 GtkWidget *widget = gport->drawing_area;
2331 GdkGLDrawable *pGlDrawable = gtk_widget_get_gl_drawable (widget);
2333 hidgl_flush_triangles (priv->hidgl);
2335 if (gdk_gl_drawable_is_double_buffered (pGlDrawable))
2336 gdk_gl_drawable_swap_buffers (pGlDrawable);
2337 else
2338 glFlush ();
2341 void
2342 ghid_finish_debug_draw (void)
2344 render_priv *priv = gport->render_priv;
2346 hidgl_flush_triangles (priv->hidgl);
2347 glPopMatrix ();
2349 ghid_end_drawing (gport, gport->drawing_area);
2352 static float
2353 determinant_2x2 (float m[2][2])
2355 float det;
2356 det = m[0][0] * m[1][1] -
2357 m[0][1] * m[1][0];
2358 return det;
2361 #if 0
2362 static float
2363 determinant_4x4 (float m[4][4])
2365 float det;
2366 det = m[0][3] * m[1][2] * m[2][1] * m[3][0]-m[0][2] * m[1][3] * m[2][1] * m[3][0] -
2367 m[0][3] * m[1][1] * m[2][2] * m[3][0]+m[0][1] * m[1][3] * m[2][2] * m[3][0] +
2368 m[0][2] * m[1][1] * m[2][3] * m[3][0]-m[0][1] * m[1][2] * m[2][3] * m[3][0] -
2369 m[0][3] * m[1][2] * m[2][0] * m[3][1]+m[0][2] * m[1][3] * m[2][0] * m[3][1] +
2370 m[0][3] * m[1][0] * m[2][2] * m[3][1]-m[0][0] * m[1][3] * m[2][2] * m[3][1] -
2371 m[0][2] * m[1][0] * m[2][3] * m[3][1]+m[0][0] * m[1][2] * m[2][3] * m[3][1] +
2372 m[0][3] * m[1][1] * m[2][0] * m[3][2]-m[0][1] * m[1][3] * m[2][0] * m[3][2] -
2373 m[0][3] * m[1][0] * m[2][1] * m[3][2]+m[0][0] * m[1][3] * m[2][1] * m[3][2] +
2374 m[0][1] * m[1][0] * m[2][3] * m[3][2]-m[0][0] * m[1][1] * m[2][3] * m[3][2] -
2375 m[0][2] * m[1][1] * m[2][0] * m[3][3]+m[0][1] * m[1][2] * m[2][0] * m[3][3] +
2376 m[0][2] * m[1][0] * m[2][1] * m[3][3]-m[0][0] * m[1][2] * m[2][1] * m[3][3] -
2377 m[0][1] * m[1][0] * m[2][2] * m[3][3]+m[0][0] * m[1][1] * m[2][2] * m[3][3];
2378 return det;
2380 #endif
2382 static void
2383 invert_2x2 (float m[2][2], float out[2][2])
2385 float scale = 1 / determinant_2x2 (m);
2386 out[0][0] = m[1][1] * scale;
2387 out[0][1] = -m[0][1] * scale;
2388 out[1][0] = -m[1][0] * scale;
2389 out[1][1] = m[0][0] * scale;
2392 #if 0
2393 static void
2394 invert_4x4 (float m[4][4], float out[4][4])
2396 float scale = 1 / determinant_4x4 (m);
2398 out[0][0] = (m[1][2] * m[2][3] * m[3][1] - m[1][3] * m[2][2] * m[3][1] +
2399 m[1][3] * m[2][1] * m[3][2] - m[1][1] * m[2][3] * m[3][2] -
2400 m[1][2] * m[2][1] * m[3][3] + m[1][1] * m[2][2] * m[3][3]) * scale;
2401 out[0][1] = (m[0][3] * m[2][2] * m[3][1] - m[0][2] * m[2][3] * m[3][1] -
2402 m[0][3] * m[2][1] * m[3][2] + m[0][1] * m[2][3] * m[3][2] +
2403 m[0][2] * m[2][1] * m[3][3] - m[0][1] * m[2][2] * m[3][3]) * scale;
2404 out[0][2] = (m[0][2] * m[1][3] * m[3][1] - m[0][3] * m[1][2] * m[3][1] +
2405 m[0][3] * m[1][1] * m[3][2] - m[0][1] * m[1][3] * m[3][2] -
2406 m[0][2] * m[1][1] * m[3][3] + m[0][1] * m[1][2] * m[3][3]) * scale;
2407 out[0][3] = (m[0][3] * m[1][2] * m[2][1] - m[0][2] * m[1][3] * m[2][1] -
2408 m[0][3] * m[1][1] * m[2][2] + m[0][1] * m[1][3] * m[2][2] +
2409 m[0][2] * m[1][1] * m[2][3] - m[0][1] * m[1][2] * m[2][3]) * scale;
2410 out[1][0] = (m[1][3] * m[2][2] * m[3][0] - m[1][2] * m[2][3] * m[3][0] -
2411 m[1][3] * m[2][0] * m[3][2] + m[1][0] * m[2][3] * m[3][2] +
2412 m[1][2] * m[2][0] * m[3][3] - m[1][0] * m[2][2] * m[3][3]) * scale;
2413 out[1][1] = (m[0][2] * m[2][3] * m[3][0] - m[0][3] * m[2][2] * m[3][0] +
2414 m[0][3] * m[2][0] * m[3][2] - m[0][0] * m[2][3] * m[3][2] -
2415 m[0][2] * m[2][0] * m[3][3] + m[0][0] * m[2][2] * m[3][3]) * scale;
2416 out[1][2] = (m[0][3] * m[1][2] * m[3][0] - m[0][2] * m[1][3] * m[3][0] -
2417 m[0][3] * m[1][0] * m[3][2] + m[0][0] * m[1][3] * m[3][2] +
2418 m[0][2] * m[1][0] * m[3][3] - m[0][0] * m[1][2] * m[3][3]) * scale;
2419 out[1][3] = (m[0][2] * m[1][3] * m[2][0] - m[0][3] * m[1][2] * m[2][0] +
2420 m[0][3] * m[1][0] * m[2][2] - m[0][0] * m[1][3] * m[2][2] -
2421 m[0][2] * m[1][0] * m[2][3] + m[0][0] * m[1][2] * m[2][3]) * scale;
2422 out[2][0] = (m[1][1] * m[2][3] * m[3][0] - m[1][3] * m[2][1] * m[3][0] +
2423 m[1][3] * m[2][0] * m[3][1] - m[1][0] * m[2][3] * m[3][1] -
2424 m[1][1] * m[2][0] * m[3][3] + m[1][0] * m[2][1] * m[3][3]) * scale;
2425 out[2][1] = (m[0][3] * m[2][1] * m[3][0] - m[0][1] * m[2][3] * m[3][0] -
2426 m[0][3] * m[2][0] * m[3][1] + m[0][0] * m[2][3] * m[3][1] +
2427 m[0][1] * m[2][0] * m[3][3] - m[0][0] * m[2][1] * m[3][3]) * scale;
2428 out[2][2] = (m[0][1] * m[1][3] * m[3][0] - m[0][3] * m[1][1] * m[3][0] +
2429 m[0][3] * m[1][0] * m[3][1] - m[0][0] * m[1][3] * m[3][1] -
2430 m[0][1] * m[1][0] * m[3][3] + m[0][0] * m[1][1] * m[3][3]) * scale;
2431 out[2][3] = (m[0][3] * m[1][1] * m[2][0] - m[0][1] * m[1][3] * m[2][0] -
2432 m[0][3] * m[1][0] * m[2][1] + m[0][0] * m[1][3] * m[2][1] +
2433 m[0][1] * m[1][0] * m[2][3] - m[0][0] * m[1][1] * m[2][3]) * scale;
2434 out[3][0] = (m[1][2] * m[2][1] * m[3][0] - m[1][1] * m[2][2] * m[3][0] -
2435 m[1][2] * m[2][0] * m[3][1] + m[1][0] * m[2][2] * m[3][1] +
2436 m[1][1] * m[2][0] * m[3][2] - m[1][0] * m[2][1] * m[3][2]) * scale;
2437 out[3][1] = (m[0][1] * m[2][2] * m[3][0] - m[0][2] * m[2][1] * m[3][0] +
2438 m[0][2] * m[2][0] * m[3][1] - m[0][0] * m[2][2] * m[3][1] -
2439 m[0][1] * m[2][0] * m[3][2] + m[0][0] * m[2][1] * m[3][2]) * scale;
2440 out[3][2] = (m[0][2] * m[1][1] * m[3][0] - m[0][1] * m[1][2] * m[3][0] -
2441 m[0][2] * m[1][0] * m[3][1] + m[0][0] * m[1][2] * m[3][1] +
2442 m[0][1] * m[1][0] * m[3][2] - m[0][0] * m[1][1] * m[3][2]) * scale;
2443 out[3][3] = (m[0][1] * m[1][2] * m[2][0] - m[0][2] * m[1][1] * m[2][0] +
2444 m[0][2] * m[1][0] * m[2][1] - m[0][0] * m[1][2] * m[2][1] -
2445 m[0][1] * m[1][0] * m[2][2] + m[0][0] * m[1][1] * m[2][2]) * scale;
2447 #endif
2450 static void
2451 ghid_unproject_to_z_plane (int ex, int ey, Coord pcb_z, Coord *pcb_x, Coord *pcb_y)
2453 float mat[2][2];
2454 float inv_mat[2][2];
2455 float x, y;
2458 ex = view_matrix[0][0] * vx +
2459 view_matrix[0][1] * vy +
2460 view_matrix[0][2] * vz +
2461 view_matrix[0][3] * 1;
2462 ey = view_matrix[1][0] * vx +
2463 view_matrix[1][1] * vy +
2464 view_matrix[1][2] * vz +
2465 view_matrix[1][3] * 1;
2466 UNKNOWN ez = view_matrix[2][0] * vx +
2467 view_matrix[2][1] * vy +
2468 view_matrix[2][2] * vz +
2469 view_matrix[2][3] * 1;
2471 ex - view_matrix[0][3] * 1
2472 - view_matrix[0][2] * vz
2473 = view_matrix[0][0] * vx +
2474 view_matrix[0][1] * vy;
2476 ey - view_matrix[1][3] * 1
2477 - view_matrix[1][2] * vz
2478 = view_matrix[1][0] * vx +
2479 view_matrix[1][1] * vy;
2482 /* NB: last_modelview_matrix is transposed in memory! */
2483 x = (float)ex - last_modelview_matrix[3][0] * 1
2484 - last_modelview_matrix[2][0] * pcb_z;
2486 y = (float)ey - last_modelview_matrix[3][1] * 1
2487 - last_modelview_matrix[2][1] * pcb_z;
2490 x = view_matrix[0][0] * vx +
2491 view_matrix[0][1] * vy;
2493 y = view_matrix[1][0] * vx +
2494 view_matrix[1][1] * vy;
2496 [view_matrix[0][0] view_matrix[0][1]] [vx] = [x]
2497 [view_matrix[1][0] view_matrix[1][1]] [vy] [y]
2500 mat[0][0] = last_modelview_matrix[0][0];
2501 mat[0][1] = last_modelview_matrix[1][0];
2502 mat[1][0] = last_modelview_matrix[0][1];
2503 mat[1][1] = last_modelview_matrix[1][1];
2505 /* if (determinant_2x2 (mat) < 0.00001) */
2506 /* printf ("Determinant is quite small\n"); */
2508 invert_2x2 (mat, inv_mat);
2510 *pcb_x = (int)(inv_mat[0][0] * x + inv_mat[0][1] * y);
2511 *pcb_y = (int)(inv_mat[1][0] * x + inv_mat[1][1] * y);
2515 bool
2516 ghid_event_to_pcb_coords (int event_x, int event_y, Coord *pcb_x, Coord *pcb_y)
2518 render_priv *priv = gport->render_priv;
2520 ghid_unproject_to_z_plane (event_x, event_y, priv->edit_depth, pcb_x, pcb_y);
2522 return true;
2525 bool
2526 ghid_pcb_to_event_coords (Coord pcb_x, Coord pcb_y, int *event_x, int *event_y)
2528 render_priv *priv = gport->render_priv;
2530 /* NB: last_modelview_matrix is transposed in memory */
2531 float w = last_modelview_matrix[0][3] * (float)pcb_x +
2532 last_modelview_matrix[1][3] * (float)pcb_y +
2533 last_modelview_matrix[2][3] * 0. +
2534 last_modelview_matrix[3][3] * 1.;
2536 *event_x = (last_modelview_matrix[0][0] * (float)pcb_x +
2537 last_modelview_matrix[1][0] * (float)pcb_y +
2538 last_modelview_matrix[2][0] * priv->edit_depth +
2539 last_modelview_matrix[3][0] * 1.) / w;
2540 *event_y = (last_modelview_matrix[0][1] * (float)pcb_x +
2541 last_modelview_matrix[1][1] * (float)pcb_y +
2542 last_modelview_matrix[2][1] * priv->edit_depth +
2543 last_modelview_matrix[3][1] * 1.) / w;
2545 return true;
2548 void
2549 ghid_view_2d (void *ball, gboolean view_2d, gpointer userdata)
2551 global_view_2d = view_2d;
2552 ghid_invalidate_all ();
2555 void
2556 ghid_port_rotate (void *ball, float *quarternion, gpointer userdata)
2558 #ifdef DEBUG_ROTATE
2559 int row, column;
2560 #endif
2562 build_rotmatrix (view_matrix, quarternion);
2564 #ifdef DEBUG_ROTATE
2565 for (row = 0; row < 4; row++) {
2566 printf ("[ %f", view_matrix[row][0]);
2567 for (column = 1; column < 4; column++) {
2568 printf (",\t%f", view_matrix[row][column]);
2570 printf ("\t]\n");
2572 printf ("\n");
2573 #endif
2575 ghid_invalidate_all ();
2579 #define LEAD_USER_WIDTH 0.2 /* millimeters */
2580 #define LEAD_USER_PERIOD (1000 / 20) /* 20fps (in ms) */
2581 #define LEAD_USER_VELOCITY 3. /* millimeters per second */
2582 #define LEAD_USER_ARC_COUNT 3
2583 #define LEAD_USER_ARC_SEPARATION 3. /* millimeters */
2584 #define LEAD_USER_INITIAL_RADIUS 10. /* millimetres */
2585 #define LEAD_USER_COLOR_R 1.
2586 #define LEAD_USER_COLOR_G 1.
2587 #define LEAD_USER_COLOR_B 0.
2589 static void
2590 draw_lead_user (hidGC gc, render_priv *priv)
2592 gtkGC gtk_gc = (gtkGC)gc;
2593 int i;
2594 double radius = priv->lead_user_radius;
2595 double width = MM_TO_COORD (LEAD_USER_WIDTH);
2596 double separation = MM_TO_COORD (LEAD_USER_ARC_SEPARATION);
2598 if (!priv->lead_user)
2599 return;
2601 glPushAttrib (GL_CURRENT_BIT | GL_COLOR_BUFFER_BIT);
2602 glEnable (GL_COLOR_LOGIC_OP);
2603 glLogicOp (GL_XOR);
2604 glColor3f (LEAD_USER_COLOR_R, LEAD_USER_COLOR_G,LEAD_USER_COLOR_B);
2607 /* arcs at the approrpriate radii */
2609 for (i = 0; i < LEAD_USER_ARC_COUNT; i++, radius -= separation)
2611 if (radius < width)
2612 radius += MM_TO_COORD (LEAD_USER_INITIAL_RADIUS);
2614 /* Draw an arc at radius */
2615 hidgl_draw_arc (gc, width, priv->lead_user_x, priv->lead_user_y,
2616 radius, radius, 0, 360, gport->view.coord_per_px);
2619 hidgl_flush_triangles (gtk_gc->hidgl_gc.hidgl);
2620 glPopAttrib ();
2623 gboolean
2624 lead_user_cb (gpointer data)
2626 render_priv *priv = data;
2627 Coord step;
2628 double elapsed_time;
2630 /* Queue a redraw */
2631 ghid_invalidate_all ();
2633 /* Update radius */
2634 elapsed_time = g_timer_elapsed (priv->lead_user_timer, NULL);
2635 g_timer_start (priv->lead_user_timer);
2637 step = MM_TO_COORD (LEAD_USER_VELOCITY * elapsed_time);
2638 if (priv->lead_user_radius > step)
2639 priv->lead_user_radius -= step;
2640 else
2641 priv->lead_user_radius = MM_TO_COORD (LEAD_USER_INITIAL_RADIUS);
2643 return TRUE;
2646 void
2647 ghid_lead_user_to_location (Coord x, Coord y)
2649 render_priv *priv = gport->render_priv;
2651 ghid_cancel_lead_user ();
2653 priv->lead_user = true;
2654 priv->lead_user_x = x;
2655 priv->lead_user_y = y;
2656 priv->lead_user_radius = MM_TO_COORD (LEAD_USER_INITIAL_RADIUS);
2657 priv->lead_user_timeout = g_timeout_add (LEAD_USER_PERIOD, lead_user_cb, priv);
2658 priv->lead_user_timer = g_timer_new ();
2661 void
2662 ghid_cancel_lead_user (void)
2664 render_priv *priv = gport->render_priv;
2666 if (priv->lead_user_timeout)
2667 g_source_remove (priv->lead_user_timeout);
2669 if (priv->lead_user_timer)
2670 g_timer_destroy (priv->lead_user_timer);
2672 if (priv->lead_user)
2673 ghid_invalidate_all ();
2675 priv->lead_user_timeout = 0;
2676 priv->lead_user_timer = NULL;
2677 priv->lead_user = false;