void CyberX3D::UpdateBoundingBox( Node *node, BoundingBox *bbox) { if (node->isGeometry3DNode()) { Geometry3DNode *gnode = (Geometry3DNode *)node; gnode->recomputeBoundingBox(); float bboxCenter[3]; float bboxSize[3]; gnode->getBoundingBoxCenter(bboxCenter); gnode->getBoundingBoxSize(bboxSize); SFMatrix mx; gnode->getTransformMatrix(&mx); for (int n=0; n<8; n++) { float point[3]; point[0] = (n < 4) ? bboxCenter[0] - bboxSize[0] : bboxCenter[0] + bboxSize[0]; point[1] = (n % 2) ? bboxCenter[1] - bboxSize[1] : bboxCenter[1] + bboxSize[1]; point[2] = ((n % 4) < 2) ? bboxCenter[2] - bboxSize[2] : bboxCenter[2] + bboxSize[2]; mx.multi(point); bbox->addPoint(point); } } for (Node *cnode=node->getChildNodes(); cnode; cnode=cnode->next()) UpdateBoundingBox(cnode, bbox); }
void SceneGraph::recomputeBoundingBox() { Node *node; float center[3]; float size[3]; float m4[4][4]; SFMatrix mx; BoundingBox bbox; for (node=getNodes(); node; node=node->nextTraversal()) { if (node->isBoundedGroupingNode()) { BoundedGroupingNode *gnode = (BoundedGroupingNode *)node; gnode->getBoundingBoxCenter(center); gnode->getBoundingBoxSize(size); // Thanks for Peter DeSantis (07/22/04) gnode->getTransformMatrix(m4); mx.setValue(m4); bbox.addBoundingBox(&mx, center, size); } else if (node->isGeometry3DNode()) { Geometry3DNode *gnode = (Geometry3DNode *)node; gnode->getBoundingBoxCenter(center); gnode->getBoundingBoxSize(size); // Thanks for Peter DeSantis (07/22/04) gnode->getTransformMatrix(m4); mx.setValue(m4); bbox.addBoundingBox(&mx, center, size); } } setBoundingBox(&bbox); }
void SFMatrix::setRotation(float x, float y, float z, float rot) { SFRotation rotation(x, y, z, rot); SFMatrix matrix; rotation.getSFMatrix(&matrix); float value[4][4]; matrix.getValue(value); setValue(value); }
void ViewpointNode::getTranslationMatrix(SFMatrix *matrix) { float position[3]; getPosition(position); SFVec3f transView(position); transView.invert(); SFMatrix mxTrans; mxTrans.setTranslation(&transView); matrix->init(); matrix->add(&mxTrans); }
void Node::getTransformMatrix(SFMatrix *mxOut) const { mxOut->init(); for (const Node *node=this; node; node=node->getParentNode()) { if (node->isTransformNode() || node->isBillboardNode()) { SFMatrix mxNode; if (node->isTransformNode()) ((TransformNode *)node)->getSFMatrix(&mxNode); else ((BillboardNode *)node)->getSFMatrix(&mxNode); mxNode.add(mxOut); mxOut->setValue(&mxNode); } } }
void Node::getTranslationMatrix(SFMatrix *mxOut) const { mxOut->init(); for (const Node *node=this; node; node=node->getParentNode()) { if (node->isTransformNode() || node->isBillboardNode()) { SFMatrix mxNode; if (node->isTransformNode()) { float translation[3]; TransformNode *transNode = (TransformNode *)node; transNode->getTranslation(translation); mxNode.setTranslation(translation); } mxNode.add(mxOut); mxOut->setValue(&mxNode); } } }
static void GetRotateMatrixFromNormal( float normal[3], SFMatrix &matrix) { SFMatrix mx; SFMatrix my; float mxValue[4][4]; float myValue[4][4]; mx.getValue(mxValue); my.getValue(myValue); float d = (float)sqrt(normal[1]*normal[1] + normal[2]*normal[2]); if (d) { float cosa = normal[2] / d; float sina = normal[1] / d; mxValue[0][0] = 1.0; mxValue[0][1] = 0.0; mxValue[0][2] = 0.0; mxValue[1][0] = 0.0; mxValue[1][1] = cosa; mxValue[1][2] = sina; mxValue[2][0] = 0.0; mxValue[2][1] = -sina; mxValue[2][2] = cosa; } float cosb = d; float sinb = normal[0]; myValue[0][0] = cosb; myValue[0][1] = 0.0; myValue[0][2] = sinb; myValue[1][0] = 0.0; myValue[1][1] = 1.0; myValue[1][2] = 0.0; myValue[2][0] = -sinb; myValue[2][1] = 0.0; myValue[2][2] = cosb; mx.setValue(mxValue); my.setValue(myValue); matrix.init(); matrix.add(&my); matrix.add(&mx); }
void SFMatrix::setDirection(float x, float y, float z) { SFMatrix mx; SFMatrix my; float mxValue[4][4]; float myValue[4][4]; float normal[3]; mx.getValue(mxValue); my.getValue(myValue); normal[0] = x; normal[1] = y; normal[2] = z; float d = (float)sqrt(normal[1]*normal[1] + normal[2]*normal[2]); if (d) { float cosa = normal[2] / d; float sina = normal[1] / d; mxValue[0][0] = 1.0; mxValue[0][1] = 0.0; mxValue[0][2] = 0.0; mxValue[1][0] = 0.0; mxValue[1][1] = cosa; mxValue[1][2] = sina; mxValue[2][0] = 0.0; mxValue[2][1] = -sina; mxValue[2][2] = cosa; } float cosb = d; float sinb = normal[0]; myValue[0][0] = cosb; myValue[0][1] = 0.0; myValue[0][2] = sinb; myValue[1][0] = 0.0; myValue[1][1] = 1.0; myValue[1][2] = 0.0; myValue[2][0] = -sinb; myValue[2][1] = 0.0; myValue[2][2] = cosb; mx.setValue(mxValue); my.setValue(myValue); init(); add(&my); add(&mx); }
void TransformNode::getSFMatrix(SFMatrix *mOut) { float center[3]; float rotation[4]; float scale[3]; float scaleOri[4]; float trans[3]; SFMatrix mSRI; SFMatrix mSR; SFMatrix mCI; SFMatrix mC; SFMatrix mT; SFMatrix mR; SFMatrix mS; getTranslation(trans); mT.setTranslation(trans); getCenter(center); mC.setTranslation(center); getRotation(rotation); mR.setRotation(rotation); getScaleOrientation(scaleOri); mSR.setRotation(scaleOri); getScale(scale); mS.setScaling(scale); getScaleOrientation(scaleOri); scaleOri[3] = -scaleOri[3]; mSRI.setRotation(scaleOri); getCenter(center); center[0] = -center[0]; center[1] = -center[1]; center[2] = -center[2]; mCI.setTranslation(center); mOut->init(); mOut->add(&mT); mOut->add(&mC); mOut->add(&mR); mOut->add(&mSR); mOut->add(&mS); mOut->add(&mSRI); mOut->add(&mCI); }
void Node::getTransformMatrix(float value[4][4]) const { SFMatrix mx; getTransformMatrix(&mx); mx.getValue(value); }
void ViewpointNode::getTranslationMatrix(float value[4][4]) { SFMatrix mx; getTranslationMatrix(&mx); mx.getValue(value); }
bool IndexedFaceSetNode::generateTextureCoordinate() { TextureCoordinateNode *texCoord = getTextureCoordinateNodes(); if (texCoord) return false; CoordinateNode *coordinateNode = getCoordinateNodes(); if (!coordinateNode) return false; texCoord = new TextureCoordinateNode(); int nPolygon = getNPolygons(); if (nPolygon <= 0) return false; float (*normal)[3] = new float[nPolygon][3]; SFVec3f *center = new SFVec3f[nPolygon]; SFVec3f *maxExtents = new SFVec3f[nPolygon]; SFVec3f *minExtents = new SFVec3f[nPolygon]; bool bPolygonBegin; int polyn; float point[3][3]; float coord[3]; int vertexn = 0; int n; bPolygonBegin = true; polyn = 0; /* int nColorIndexes = idxFaceSet->getNColorIndexes(); int nNormalIndexes = idxFaceSet->getNNormalIndexes(); int nTexCoordIndexes = idxFaceSet->getNTexCoordIndexes(); */ int nCoordIndexes = getNCoordIndexes(); for (n=0; n<nCoordIndexes; n++) { int coordIndex = getCoordIndex(n); if (coordIndex != -1) { if (vertexn < 3) coordinateNode->getPoint(coordIndex, point[vertexn]); float point[3]; coordinateNode->getPoint(coordIndex, point); if (bPolygonBegin) { maxExtents[polyn].setValue(point); minExtents[polyn].setValue(point); center[polyn].setValue(point); bPolygonBegin = false; } else { SetExtents(maxExtents[polyn], minExtents[polyn], point); center[polyn].add(point); } vertexn++; } else { GetNormalFromVertices(point, normal[polyn]); center[polyn].scale(1.0f / (float)vertexn); maxExtents[polyn].sub(center[polyn]); minExtents[polyn].sub(center[polyn]); vertexn = 0; bPolygonBegin = true; polyn++; } } float minx, miny, maxx, maxy, xlength, ylength; SFMatrix matrix; bPolygonBegin = true; polyn = 0; for (n=0; n<nCoordIndexes; n++) { int coordIndex = getCoordIndex(n); if (coordIndex != -1) { if (bPolygonBegin) { GetRotateMatrixFromNormal(normal[polyn], matrix); matrix.multi(&minExtents[polyn]); matrix.multi(&maxExtents[polyn]); minx = minExtents[polyn].getX(); miny = minExtents[polyn].getY(); maxx = maxExtents[polyn].getX(); maxy = maxExtents[polyn].getY(); xlength = (float)fabs(maxExtents[polyn].getX() - minExtents[polyn].getX()); ylength = (float)fabs(maxExtents[polyn].getY() - minExtents[polyn].getY()); if (xlength == 0.0f || ylength == 0.0f) { delete texCoord; delete []minExtents; delete []maxExtents; delete []center; delete []normal; return false; } bPolygonBegin = false; } coordinateNode->getPoint(coordIndex, coord); coord[0] -= center[polyn].getX(); coord[1] -= center[polyn].getY(); coord[2] -= center[polyn].getZ(); matrix.multi(coord); coord[0] = (float)fabs(coord[0] - minx) / xlength; coord[1] = (float)fabs(coord[1] - miny) / ylength; texCoord->addPoint(coord); } else { // coord[0] = coord[1] = 0.0f; // texCoord->addPoint(coord); bPolygonBegin = true; polyn++; } } addChildNode(texCoord); delete []minExtents; delete []maxExtents; delete []center; delete []normal; return true; }
void SFRotation::multi(SFVec3f *vector) { SFMatrix m; getSFMatrix(&m); m.multi(vector); }
void SFRotation::multi(float *x, float *y, float *z) { SFMatrix m; getSFMatrix(&m); m.multi(x, y, z); }
void SFRotation::multi(float vector[]) { SFMatrix m; getSFMatrix(&m); m.multi(vector); }