home *** CD-ROM | disk | FTP | other *** search
/ PC Pro 2002 April / pcpro0402.iso / essentials / graphics / Gimp / gimp-src-20001226.exe / src / gimp / plug-ins / Lighting / lighting_main.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-08-31  |  10.7 KB  |  317 lines

  1. /* Lighting Effects 0.2.2 -- image filter plug-in for The Gimp program
  2.  *
  3.  * Copyright (C) 1996-98 Tom Bech
  4.  * Copyright (C) 1996-98 Federico Mena Quintero
  5.  *
  6.  * E-mail: tomb@gimp.org (Tom) or quartic@gimp.org (Federico)
  7.  *
  8.  * This program is free software; you can redistribute it and/or modify
  9.  * it under the terms of the GNU General Public License as published by
  10.  * the Free Software Foundation; either version 2 of the License, or
  11.  * (at your option) any later version.
  12.  *
  13.  * This program is distributed in the hope that it will be useful,
  14.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16.  * GNU General Public License for more details.
  17.  *
  18.  * You should have received a copy of the GNU General Public License
  19.  * along with this program; if not, write to the Free Software
  20.  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  21.  */
  22.  
  23. #include <gtk/gtk.h>
  24.  
  25. #include <libgimp/gimp.h>
  26.  
  27. #include <gck/gck.h>
  28.  
  29. #include "lighting_apply.h"
  30. #include "lighting_image.h"
  31. #include "lighting_main.h"
  32. #include "lighting_preview.h"
  33. #include "lighting_shade.h"
  34. #include "lighting_ui.h"
  35.  
  36. #include "config.h"
  37. #include "libgimp/stdplugins-intl.h"
  38.  
  39. LightingValues mapvals;
  40.  
  41. /******************/
  42. /* Implementation */
  43. /******************/
  44.  
  45. /*************************************/
  46. /* Set parameters to standard values */
  47. /*************************************/
  48.  
  49. static void
  50. set_default_settings (void)
  51. {
  52.   gimp_vector3_set (&mapvals.viewpoint,   0.5, 0.5, 0.25);
  53.   gimp_vector3_set (&mapvals.planenormal, 0.0, 0.0, 1.0);
  54.   
  55.   gimp_vector3_set (&mapvals.lightsource.position,   1.0,  0.0, 1.0);
  56.   gimp_vector3_set (&mapvals.lightsource.direction, -1.0, -1.0, 1.0);
  57.  
  58.   gck_rgb_set (&mapvals.lightsource.color, 1.0, 1.0, 1.0);
  59.   mapvals.lightsource.intensity = 1.0;
  60.   mapvals.lightsource.type      = POINT_LIGHT;
  61.  
  62.   mapvals.material.ambient_int  =  0.3;
  63.   mapvals.material.diffuse_int  =  1.0;
  64.   mapvals.material.diffuse_ref  =  0.4;
  65.   mapvals.material.specular_ref =  0.6;
  66.   mapvals.material.highlight    = 27.0;
  67.  
  68.   mapvals.pixel_treshold      = 0.25;
  69.   mapvals.max_depth           =  3.0;
  70.   mapvals.preview_zoom_factor =  1.0;
  71.  
  72.   mapvals.bumpmaptype = 0;
  73.   mapvals.bumpmin     = 0.0;
  74.   mapvals.bumpmax     = 0.1;
  75.  
  76.   mapvals.antialiasing           = FALSE;
  77.   mapvals.create_new_image       = FALSE;
  78.   mapvals.transparent_background = FALSE;
  79.   mapvals.bump_mapped            = FALSE;
  80.   mapvals.env_mapped             = FALSE;
  81.   mapvals.ref_mapped             = FALSE;
  82.   mapvals.previewquality         = FALSE;
  83.  
  84.   mapvals.bumpmap_id = -1;
  85.   mapvals.envmap_id  = -1;
  86. }
  87.  
  88. static void
  89. check_drawables (void)
  90. {
  91.   if (mapvals.bumpmap_id != -1 &&
  92.       gimp_drawable_image_id (mapvals.bumpmap_id) == -1)
  93.     {
  94.       mapvals.bump_mapped = FALSE;
  95.       mapvals.bumpmap_id  = -1;
  96.     }
  97.  
  98.   if (mapvals.envmap_id != -1 &&
  99.       gimp_drawable_image_id (mapvals.envmap_id) == -1)
  100.     {
  101.       mapvals.env_mapped = FALSE;
  102.       mapvals.envmap_id  = -1;
  103.     }
  104.  
  105.   if (mapvals.bump_mapped)
  106.     {
  107.       if (!gimp_drawable_is_gray (mapvals.bumpmap_id) || 
  108.       (gimp_drawable_width (mapvals.drawable_id) !=
  109.        gimp_drawable_width(mapvals.bumpmap_id)) ||
  110.       (gimp_drawable_height (mapvals.drawable_id) !=
  111.        gimp_drawable_height(mapvals.bumpmap_id)))
  112.         {
  113.           mapvals.bump_mapped = FALSE;
  114.           mapvals.bumpmap_id  = -1;
  115.         }
  116.     }
  117.  
  118.   if (mapvals.env_mapped)
  119.     {
  120.       if (gimp_drawable_is_gray (mapvals.envmap_id) || 
  121.           gimp_drawable_has_alpha (mapvals.envmap_id))
  122.         {
  123.           mapvals.bump_mapped = FALSE;
  124.           mapvals.bumpmap_id  = -1;
  125.         }
  126.     }
  127. }
  128.  
  129. static void
  130. query (void)
  131. {
  132.   static GimpParamDef args[] =
  133.   {
  134.     { GIMP_PDB_INT32,    "run_mode",              "Interactive (0), non-interactive (1)" },
  135.     { GIMP_PDB_IMAGE,    "image",                 "Input image" },
  136.     { GIMP_PDB_DRAWABLE, "drawable",              "Input drawable" },
  137.     { GIMP_PDB_DRAWABLE, "bumpdrawable",          "Bumpmap drawable (set to 0 if disabled)" },
  138.     { GIMP_PDB_DRAWABLE, "envdrawable",           "Environmentmap drawable (set to 0 if disabled)" },
  139.     { GIMP_PDB_INT32,    "dobumpmap",             "Enable bumpmapping (TRUE/FALSE)" },
  140.     { GIMP_PDB_INT32,    "doenvmap",              "Enable envmapping (TRUE/FALSE)" },
  141.     { GIMP_PDB_INT32,    "bumpmaptype",           "Type of mapping (0=linear,1=log, 2=sinusoidal, 3=spherical)" },
  142.     { GIMP_PDB_INT32,    "lighttype",             "Type of lightsource (0=point,1=directional,3=spot,4=none)" },
  143.     { GIMP_PDB_COLOR,    "lightcolor",            "Lightsource color (r,g,b)" },
  144.     { GIMP_PDB_FLOAT,    "lightposition_x",       "Lightsource position (x,y,z)" },
  145.     { GIMP_PDB_FLOAT,    "lightposition_y",       "Lightsource position (x,y,z)" },
  146.     { GIMP_PDB_FLOAT,    "lightposition_z",       "Lightsource position (x,y,z)" },
  147.     { GIMP_PDB_FLOAT,    "lightdirection_x",      "Lightsource direction [x,y,z]" },
  148.     { GIMP_PDB_FLOAT,    "lightdirection_y",      "Lightsource direction [x,y,z]" },
  149.     { GIMP_PDB_FLOAT,    "lightdirection_z",      "Lightsource direction [x,y,z]" },
  150.     { GIMP_PDB_FLOAT,    "ambient_intensity",     "Material ambient intensity (0..1)" },
  151.     { GIMP_PDB_FLOAT,    "diffuse_intensity",     "Material diffuse intensity (0..1)" },
  152.     { GIMP_PDB_FLOAT,    "diffuse_reflectivity",  "Material diffuse reflectivity (0..1)" },
  153.     { GIMP_PDB_FLOAT,    "specular_reflectivity", "Material specular reflectivity (0..1)" },
  154.     { GIMP_PDB_FLOAT,    "highlight",             "Material highlight (0..->), note: it's expotential" },
  155.     { GIMP_PDB_INT32,    "antialiasing",          "Apply antialiasing (TRUE/FALSE)" },
  156.     { GIMP_PDB_INT32,    "newimage",              "Create a new image (TRUE/FALSE)" },
  157.     { GIMP_PDB_INT32,    "transparentbackground", "Make background transparent (TRUE/FALSE)" }
  158.   };
  159.   static gint nargs = sizeof (args) / sizeof (args[0]);
  160.  
  161.   INIT_I18N();
  162.  
  163.   gimp_install_procedure ("plug_in_lighting",
  164.               "Apply various lighting effects to an image",
  165.               "No help yet",
  166.               "Tom Bech & Federico Mena Quintero",
  167.               "Tom Bech & Federico Mena Quintero",
  168.               "Version 0.2.0, March 15 1998",
  169.               N_("<Image>/Filters/Light Effects/Lighting Effects..."),
  170.               "RGB*",
  171.               GIMP_PLUGIN,
  172.               nargs, 0,
  173.               args, NULL);
  174. }
  175.  
  176. static void
  177. run (gchar   *name,
  178.      gint     nparams,
  179.      GimpParam  *param,
  180.      gint    *nreturn_vals,
  181.      GimpParam **return_vals)
  182. {
  183.   static GimpParam values[1];
  184.   GimpDrawable *drawable;
  185.   GimpRunModeType run_mode;
  186.   GimpPDBStatusType status = GIMP_PDB_SUCCESS;
  187.  
  188.   run_mode = param[0].data.d_int32;
  189.  
  190.   if (run_mode == GIMP_RUN_INTERACTIVE)
  191.     {
  192.       INIT_I18N_UI();
  193.     }
  194.   else
  195.     {
  196.       INIT_I18N();
  197.     }
  198.  
  199.   *nreturn_vals = 1;
  200.   *return_vals = values;
  201.  
  202.   values[0].type = GIMP_PDB_STATUS;
  203.   values[0].data.d_status = status;
  204.  
  205.   /* Set default values */
  206.   /* ================== */
  207.  
  208.   set_default_settings ();
  209.  
  210.   /* Possibly retrieve data */
  211.   /* ====================== */
  212.  
  213.   gimp_get_data ("plug_in_lighting", &mapvals);
  214.  
  215.   /* Get the specified drawable */
  216.   /* ========================== */
  217.   
  218.   drawable = gimp_drawable_get (param[2].data.d_drawable);
  219.  
  220.   mapvals.drawable_id = drawable->id;
  221.  
  222.   check_drawables ();
  223.  
  224.   if (status == GIMP_PDB_SUCCESS)
  225.     {
  226.       /* Make sure that the drawable is RGBA or RGB color */
  227.       /* ================================================ */
  228.  
  229.       if (gimp_drawable_is_rgb (drawable->id))
  230.     {
  231.       /* Set the tile cache size */
  232.           /* ======================= */
  233.  
  234.       gimp_tile_cache_ntiles (TILE_CACHE_SIZE);
  235.           
  236.           switch (run_mode)
  237.             {
  238.               case GIMP_RUN_INTERACTIVE:
  239.                 if (main_dialog (drawable))
  240.           {
  241.             compute_image ();
  242.  
  243.             gimp_set_data ("plug_in_lighting",
  244.                    &mapvals, sizeof (LightingValues));
  245.           }
  246.               break;
  247.  
  248.               case GIMP_RUN_WITH_LAST_VALS:
  249.                 image_setup (drawable, FALSE);
  250.                 compute_image ();
  251.                 break;
  252.  
  253.               case GIMP_RUN_NONINTERACTIVE:
  254.                 if (nparams != 24)
  255.           {
  256.             status = GIMP_PDB_CALLING_ERROR;
  257.           }
  258.                 else
  259.                   {
  260.                     mapvals.bumpmap_id              = param[3].data.d_drawable;
  261.                     mapvals.envmap_id               = param[4].data.d_drawable;
  262.                     mapvals.bump_mapped             = (gint)param[5].data.d_int32;
  263.                     mapvals.env_mapped              = (gint)param[6].data.d_int32;
  264.                     mapvals.bumpmaptype             = (gint)param[7].data.d_int32;
  265.                     mapvals.lightsource.type        = (LightType)param[8].data.d_int32;
  266.                     mapvals.lightsource.color.r     = param[9].data.d_color.red;
  267.                     mapvals.lightsource.color.g     = param[9].data.d_color.green;
  268.                     mapvals.lightsource.color.b     = param[9].data.d_color.blue;
  269.                     mapvals.lightsource.position.x  = param[10].data.d_float;
  270.                     mapvals.lightsource.position.y  = param[11].data.d_float;
  271.                     mapvals.lightsource.position.z  = param[12].data.d_float;
  272.                     mapvals.lightsource.direction.x = param[13].data.d_float;
  273.                     mapvals.lightsource.direction.y = param[14].data.d_float;
  274.                     mapvals.lightsource.direction.z = param[15].data.d_float;
  275.                     mapvals.material.ambient_int    = param[16].data.d_float;
  276.                     mapvals.material.diffuse_int    = param[17].data.d_float;
  277.                     mapvals.material.diffuse_ref    = param[18].data.d_float;
  278.                     mapvals.material.specular_ref   = param[19].data.d_float;
  279.                     mapvals.material.highlight      = param[20].data.d_float;
  280.                     mapvals.antialiasing            = (gint)param[21].data.d_int32;
  281.                     mapvals.create_new_image        = (gint)param[22].data.d_int32;
  282.                     mapvals.transparent_background  = (gint)param[23].data.d_int32;
  283.  
  284.                     check_drawables ();
  285.                     image_setup (drawable, FALSE);
  286.                     compute_image ();
  287.                   }
  288.               default:
  289.                 break;
  290.             }
  291.         }
  292.       else
  293.         status = GIMP_PDB_EXECUTION_ERROR;
  294.     }
  295.  
  296.   if (run_mode != GIMP_RUN_NONINTERACTIVE)
  297.     gimp_displays_flush ();
  298.  
  299.   values[0].data.d_status = status;
  300.   gimp_drawable_detach (drawable);
  301.  
  302.   if (xpostab)
  303.     g_free (xpostab);
  304.   if (ypostab)
  305.     g_free (ypostab);
  306. }
  307.  
  308. GimpPlugInInfo PLUG_IN_INFO =
  309. {
  310.   NULL,  /* init_proc  */
  311.   NULL,  /* quit_proc  */
  312.   query, /* query_proc */
  313.   run,   /* run_proc   */
  314. };
  315.  
  316. MAIN ()
  317.