view resources/render/SceneGraph.js @ 0:0b8d8ce99f46 default tip

commit
author Daiki KINJYO <e085722@ie.u-ryukyu.ac.jp>
date Mon, 14 Feb 2011 17:06:56 +0900
parents
children
line wrap: on
line source

// ループさせる処理を入れる
function MainLoop(ctx,sgroot,w,h){
	draw_object(ctx,sg_exec_tree,sgroot);
	collision_object(sg_exec_tree,sgroot,w,h);
	move_object(sg_exec_tree,sgroot,w,h);
	camera_object(ctx, sgroot, sgroot.camera);
}


/* 再帰的に呼び出して全てのnodeのcollisionを実行する */
function collision_object(node,sgroot,w,h){
	if(node.collision != null){
		node.collision(node,sgroot,w,h);
		node.frame++;
	}

	for(num in node.child){
		collision_object(node.child[num],sgroot,w,h);
	}
}


/* 再帰的に呼び出して全てのnodeのmoveを実行する */
function move_object(node,sgroot,w,h){
	if(node.move != null){
		node.move(node,sgroot,w,h);
	}

	for(num in node.child){
		move_object(node.child[num],sgroot,w,h);
	}
}


/* 再帰的に呼び出して全てのnodeをdrawする */
function draw_object(ctx,node,sgroot){
	if(node.id != null){
		drawObject_link(ctx, node, node.angle, node.xyz, node.scale, sgroot.sg_src[node.id]);
	}else{
		create_matrix(node, node.angle, node.xyz, node.scale);
	}

	for(num in node.child){
		draw_object(ctx,node.child[num],sgroot);
	}
}


function camera_object(ctx, sgroot, node){
	sgroot.getCamera(ctx, node)
}


hand_mat = function(node){
	for(var num in node.child){
		node.child[num].parents_mat = new J3DIMatrix4();
		node.mat.getAsArrayMatrix(node.child[num].parents_mat.$matrix);
	}
}

//mapを作成
function createMapFromXMLfile(gl, sgroot, url){
	sgroot.createFromXMLfile(gl, sgroot, url);
	var map_name = parseName(url);
	var map = new sgroot.createSceneGraph3();
	map.set_move_collision(no_move_idle, no_collision_idle);
	var map_obj = new Array();
	for (i = 0; i < map_name.length; i = i + 1){
		map_obj[i] = new sgroot.createSceneGraph1(map_name[i]);
		map_obj[i].set_move_collision(no_move_idle, no_collision_idle);
		map_obj[i].angle[0] = 180;
		map_obj[i].angle[1] = 90;
		map_obj[i].xyz[0] = sgroot.sg_src[map_name[i]].model_x;
		map_obj[i].xyz[1] = sgroot.sg_src[map_name[i]].model_y;
		map_obj[i].xyz[2] = sgroot.sg_src[map_name[i]].model_z;
		map_obj[i].scale  = 1;
       	    map_obj[i].collision[0]  = new J3DIVector3( 81.002827,-1.639731, 81.002815);
       	    map_obj[i].collision[1]  = new J3DIVector3( 81.002827,-1.639731,-81.002815);
	    map_obj[i].collision[2]  = new J3DIVector3(-81.002827,-1.639731,-81.002815);
	    map.addChild(map_obj[i]);
	}
	return map;
}





SceneGraph = function(){
	this.next = null
	this.prev = null;
	this.last = null;
	this.parents = null;
	this.children = null;
	this.lastChild = null;

	this.xyz = new Array(0,0,0);
	this.angle = new Array(0,0,0);
	this.stack_xyz = new Array(0,0,0);
	this.stack_angle = new Array(0,0,0);
	this.w = 0;
	this.h = 0;
	this.scale = 1.0;
	this.dx = 0.0;
	this.dy = 0.0;
	this.dz = 0.0;
	this.id = null;
	this.num = 0;
	this.parents_mat = null;
	this.mat = null;
	this.link = false;

	this.move = null;
	this.collision = null;
	this.child = new Array();


	this.flag_remove = 0;
	this.flag_drawable = 1;

	this.frame = 0;
}


// SceneGraphを削除する
SceneGraph.prototype.remove = function(node){
	if(node.parents != null){
		var length = node.parents.child.length;


		if(length != 0){
			// 親の書き換え
			for(num in node.child){
				node.child[num].parents = node.parents;
			}

			// 要素を取り除く
			node.parents.child.splice(node.num, 1);

			// 取り除いたnodeのchildをnodeのparentsに追加
			for(num in node.child){
				if(node.child[num].id != "LOCK"){
					node.parents.child.push(node.child[num]);
				}
			}


			// idの振りなおし
			for(num in node.parents.child){
				node.parents.child[num].num = num;
			}
		}else{
			node.parents.child.length = 0;
		}
	}
}


/* moveとcollisionをsetする */
SceneGraph.prototype.set_move_collision = function(new_move, new_collision){
	this.move = new_move;
	this.collision = new_collision;
}


SceneGraph.prototype.addBrother = function(bro){
	if(this.parents != null){
		parents.addChild(bro);
	}else{
		alert("error : doesn't have parent");
	}

	return bro;
}


/* 子どもの追加 */
SceneGraph.prototype.addChild = function(node){	
	var num = this.child.length;
	this.child.push(node);
	node.parents = this;
	node.num = num;
}