Exemplo n.º 1
0
static gint
draw_line (gint        n,
	   gint        startx,
	   gint        starty,
	   gint        pw,
	   gint        ph,
	   gdouble     cx1,
	   gdouble     cy1,
	   gdouble     cx2,
	   gdouble     cy2,
	   GimpVector3 a,
	   GimpVector3 b)
{
  gdouble x1, y1, x2, y2;
  gint    i = n;

  gimp_vector_3d_to_2d (startx, starty, pw, ph,
			&x1, &y1, &mapvals.viewpoint, &a);
  gimp_vector_3d_to_2d (startx, starty, pw, ph,
			&x2, &y2, &mapvals.viewpoint, &b);

  if (clip_line (&x1, &y1, &x2, &y2, cx1, cy1, cx2, cy2) == TRUE)
    {
      linetab[i].x1 = RINT (x1);
      linetab[i].y1 = RINT (y1);
      linetab[i].x2 = RINT (x2);
      linetab[i].y2 = RINT (y2);
      linetab[i].linewidth = 3;
      linetab[i].linestyle = GDK_LINE_SOLID;
      gdk_gc_set_line_attributes (gc,
				  linetab[i].linewidth,
				  linetab[i].linestyle,
				  GDK_CAP_NOT_LAST,
				  GDK_JOIN_MITER);
      gdk_draw_line (previewarea->window, gc,
		     linetab[i].x1, linetab[i].y1,
		     linetab[i].x2, linetab[i].y2);
      i++;
    }

  return i;
}
Exemplo n.º 2
0
static void
draw_lights (gint startx,
	     gint starty,
	     gint pw,
	     gint ph)
{
  gdouble dxpos, dypos;
  gint    xpos, ypos;

  clear_light_marker ();

  gimp_vector_3d_to_2d (startx, starty, pw, ph,
			&dxpos, &dypos, &mapvals.viewpoint,
			&mapvals.lightsource.position);
  xpos = RINT (dxpos);
  ypos = RINT (dypos);

  if (xpos >= 0 && xpos <= PREVIEW_WIDTH &&
      ypos >= 0 && ypos <= PREVIEW_HEIGHT)
    draw_light_marker (xpos, ypos);
}
Exemplo n.º 3
0
static void
draw_handles (void)
{
  gdouble     dxpos, dypos;
  gint        startx, starty, pw, ph;
  GimpVector3 viewpoint;
  GimpVector3 light_position;
  gint        k      = mapvals.light_selected;

  gfloat length;
  gfloat delta_x = 0.0;
  gfloat delta_y = 0.0;

  /* calculate handle position */
  compute_preview_rectangle (&startx, &starty, &pw, &ph);
  switch (mapvals.lightsource[k].type)
    {
    case NO_LIGHT:
      return;
    case POINT_LIGHT:
    case SPOT_LIGHT:
      /* swap z to reverse light position */
      viewpoint = mapvals.viewpoint;
      viewpoint.z = -viewpoint.z;
      light_position = mapvals.lightsource[k].position;
      gimp_vector_3d_to_2d (startx, starty, pw, ph, &dxpos, &dypos,
                            &viewpoint, &light_position);
      handle_xpos = (gint) (dxpos + 0.5);
      handle_ypos = (gint) (dypos + 0.5);
      break;
    case DIRECTIONAL_LIGHT:
      light_position.x = light_position.y = 0.5;
      light_position.z = 0;
      viewpoint.z = -viewpoint.z;
      gimp_vector_3d_to_2d (startx, starty, pw, ph, &dxpos, &dypos,
                            &viewpoint, &light_position);
      length = PREVIEW_HEIGHT / 4;
      delta_x = mapvals.lightsource[k].direction.x * length;
      delta_y = mapvals.lightsource[k].direction.y * length;
      handle_xpos = dxpos + delta_x;
      handle_ypos = dypos + delta_y;
      break;
    }

  gdk_gc_set_function (gc, GDK_COPY);

  if (mapvals.lightsource[k].type != NO_LIGHT)
    {
      GdkColor  color;

      /* Restore background if it has been saved */
      /* ======================================= */

      if (backbuf.image != NULL)
        {
          gdk_gc_set_function (gc, GDK_COPY);
          gdk_draw_image (previewarea->window, gc,
                          backbuf.image, 0, 0, backbuf.x,
                          backbuf.y, backbuf.w, backbuf.h);
          g_object_unref (backbuf.image);
          backbuf.image = NULL;
        }

      /* calculate backbuffer */
      switch (mapvals.lightsource[k].type)
        {
        case POINT_LIGHT:
          backbuf.x = handle_xpos - LIGHT_SYMBOL_SIZE / 2;
          backbuf.y = handle_ypos - LIGHT_SYMBOL_SIZE / 2;
          backbuf.w = LIGHT_SYMBOL_SIZE;
          backbuf.h = LIGHT_SYMBOL_SIZE;
          break;
        case DIRECTIONAL_LIGHT:
          if (delta_x <= 0)
            backbuf.x = handle_xpos;
          else
            backbuf.x = startx + pw/2;
          if (delta_y <= 0)
            backbuf.y = handle_ypos;
          else
            backbuf.y = starty + ph/2;
          backbuf.x -= LIGHT_SYMBOL_SIZE/2;
          backbuf.y -= LIGHT_SYMBOL_SIZE/2;
          backbuf.w = fabs(delta_x) + LIGHT_SYMBOL_SIZE;
          backbuf.h = fabs(delta_y) + LIGHT_SYMBOL_SIZE;
          break;
        case SPOT_LIGHT:
          backbuf.x = handle_xpos - LIGHT_SYMBOL_SIZE / 2;
          backbuf.y = handle_ypos - LIGHT_SYMBOL_SIZE / 2;
          backbuf.w = LIGHT_SYMBOL_SIZE;
          backbuf.h = LIGHT_SYMBOL_SIZE;
          break;
        case NO_LIGHT:
          break;
        }

      /* Save background */
      /* =============== */
      if ((backbuf.x >= 0) &&
          (backbuf.x <= PREVIEW_WIDTH) &&
          (backbuf.y >= 0) && (backbuf.y <= PREVIEW_HEIGHT))
        {
          /* clip coordinates to preview widget sizes */
          if ((backbuf.x + backbuf.w) > PREVIEW_WIDTH)
            backbuf.w = (PREVIEW_WIDTH - backbuf.x);

          if ((backbuf.y + backbuf.h) > PREVIEW_HEIGHT)
            backbuf.h = (PREVIEW_HEIGHT - backbuf.y);

          backbuf.image = gdk_drawable_get_image (previewarea->window,
                                                  backbuf.x, backbuf.y,
                                                  backbuf.w, backbuf.h);
        }

      color.red   = 0x0;
      color.green = 0x0;
      color.blue  = 0x0;
      gdk_gc_set_rgb_bg_color (gc, &color);

      color.red   = 0x0;
      color.green = 0x4000;
      color.blue  = 0xFFFF;
      gdk_gc_set_rgb_fg_color (gc, &color);

      /* draw circle at light position */
      switch (mapvals.lightsource[k].type)
        {
        case POINT_LIGHT:
        case SPOT_LIGHT:
          gdk_draw_arc (previewarea->window, gc, TRUE,
                        handle_xpos - LIGHT_SYMBOL_SIZE / 2,
                        handle_ypos - LIGHT_SYMBOL_SIZE / 2,
                        LIGHT_SYMBOL_SIZE,
                        LIGHT_SYMBOL_SIZE, 0, 360 * 64);
          break;
        case DIRECTIONAL_LIGHT:
          gdk_draw_arc (previewarea->window, gc, TRUE,
                        handle_xpos - LIGHT_SYMBOL_SIZE / 2,
                        handle_ypos - LIGHT_SYMBOL_SIZE / 2,
                        LIGHT_SYMBOL_SIZE,
                        LIGHT_SYMBOL_SIZE, 0, 360 * 64);
          gdk_draw_line (previewarea->window, gc,
                         handle_xpos, handle_ypos, startx+pw/2 , starty + ph/2);
          break;
        case NO_LIGHT:
          break;
        }
    }
}
Exemplo n.º 4
0
static void
draw_handles (void)
{
  gdouble     dxpos, dypos;
  gint        startx, starty, pw, ph;
  GimpVector3 viewpoint;
  GimpVector3 light_position;
  gint        k      = mapvals.light_selected;

  gfloat length;
  gfloat delta_x = 0.0;
  gfloat delta_y = 0.0;

  /* calculate handle position */
  compute_preview_rectangle (&startx, &starty, &pw, &ph);
  switch (mapvals.lightsource[k].type)
    {
    case NO_LIGHT:
      return;
    case POINT_LIGHT:
    case SPOT_LIGHT:
      /* swap z to reverse light position */
      viewpoint = mapvals.viewpoint;
      viewpoint.z = -viewpoint.z;
      light_position = mapvals.lightsource[k].position;
      gimp_vector_3d_to_2d (startx, starty, pw, ph, &dxpos, &dypos,
                            &viewpoint, &light_position);
      handle_xpos = (gint) (dxpos + 0.5);
      handle_ypos = (gint) (dypos + 0.5);
      break;
    case DIRECTIONAL_LIGHT:
      light_position.x = light_position.y = 0.5;
      light_position.z = 0;
      viewpoint.z = -viewpoint.z;
      gimp_vector_3d_to_2d (startx, starty, pw, ph, &dxpos, &dypos,
                            &viewpoint, &light_position);
      length = PREVIEW_HEIGHT / 4;
      delta_x = mapvals.lightsource[k].direction.x * length;
      delta_y = mapvals.lightsource[k].direction.y * length;
      handle_xpos = dxpos + delta_x;
      handle_ypos = dypos + delta_y;
      break;
    }

  if (mapvals.lightsource[k].type != NO_LIGHT)
    {
      GdkColor  color;
      cairo_t *cr;
      cr = gdk_cairo_create (gtk_widget_get_window (previewarea));

      cairo_set_line_width (cr, 1.0);

      color.red   = 0x0;
      color.green = 0x4000;
      color.blue  = 0xFFFF;
      gdk_cairo_set_source_color (cr, &color);

      /* draw circle at light position */
      switch (mapvals.lightsource[k].type)
        {
        case POINT_LIGHT:
        case SPOT_LIGHT:
          cairo_arc (cr, handle_xpos, handle_ypos,
                     LIGHT_SYMBOL_SIZE/2, 0, 2 * M_PI);
          cairo_fill (cr);
          break;
        case DIRECTIONAL_LIGHT:
          cairo_arc (cr, handle_xpos, handle_ypos,
                     LIGHT_SYMBOL_SIZE/2, 0, 2 * M_PI);
          cairo_fill (cr);
          cairo_move_to (cr, handle_xpos, handle_ypos);
          cairo_line_to (cr, startx + pw/2, starty + ph/2);
          cairo_stroke (cr);
          break;
        case NO_LIGHT:
          break;
        }
      cairo_destroy (cr);
    }
}
Exemplo n.º 5
0
static void
draw_wireframe_sphere (gint startx,
		       gint starty,
		       gint pw,
		       gint ph)
{
  GimpVector3 p[2 * (WIRESIZE + 5)];
  gint        cnt, cnt2, n = 0;
  gdouble     x1, y1, x2, y2, twopifac, cx1, cy1, cx2, cy2;

  /* Compute wireframe points */
  /* ======================== */

  twopifac = (2.0 * G_PI) / WIRESIZE;

  for (cnt = 0; cnt < WIRESIZE; cnt++)
    {
      p[cnt].x = mapvals.radius * cos ((gdouble) cnt * twopifac);
      p[cnt].y = 0.0;
      p[cnt].z = mapvals.radius * sin ((gdouble) cnt * twopifac);
      gimp_vector3_rotate (&p[cnt],
			   gimp_deg_to_rad (mapvals.alpha),
			   gimp_deg_to_rad (mapvals.beta),
			   gimp_deg_to_rad (mapvals.gamma));
      gimp_vector3_add (&p[cnt], &p[cnt], &mapvals.position);
    }

  p[cnt] = p[0];

  for (cnt = WIRESIZE + 1; cnt < 2 * WIRESIZE + 1; cnt++)
    {
      p[cnt].x = mapvals.radius * cos ((gdouble) (cnt-(WIRESIZE+1))*twopifac);
      p[cnt].y = mapvals.radius * sin ((gdouble) (cnt-(WIRESIZE+1))*twopifac);
      p[cnt].z = 0.0;
      gimp_vector3_rotate (&p[cnt],
			   gimp_deg_to_rad (mapvals.alpha),
			   gimp_deg_to_rad (mapvals.beta),
			   gimp_deg_to_rad (mapvals.gamma));
      gimp_vector3_add (&p[cnt], &p[cnt], &mapvals.position);
    }

  p[cnt] = p[WIRESIZE+1];
  cnt++;
  cnt2 = cnt;

  /* Find rotated axis */
  /* ================= */

  gimp_vector3_set (&p[cnt], 0.0, -0.35, 0.0);
  gimp_vector3_rotate (&p[cnt],
		       gimp_deg_to_rad (mapvals.alpha),
		       gimp_deg_to_rad (mapvals.beta),
		       gimp_deg_to_rad (mapvals.gamma));
  p[cnt+1] = mapvals.position;

  gimp_vector3_set (&p[cnt+2], 0.0, 0.0, -0.35);
  gimp_vector3_rotate (&p[cnt+2],
		       gimp_deg_to_rad (mapvals.alpha),
		       gimp_deg_to_rad (mapvals.beta),
		       gimp_deg_to_rad (mapvals.gamma));
  p[cnt+3] = mapvals.position;

  p[cnt + 4] = p[cnt];
  gimp_vector3_mul (&p[cnt + 4], -1.0);
  p[cnt + 5] = p[cnt + 1];

  gimp_vector3_add (&p[cnt],     &p[cnt],     &mapvals.position);
  gimp_vector3_add (&p[cnt + 2], &p[cnt + 2], &mapvals.position);
  gimp_vector3_add (&p[cnt + 4], &p[cnt + 4], &mapvals.position);

  /* Draw the circles (equator and zero meridian) */
  /* ============================================ */

  cx1 = (gdouble) startx;
  cy1 = (gdouble) starty;
  cx2 = cx1 + (gdouble) pw;
  cy2 = cy1 + (gdouble) ph;

  for (cnt = 0; cnt < cnt2 - 1; cnt++)
    {
      if (p[cnt].z > mapvals.position.z && p[cnt + 1].z > mapvals.position.z)
        {
          gimp_vector_3d_to_2d (startx, starty, pw, ph,
				&x1, &y1, &mapvals.viewpoint, &p[cnt]);
          gimp_vector_3d_to_2d (startx, starty, pw, ph,
				&x2, &y2, &mapvals.viewpoint, &p[cnt + 1]);

          if (clip_line (&x1, &y1, &x2, &y2, cx1, cy1, cx2, cy2) == TRUE)
            {
              linetab[n].x1 = (gint) (x1 + 0.5);
              linetab[n].y1 = (gint) (y1 + 0.5);
              linetab[n].x2 = (gint) (x2 + 0.5);
              linetab[n].y2 = (gint) (y2 + 0.5);
              linetab[n].linewidth = 3;
              linetab[n].linestyle = GDK_LINE_SOLID;
              gdk_gc_set_line_attributes (gc,
					  linetab[n].linewidth,
					  linetab[n].linestyle,
					  GDK_CAP_NOT_LAST,
					  GDK_JOIN_MITER);
              gdk_draw_line (previewarea->window, gc,
			     linetab[n].x1, linetab[n].y1,
			     linetab[n].x2, linetab[n].y2);
              n++;
            }
        }
    }

  /* Draw the axis (pole to pole and center to zero meridian) */
  /* ======================================================== */

  for (cnt = 0; cnt < 3; cnt++)
    {
      gimp_vector_3d_to_2d (startx, starty, pw, ph,
			    &x1, &y1, &mapvals.viewpoint, &p[cnt2]);
      gimp_vector_3d_to_2d (startx, starty, pw, ph,
			    &x2, &y2, &mapvals.viewpoint, &p[cnt2 + 1]);

      if (clip_line (&x1, &y1, &x2, &y2, cx1, cy1, cx2, cy2) == TRUE)
        {
          linetab[n].x1 = RINT (x1);
          linetab[n].y1 = RINT (y1);
          linetab[n].x2 = RINT (x2);
          linetab[n].y2 = RINT (y2);

          if (p[cnt2].z < mapvals.position.z || p[cnt2+1].z < mapvals.position.z)
            {
              linetab[n].linewidth = 1;
              linetab[n].linestyle = GDK_LINE_DOUBLE_DASH;
            }
          else
            {
              linetab[n].linewidth = 3;
              linetab[n].linestyle = GDK_LINE_SOLID;
            }

          gdk_gc_set_line_attributes (gc,
				      linetab[n].linewidth,
				      linetab[n].linestyle,
				      GDK_CAP_NOT_LAST,
				      GDK_JOIN_MITER);
          gdk_draw_line (previewarea->window, gc,
			 linetab[n].x1, linetab[n].y1,
			 linetab[n].x2, linetab[n].y2);
          n++;
        }

      cnt2 += 2;
    }

  /* Mark end of lines */
  /* ================= */

  linetab[n].x1 = -1;
}
Exemplo n.º 6
0
static void
draw_wireframe_plane (gint startx,
		      gint starty,
		      gint pw,
		      gint ph)
{
  GimpVector3 v1, v2, a, b, c, d, dir1, dir2;
  gint        cnt, n = 0;
  gdouble     x1, y1, x2, y2, cx1, cy1, cx2, cy2, fac;

  /* Find rotated box corners */
  /* ======================== */

  gimp_vector3_set (&v1, 0.5, 0.0, 0.0);
  gimp_vector3_set (&v2, 0.0, 0.5, 0.0);

  gimp_vector3_rotate (&v1,
		       gimp_deg_to_rad (mapvals.alpha),
		       gimp_deg_to_rad (mapvals.beta),
		       gimp_deg_to_rad (mapvals.gamma));

  gimp_vector3_rotate (&v2,
		       gimp_deg_to_rad (mapvals.alpha),
		       gimp_deg_to_rad (mapvals.beta),
		       gimp_deg_to_rad (mapvals.gamma));

  dir1 = v1; gimp_vector3_normalize (&dir1);
  dir2 = v2; gimp_vector3_normalize (&dir2);

  fac = 1.0 / (gdouble) WIRESIZE;

  gimp_vector3_mul (&dir1, fac);
  gimp_vector3_mul (&dir2, fac);

  gimp_vector3_add (&a, &mapvals.position, &v1);
  gimp_vector3_sub (&b, &a, &v2);
  gimp_vector3_add (&a, &a, &v2);
  gimp_vector3_sub (&d, &mapvals.position, &v1);
  gimp_vector3_sub (&d, &d, &v2);

  c = b;

  cx1 = (gdouble) startx;
  cy1 = (gdouble) starty;
  cx2 = cx1 + (gdouble) pw;
  cy2 = cy1 + (gdouble) ph;

  for (cnt = 0; cnt <= WIRESIZE; cnt++)
    {
      gimp_vector_3d_to_2d (startx, starty, pw, ph,
			    &x1, &y1, &mapvals.viewpoint, &a);
      gimp_vector_3d_to_2d (startx, starty, pw, ph,
			    &x2, &y2, &mapvals.viewpoint, &b);

      if (clip_line (&x1, &y1, &x2, &y2, cx1, cy1, cx2, cy2) == TRUE)
        {
          linetab[n].x1 = RINT (x1);
          linetab[n].y1 = RINT (y1);
          linetab[n].x2 = RINT (x2);
          linetab[n].y2 = RINT (y2);
          linetab[n].linewidth = 1;
          linetab[n].linestyle = GDK_LINE_SOLID;
          gdk_gc_set_line_attributes (gc,
				      linetab[n].linewidth,
				      linetab[n].linestyle,
				      GDK_CAP_NOT_LAST,
				      GDK_JOIN_MITER);
          gdk_draw_line (previewarea->window, gc,
			 linetab[n].x1, linetab[n].y1,
			 linetab[n].x2, linetab[n].y2);
          n++;
        }

      gimp_vector_3d_to_2d (startx, starty, pw, ph,
			    &x1, &y1, &mapvals.viewpoint, &c);
      gimp_vector_3d_to_2d (startx, starty, pw, ph,
			    &x2, &y2, &mapvals.viewpoint, &d);

      if (clip_line (&x1, &y1, &x2, &y2, cx1, cy1, cx2, cy2) == TRUE)
        {
          linetab[n].x1 = RINT (x1);
          linetab[n].y1 = RINT (y1);
          linetab[n].x2 = RINT (x2);
          linetab[n].y2 = RINT (y2);
          linetab[n].linewidth = 1;
          linetab[n].linestyle = GDK_LINE_SOLID;
          gdk_gc_set_line_attributes (gc,
				      linetab[n].linewidth,
				      linetab[n].linestyle,
				      GDK_CAP_NOT_LAST,
				      GDK_JOIN_MITER);
          gdk_draw_line (previewarea->window, gc,
			 linetab[n].x1, linetab[n].y1,
			 linetab[n].x2, linetab[n].y2);
          n++;
        }

      gimp_vector3_sub (&a, &a, &dir1);
      gimp_vector3_sub (&b, &b, &dir1);
      gimp_vector3_add (&c, &c, &dir2);
      gimp_vector3_add (&d, &d, &dir2);
    }

  /* Mark end of lines */
  /* ================= */

  linetab[n].x1 = -1;
}