Matrix Camera::GetModelViewMatrix() { Matrix M(u[0], u[1], u[2], 0, v[0], v[1], v[2], 0, w[0], w[1], w[2], 0, 0, 0, 0, 1); Matrix trans_eye = trans_mat(Vector(-eye[0],-eye[1],-eye[2])); return M*trans_eye; }
Matrix Camera::GetInverseModelViewMatrix() { Matrix M(u[0], v[0], w[0], 0, u[1], v[1], w[1], 0, u[2], v[2], w[2], 0, 0, 0, 0, 1); Matrix trans_eye = trans_mat(Vector(eye[0],eye[1],eye[2])); return trans_eye*M; }
/** * void arglCameraFrustumRH(const ARParam *cparam, const double focalmin, const double focalmax, GLdouble m_projection[16]) * 関数の置き換え * NyARParamからOpenGLのProjectionを作成します。 * @param i_arparam * @param o_gl_projection * double[16]を指定して下さい。 */ void NyARGLUtil::toCameraFrustumRH(const NyARToolkitCPP::NyARParam& i_arparam,GLdouble* o_gl_projection) { NyARToolkitCPP::NyARMat trans_mat(3, 4); NyARToolkitCPP::NyARMat icpara_mat(3, 4); double p[3][3], q[4][4]; int i, j; const NyARToolkitCPP::TNyARIntSize& size=i_arparam.getScreenSize(); const int width = size.w; const int height = size.h; i_arparam.getPerspectiveProjectionMatrix().decompMat(icpara_mat, trans_mat); double* icpara = icpara_mat.getArray(); double* trans = trans_mat.getArray(); for (i = 0; i < 4; i++) { icpara[1*4+i] = (height - 1) * (icpara[2*4+i]) - icpara[1*4+i]; } for (i = 0; i < 3; i++) { for (j = 0; j < 3; j++) { p[i][j] = icpara[i*4+j] / icpara[2*4+2]; } } q[0][0] = (2.0 * p[0][0] / (width - 1)); q[0][1] = (2.0 * p[0][1] / (width - 1)); q[0][2] = -((2.0 * p[0][2] / (width - 1)) - 1.0); q[0][3] = 0.0; q[1][0] = 0.0; q[1][1] = -(2.0 * p[1][1] / (height - 1)); q[1][2] = -((2.0 * p[1][2] / (height - 1)) - 1.0); q[1][3] = 0.0; q[2][0] = 0.0; q[2][1] = 0.0; q[2][2] = (view_distance_max + view_distance_min) / (view_distance_min - view_distance_max); q[2][3] = 2.0 * view_distance_max * view_distance_min / (view_distance_min - view_distance_max); q[3][0] = 0.0; q[3][1] = 0.0; q[3][2] = -1.0; q[3][3] = 0.0; for (i = 0; i < 4; i++) { // Row. // First 3 columns of the current row. for (j = 0; j < 3; j++) { // Column. o_gl_projection[i + j * 4] = q[i][0] * trans[0*4+j] + q[i][1] * trans[1*4+j] + q[i][2] * trans[2*4+j]; } // Fourth column of the current row. o_gl_projection[i + 3 * 4] = q[i][0] * trans[0*4+3] + q[i][1] * trans[1*4+3] + q[i][2] * trans[2*4+3] + q[i][3]; } return; }
int main(int argc, char **argv) { int i, j; int row, col; double **a, **b; FILE *fin; if(argc != 2) { fprintf(stderr, "\n[使用法] : transmat <file>\n"); exit(1); } /* 次元数を確認 */ get_size(argv[1], &row, &col); /* 配列を確保 */ a = new_double_matrix(col, row); /* ファイルの有無を確認 */ if((fin = fopen(argv[1], "r")) == NULL) { fprintf(stderr, "\n[エラー] : ファイル %s が開けません\n", argv[1]); exit(2); } for(i = 0; i < col; i++) for(j = 0; j < row; j++) fscanf(fin, "%lf", &a[i][j]); fclose(fin); /* 転置した行列を出力 */ b = trans_mat(a, row, col); for(i = 0; i < row; i++){ for(j = 0; j < col; j++){ fprintf(stdout, "%.6f", b[i][j]); if(j != row-1) printf(" "); } fprintf(stdout, "\n"); } /* 配列を確保 */ free_double_matrix(a); free_double_matrix(b); exit(0); }
void Lightning::fractalize_ball(LightningNode *lightningRoot) { if (head == NULL || lightningRoot->depth == 0) { return; } Matrix transform = Matrix(); Vector translateToTail; double rotX; double rotY; double rotZ; float scale; LightningNode *tmp; scale = pow(0.7, (maxFracDepth - lightningRoot->depth + 1)); for (int i = 0; i < lightningRoot->numBranches; i++) { lightningRoot->branches[i] = getRootCopy(); tmp = lightningRoot->branches[i]; if (i == 0) { tailPoint = topPoint; headPoint = botPoint; } else if (i == 1) { tailPoint = botPoint; headPoint = topPoint; } else if (i == 2) { tailPoint = lPoint; headPoint = rPoint; } else if (i == 3) { tailPoint = rPoint; headPoint = lPoint; } else if (i == 4) { tailPoint = fPoint; headPoint = bPoint; } else if (i == 5) { tailPoint = bPoint; headPoint = fPoint; } else { exit(1); } transform = lightningRoot->transform * scale_mat(Vector(scale, scale, scale)); translateToTail = (lightningRoot->transform * tailPoint) - (transform * (tmp->transform * headPoint)); transform = trans_mat(translateToTail) * transform; applyToPath(tmp, transform, lightningRoot->depth - 1); //TODO: Insert while loop for recursion while (tmp != NULL) { fractalize(tmp); tmp = tmp->next; } } }
void Camera::Orient(Point& eye, Vector& look, Vector& up) { Matrix orient; Vector lookVector = normalize(look); m_n = -lookVector; Vector temp(cross(lookVector, up)); m_u = normalize(temp); m_v = cross(m_n, m_u); orient(0,0) = m_u[0]; orient(0,1) = m_u[1]; orient(0,2) = m_u[2]; orient(1,0) = m_v[0]; orient(1,1) = m_v[1]; orient(1,2) = m_v[2]; orient(2,0) = m_n[0]; orient(2,1) = m_n[1]; orient(2,2) = m_n[2]; m_worldToCamera = orient * inv_trans_mat(Vector(eye[0], eye[1], eye[2])); m_cameraToWorld = trans_mat(Vector(eye[0], eye[1], eye[2])) * transpose(orient); }
void OpenSGNavGrab::updateNavigation(const gmtl::Matrix44f& wandMatrix) { // Travel in model // - Find forward direction of wand // - Translate along that direction const gmtl::Vec3f z_dir(0.0f, 0.0f, mVelocity); const gmtl::Vec3f trans = wandMatrix * z_dir; OSG::Matrix trans_mat(OSG::Matrix::identity()); trans_mat.setTranslate(trans[0], trans[1], trans[2]); #if OSG_MAJOR_VERSION < 2 CPEdit(mSceneTransform, OSG::Transform::MatrixFieldMask); mSceneTransform->getMatrix().multLeft(trans_mat); #else mSceneTransform->editMatrix().multLeft(trans_mat); #endif }
void Lightning::fractalize(LightningNode *lightningRoot) { if (head == NULL || lightningRoot->depth == 0) { return; } if (fracType == 1) { fractalize_ball(lightningRoot); return; } Matrix transform = Matrix(); Vector translateToTail; double rotX; double rotY; double rotZ; float scale; LightningNode *tmp; scale = pow(FRACSCALE, (maxFracDepth - lightningRoot->depth + 1)); for (int i = 0; i < lightningRoot->numBranches; i++) { lightningRoot->branches[i] = getRootCopy(); tmp = lightningRoot->branches[i]; srand(time(NULL) * (i + 5)); rotX = ((double)(rand() % 60) - 30.0) * PI / 180.0; srand(time(NULL) * i); rotY = ((double)(rand() % 360)) * PI / 180.0; srand(time(NULL) + i); rotZ = ((double)(rand() % 60) - 30.0) * PI / 180.0; transform = rotY_mat(rotY) * (rotZ_mat(rotZ) * rotX_mat(rotX)) * scale_mat(Vector(scale, scale, scale)); transform = lightningRoot->transform * transform; translateToTail = (lightningRoot->transform * tailPoint) - (transform * (tmp->transform * headPoint)); transform = trans_mat(translateToTail) * transform; applyToPath(tmp, transform, lightningRoot->depth - 1); //TODO: Insert while loop for recursion while (tmp != NULL) { fractalize(tmp); tmp = tmp->next; } } }
Matrix Camera::GetModelViewMatrix() { //set change of basis basisRotation[0] = u[0]; basisRotation[4] = u[1]; basisRotation[8] = u[2]; basisRotation[1] = v[0]; basisRotation[5] = v[1]; basisRotation[9] = v[2]; basisRotation[2] = w[0]; basisRotation[6] = w[1]; basisRotation[10] = w[2]; //printf("basisRotation\n"); //basisRotation.print(); Vector eyeTranslateV (-eyePoint[0], -eyePoint[1], -eyePoint[2]); Matrix eyeTranslate = trans_mat(eyeTranslateV); //printf("eye: \n"); //eyeTranslate.print(); modelView = basisRotation * eyeTranslate; //Point translatedEye = (basisRotation * -eyePoint); //fprintf(stderr,"\ntranslatedEye:\n"); //translatedEye.print(); //set translation //modelView = basisRotation; //modelView[12] = translatedEye[0]; //modelView[13] = translatedEye[1]; //modelView[14] = translatedEye[2]; //fprintf(stderr,"\nmodelView:\n"); //modelView.print(); return modelView; }
FlattenedNode *Lightning::lightning_recursive_flatten(FlattenedNode *graphList, Matrix transform, Matrix *rTrans, SceneNode *child, Point *eyePoint) { Matrix rec_transform = Matrix(); Matrix tmp_trans = Matrix(); FlattenedNode* tmp_flattened; if (rTrans == NULL) { rec_transform = transform * rec_transform; } else { rec_transform = *rTrans; } if (child == NULL) { return graphList; } for (int i = 0; i < child->transformations.size(); i++) { switch (child->transformations[i]->type) { case TRANSFORMATION_TRANSLATE: { tmp_trans = tmp_trans * trans_mat(child->transformations[i]->translate); break; } case TRANSFORMATION_SCALE: { tmp_trans = tmp_trans * scale_mat(child->transformations[i]->scale); break; } case TRANSFORMATION_ROTATE: { tmp_trans = tmp_trans * rot_mat(child->transformations[i]->rotate, child->transformations[i]->angle); break; } case TRANSFORMATION_MATRIX: { tmp_trans = tmp_trans * child->transformations[i]->matrix; break; } default: break; } } rec_transform = rec_transform * tmp_trans; for (int i = 0; i < child->primitives.size(); i++) { tmp_flattened = new FlattenedNode(); tmp_flattened->primitive = child->primitives[i]; if (fracType == 1) { tmp_flattened->transform = scale_mat(Vector(0.35, 0.35, 0.35)) * rec_transform; } else { tmp_flattened->transform = scale_mat(Vector(STOCKSCALE, STOCKSCALE, STOCKSCALE)) * rec_transform; } tmp_flattened->invTransform = invert(tmp_flattened->transform); tmp_flattened->objEyeP = tmp_flattened->invTransform * *eyePoint; SceneMaterial *currentMaterial = &(tmp_flattened->primitive->material); if (currentMaterial->textureMap != NULL && currentMaterial->textureMap->isUsed) { currentMaterial->openedTMap = new ppm(currentMaterial->textureMap->filename); } tmp_flattened->next = graphList; graphList = tmp_flattened; } for (int i = 0; i < child->children.size(); i++) { graphList = lightning_recursive_flatten(graphList, transform, &rec_transform, child->children[i], eyePoint); } return graphList; }
void Lightning::centerAtOrigin(Point maxPoint, Point minPoint) { Vector translate = (Point(0.0,0.0,0.0) - (maxPoint + minPoint)) / 2.0; Matrix tMat = trans_mat(translate); applyToPath(head, tMat, head->depth); }
void Lightning::generate(unsigned length) { if (fracType == 1) { generate_ball(); return; } double rotX; double rotY; double rotZ; float scale; int branchingTest; unsigned totalBolts = 0; Point minPoint, maxPoint; Point tmpPoint; Vector translate; LightningNode *tmp; LightningNode *prev; if (head != NULL || length == 0) { refresh(); } if (length == 0) { return; } head = new LightningNode; tmp = head; prev = head; for (unsigned i = 0; i < length; i++) { tmp->depth = maxFracDepth; //Random rotation of the bolt srand(time(NULL)*i); rotX = ((double)(rand() % 60) - 30.0) * PI / 180.0; srand(time(NULL)*(i+i)); rotY = ((double)(rand() % 360)) * PI / 180.0; srand(time(NULL)*(i * i)); rotZ = ((double)(rand() % 60) - 30.0) * PI / 180.0; tmp->transform = rotY_mat(rotY) * (rotZ_mat(rotZ) * rotX_mat(rotX)); //Random scaling of the bolt scale = 1.0;//((double)(rand() % 3) / 10.0) + 0.8; tmp->transform = scale_mat(Vector(scale, scale, scale)) * tmp->transform; //Inheriting previous matrices if (tmp == head) { minPoint = tmp->transform * headPoint; maxPoint = minPoint; tmpPoint = tmp->transform * tailPoint; if (tmpPoint[0] > maxPoint[0]) { maxPoint[0] = tmpPoint[0]; } else if (tmpPoint[0] < minPoint[0]) { minPoint[0] = tmpPoint[0]; } if (tmpPoint[1] > maxPoint[1]) { maxPoint[1] = tmpPoint[1]; } else if (tmpPoint[1] < minPoint[1]) { minPoint[1] = tmpPoint[1]; } if (tmpPoint[2] > maxPoint[2]) { maxPoint[2] = tmpPoint[2]; } else if (tmpPoint[2] < minPoint[2]) { minPoint[2] = tmpPoint[2]; } } else { tmp->transform = prev->transform * tmp->transform; tmpPoint = tmp->transform * headPoint; translate = (prev->transform * tailPoint) - (tmpPoint); tmp->transform = trans_mat(translate) * tmp->transform; if (tmpPoint[0] > maxPoint[0]) { maxPoint[0] = tmpPoint[0]; } else if (tmpPoint[0] < minPoint[0]) { minPoint[0] = tmpPoint[0]; } if (tmpPoint[1] > maxPoint[1]) { maxPoint[1] = tmpPoint[1]; } else if (tmpPoint[1] < minPoint[1]) { minPoint[1] = tmpPoint[1]; } if (tmpPoint[2] > maxPoint[2]) { maxPoint[2] = tmpPoint[2]; } else if (tmpPoint[2] < minPoint[2]) { minPoint[2] = tmpPoint[2]; } tmpPoint = tmp->transform * tailPoint; if (tmpPoint[0] > maxPoint[0]) { maxPoint[0] = tmpPoint[0]; } else if (tmpPoint[0] < minPoint[0]) { minPoint[0] = tmpPoint[0]; } if (tmpPoint[1] > maxPoint[1]) { maxPoint[1] = tmpPoint[1]; } else if (tmpPoint[1] < minPoint[1]) { minPoint[1] = tmpPoint[1]; } if (tmpPoint[2] > maxPoint[2]) { maxPoint[2] = tmpPoint[2]; } else if (tmpPoint[2] < minPoint[2]) { minPoint[2] = tmpPoint[2]; } } //Give it branches srand((time(NULL) * 24678 * i) + (i *2)); branchingTest = (rand()) % 200; if (branchingTest >= THRESHOLD3) { tmp->numBranches = 3; tmp->branches = new LightningNode*[3]; tmp->branches[0] = NULL; tmp->branches[1] = NULL; tmp->branches[2] = NULL; branchesPerLine += 3; } else if (branchingTest >= THRESHOLD2) { tmp->numBranches = 2; tmp->branches = new LightningNode*[2]; tmp->branches[0] = NULL; tmp->branches[1] = NULL; branchesPerLine += 2; } else if (branchingTest >= THRESHOLD1) { tmp->numBranches = 1; tmp->branches = new LightningNode*[1]; tmp->branches[0] = NULL; branchesPerLine++; } else { tmp->numBranches = 0; tmp->branches = NULL; } if (i < (length - 1)) { tmp->next = new LightningNode; prev = tmp; tmp = tmp->next; tmp->next = NULL; } } std::cout<<"Total bolts rendered: "; for (int i = 0; i <= maxFracDepth; i++) { totalBolts += length * pow(branchesPerLine, i); } std::cout<<totalBolts<<std::endl; centerAtOrigin(maxPoint, minPoint); //TODO: Fractal compatibility tmp = head; while (tmp != NULL) { fractalize(tmp); tmp = tmp->next; } }
void Camera::Translate(const Vector &v) { m_cameraToWorld = m_cameraToWorld * trans_mat(v); m_worldToCamera = inv_trans_mat(v) * m_worldToCamera; }