Back to index

extremetuxracer  0.5beta
hier_cb.cpp
Go to the documentation of this file.
00001 /* 
00002  * PPRacer 
00003  * Copyright (C) 2004-2005 Volker Stroebel <volker@planetpenguin.de>
00004  *
00005  * Copyright (C) 1999-2001 Jasmin F. Patry
00006  * 
00007  * This program is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU General Public License
00009  * as published by the Free Software Foundation; either version 2
00010  * of the License, or (at your option) any later version.
00011  * 
00012  * This program is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  * 
00017  * You should have received a copy of the GNU General Public License
00018  * along with this program; if not, write to the Free Software
00019  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00020  */
00021 
00022 #include "hier.h"
00023 #include "tcl_util.h"
00024 
00025 /*
00026  * Callbacks
00027  */
00028 
00029 int tux_rotate( ClientData cd, Tcl_Interp *ip, int argc, CONST84 char *argv[] ) 
00030 {
00031     char *errmsg;
00032 
00033     const char *nodename;
00034     char axis;
00035     double angle ;
00036 
00037     if (4 != argc) {
00038         Tcl_AppendResult(ip, argv[0], ": wrong number of arguments\n", 
00039                       "Usage: ", argv[0], " <node> [x|y|z] <angle>",
00040                       (char *)0 );
00041         return TCL_ERROR;
00042     }
00043 
00044     /* obtain the nodename */
00045     nodename = argv[1];
00046 
00047     /* obtain the axis */
00048     axis = argv[2][0];
00049     if ('x' != axis && 'y' != axis && 'z' != axis) {
00050         Tcl_AppendResult(ip, argv[0], ": invalid rotation axes", 
00051                       (char *)0 );
00052         return TCL_ERROR;
00053     }
00054     
00055     /* obtain the angle */
00056     if (TCL_OK != Tcl_GetDouble(ip, argv[3], &angle)) {
00057         Tcl_AppendResult(ip, argv[0], ": invalid rotation angle", 
00058                       (char *)0 );
00059         return TCL_ERROR;
00060     }
00061     
00062     errmsg = rotate_scene_node(nodename,axis,angle);
00063 
00064     /* report error, if any */
00065     if (errmsg) {
00066         Tcl_AppendResult(ip, argv[0], ": ", errmsg, (char *)0 );
00067         return TCL_ERROR;
00068     }
00069   
00070     return TCL_OK;
00071 }
00072 
00073 int tux_translate( ClientData cd, Tcl_Interp *ip, int argc, CONST84 char *argv[] ) 
00074 {
00075     char *errmsg;
00076 
00077     const char *nodename;
00078     double vec[3];
00079 
00080     if (3 != argc) {
00081         Tcl_AppendResult(ip, argv[0], ": invalid number of arguments\n", 
00082                       "Usage: ", argv[0], " <node> { <x> <y> <z> }",
00083                       (char *)0 );
00084         return TCL_ERROR;
00085     }
00086 
00087     /* obtain the nodename */
00088     nodename = argv[1];
00089 
00090     /* obtain the translation vector */
00091     if (TCL_OK != get_tcl_tuple(ip,argv[2],vec,3)) {
00092         Tcl_AppendResult(ip, argv[0], ": invalid translation vector", 
00093                       (char *)0 );
00094         return TCL_ERROR;
00095     }
00096     
00097     errmsg = translate_scene_node(nodename,pp::Vec3d(vec));
00098 
00099     /* report error, if any */
00100     if (errmsg) {
00101         Tcl_AppendResult(ip, argv[0], ": ", errmsg, (char *)0 );
00102         return TCL_ERROR;
00103     }
00104   
00105     return TCL_OK;
00106 }
00107 
00108 int tux_scale( ClientData cd, Tcl_Interp *ip, int argc, CONST84 char *argv[] ) 
00109 {
00110     char *errmsg;
00111 
00112     const char *nodename;
00113     double origin[3];
00114     double factors[3]; 
00115 
00116     if (4 != argc) {
00117         Tcl_AppendResult(ip, argv[0], ": invalid number of arguments\n", 
00118                       "Usage: ", argv[0], " <node> { <origin> } "
00119                       "{ <translation vector> }",
00120                       (char *)0 );
00121         return TCL_ERROR;
00122     }
00123 
00124     /* obtain the nodename */
00125     nodename = argv[1];
00126 
00127     /* obtain the origin point */
00128     if (TCL_OK != get_tcl_tuple(ip,argv[2],origin,3)) {
00129         Tcl_AppendResult(ip, argv[0], ": invalid origin point", 
00130                       (char *)0 );
00131         return TCL_ERROR;
00132     }
00133 
00134     /* obtain the scale factors */
00135     if (TCL_OK != get_tcl_tuple(ip,argv[3],factors,3)) {
00136         Tcl_AppendResult(ip, argv[0], ": invalid scale factors", 
00137                       (char *)0 );
00138         return TCL_ERROR;
00139     }
00140     
00141     errmsg = scale_scene_node(nodename,pp::Vec3d(origin),factors);
00142 
00143     /* report error, if any */
00144     if (errmsg) {
00145         Tcl_AppendResult(ip, argv[0], ": ", errmsg, (char *)0 );
00146         return TCL_ERROR;
00147     }
00148   
00149     return TCL_OK;
00150 }
00151 
00152 int tux_transform( ClientData cd, Tcl_Interp *ip, int argc, CONST84 char *argv[] ) 
00153 {
00154     char *errmsg;
00155 
00156     const char *parent_name;
00157     const char *child_name;
00158 
00159     if (3 != argc) {
00160         Tcl_AppendResult(ip, argv[0], ": invalid number of arguments\n", 
00161                       "Usage: ", argv[0], " <parent node> <child node>",
00162                       (char *)0 );
00163         return TCL_ERROR;
00164     }
00165 
00166     /* obtain parent's name */
00167     parent_name = argv[1];
00168 
00169     /* obtain child's name */
00170     child_name  = argv[2];
00171 
00172     errmsg = create_tranform_node(parent_name, child_name);
00173 
00174     /* report error, if any */
00175     if (errmsg) {
00176         Tcl_AppendResult(ip, argv[0], ": ", errmsg, (char *)0 );
00177         return TCL_ERROR;
00178     }
00179     return TCL_OK;
00180 }
00181 
00182 int tux_sphere( ClientData cd, Tcl_Interp *ip, int argc, CONST84 char *argv[] ) 
00183 {
00184     char *errmsg;
00185 
00186     const char *parent_name;
00187     const char *child_name;
00188     double resolution;
00189 
00190     if (4 != argc) {
00191        Tcl_AppendResult( ip, argv[0], ": wrong number of arguments\n",
00192                       "Usage: ", argv[0], " <parent node> <child node> "
00193                        "<radius>",
00194                        (char*) 0 );
00195         return TCL_ERROR;
00196     }
00197 
00198     /* obtain parent's name */
00199     parent_name = argv[1];
00200 
00201     /* obtain child's name */
00202     child_name  = argv[2];
00203 
00204     if ( TCL_OK != Tcl_GetDouble( ip, argv[3], &resolution ) ) {
00205        Tcl_AppendResult( ip, argv[0], ": resolution is invalid",
00206                        (char*) 0 );
00207        return TCL_ERROR;
00208     }
00209 
00210     errmsg = create_sphere_node(parent_name, child_name, resolution);
00211 
00212     /* report error, if any */
00213     if (errmsg) {
00214         Tcl_AppendResult(ip, argv[0], ": ", errmsg, (char *)0 );
00215         return TCL_ERROR;
00216     }
00217     return TCL_OK;
00218 }
00219 
00220 
00221 int tux_material( ClientData cd, Tcl_Interp *ip, int argc, CONST84 char *argv[] ) 
00222 {
00223     char *errmsg;
00224 
00225     const char *mat_name;
00226     double diffuse[3];
00227     double specular[3];
00228     double spec_exp;
00229 
00230     if (5 != argc) {
00231         Tcl_AppendResult(ip, argv[0], ": invalid number of arguments\n", 
00232                       "Usage: ", argv[0], " <name> { <ambient color> } "
00233                       "{ <specular color> } <specular exponent",
00234                       (char *)0 );
00235         return TCL_ERROR;
00236     }
00237 
00238     /* obtain material name */
00239     mat_name = argv[1];
00240 
00241     /* obtain diffuse color */
00242     if (TCL_OK != get_tcl_tuple(ip,argv[2],diffuse,3)) {
00243         Tcl_AppendResult(ip, argv[0], ": invalid diffuse color", 
00244                       (char *)0 );
00245         return TCL_ERROR;
00246     }
00247 
00248     /* obtain specular color */
00249     if (TCL_OK != get_tcl_tuple(ip,argv[3],specular,3)) {
00250         Tcl_AppendResult(ip, argv[0], ": invalid specular color", 
00251                       (char *)0 );
00252         return TCL_ERROR;
00253     }
00254 
00255     /* obtain specular exponent */
00256     if (TCL_OK != Tcl_GetDouble(ip,argv[4],&spec_exp)) {
00257         Tcl_AppendResult(ip, argv[0], ": invalid specular exponent", 
00258                       (char *)0 );
00259         return TCL_ERROR;
00260     }
00261 
00262     errmsg = create_material(mat_name,pp::Color(diffuse),
00263                 pp::Color(specular), spec_exp);
00264 
00265     /* report error, if any */
00266     if (errmsg) {
00267         Tcl_AppendResult(ip, argv[0], ": ", errmsg, (char *)0 );
00268         return TCL_ERROR;
00269     }
00270     return TCL_OK;
00271 }
00272 
00273 int tux_surfaceproperty( ClientData cd, Tcl_Interp *ip, 
00274                       int argc, CONST84 char *argv[] ) 
00275 {
00276     char *errmsg;
00277 
00278     const char *node_name;
00279     const char *mat_name;
00280 
00281     if (3 != argc) {
00282         Tcl_AppendResult(ip, argv[0], ": invalid number of arguments\n", 
00283                       "Usage: ", argv[0], " <node> <material name>",
00284                       (char *)0 );
00285         return TCL_ERROR;
00286     }
00287 
00288     /* obtain node name */
00289     node_name = argv[1];
00290 
00291     /* obtain material name */
00292     mat_name  = argv[2];
00293 
00294     errmsg = set_scene_node_material(node_name, mat_name);
00295 
00296     /* report error, if any */
00297     if (errmsg) {
00298         Tcl_AppendResult(ip, argv[0], ": ", errmsg, (char *)0 );
00299         return TCL_ERROR;
00300     }
00301     return TCL_OK;
00302 }
00303 
00304 int tux_shadow( ClientData cd, Tcl_Interp *ip, int argc, CONST84 char *argv[] ) 
00305 {
00306     char *errmsg;
00307 
00308     const char *node_name;
00309     const char *state;
00310 
00311     if (3 != argc) {
00312         Tcl_AppendResult(ip, argv[0], ": invalid number of arguments\n", 
00313                       "Usage: ", argv[0], " <node> [on|off]",
00314                       (char *)0 );
00315         return TCL_ERROR;
00316     }
00317 
00318     node_name = argv[1];
00319     state = argv[2];
00320 
00321     errmsg = set_scene_node_shadow_state(node_name, state);
00322 
00323     /* report error, if any */
00324     if (errmsg) {
00325         Tcl_AppendResult(ip, argv[0], ": ", errmsg, (char *)0 );
00326         return TCL_ERROR;
00327     }
00328     return TCL_OK;
00329 }
00330 
00331 int tux_eye( ClientData cd, Tcl_Interp *ip, int argc, CONST84 char *argv[] ) 
00332 {
00333     char *errmsg;
00334 
00335     const char *node_name;
00336     const char *which_eye;
00337 
00338     if (3 != argc) {
00339         Tcl_AppendResult(ip, argv[0], ": invalid number of arguments\n", 
00340                       "Usage: ", argv[0], " <node> [left|right]",
00341                       (char *)0 );
00342         return TCL_ERROR;
00343     }
00344 
00345     node_name = argv[1];
00346     which_eye = argv[2];
00347 
00348     errmsg = set_scene_node_eye( node_name, which_eye );
00349 
00350     /* report error, if any */
00351     if (errmsg) {
00352         Tcl_AppendResult(ip, argv[0], ": ", errmsg, (char *)0 );
00353         return TCL_ERROR;
00354     }
00355     return TCL_OK;
00356 }
00357 
00358 int registerHierCallbacks (
00359     Tcl_Interp *ip
00360 ) {
00361     Tcl_CreateCommand(ip, "tux_rotate",          tux_rotate,          0,0);
00362     Tcl_CreateCommand(ip, "tux_translate",       tux_translate,       0,0);
00363     Tcl_CreateCommand(ip, "tux_scale",           tux_scale,           0,0);
00364     Tcl_CreateCommand(ip, "tux_sphere",          tux_sphere,          0,0);
00365     Tcl_CreateCommand(ip, "tux_transform",       tux_transform,       0,0);
00366     Tcl_CreateCommand(ip, "tux_material",        tux_material,        0,0);
00367     Tcl_CreateCommand(ip, "tux_surfaceproperty", tux_surfaceproperty, 0,0);
00368     Tcl_CreateCommand(ip, "tux_shadow",          tux_shadow,          0,0);
00369     Tcl_CreateCommand(ip, "tux_eye",             tux_eye,             0,0);
00370 
00371     return TCL_OK;
00372 }