1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452
|
/*
* PPRacer
* Copyright (C) 2004-2005 Volker Stroebel <volker@planetpenguin.de>
*
* Copyright (C) 1999-2001 Jasmin F. Patry
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "hier.h"
#include "hier_util.h"
#include "game_config.h"
#include "ppgltk/alg/defs.h"
/*
* These hash tables map from names to node pointers and material data, resp.
*/
Tcl_HashTable g_hier_node_table;
Tcl_HashTable g_hier_material_table;
/*
* State variables
*/
/* Default Material */
material_t g_hier_default_material ={
pp::Color(0., 0., 1.), /* diffuse color = blue */
pp::Color(0., 0., 0.), /* specular color = black */
0.0 /* specular exp. = 0.0 */
};
/*
* Functions used to access and update the name-to-pointer hash tables
*/
/* Add a new node name to the node name hash table.
node_name contains the child's name only. */
static int
add_scene_node( const char *parent_name, const char *node_name, scene_node_t *node )
{
Tcl_HashEntry *entry;
int newEntry;
char new_name[1024];
/* Add the current node to the hash table
*/
if (0 == strcmp(parent_name,":")) {
sprintf(new_name, ":%s", node_name);
} else {
sprintf(new_name, "%s:%s", parent_name, node_name);
}
node->name = (char*)malloc( strlen(new_name) + 1 );
node->name = strcpy(node->name, new_name);
entry = Tcl_CreateHashEntry(&g_hier_node_table,new_name,&newEntry);
if (newEntry) {
Tcl_SetHashValue(entry, node);
} else {
return TCL_ERROR;
}
return TCL_OK;
}
/* Get node pointer from node name */
int
get_scene_node( const char *node_name, scene_node_t **node )
{
Tcl_HashEntry *entry;
entry = Tcl_FindHashEntry(&g_hier_node_table, node_name);
if (0 == entry) {
if(0 == strcmp(node_name, ":")) {
/* the root has been specified
*/
*node = 0;
} else {
return TCL_ERROR;
}
} else {
*node = (scene_node_t*)Tcl_GetHashValue(entry);
}
return TCL_OK;
}
/* Add a new material name to the material name hash table. */
int
add_material( const char *mat_name, material_t *mat )
{
Tcl_HashEntry *entry;
int newEntry;
entry = Tcl_CreateHashEntry(&g_hier_material_table,mat_name,&newEntry);
if (newEntry) {
Tcl_SetHashValue(entry, mat);
} else {
return TCL_ERROR;
}
return TCL_OK;
}
/* Get material pointer from material name */
int
get_material( const char *mat_name, material_t **mat )
{
Tcl_HashEntry *entry;
entry = Tcl_FindHashEntry(&g_hier_material_table, mat_name);
if (0 == entry) {
return TCL_ERROR;
} else {
*mat = (material_t*)Tcl_GetHashValue(entry);
}
return TCL_OK;
}
/* Creates a new node, add the node to the hash table, and inserts the
node into the DAG. Default values are given to all fields except
the type-specific ones (geom, param). */
char* create_scene_node( const char *parent_name, const char *child_name,
scene_node_t **node )
{
scene_node_t *parent, *child;
if ( get_scene_node( parent_name, &parent ) != TCL_OK ) {
return "Parent node does not exist";
}
/* Create node */
child = (scene_node_t *)malloc( sizeof( scene_node_t ) );
/* Initialize node */
child->parent = parent;
child->next = NULL;
child->child = NULL;
child->mat = NULL;
child->render_shadow = true;
child->eye = false;
child->trans.makeIdentity();
child->invtrans.makeIdentity();
if ( add_scene_node( parent_name, child_name, child ) != TCL_OK ) {
free( child );
return "Child already exists";
}
/* Add node to parent's children */
if ( parent != NULL ) {
if ( parent->child == NULL ) {
parent->child = child;
} else {
for (parent = parent->child; parent->next != NULL;
parent = parent->next) {/* do nothing */}
parent->next = child;
}
}
*node = child;
return NULL;
}
char*
reset_scene_node( char *node )
{
scene_node_t *nodePtr;
if ( get_scene_node( node, &nodePtr ) != TCL_OK ) {
return "No such node";
}
nodePtr->trans.makeIdentity();
nodePtr->invtrans.makeIdentity();
return NULL;
}
char*
rotate_scene_node( const char *node, char axis, double angle )
{
scene_node_t *nodePtr;
pp::Matrix rotMatrix;
if ( get_scene_node( node, &nodePtr ) != TCL_OK ) {
return "No such node";
}
rotMatrix.makeRotation( angle, axis );
nodePtr->trans=nodePtr->trans*rotMatrix;
rotMatrix.makeRotation( -angle, axis );
nodePtr->invtrans=rotMatrix*nodePtr->invtrans;
return NULL;
}
char*
translate_scene_node( const char *node, pp::Vec3d vec )
{
scene_node_t *nodePtr;
pp::Matrix xlateMatrix;
if ( get_scene_node( node, &nodePtr ) != TCL_OK ) {
return "No such node";
}
xlateMatrix.makeTranslation( vec.x, vec.y, vec.z );
nodePtr->trans=nodePtr->trans*xlateMatrix;
xlateMatrix.makeTranslation( -vec.x, -vec.y, -vec.z );
nodePtr->invtrans=xlateMatrix*nodePtr->invtrans;
return NULL;
}
char*
scale_scene_node( const char *node, pp::Vec3d center, double factor[3] )
{
scene_node_t *nodePtr;
pp::Matrix matrix;
if ( get_scene_node( node, &nodePtr ) != TCL_OK ) {
return "No such node";
}
matrix.makeTranslation( -center.x, -center.y, -center.z );
nodePtr->trans=nodePtr->trans*matrix;
matrix.makeTranslation( center.x, center.y, center.z );
nodePtr->invtrans=matrix*nodePtr->invtrans;
matrix.makeScaling( factor[0], factor[1], factor[2] );
nodePtr->trans=nodePtr->trans*matrix;
matrix.makeScaling( 1./factor[0], 1./factor[1], 1./factor[2] );
nodePtr->invtrans=matrix*nodePtr->invtrans;
matrix.makeTranslation( center.x, center.y, center.z );
nodePtr->trans=nodePtr->trans*matrix;
matrix.makeTranslation( -center.x, -center.y, -center.z );
nodePtr->invtrans=matrix*nodePtr->invtrans;
return NULL;
}
char*
transform_scene_node( char *node, pp::Matrix mat, pp::Matrix invmat )
{
scene_node_t *nodePtr;
if ( get_scene_node( node, &nodePtr ) != TCL_OK ) {
return "No such node";
}
nodePtr->trans=nodePtr->trans*mat;
nodePtr->invtrans=invmat*nodePtr->invtrans;
return NULL;
}
char*
set_scene_node_material( const char *node, const char *mat )
{
material_t *matPtr;
scene_node_t *nodePtr;
if ( get_scene_node( node, &nodePtr ) != TCL_OK ) {
return "No such node";
}
if ( get_material( mat, &matPtr ) != TCL_OK ) {
return "No such material";
}
nodePtr->mat = matPtr;
return NULL;
}
char*
set_scene_node_shadow_state( const char *node, const char *state )
{
scene_node_t *nodePtr;
if ( get_scene_node( node, &nodePtr ) != TCL_OK ) {
return "No such node";
}
if ( strcmp( state, "off" ) == 0 ) {
nodePtr->render_shadow = false;
} else if ( strcmp ( state, "on" ) == 0 ) {
nodePtr->render_shadow = true;
} else {
return "Shadow state must be 'on' or 'off'";
}
return NULL;
}
char*
set_scene_node_eye( const char *node, const char *which_eye )
{
scene_node_t *nodePtr;
if ( get_scene_node( node, &nodePtr ) != TCL_OK ) {
return "No such node";
}
if ( strcmp( which_eye, "right" ) == 0 ) {
nodePtr->eye = true;
nodePtr->which_eye = TuxRightEye;
} else if ( strcmp ( which_eye, "left" ) == 0 ) {
nodePtr->eye = true;
nodePtr->which_eye = TuxLeftEye;
} else {
return "'eye' must be right or left";
}
return NULL;
}
char*
create_tranform_node( const char *parent_name, const char *child_name )
{
scene_node_t *node;
char *msg;
msg = create_scene_node(parent_name, child_name, &node);
if ( msg != NULL ) {
return msg;
}
node->geom = Empty;
return NULL;
}
char*
create_sphere_node( const char *parent_name, const char *child_name, double resolution )
{
scene_node_t *node;
char *msg;
msg = create_scene_node(parent_name, child_name, &node);
if ( msg != NULL ) {
return msg;
}
node->geom = Sphere;
node->param.sphere.radius = 1.0;
node->param.sphere.divisions = MIN(
MAX_SPHERE_DIVISIONS, MAX(
MIN_SPHERE_DIVISIONS,
ROUND_TO_NEAREST( getparam_tux_sphere_divisions() * resolution )
) );
return NULL;
}
char*
create_material( const char *mat, pp::Color diffuse,
pp::Color specular, double specular_exp )
{
material_t *matPtr;
matPtr = (material_t *)malloc( sizeof( material_t ) );
matPtr->diffuse.r = diffuse.r;
matPtr->diffuse.g = diffuse.g;
matPtr->diffuse.b = diffuse.b;
matPtr->diffuse.a = 1.0;
matPtr->specular.r = specular.r;
matPtr->specular.g = specular.g;
matPtr->specular.b = specular.b;
matPtr->specular.a = 1.0;
matPtr->specular_exp = specular_exp;
if ( add_material( mat, matPtr ) != TCL_OK ) {
free( matPtr );
return "Material already exists";
}
return NULL;
}
void initialize_scene_graph()
{
/* Initialize state */
g_hier_default_material.diffuse.r = 0.0;
g_hier_default_material.diffuse.g = 0.0;
g_hier_default_material.diffuse.b = 1.0;
g_hier_default_material.diffuse.a = 1.0;
g_hier_default_material.specular.r = 0.0;
g_hier_default_material.specular.g = 0.0;
g_hier_default_material.specular.b = 0.0;
g_hier_default_material.specular.a = 1.0;
g_hier_default_material.specular_exp = 0.0;
Tcl_InitHashTable(&g_hier_node_table,TCL_STRING_KEYS);
Tcl_InitHashTable(&g_hier_material_table,TCL_STRING_KEYS);
}
void draw_scene_graph( char *node )
{
scene_node_t *nodePtr;
if ( get_scene_node( node, &nodePtr ) != TCL_OK ) {
handle_error( 1, "draw_scene_graph: No such node `%s'", node );
}
traverse_dag( nodePtr, &g_hier_default_material );
}
bool collide( char *node, pp::Polyhedron ph )
{
scene_node_t *nodePtr;
pp::Matrix mat, invmat;
mat.makeIdentity();
invmat.makeIdentity();
if ( get_scene_node( node, &nodePtr ) != TCL_OK ) {
handle_error( 1, "draw_scene_graph: No such node `%s'", node );
}
return check_polyhedron_collision_with_dag( nodePtr, mat, invmat, ph );
}
|