diff TaskManager/Test/test_render/chain.cpp @ 395:208ba3551474 draft

chain on SPE
author game@localhost.localdomain
date Thu, 17 Sep 2009 16:55:18 +0900
parents 3d1e86396d16
children 0b623693e6ec
line wrap: on
line diff
--- a/TaskManager/Test/test_render/chain.cpp	Wed Sep 16 22:33:40 2009 +0900
+++ b/TaskManager/Test/test_render/chain.cpp	Thu Sep 17 16:55:18 2009 +0900
@@ -1,27 +1,34 @@
 #include <iostream>
 #include <math.h>
 #include "SceneGraphRoot.h"
+#include "SceneGraph.h"
 #include "SGList.h"
+#include "TaskManager.h"
+#include "Func.h"
 
 #define FALSE 0
 #define TRUE !FALSE
 #define CHAIN_LEN 50
 
-static double m = 100.0;
-static double k = 7000.0;
-static double g = 9.8;
-static double dt = 0.003;
 static double chain_width = 10;
-static double safe = 0.995;
 
 typedef struct {
     double x, y, next_x, next_y;
     double vx, vy, next_vx, next_vy;
+    double angle[3];
     int can_move;
+    SceneGraphPtr parent;
+    int id;
+    //int parent;
 } CHAIN_VARS;
 
+/* SceneGraph の property */
+CHAIN_VARS* property;
+
 CHAIN_VARS cv[CHAIN_LEN];
 
+void createSceneGraphFromProperty(CHAIN_VARS* p) ;
+
 void
 init_chain_vars(CHAIN_VARS *cv) {
     cv->x = 0, cv->y = 0, cv->next_x = 0, cv->next_y = 0;
@@ -30,10 +37,13 @@
 }
 
 void
-set_vector(CHAIN_VARS *cv, SceneGraphPtr sg) {
-    sg->xyz[0] = (float)cv->next_x;
-    sg->xyz[1] = (float)cv->next_y;
+set_vector(CHAIN_VARS *p, SceneGraphPtr sg) {
+    sg->xyz[0] = p->next_x;
+    sg->xyz[1] = p->next_y;
     sg->xyz[2] = 0.0f;
+    sg->angle[0] = p->angle[0];
+    sg->angle[1] = p->angle[1];
+    sg->angle[2] = p->angle[2];
 }
 
 
@@ -43,20 +53,20 @@
     Pad *pad = sgroot->getController();
 
     if (pad->circle.isHold()) {
-        cv[CHAIN_LEN-1].can_move = FALSE;
+        property[CHAIN_LEN-1].can_move = FALSE;
         if (pad->left.isHold()) {
-            cv[CHAIN_LEN-1].x += -5.0;
+            property[CHAIN_LEN-1].x += -5.0;
         } else if (pad->right.isHold()) {
-            cv[CHAIN_LEN-1].x += 5.0;
+            property[CHAIN_LEN-1].x += 5.0;
         }
 
         if (pad->up.isHold()) {
-            cv[CHAIN_LEN-1].y += -5.0;
+            property[CHAIN_LEN-1].y += -5.0;
         } else if (pad->down.isHold()) {
-            cv[CHAIN_LEN-1].y += 5.0;
+            property[CHAIN_LEN-1].y += 5.0;
         }
     } else {
-        cv[CHAIN_LEN-1].can_move = TRUE;
+        property[CHAIN_LEN-1].can_move = TRUE;
     }
 }
 
@@ -64,56 +74,17 @@
 chain_move(SceneGraphPtr sg, int w, int h)
 {
     int id = sg->id;
+    CHAIN_VARS* p = (CHAIN_VARS*)sg->propertyptr;
     if(id == 0) {
-        for(int cnt = 0; cnt < 600; cnt++) {
-            for(int i = 0; i < CHAIN_LEN; i++) {
-                if(cv[i].can_move) {
-                    double dx = cv[i-1].x - cv[i].x;
-                    double dy = cv[i-1].y - cv[i].y;
-                    double l = sqrt(dx * dx + dy * dy);
-                    double a = k * (l - chain_width) / m;
-                    double ax = a * dx / l;
-                    double ay = a * dy / l;
-                    if(i < CHAIN_LEN - 1) {
-                        dx = cv[i+1].x - cv[i].x;
-                        dy = cv[i+1].y - cv[i].y;
-                        l = sqrt(dx * dx + dy * dy);
-                        a = k * (l - chain_width) / m;
-                        ax += a * dx / l;
-                        ay += a * dy / l;
-                    }
-                    ay += g;
-                    cv[i].vx *= safe;
-                    cv[i].vy *= safe;
-                    cv[i].next_vx = cv[i].vx + ax * dt;
-                    cv[i].next_vy = cv[i].vy + ay * dt;
-                    cv[i].next_x = cv[i].x + cv[i].vx * dt;
-                    cv[i].next_y = cv[i].y + cv[i].vy * dt;
-                } else {
-                    cv[i].next_x = cv[i].x;
-                    cv[i].next_y = cv[i].y;
-                }
-            }
-            for(int i = 0; i < CHAIN_LEN; i++) {
-                cv[i].vx = cv[i].next_vx;
-                cv[i].vy = cv[i].next_vy;
-                cv[i].x = cv[i].next_x;
-                cv[i].y = cv[i].next_y;
-            }
-        }
-        //    cout << id << ", " << sg->xyz[1] << endl;
+	HTaskPtr chain_cal;
+	chain_cal = manager->create_task(CHAINCAL_TASK);
+	chain_cal->add_inData(&property[CHAIN_LEN-1], sizeof(CHAIN_VARS));
+	chain_cal->add_param(id);
+	chain_cal->add_outData(property, sizeof(CHAIN_VARS)*CHAIN_LEN);
+	chain_cal->spawn();
+	createSceneGraphFromProperty(p);
     }
-    set_vector(&cv[id], sg);
-    int p, n;
-    p = n = id;
-    if(p != 0) {
-        p--;
-    }
-    if(n != CHAIN_LEN - 1) {
-        n++;
-    }
-    sg->angle[2-(id%2)*2]
-        = 90 + atan((cv[p].next_y - cv[n].next_y) / (cv[p].next_x - cv[n].next_x)) * 180 / M_PI;
+    
 }
 
 void
@@ -122,34 +93,62 @@
 
 }
 
+void 
+createSceneGraphFromProperty(CHAIN_VARS* p) 
+{
+    SceneGraphPtr chain_copy;
+    chain_copy = sgroot->createSceneGraph(CHAIN);
+    chain_copy->propertyptr = (void*)p;
+    set_vector(p, chain_copy);
+    p->parent->addChild(chain_copy);
+}
+
 void
 chain_init(int w, int h)
 {
     SceneGraphPtr root_chain, chain;
     CHAIN_VARS rcv;
 
+    HTaskPtr chain_init;
+    
     sgroot->createFromXMLfile("xml_file/chain.xml");
 
+    /* SPE に送る property の配列の領域確保 */
+    property = (CHAIN_VARS*)manager->allocate(sizeof(CHAIN_VARS)*CHAIN_LEN);
+
     root_chain = sgroot->createSceneGraph(CHAIN);
     root_chain->set_move_collision(chain_move_ope, chain_collision);
     init_chain_vars(&rcv);
     rcv.next_x = w / 2;
     rcv.next_y = 0.0;
+    rcv.angle[0] = 0;
+    rcv.angle[1] = 0;
+    rcv.angle[2] = 0;
+
     set_vector(&rcv, root_chain);
 
     for(int i = 0; i < CHAIN_LEN; i++) {
         chain = sgroot->createSceneGraph(CHAIN);
-        chain->id = i;
-        init_chain_vars(&cv[i]);
-        cv[i].x = 0;
-        cv[i].y = chain_width * i;
-        set_vector(&cv[i], chain);
-        chain->angle[1] = -90 * (i % 2);
+        property[i].id = i;
+        init_chain_vars(&property[i]);
+        property[i].x = 0;
+        property[i].y = chain_width * i;
+        set_vector(&property[i], chain);
+        property->angle[1] = -90 * (i % 2);
         chain->set_move_collision(chain_move, chain_collision);
 
         root_chain->addChild(chain);
+	property[i].parent = root_chain;
     }
     cv[0].can_move = FALSE;
 
+    // property を SPU の共有領域へコピーする
+    chain_init = manager->create_task(CHAININIT_TASK);
+    chain_init->add_inData(property, sizeof(CHAIN_VARS)*CHAIN_LEN);
+    chain_init->add_param(CHAIN_LEN);
+    chain_init->set_cpu(SPE_0);
+
+
     sgroot->setSceneData(root_chain);
 }
+