diff TaskManager/Test/test_render/Camera.cpp @ 221:0b0e0f323742 draft

Cameraの設定、Makefile 修正
author gongo@gendarme.local
date Tue, 10 Feb 2009 01:47:12 +0900
parents 335ea3665fcd
children 0351818cf0fe
line wrap: on
line diff
--- a/TaskManager/Test/test_render/Camera.cpp	Mon Feb 09 21:58:45 2009 +0900
+++ b/TaskManager/Test/test_render/Camera.cpp	Tue Feb 10 01:47:12 2009 +0900
@@ -9,31 +9,43 @@
     Pad *pad = sgroot->getController();
     CameraPtr node = (CameraPtr)_node;
 
-    if (pad->r2.isHold()) {
-	node->angle[1] += 2.0f;
-	if (node->angle[1] > 360.0f) {
-	    node->angle[1] -= 360.0f;
-	}
-    } else if (pad->l2.isHold()) {
-	node->angle[1] -= 2.0f;
-	if (node->angle[1] < 0.0f) {
-	    node->angle[1] += 360.0f;
-	}
+    if (pad->right.isHold()) {
+	node->xyz[0] += 10.0f;
+    } else if (pad->left.isHold()) {
+	node->xyz[0] -= 10.0f;
     }
 
-#if 0
     if (pad->up.isPush() || pad->up.isHold()) {
 	node->xyz[1] += 2.0f;
     } else if (pad->down.isPush() || pad->down.isHold()) {
 	node->xyz[1] -= 2.0f;
     }
-#endif
 
     if (pad->r1.isPush() || pad->r1.isHold()) {
 	node->xyz[2] += 10.0f;
     } else if (pad->l1.isPush() || pad->l1.isHold()) {
 	node->xyz[2] -= 10.0f;
     }
+
+    if (pad->r2.isHold()) {
+	if (node->zd[0] <= 1.0f) {
+	    node->zd[0] += 0.02f;
+	}
+	if (node->zd[2] >= 0.0f) {
+	    node->zd[2] -= 0.02f;
+	}
+    } else if (pad->l2.isHold()) {
+	if (node->zd[0] > -1.0f) {
+	    node->zd[0] -= -0.02f;
+	}
+	if (node->zd[2] >= 0.0f) {
+	    node->zd[2] -= 0.02f;
+	}
+    } else {
+	node->zd[0] = 0.0f;
+	node->zd[2] = 1.0f;
+    }
+
 }
 
 static void
@@ -42,7 +54,11 @@
 {
 }
 
-Camera::Camera(void)
+/**
+ * @param w Width of screen
+ * @param h Height of screen
+ */
+Camera::Camera(float w, float h)
 {
     name = (const char*)"Camera";
 
@@ -60,21 +76,34 @@
     near = 0.0f;
     far  = 1000.0f;
 
-    //xyz[0] = 320.0f;
-    //xyz[1] = 240.0f;
-    //xyz[2] = -100.0f;
-    //xyz[3] = 1.0f;
+    m_view = new float[16];
+    m_pers = new float[16];
+    m_screen = new float[16];
+
+    // Screen の真ん中を初期値とする
+    xyz[0] = w/2.0f;
+    xyz[1] = h/2.0f;
+
+    // min(w, h) がちょうど一杯見えるような z の位置の計算
+    xyz[2] = -(((xyz[1] < xyz[0]) ? xyz[1] : xyz[0])/tanf((fov/2.0f)*M_PI/180.0f));
+    xyz[3] = 1.0f;
     
     this->set_move_collision(camera_move, camera_collision);
 }
 
+Camera::~Camera(void)
+{
+    delete [] m_view;
+    delete [] m_pers;
+    delete [] m_screen;
+}
+
 void
-Camera::createRotMatrix(float *m)
+Camera::updateView(void)
 {
     float radx,rady,radz;
-    float cx[4], cy[4], cz[4];
-    float p[4];
-    float *matrix = new float[16];
+    float cx[4], cy[4], cz[4], p[4];
+    float tm[16];
 
     radx = angle[0]*3.14/180;
     rady = angle[1]*3.14/180;
@@ -88,145 +117,130 @@
     float cosz = cos(radz);
 
     /* View Transform */
-    matrix[0] = cosz*cosy+sinz*sinx*siny;
-    matrix[1] = sinz*cosx;
-    matrix[2] = -cosz*siny+sinz*sinx*cosy;
-    matrix[3] = 0.0f;
-    matrix[4] = -sinz*cosy+cosz*sinx*siny;
-    matrix[5] = cosz*cosx;
-    matrix[6] = sinz*siny+cosz*sinx*cosy;
-    matrix[7] = 0.0f;
-    matrix[8] = cosx*siny;
-    matrix[9] = -sinx;
-    matrix[10] = cosx*cosy;
-    matrix[11] = 0.0f;
-    matrix[12] = 0.0f;
-    matrix[13] = 0.0f;
-    matrix[14] = 0.0f;
-    matrix[15] = 1.0f;
+    tm[0] = cosz*cosy+sinz*sinx*siny;
+    tm[1] = sinz*cosx;
+    tm[2] = -cosz*siny+sinz*sinx*cosy;
+    tm[3] = 0.0f;
+    tm[4] = -sinz*cosy+cosz*sinx*siny;
+    tm[5] = cosz*cosx;
+    tm[6] = sinz*siny+cosz*sinx*cosy;
+    tm[7] = 0.0f;
+    tm[8] = cosx*siny;
+    tm[9] = -sinx;
+    tm[10] = cosx*cosy;
+    tm[11] = 0.0f;
+    tm[12] = 0.0f;
+    tm[13] = 0.0f;
+    tm[14] = 0.0f;
+    tm[15] = 1.0f;
 
-    applyMatrix(cz, matrix, zd);
-    applyMatrix(cy, matrix, yd);
-    applyMatrix(p, matrix, xyz);
-
-    // matrix 使い回すためにクリア
-    unitMatrix(matrix);
+    applyMatrix(cz, tm, zd);
+    applyMatrix(cy, tm, yd);
+    applyMatrix(p, tm, xyz);
 
     outerProduct(cx, cy, cz);
-    normalize(&matrix[0], cx);
-    normalize(&matrix[8], cz);
-    outerProduct(&matrix[4], &matrix[8], &matrix[0]);
-    transMatrix(matrix, matrix, p);
-    inversMatrix(m, matrix);
+    normalize(cx, cx);
+    normalize(cz, cz);
+    outerProduct(cy, cz, cx);
+
+    m_view[ 0] = cx[0];
+    m_view[ 1] = cy[0];
+    m_view[ 2] = cz[0];
+    m_view[ 3] = 0.0f;
+
+    m_view[ 4] = cx[1];
+    m_view[ 5] = cy[1];
+    m_view[ 6] = cz[1];
+    m_view[ 7] = 0.0f;
+
+    m_view[ 8] = cx[2];
+    m_view[ 9] = cy[2];
+    m_view[10] = cz[2];
+    m_view[11] = 0.0f;
+
+    m_view[12] = innerProduct(xyz, cx)*(-1);
+    m_view[13] = innerProduct(xyz, cy)*(-1);
+    m_view[14] = innerProduct(xyz, cz)*(-1);
+    m_view[15] = 1.0f;
 }
 
 void
-Camera::createViewTransformMatrix(float *view, float *cx, float *cy, float *cz)
+Camera::updatePerspective(float w, float h)
 {
-    view[ 0] = cx[0];
-    view[ 1] = cy[0];
-    view[ 2] = cz[0];
-    view[ 3] = 0.0f;
+    float sx, sy, sz;
+    float aspect = w/h;
+
+    sy = (1.0f/tanf((fov/2.0f)*M_PI/180.0f));
+    sx = sy/aspect;
+    //sz = far/(far+near);
+    sz = far/(far-near);
+
+    m_pers[ 0] = sx;
+    m_pers[ 1] = 0.0f;
+    m_pers[ 2] = 0.0f;
+    m_pers[ 3] = 0.0f;
 
-    view[ 4] = cx[1];
-    view[ 5] = cy[1];
-    view[ 6] = cz[1];
-    view[ 7] = 0.0f;
+    m_pers[ 4] = 0.0f;
+    m_pers[ 5] = sy;
+    m_pers[ 6] = 0.0f;
+    m_pers[ 7] = 0.0f;
 
-    view[ 8] = cx[2];
-    view[ 9] = cy[2];
-    view[10] = cz[2];
-    view[11] = 0.0f;
+    m_pers[ 8] = 0.0f;
+    m_pers[ 9] = 0.0f;
+    m_pers[10] = sz;
+    m_pers[11] = 1.0f;
 
-    view[12] = innerProduct(xyz, cx)*(-1);
-    view[13] = innerProduct(xyz, cy)*(-1);
-    view[14] = innerProduct(xyz, cz)*(-1);
-    view[15] = 1.0f;
+    m_pers[12] = 0.0f;
+    m_pers[13] = 0.0f;
+    m_pers[14] = -near*sz;
+    m_pers[15] = 0.0f;
 }
 
 void
-Camera::createPerspectiveTransformMatrix(float *pm, float aspect)
-{
-    float sx, sy, sz;
-
-    sy = 1.0f/tanf((fov/2.0f)*M_PI/180.0f);
-    sx = sy / aspect;
-    sz = far/(far+near);
-
-    pm[ 0] = sx;
-    pm[ 1] = 0.0f;
-    pm[ 2] = 0.0f;
-    pm[ 3] = 0.0f;
-
-    pm[ 4] = 0.0f;
-    pm[ 5] = sy;
-    pm[ 6] = 0.0f;
-    pm[ 7] = 0.0f;
-
-    pm[ 8] = 0.0f;
-    pm[ 9] = 0.0f;
-    pm[10] = sz;
-    pm[11] = -near*sz;
-
-    pm[12] = 0.0f;
-    pm[13] = 0.0f;
-    pm[14] = -1.0f;
-    pm[15] = 0.0f;
-}
-
-void
-Camera::createScreenTransformMatrix(float *sm, float _w, float _h)
+Camera::updateScreen(float _w, float _h)
 {
     float w = _w/2.0f;
     float h = _h/2.0f;
-    float r = far/(far-near);
 
-    sm[ 0] = w;
-    sm[ 1] = 0.0f;
-    sm[ 2] = 0.0f;
-    sm[ 3] = 0.0f;
+    m_screen[ 0] = w;
+    m_screen[ 1] = 0.0f;
+    m_screen[ 2] = 0.0f;
+    m_screen[ 3] = 0.0f;
 
-    sm[ 4] = 0.0f;
-    sm[ 5] = h;
-    sm[ 6] = 0.0f;
-    sm[ 7] = 0.0f;
+    m_screen[ 4] = 0.0f;
+    m_screen[ 5] = h;
+    m_screen[ 6] = 0.0f;
+    m_screen[ 7] = 0.0f;
 
-    sm[ 8] = 0.0f;
-    sm[ 9] = 0.0f;
-    sm[10] = 1.0f;
-    sm[11] = 0.0f;
+    m_screen[ 8] = 0.0f;
+    m_screen[ 9] = 0.0f;
+    m_screen[10] = 1.0f;
+    m_screen[11] = 0.0f;
 
-    sm[12] = w;
-    sm[13] = h;
-    sm[14] = 0.0f;
-    sm[15] = 1.0f;    
+    m_screen[12] = w;
+    m_screen[13] = h;
+    m_screen[14] = 0.0f;
+    m_screen[15] = 1.0f;    
 }
 
 void
 Camera::setCamera(float *pose)
 {
-    memcpy(xyz, &pose[12], sizeof(float)*4);
+    //memcpy(xyz, &pose[12], sizeof(float)*4);
 }
 
 void
-Camera::update(int w, int h)
+Camera::update(float w, float h)
 {
-#if 0
-    // うーん。。。
-    float view[16]; // view transform matrix
-    float pers[16]; // perspective transform matrix
-    float screen[16]; // screen transform matrix
+#if 1
     float tmp[16];
 
-    // view tansform
-    createRotMatrix(view);
-    createPerspectiveTransformMatrix(pers, (float)w/(float)h);
-    createScreenTransformMatrix(screen, (float)w, (float)h);
+    updateView();
+    updatePerspective(w, h);
+    updateScreen(w, h);
     
-    matrix4x4(tmp, view, pers);
-    matrix4x4(matrix, tmp, screen);
-    //matrix4x4(tmp, pers, view);
-    //matrix4x4(matrix, screen, tmp);
+    matrix4x4(tmp, this->m_pers, this->m_screen);
+    matrix4x4(this->matrix, this->m_view, tmp);
 #else
     get_matrix(matrix, angle, xyz, NULL);
 #endif