/*************************************************************** * Function: acceptCAVEGeodeShape() * * 'refShapeCenter' is reference center of 'CAVEGeodeShape', use * this vector as reference center in design object space, which * is associated with center of BoundingBox. Apply the offset of * 'shapeCenter - refShapeCenter' on top level of scene graph and * then rescale them in lower levels to fit into the scale of * surface icon space. * ***************************************************************/ void CAVEGroupEditGeodeWireframe::acceptCAVEGeodeShape(CAVEGeodeShape *shapeGeode, const osg::Vec3 &refShapeCenter) { mRefShapeCenter = refShapeCenter; updateCAVEGeodeShape(shapeGeode); /* create 'MOVE' operational wireframe geode[0] */ MatrixTransform *moveTrans = new MatrixTransform; CAVEGeodeEditWireframeMove *moveGeode = new CAVEGeodeEditWireframeMove; mMoveSwitch->addChild(moveTrans); mMoveMatTransVector.push_back(moveTrans); moveTrans->addChild(moveGeode); moveTrans->setMatrix(mBoundBoxScaleMat * mAccRootMat); /* create 'ROTATE' operational wireframe geode[0] */ MatrixTransform *rotateTrans = new MatrixTransform; CAVEGeodeEditWireframeRotate *rotateGeode = new CAVEGeodeEditWireframeRotate; mRotateSwitch->addChild(rotateTrans); mRotateMatTransVector.push_back(rotateTrans); rotateTrans->addChild(rotateGeode); rotateTrans->setMatrix(mBoundSphereScaleMat * mAccRootMat); /* create 'MANIPULATE' operational wireframe geode[0] */ MatrixTransform *manipulateTrans = new MatrixTransform; CAVEGeodeEditWireframeManipulate *manipulateGeode = new CAVEGeodeEditWireframeManipulate; mManipulateSwitch->addChild(manipulateTrans); mManipulateMatTransVector.push_back(manipulateTrans); manipulateTrans->addChild(manipulateGeode); manipulateTrans->setMatrix(mBoundBoxScaleMat * mAccRootMat); }
TextureWidget::TextureWidget(Interaction* interaction, float width, float height) : Widget(), Events() { StateSet* stateSet; _interaction = interaction; _width = width; _height = height; createBackground(); createTexturesGeometry(); initLabels(); initTextures(); _texture[0] = new Geode(); _texture[0]->addDrawable(_geom); _texture[0]->addDrawable(_label0); _texture[0]->addDrawable(_texGeom0); stateSet = _texGeom0->getOrCreateStateSet(); stateSet->setMode(GL_LIGHTING, StateAttribute::OFF); stateSet->setRenderingHint(StateSet::TRANSPARENT_BIN); stateSet->setTextureAttributeAndModes(StateAttribute::TEXTURE, _tex0, StateAttribute::ON); _texture[1] = new Geode(); _texture[1]->addDrawable(_geom); _texture[1]->addDrawable(_label1); _texture[1]->addDrawable(_texGeom1); stateSet = _texGeom1->getOrCreateStateSet(); stateSet->setMode(GL_LIGHTING, StateAttribute::OFF); stateSet->setRenderingHint(StateSet::TRANSPARENT_BIN); stateSet->setTextureAttributeAndModes(StateAttribute::TEXTURE, _tex1, StateAttribute::ON); _swTexture = new Switch(); _swTexture->addChild(_texture[0]); _swTexture->addChild(_texture[1]); _swTexture->setSingleChildOn(0); _node->addChild(_swTexture.get()); _leftGeode = new Geode(); _rightGeode = new Geode(); MatrixTransform* transLeft = new MatrixTransform(); MatrixTransform* transRight = new MatrixTransform(); Matrix trans; trans.makeTranslate(Vec3(-_width/2.0, -_height/2.0 - DEFAULT_LABEL_HEIGHT/2.0, 3*EPSILON_Z)); transLeft->setMatrix(trans); transLeft->addChild(_leftGeode); trans.makeTranslate(Vec3(+_width/2.0, -_height/2.0 - DEFAULT_LABEL_HEIGHT/2.0, 3*EPSILON_Z)); transRight->setMatrix(trans); transRight->addChild(_rightGeode); _node->addChild(transLeft); _node->addChild(transRight); _interaction->addListener(this, this); }
MatrixTransform* ChessUtils::loadOSGModel(string name, float modelSize, Material* material, bool overrideMaterial, Vec3 modelCenterShift, double rotationAngle, Vec3 rotationAxis, Vec3 modelCenterOffsetPercentage) { // create a new node by reading in model from file Node* modelNode = osgDB::readNodeFile(name); if (modelNode != NULL) { // apply material if (material != NULL) { if (overrideMaterial) { modelNode->getOrCreateStateSet()->setAttributeAndModes(material, osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE); } else { modelNode->getOrCreateStateSet()->setAttributeAndModes(material, osg::StateAttribute::ON); } } //put model in origin //osg::BoundingSphere bound = modelNode->getBound(); osg::ComputeBoundsVisitor cbVisitorOrigin; modelNode->accept(cbVisitorOrigin); osg::BoundingBox bound = cbVisitorOrigin.getBoundingBox(); double scaleRatio = modelSize / bound.radius(); MatrixTransform* unitTransform = new MatrixTransform(); unitTransform->postMult(Matrix::translate(-bound.center().x(), -bound.center().y(), -bound.center().z())); unitTransform->postMult(Matrix::rotate(rotationAngle, rotationAxis)); unitTransform->postMult(Matrix::scale(scaleRatio, scaleRatio, scaleRatio)); unitTransform->addChild(modelNode); // put model in specified location osg::ComputeBoundsVisitor cbVisitor; unitTransform->accept(cbVisitor); osg::BoundingBox boundingBox = cbVisitor.getBoundingBox(); float modelXOffset = (boundingBox.xMax() - boundingBox.xMin()) * modelCenterOffsetPercentage.x(); float modelYOffset = (boundingBox.yMax() - boundingBox.yMin()) * modelCenterOffsetPercentage.y(); float modelZOffset = (boundingBox.zMax() - boundingBox.zMin()) * modelCenterOffsetPercentage.z(); unitTransform->postMult(Matrix::translate(modelXOffset, modelYOffset, modelZOffset)); MatrixTransform* modelPositionTransform = new MatrixTransform(); modelPositionTransform->postMult(Matrix::translate(modelCenterShift)); modelPositionTransform->addChild(unitTransform); return modelPositionTransform; } return NULL; }
Transform* PatchSet::createPatch(const std::string& filename, PatchOptions* poptions) { Patch* patch = new Patch; patch->setPatchSet(this); Vec2d ll, ur; poptions->getPatchExtents(ll, ur); Vec2d range = (ur - ll); ref_ptr<Patch::Data> data = new Patch::Data; int patchDim = _resolution + 1; Vec3Array* verts = new Vec3Array(patchDim * patchDim); for (int j = 0; j < patchDim; ++j) for (int i = 0; i < patchDim; ++i) (*verts)[patchDim * j + i] = Vec3((ll.x() + i * range.x() / static_cast<float>(_resolution)) * 81920.0, (ll.y() + j * range.y() / static_cast<float>(_resolution)) * 81920.0, 0.0); data->vertexData.array = verts; data->vertexData.binding = Geometry::BIND_PER_VERTEX; Vec3Array* norms = new Vec3Array(1); (*norms)[0] = Vec3d(0.0, 0.0, 1.0); data->normalData.array = norms; data->normalData.binding = Geometry::BIND_OVERALL; Vec4Array* colors = new Vec4Array(1); (*colors)[0] = Vec4(1.0, 1.0, 1.0, 1.0); data->colorData.array = colors; data->colorData.binding = Geometry::BIND_OVERALL; patch->setData(data); MatrixTransform* transform = new MatrixTransform; transform->addChild(patch); return transform; }
MatrixTransform* loadModel(std::string name, float scale, float rotX, float rotY, float rotZ, osg::Vec3 translate) { __FUNCTION_HEADER__ Node* n = NULL; //did we already load (or try to load) this one? std::map<std::string, osg::ref_ptr<osg::Node> >::iterator i; bool haz = false; for(i = gLoadedModels.begin(); i != gLoadedModels.end(); i++) { if(i->first == name) { haz = true; // printf("We already have %s\n", name.c_str()); n = i->second; } } if(!haz) //if we didn't try to laod it before, do so now { n = osgDB::readNodeFile(findDataFile(name)); gLoadedModels[name] = n; if(!n) logError("Unable to load model %s\n", name.c_str()); else printf("Loaded %s\n", name.c_str()); } if(!n) return NULL; MatrixTransform* m = new MatrixTransform(); m->setName(name); m->addChild(n); Matrix mat = Matrix::scale(scale, scale, scale); mat = mat * Matrix::rotate(rotX / 57.296, Vec3(1, 0, 0)); mat = mat * Matrix::rotate(rotY / 57.296, Vec3(0, 1, 0)); mat = mat * Matrix::rotate(rotZ / 57.296, Vec3(0, 0, 1)); mat = mat * Matrix::translate(translate); m->setMatrix(mat); return m; }
int Tree::generate(MatrixTransform *curr, int start, int level){ //Node *curr = root; printf("before: %d\n", start); int index_jump; i=start; while(i < result.size()){ printf("level is: %d\n", level); if(result[i] == '['){ MatrixTransform *tmp = new MatrixTransform(); //tmp->M = glm::scale(glm::mat4(1.0f), glm::vec3(0.5f, 0.5f, 0.5f)); curr->addChild(tmp); generate(tmp, i+1, level+1); i++; //i += index_jump; } else if(result[i] == ']'){ i++; return i-start+1; } else if(result[i] == 'F'){ MatrixTransform *tmp = new MatrixTransform(); tmp->M = glm::translate(glm::mat4(1.0f), glm::vec3(0.0f, 1.0f, 0.0f))*tmp->M; curr->addChild(tmp); Geode *child = new Geode(obj); tmp->addChild(child); index_jump = generate(tmp, i+1, level+1); i++; //i += index_jump; } else if(result[i] == '+'){ i++; curr->M = glm::rotate(glm::mat4(1.0f), 20.0f/180.0f*glm::pi<float>(), glm::vec3(0.0f, 0.0f, 1.0f))*curr->M; } else if(result[i] == '-'){ i++; curr->M = glm::rotate(glm::mat4(1.0f), -20.0f/180.0f*glm::pi<float>(), glm::vec3(0.0f, 0.0f, 1.0f))*curr->M; } } printf("after: %d\n", i); return 0; }
Boat::Boat() { mT = 0; mTV = 0.1; MatrixTransform* modelTransform = new MatrixTransform(); mPat->addChild(modelTransform); //load a boat model MatrixTransform* n = Util::loadModel("jetski.3DS", 1.0/12 / 4, -90); if(n) { // Util::setTint(n, Vec4(1, 0, 0, 1)); //the texture doesn't apply automatically for some reason, so let's do it manually Image* image = osgDB::readImageFile(Util::findDataFile("jetski.jpg")); Texture2D* tex = new Texture2D(image); n->getOrCreateStateSet()->setTextureAttributeAndModes(0, tex); } modelTransform->addChild(n); }
/*************************************************************** * Function: applyScaling() * * 'gridUnitScaleVect' is guranteed to be non-negative vector * ***************************************************************/ void CAVEGroupEditGeodeWireframe::applyScaling( const short &nOffsetSegs, const Vec3 &gridUnitScaleVect, const std::string &gridUnitScaleInfo) { mManipulateSwitch->setAllChildrenOn(); /* scale to other direction: clear all children of 'mManipulateSwitch' except child[0], rebuild offset tree */ if (mScaleNumSegs * nOffsetSegs <= 0) { unsigned int numChildren = mManipulateSwitch->getNumChildren(); if (numChildren > 1) { mManipulateSwitch->removeChildren(1, numChildren - 1); MatrixTransVector::iterator itrMatTrans = mManipulateMatTransVector.begin(); mManipulateMatTransVector.erase(itrMatTrans + 1, itrMatTrans + numChildren); } mScaleNumSegs = 0; } /* decide unit offset vector and start/end index of children under 'mManipulateSwitch' */ short idxStart = 0, idxEnd = 0; if (nOffsetSegs != 0) { idxStart = mScaleNumSegs; idxEnd = nOffsetSegs; } idxStart = idxStart > 0 ? idxStart: -idxStart; idxEnd = idxEnd > 0 ? idxEnd : -idxEnd; /* update the first wireframe with global translation and rotation */ if (idxStart == 0) mManipulateMatTransVector[0]->setMatrix(mBoundBoxScaleMat * mAccRootMat); /* create or remove a sequence of extra children under 'mMoveSwitch' */ if (idxStart < idxEnd) { for (short i = idxStart + 1; i <= idxEnd; i++) { /* generate scaling vector with non-negative values */ Vec3 scaleVect = Vec3(1, 1, 1); if (nOffsetSegs > 0) scaleVect += gridUnitScaleVect * i; else scaleVect -= gridUnitScaleVect * i; scaleVect.x() = scaleVect.x() > 0 ? scaleVect.x() : 0; scaleVect.y() = scaleVect.y() > 0 ? scaleVect.y() : 0; scaleVect.z() = scaleVect.z() > 0 ? scaleVect.z() : 0; Matrixd scaleMat; scaleMat.makeScale(scaleVect); MatrixTransform *scaleTrans = new MatrixTransform; CAVEGeodeEditWireframeManipulate *manipulateGeode = new CAVEGeodeEditWireframeManipulate; mManipulateSwitch->addChild(scaleTrans); mManipulateMatTransVector.push_back(scaleTrans); scaleTrans->addChild(manipulateGeode); scaleTrans->setMatrix(mBoundBoxScaleMat * mAccRootMat * scaleMat); } } else if (idxStart > idxEnd) { mManipulateSwitch->removeChildren(idxEnd + 1, idxStart - idxEnd); MatrixTransVector::iterator itrMatTrans = mManipulateMatTransVector.begin(); itrMatTrans += idxEnd + 1; mManipulateMatTransVector.erase(itrMatTrans, itrMatTrans + (idxStart - idxEnd)); } mScaleNumSegs = nOffsetSegs; mScaleUnitVect = gridUnitScaleVect; if (!mPrimaryFlag) return; /* update info text if 'this' wireframe is primary */ mEditInfoTextSwitch->setAllChildrenOn(); float scaleUnit = gridUnitScaleVect.x() > 0 ? gridUnitScaleVect.x() : gridUnitScaleVect.y(); scaleUnit = scaleUnit > 0 ? scaleUnit : gridUnitScaleVect.z(); char info[128]; float scaleval = scaleUnit * nOffsetSegs + 1.0f; scaleval = scaleval > 0 ? scaleval:0; sprintf(info, "Scale = %3.2f \nSnapping = ", scaleval); mEditInfoText->setText(info + gridUnitScaleInfo); }
/*************************************************************** * Function: applyRotation() * * 'axisSVect': rotational snapping values around each axis, * for instance, Vec3(2, 0, 0) means rotation around X-axis by * two times of 'gridUnitAngle' * only one component of 'axisIntVect' is supposed to be non-zero * ***************************************************************/ void CAVEGroupEditGeodeWireframe::applyRotation(const osg::Vec3s &axisSVect, const float &gridUnitAngle, const string &gridUnitAngleInfo) { if (!mPrimaryFlag) return; mRotateSwitch->setAllChildrenOn(); /* rotate in other direction: clear all children of 'mRotateSwitch' except child[0], rebuild offset tree */ if ((mRotateSVect.x() * axisSVect.x() + mRotateSVect.y() * axisSVect.y() + mRotateSVect.z() * axisSVect.z()) <= 0) { unsigned int numChildren = mRotateSwitch->getNumChildren(); if (numChildren > 1) { mRotateSwitch->removeChildren(1, numChildren - 1); MatrixTransVector::iterator itrMatTrans = mRotateMatTransVector.begin(); mRotateMatTransVector.erase(itrMatTrans + 1, itrMatTrans + numChildren); } mRotateSVect = Vec3s(0, 0, 0); } /* decide unit rotation quat and start/end index of children under 'mRotateSwitch' */ Vec3 gridRotationAxis; short idxStart = 0, idxEnd = 0; if (axisSVect.x() != 0) { idxStart = mRotateSVect.x(); idxEnd = axisSVect.x(); if (axisSVect.x() > 0) gridRotationAxis = Vec3(1, 0, 0); else gridRotationAxis = Vec3(-1, 0, 0); } else if (axisSVect.y() != 0) { idxStart = mRotateSVect.y(); idxEnd = axisSVect.y(); if (axisSVect.y() > 0) gridRotationAxis = Vec3(0, 1, 0); else gridRotationAxis = Vec3(0, -1, 0); } else if (axisSVect.z() != 0) { idxStart = mRotateSVect.z(); idxEnd = axisSVect.z(); if (axisSVect.z() > 0) gridRotationAxis = Vec3(0, 0, 1); else gridRotationAxis = Vec3(0, 0, -1); } idxStart = idxStart > 0 ? idxStart: -idxStart; idxEnd = idxEnd > 0 ? idxEnd : -idxEnd; /* create or remove a sequence of extra children under 'mRotateSwitch' */ if (idxStart < idxEnd) { for (short i = idxStart + 1; i <= idxEnd; i++) { Matrixd rotMat; rotMat.makeRotate(gridUnitAngle * i, gridRotationAxis); MatrixTransform *rotateTrans = new MatrixTransform; CAVEGeodeEditWireframeRotate *rotateGeode = new CAVEGeodeEditWireframeRotate; mRotateSwitch->addChild(rotateTrans); mRotateMatTransVector.push_back(rotateTrans); rotateTrans->addChild(rotateGeode); rotateTrans->setMatrix(mBoundSphereScaleMat * rotMat); } } else if (idxStart > idxEnd) { mRotateSwitch->removeChildren(idxEnd + 1, idxStart - idxEnd); MatrixTransVector::iterator itrMatTrans = mRotateMatTransVector.begin(); itrMatTrans += idxEnd + 1; mRotateMatTransVector.erase(itrMatTrans, itrMatTrans + (idxStart - idxEnd)); } mRotateSVect = axisSVect; /* update info text if 'this' wireframe is primary */ mEditInfoTextSwitch->setAllChildrenOn(); char info[128]; const float gridUnitAngleDegree = gridUnitAngle * 180 / M_PI; sprintf(info, "Angle = %3.2f\nSnapping = ", gridUnitAngleDegree * idxEnd); mEditInfoText->setText(info + gridUnitAngleInfo); }
/*************************************************************** * Function: applyTranslation() * * 'gridSVect': number of snapping segments along each direction * 'gridUnitLegnth': actual length represented by each segment, * only one component of 'gridSVect' is supposed to be non-zero * ***************************************************************/ void CAVEGroupEditGeodeWireframe::applyTranslation(const osg::Vec3s &gridSVect, const float &gridUnitLegnth, const string &gridUnitLegnthInfo) { mMoveSwitch->setAllChildrenOn(); /* move to other direction: clear all children of 'mMoveSwitch' except child[0], rebuild offset tree */ if ((mMoveSVect.x() * gridSVect.x() + mMoveSVect.y() * gridSVect.y() + mMoveSVect.z() * gridSVect.z()) <= 0) { unsigned int numChildren = mMoveSwitch->getNumChildren(); if (numChildren > 1) { mMoveSwitch->removeChildren(1, numChildren - 1); MatrixTransVector::iterator itrMatTrans = mMoveMatTransVector.begin(); mMoveMatTransVector.erase(itrMatTrans + 1, itrMatTrans + numChildren); } mMoveSVect = Vec3s(0, 0, 0); } /* decide unit offset vector and start/end index of children under 'mMoveSwitch' */ Vec3 gridOffsetVect; short idxStart = 0, idxEnd = 0; if (gridSVect.x() != 0) { idxStart = mMoveSVect.x(); idxEnd = gridSVect.x(); if (gridSVect.x() > 0) gridOffsetVect = Vec3(gridUnitLegnth, 0, 0); else gridOffsetVect = Vec3(-gridUnitLegnth, 0, 0); } else if (gridSVect.y() != 0) { idxStart = mMoveSVect.y(); idxEnd = gridSVect.y(); if (gridSVect.y() > 0) gridOffsetVect = Vec3(0, gridUnitLegnth, 0); else gridOffsetVect = Vec3(0, -gridUnitLegnth, 0); } else if (gridSVect.z() != 0) { idxStart = mMoveSVect.z(); idxEnd = gridSVect.z(); if (gridSVect.z() > 0) gridOffsetVect = Vec3(0, 0, gridUnitLegnth); else gridOffsetVect = Vec3(0, 0, -gridUnitLegnth); } idxStart = idxStart > 0 ? idxStart: -idxStart; idxEnd = idxEnd > 0 ? idxEnd : -idxEnd; /* update the first wireframe with global translation and rotation */ if (idxStart == 0) mMoveMatTransVector[0]->setMatrix(mBoundBoxScaleMat * mAccRootMat); /* create or remove a sequence of extra children under 'mMoveSwitch' */ if (idxStart < idxEnd) { for (short i = idxStart + 1; i <= idxEnd; i++) { Matrixd transMat; transMat.makeTranslate(gridOffsetVect * i); MatrixTransform *moveTrans = new MatrixTransform; CAVEGeodeEditWireframeMove *moveGeode = new CAVEGeodeEditWireframeMove; mMoveSwitch->addChild(moveTrans); mMoveMatTransVector.push_back(moveTrans); moveTrans->addChild(moveGeode); moveTrans->setMatrix(mBoundBoxScaleMat * mAccRootMat * transMat); } } else if (idxStart > idxEnd) { mMoveSwitch->removeChildren(idxEnd + 1, idxStart - idxEnd); MatrixTransVector::iterator itrMatTrans = mMoveMatTransVector.begin(); itrMatTrans += idxEnd + 1; mMoveMatTransVector.erase(itrMatTrans, itrMatTrans + (idxStart - idxEnd)); } mMoveSVect = gridSVect; if (!mPrimaryFlag) return; /* update info text if 'this' wireframe is primary */ mEditInfoTextSwitch->setAllChildrenOn(); char info[128]; sprintf(info, "Offset = %3.2f m\nSnapping = ", gridUnitLegnth * idxEnd); mEditInfoText->setText(info + gridUnitLegnthInfo); }
void Window::loadCharacter() { Matrix4 temp; Matrix4 id; id.identity(); //Setting up Controller temp.makeTranslate(0, 0, 10); control = MatrixTransform(temp); //Setting up Head headCube = Cube(2); headCube.setTexture(head); headRotation = MatrixTransform(id); headScaling = MatrixTransform(id); temp.makeTranslate(0, 7, 0); headTranslation = MatrixTransform(temp); headRotation.addChild(&headCube); headScaling.addChild(&headRotation); headTranslation.addChild(&headScaling); control.addChild(&headTranslation); //Setting up body bodyCube = Cube(1); bodyCube.setTexture(body); bodyRotation = MatrixTransform(id); temp.makeScale(2, 3, 1); bodyScaling = MatrixTransform(temp); temp.makeTranslate(0, 4.5, 0); bodyTranslation = MatrixTransform(temp); bodyRotation.addChild(&bodyCube); bodyScaling.addChild(&bodyRotation); bodyTranslation.addChild(&bodyScaling); control.addChild(&bodyTranslation); //Setting up Arms armCube = Cube(1); armCube.setTexture(arm); leftArmRotation = MatrixTransform(id); rightArmRotation = MatrixTransform(id); temp.makeScale(1, 3, 1); leftArmScaling = MatrixTransform(temp); rightArmScaling = MatrixTransform(temp); temp.makeTranslate(1.5, 4.5, 0); leftArmTranslation = MatrixTransform(temp); temp.makeTranslate(-(1.5), 4.5, 0); rightArmTranslation = MatrixTransform(temp); leftArmScaling.addChild(&armCube); rightArmScaling.addChild(&armCube); leftArmRotation.addChild(&leftArmScaling); rightArmRotation.addChild(&rightArmScaling); leftArmTranslation.addChild(&leftArmRotation); rightArmTranslation.addChild(&rightArmRotation); control.addChild(&leftArmTranslation); control.addChild(&rightArmTranslation); //Setting up Legs legCube = Cube(1); legCube.setTexture(leg); leftLegRotation = MatrixTransform(id); rightLegRotation = MatrixTransform(id); temp.makeScale(1, 3, 1); leftLegScaling = MatrixTransform(temp); rightLegScaling = MatrixTransform(temp); temp.makeTranslate(.5, 1.5, 0); leftLegTranslation = MatrixTransform(temp); temp.makeTranslate(-.5, 1.5, 0); rightLegTranslation = MatrixTransform(temp); leftLegScaling.addChild(&legCube); rightLegScaling.addChild(&legCube); leftLegRotation.addChild(&leftLegScaling); rightLegRotation.addChild(&rightLegScaling); leftLegTranslation.addChild(&leftLegRotation); rightLegTranslation.addChild(&rightLegRotation); control.addChild(&leftLegTranslation); control.addChild(&rightLegTranslation); character.addChild(&control); }
/*************************************************************** * Function: ANIMLoadSketchBook() ***************************************************************/ void ANIMLoadSketchBook(PositionAttitudeTransform** xformScaleFwd, PositionAttitudeTransform** xformScaleBwd, int &numPages, ANIMPageEntry ***pageEntryArray) { *xformScaleFwd = new PositionAttitudeTransform; *xformScaleBwd = new PositionAttitudeTransform; MatrixTransform *sketchbookTrans = new MatrixTransform; Matrixf transMat, scaleMat; transMat.makeTranslate(Vec3(0, 0, ANIM_VIRTUAL_SPHERE_RADIUS)); scaleMat.makeScale(Vec3(ANIM_PARA_PAINT_FRAME_ZOOM_FACTOR, ANIM_PARA_PAINT_FRAME_ZOOM_FACTOR, ANIM_PARA_PAINT_FRAME_ZOOM_FACTOR)); sketchbookTrans->setMatrix(transMat * scaleMat); (*xformScaleFwd)->addChild(sketchbookTrans); (*xformScaleBwd)->addChild(sketchbookTrans); // load sketch book node from VRML file, create page geodes Node* sketchbookNode = osgDB::readNodeFile(ANIMDataDir() + "VRMLFiles/SketchBook.WRL"); sketchbookTrans->addChild(sketchbookNode); // Load floorplan filenames from config file bool isFile = true; int j = 0, numTex; std::string file = "", dir, path; std::vector<std::string> filenames; dir = cvr::ConfigManager::getEntry("dir", "Plugin.CaveCADBeta.Floorplans", "/home/cehughes"); dir = dir + "/"; path = "Plugin.CaveCADBeta.Floorplans.0"; file = cvr::ConfigManager::getEntry(path, "", &isFile); while (isFile) { filenames.push_back(dir + file); j++; char buf[50]; sprintf(buf, "Plugin.CaveCADBeta.Floorplans.%d", j); std::string path = std::string(buf); file = cvr::ConfigManager::getEntry(path, "", &isFile); } numPages = j; //numPages = 3; // create tree structured page entry array *pageEntryArray = new ANIMPageEntry*[numPages]; for (int i = 0; i < numPages; i++) { /* char idxStr[16]; if (i < 10) { sprintf(idxStr, "0%d", i); } else if (i < 100) { sprintf(idxStr, "%d", i); }*/ string filename = filenames[i];//ANIMDataDir() + "Textures/Floorplans/Floorplan" + string(idxStr) + string(".JPG"); (*pageEntryArray)[i] = new ANIMPageEntry; Switch *singlePageSwitch = new Switch; PositionAttitudeTransform *flipUpTrans = new PositionAttitudeTransform; PositionAttitudeTransform *flipDownTrans = new PositionAttitudeTransform; sketchbookTrans->addChild(singlePageSwitch); singlePageSwitch->addChild(flipUpTrans); singlePageSwitch->addChild(flipDownTrans); singlePageSwitch->setAllChildrenOff(); // set up flip up / flip down animation paths for each page Geode *flipUpGeode, *flipDownGeode; AnimationPathCallback *flipUpCallback, *flipDownCallback; ANIMCreateSinglePageGeodeAnimation(filename, &flipUpGeode, &flipDownGeode, &flipUpCallback, &flipDownCallback); flipUpTrans->addChild(flipUpGeode); flipUpTrans->setUpdateCallback(flipUpCallback); flipDownTrans->addChild(flipDownGeode); flipDownTrans->setUpdateCallback(flipDownCallback); // write into page entry array record (*pageEntryArray)[i]->mSwitch = singlePageSwitch; (*pageEntryArray)[i]->mFlipUpAnim = flipUpCallback; (*pageEntryArray)[i]->mFlipDownAnim = flipDownCallback; (*pageEntryArray)[i]->mPageGeode = flipDownGeode; (*pageEntryArray)[i]->mTexFilename = filename; } (*pageEntryArray)[0]->mSwitch->setSingleChildOn(1); (*pageEntryArray)[1]->mSwitch->setSingleChildOn(0); // size of floorplan // FIX THIS - put sizes in the config file or something float alt = -2.9f; (*pageEntryArray)[0]->mLength = 32; (*pageEntryArray)[0]->mWidth = 16; (*pageEntryArray)[0]->mAlti = alt; (*pageEntryArray)[1]->mLength = 128; (*pageEntryArray)[1]->mWidth = 128; (*pageEntryArray)[1]->mAlti = alt; (*pageEntryArray)[2]->mLength = 32; (*pageEntryArray)[2]->mWidth = 16; (*pageEntryArray)[2]->mAlti = alt; (*pageEntryArray)[3]->mLength = 32; (*pageEntryArray)[3]->mWidth = 32; (*pageEntryArray)[3]->mAlti = alt; // set up the forward / backward scale animation paths for sketch book root switch AnimationPath* animationPathScaleFwd = new AnimationPath; AnimationPath* animationPathScaleBwd = new AnimationPath; animationPathScaleFwd->setLoopMode(AnimationPath::NO_LOOPING); animationPathScaleBwd->setLoopMode(AnimationPath::NO_LOOPING); Vec3 scaleFwd, scaleBwd; float step = 1.f / ANIM_VIRTUAL_SPHERE_NUM_SAMPS; for (int i = 0; i < ANIM_VIRTUAL_SPHERE_NUM_SAMPS + 1; i++) { float val = i * step; scaleFwd = Vec3(val, val, val); scaleBwd = Vec3(1.f-val, 1.f-val, 1.f-val); animationPathScaleFwd->insert(val, AnimationPath::ControlPoint(Vec3(),Quat(), scaleFwd)); animationPathScaleBwd->insert(val, AnimationPath::ControlPoint(Vec3(),Quat(), scaleBwd)); } AnimationPathCallback *animCallbackFwd = new AnimationPathCallback(animationPathScaleFwd, 0.0, 1.f / ANIM_VIRTUAL_SPHERE_LAPSE_TIME); AnimationPathCallback *animCallbackBwd = new AnimationPathCallback(animationPathScaleBwd, 0.0, 1.f / ANIM_VIRTUAL_SPHERE_LAPSE_TIME); (*xformScaleFwd)->setUpdateCallback(animCallbackFwd); (*xformScaleBwd)->setUpdateCallback(animCallbackBwd); }
/*************************************************************** * Function: ANIMCreateRefSkyDome() * ***************************************************************/ MatrixTransform *ANIMCreateRefSkyDome(StateSet **stateset) { /* sky dome geometry */ Sphere *skyShape = new Sphere(); ShapeDrawable* skyDrawable = new ShapeDrawable(skyShape); Geode* skyGeode = new Geode(); MatrixTransform *skyDomeTrans = new MatrixTransform; //osg::Matrix m; //m.makeRotate(osg::Quat(M_PI/2, osg::Vec3(0, 1, 0))); //skyDomeTrans->setMatrix(m); skyShape->setRadius(ANIM_SKYDOME_RADIUS); skyGeode->addDrawable(skyDrawable); skyDomeTrans->addChild(skyGeode); // apply simple colored materials Material* material = new Material; material->setDiffuse(Material::FRONT_AND_BACK, Vec4(1.0f, 1.0f, 1.0f, 1.0f)); material->setAlpha(Material::FRONT_AND_BACK, 1.0f); (*stateset) = new StateSet(); (*stateset)->setAttributeAndModes(material, StateAttribute::OVERRIDE | StateAttribute::ON); (*stateset)->setMode(GL_BLEND, StateAttribute::OVERRIDE | StateAttribute::ON ); (*stateset)->setRenderingHint(StateSet::TRANSPARENT_BIN); skyGeode->setStateSet(*stateset); // skyGeode->setNodeMask(0xFFFFFF & ~(0x2 | 0x3)); // load sky dome shader Uniform* sunrUniform = new Uniform("hazeRadisuMin", 0.975f); (*stateset)->addUniform(sunrUniform); Uniform* sunRUniform = new Uniform("hazeRadisuMax", 0.995f); (*stateset)->addUniform(sunRUniform); Uniform* sunDirUniform = new Uniform("sundir", Vec4(0.0, 0.0, 1.0, 1.0)); (*stateset)->addUniform(sunDirUniform); Uniform* suncolorUniform = new Uniform("suncolor", Vec4(1.0, 1.0, 1.0, 1.0)); (*stateset)->addUniform(suncolorUniform); Uniform* skycolorUniform = new Uniform("skycolor", Vec4(0.5, 0.5, 1.0, 1.0)); (*stateset)->addUniform(skycolorUniform); Uniform* skyfadingcolorUniform = new Uniform("skyfadingcolor", Vec4(0.8, 0.8, 0.8, 1.0)); (*stateset)->addUniform(skyfadingcolorUniform); Uniform* skymaskingcolorUniform = new Uniform("skymaskingcolor", Vec4(1.0, 1.0, 1.0, 1.0)); (*stateset)->addUniform(skymaskingcolorUniform); Uniform *matShaderToWorldUniform = new Uniform("shaderToWorldMat", Matrixd()); (*stateset)->addUniform(matShaderToWorldUniform); Image* imageSky = osgDB::readImageFile(ANIMDataDir() + "Textures/NightSky.JPG"); Texture2D* textureSky = new Texture2D(imageSky); (*stateset)->setTextureAttributeAndModes(0, textureSky, StateAttribute::ON); Image* imagePara = osgDB::readImageFile(ANIMDataDir() + "Textures/Paramounts/Paramount00.JPG"); Texture2D* texturePara = new Texture2D(imagePara); (*stateset)->setTextureAttributeAndModes(1, texturePara, StateAttribute::ON); Uniform* skyNightSampler = new Uniform("texNightSky", 0); (*stateset)->addUniform(skyNightSampler); Uniform* paraImageTextureSampler = new Uniform("texParamount", 1); (*stateset)->addUniform(paraImageTextureSampler); Program* programSky = new Program; (*stateset)->setAttribute(programSky); programSky->addShader(Shader::readShaderFile(Shader::VERTEX, ANIMDataDir() + "Shaders/EnvSky.vert")); programSky->addShader(Shader::readShaderFile(Shader::FRAGMENT, ANIMDataDir() + "Shaders/EnvSky.frag")); return skyDomeTrans; }
/*************************************************************** * Function: ANIMLoadSketchBook() ***************************************************************/ void ANIMLoadSketchBook(PositionAttitudeTransform** xformScaleFwd, PositionAttitudeTransform** xformScaleBwd, int &numPages, ANIMPageEntry ***pageEntryArray) { *xformScaleFwd = new PositionAttitudeTransform; *xformScaleBwd = new PositionAttitudeTransform; MatrixTransform *sketchbookTrans = new MatrixTransform; Matrixf transMat, scaleMat; transMat.makeTranslate(Vec3(0, 0, ANIM_VIRTUAL_SPHERE_RADIUS)); scaleMat.makeScale(Vec3(ANIM_PARA_PAINT_FRAME_ZOOM_FACTOR, ANIM_PARA_PAINT_FRAME_ZOOM_FACTOR, ANIM_PARA_PAINT_FRAME_ZOOM_FACTOR)); sketchbookTrans->setMatrix(transMat * scaleMat); (*xformScaleFwd)->addChild(sketchbookTrans); (*xformScaleBwd)->addChild(sketchbookTrans); /* load sketch book node from VRML file, create page geodes */ Node* sketchbookNode = osgDB::readNodeFile(ANIMDataDir() + "VRMLFiles/SketchBook.WRL"); sketchbookTrans->addChild(sketchbookNode); /* create tree structured page entry array */ numPages = 3; *pageEntryArray = new ANIMPageEntry*[numPages]; for (int i = 0; i < numPages; i++) { char idxStr[16]; if (i < 10) sprintf(idxStr, "0%d", i); else if (i < 100) sprintf(idxStr, "%d", i); string filename = ANIMDataDir() + "Textures/Floorplans/Floorplan" + string(idxStr) + string(".JPG"); (*pageEntryArray)[i] = new ANIMPageEntry; Switch *singlePageSwitch = new Switch; PositionAttitudeTransform *flipUpTrans = new PositionAttitudeTransform; PositionAttitudeTransform *flipDownTrans = new PositionAttitudeTransform; sketchbookTrans->addChild(singlePageSwitch); singlePageSwitch->addChild(flipUpTrans); singlePageSwitch->addChild(flipDownTrans); singlePageSwitch->setAllChildrenOff(); /* set up flip up / flip down animation paths for each page */ Geode *flipUpGeode, *flipDownGeode; AnimationPathCallback *flipUpCallback, *flipDownCallback; ANIMCreateSinglePageGeodeAnimation(filename, &flipUpGeode, &flipDownGeode, &flipUpCallback, &flipDownCallback); flipUpTrans->addChild(flipUpGeode); flipUpTrans->setUpdateCallback(flipUpCallback); flipDownTrans->addChild(flipDownGeode); flipDownTrans->setUpdateCallback(flipDownCallback); /* write into page entry array record */ (*pageEntryArray)[i]->mSwitch = singlePageSwitch; (*pageEntryArray)[i]->mFlipUpAnim = flipUpCallback; (*pageEntryArray)[i]->mFlipDownAnim = flipDownCallback; (*pageEntryArray)[i]->mPageGeode = flipDownGeode; (*pageEntryArray)[i]->mTexFilename = filename; } (*pageEntryArray)[0]->mSwitch->setSingleChildOn(1); (*pageEntryArray)[1]->mSwitch->setSingleChildOn(0); /* size of floorplan */ (*pageEntryArray)[0]->mLength = 32; (*pageEntryArray)[0]->mWidth = 16; (*pageEntryArray)[0]->mAlti = -1.5f; (*pageEntryArray)[1]->mLength = 128; (*pageEntryArray)[1]->mWidth = 128; (*pageEntryArray)[1]->mAlti = -1.5f; (*pageEntryArray)[2]->mLength = 32; (*pageEntryArray)[2]->mWidth = 16; (*pageEntryArray)[2]->mAlti = -1.5f; /* set up the forward / backward scale animation paths for sketch book root switch */ AnimationPath* animationPathScaleFwd = new AnimationPath; AnimationPath* animationPathScaleBwd = new AnimationPath; animationPathScaleFwd->setLoopMode(AnimationPath::NO_LOOPING); animationPathScaleBwd->setLoopMode(AnimationPath::NO_LOOPING); Vec3 scaleFwd, scaleBwd; float step = 1.f / ANIM_VIRTUAL_SPHERE_NUM_SAMPS; for (int i = 0; i < ANIM_VIRTUAL_SPHERE_NUM_SAMPS + 1; i++) { float val = i * step; scaleFwd = Vec3(val, val, val); scaleBwd = Vec3(1.f-val, 1.f-val, 1.f-val); animationPathScaleFwd->insert(val, AnimationPath::ControlPoint(Vec3(),Quat(), scaleFwd)); animationPathScaleBwd->insert(val, AnimationPath::ControlPoint(Vec3(),Quat(), scaleBwd)); } AnimationPathCallback *animCallbackFwd = new AnimationPathCallback(animationPathScaleFwd, 0.0, 1.f / ANIM_VIRTUAL_SPHERE_LAPSE_TIME); AnimationPathCallback *animCallbackBwd = new AnimationPathCallback(animationPathScaleBwd, 0.0, 1.f / ANIM_VIRTUAL_SPHERE_LAPSE_TIME); (*xformScaleFwd)->setUpdateCallback(animCallbackFwd); (*xformScaleBwd)->setUpdateCallback(animCallbackBwd); }
osg::Node* Shape::createOSGAxes(const base::Dimension3& dim) { const Real s = 1.5; Real d = Math::minimum(0.06,Math::minimum(dim.x,dim.y,dim.z)/16.0); Group* g = NewObj Group; g->setName("debug"); // color the axes X:red, Y:green and Z:blue, with white end cones StateSet* red = NewObj StateSet(); osg::Material* rmat = NewObj osg::Material(); Vec4 cred(1,0,0,1); // mat->setEmission( osg::Material::FRONT_AND_BACK, Vec4(0,0,0,0) ); //mat->setAmbient( osg::Material::FRONT_AND_BACK, col ); rmat->setDiffuse( osg::Material::FRONT_AND_BACK, cred ); rmat->setSpecular( osg::Material::FRONT_AND_BACK, Vec4(1,1,1,0) ); rmat->setShininess( osg::Material::FRONT_AND_BACK, 1.0); red->setAttribute( rmat ); StateSet* green = NewObj StateSet(); osg::Material* gmat = NewObj osg::Material(); Vec4 cgreen(0,1,0,1); // mat->setEmission( osg::Material::FRONT_AND_BACK, Vec4(0,0,0,0) ); //mat->setAmbient( osg::Material::FRONT_AND_BACK, col ); gmat->setDiffuse( osg::Material::FRONT_AND_BACK, cgreen ); gmat->setSpecular( osg::Material::FRONT_AND_BACK, Vec4(1,1,1,0) ); gmat->setShininess( osg::Material::FRONT_AND_BACK, 1.0); green->setAttribute( gmat ); StateSet* blue = NewObj StateSet(); osg::Material* bmat = NewObj osg::Material(); Vec4 cblue(0,0,1,1); // mat->setEmission( osg::Material::FRONT_AND_BACK, Vec4(0,0,0,0) ); //mat->setAmbient( osg::Material::FRONT_AND_BACK, col ); bmat->setDiffuse( osg::Material::FRONT_AND_BACK, cblue ); bmat->setSpecular( osg::Material::FRONT_AND_BACK, Vec4(1,1,1,0) ); bmat->setShininess( osg::Material::FRONT_AND_BACK, 1.0); blue->setAttribute( bmat ); StateSet* white = NewObj StateSet(); osg::Material* wmat = NewObj osg::Material(); Vec4 cwhite(1,1,1,1); // mat->setEmission( osg::Material::FRONT_AND_BACK, Vec4(0,0,0,0) ); //mat->setAmbient( osg::Material::FRONT_AND_BACK, col ); wmat->setDiffuse( osg::Material::FRONT_AND_BACK, cwhite ); wmat->setSpecular( osg::Material::FRONT_AND_BACK, Vec4(1,1,1,0) ); wmat->setShininess( osg::Material::FRONT_AND_BACK, 1.0); white->setAttribute( wmat ); // a long Clyinder for the axis and a cone-like cylinder // for the arrow head of each X,Y and Z. MatrixTransform* xrot = NewObj MatrixTransform(); xrot->setMatrix(osg::Matrix::rotate(consts::Pi/2.0,Vec3(0,1,0))); xrot->postMult(osg::Matrix::translate(s*dim.x/4.0,0,0)); g->addChild(xrot); MatrixTransform* yrot = NewObj MatrixTransform(); yrot->setMatrix(osg::Matrix::rotate(consts::Pi/2.0,Vec3(-1,0,0))); yrot->postMult(osg::Matrix::translate(0,s*dim.y/4.0,0)); g->addChild(yrot); MatrixTransform* zrot = NewObj MatrixTransform(); zrot->setMatrix(osg::Matrix::translate(0,0,s*dim.z/4.0)); g->addChild(zrot); // the cylinder axes ref<Cylinder> xc(NewObj Cylinder(s*dim.x/2.0,d)); xrot->addChild(xc->createOSGVisual()); xrot->setStateSet(red); ref<Cylinder> yc(NewObj Cylinder(s*dim.y/2.0,d)); yrot->addChild(yc->createOSGVisual()); yrot->setStateSet(green); ref<Cylinder> zc(NewObj Cylinder(s*dim.z/2.0,d)); zrot->addChild(zc->createOSGVisual()); zrot->setStateSet(blue); // Translate each axis cone to the end MatrixTransform* xtrans = NewObj MatrixTransform(); xtrans->setMatrix(osg::Matrix::translate(0,0,s*dim.x/4.0+d)); xrot->addChild(xtrans); MatrixTransform* ytrans = NewObj MatrixTransform(); ytrans->setMatrix(osg::Matrix::translate(0,0,s*dim.y/4.0+d)); yrot->addChild(ytrans); MatrixTransform* ztrans = NewObj MatrixTransform(); ztrans->setMatrix(osg::Matrix::translate(0,0,s*dim.z/4.0+d)); zrot->addChild(ztrans); // the end cones ref<Cone> cone(NewObj Cone(4*d,2*d)); osg::Node* coneNode = cone->createOSGVisual(); coneNode->setStateSet(white); xtrans->addChild(coneNode); ytrans->addChild(coneNode); ztrans->addChild(coneNode); return g; }
void Window::displayCallback() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // clear color and depth buffers glDisable(GL_LIGHTING); glMatrixMode(GL_MODELVIEW); // make sure we're in Modelview mode Globals::camera.setPlane(); glLoadMatrixd(Globals::camera.c.getPointer()); if (frustum == 1) { for (int i = -numRobots; i < numRobots; i++) { for (int j = -numRobots; j < numRobots; j++) { renderInt = 0; Vector3 x(i * 20, 0, j * 20); Matrix4 translate; translate.makeTranslate(i * 20, 0, j * 20); MatrixTransform transMatrix(translate); MatrixTransform bound; bound.addChild(&wireSphere()); transMatrix.addChild(&bound); //if (Globals::camera.sphereInFrustum(x,10) != Globals::camera.OUTSIDE) transMatrix.draw(Globals::camera.c); if (renderInt == 1) { Robot myBot(rotateAngle, i * 20, j * 20, boundingSphere, frustum); //if (Globals::camera.sphereInFrustum(x, 10) != Globals::camera.OUTSIDE) //{ myBot.draw(Globals::camera.c); //} if (fuckyou) rotateAngle += .05; else rotateAngle += -.05; if (rotateAngle > 75) fuckyou = 0; if (rotateAngle < -75) fuckyou = 1; } } } } else { for (int i = -numRobots; i < numRobots; i++) { for (int j = -numRobots; j < numRobots; j++) { Vector3 x(i * 20, 0, j * 20); Matrix4 translate; translate.makeTranslate(i * 20, 0, j * 20); MatrixTransform transMatrix(translate); MatrixTransform bound; bound.addChild(&wireSphere()); transMatrix.addChild(&bound); transMatrix.draw(Globals::camera.c); Robot myBot(rotateAngle, i * 20, j * 20, boundingSphere, frustum); //if (Globals::camera.sphereInFrustum(x, 10) != Globals::camera.OUTSIDE) //{ myBot.draw(Globals::camera.c); //} if (fuckyou) rotateAngle += .05; else rotateAngle += -.05; if (rotateAngle > 75) fuckyou = 0; if (rotateAngle < -75) fuckyou = 1; } } } float time = glutGet(GLUT_ELAPSED_TIME); frame++; if (time - noculltimer > 1000) { float fps = frame*1000.0 / (time - noculltimer); noculltimer = time; frame = 0; if (frustum == 0) cout << "no culling FPS: " << fps << endl; else cout << "culling FPS: " << fps << endl; } //End of Lab 4 //What i'm adding /* Not needed for Lab 4 Matrix4 dragTranslate; dragTranslate.makeTranslate(-dragonMidX, -dragonMidY, -dragonMidZ); Matrix4 dragScale; dragScale.makeScale(dragonScale, dragonScale, dragonScale); Matrix4 bunnyTranslate; bunnyTranslate.makeTranslate(-bunnyMidX, -bunnyMidY, -bunnyMidZ); Matrix4 bunnyScaling; bunnyScaling.makeScale(bunnyScale, bunnyScale, bunnyScale); //End of what I added if (mode == 1) { // Tell OpenGL what ModelView matrix to use: Matrix4 glmatrix; glmatrix = Globals::cube.getMatrix(); glmatrix.transpose(); glLoadMatrixd(glmatrix.getPointer()); // Draw all six faces of the cube: glBegin(GL_QUADS); glColor3f(0.0, 1.0, 0.0); // This makes the cube green; the parameters are for red, green and blue. // To change the color of the other faces you will need to repeat this call before each face is drawn. // Draw front face: glNormal3f(0.0, 0.0, 1.0); glVertex3f(-5.0, 5.0, 5.0); glVertex3f(5.0, 5.0, 5.0); glVertex3f(5.0, -5.0, 5.0); glVertex3f(-5.0, -5.0, 5.0); // Draw left side: glNormal3f(-1.0, 0.0, 0.0); glVertex3f(-5.0, 5.0, 5.0); glVertex3f(-5.0, 5.0, -5.0); glVertex3f(-5.0, -5.0, -5.0); glVertex3f(-5.0, -5.0, 5.0); // Draw right side: glNormal3f(1.0, 0.0, 0.0); glVertex3f(5.0, 5.0, 5.0); glVertex3f(5.0, 5.0, -5.0); glVertex3f(5.0, -5.0, -5.0); glVertex3f(5.0, -5.0, 5.0); // Draw back face: glNormal3f(0.0, 0.0, -1.0); glVertex3f(-5.0, 5.0, -5.0); glVertex3f(5.0, 5.0, -5.0); glVertex3f(5.0, -5.0, -5.0); glVertex3f(-5.0, -5.0, -5.0); // Draw top side: glNormal3f(0.0, 1.0, 0.0); glVertex3f(-5.0, 5.0, 5.0); glVertex3f(5.0, 5.0, 5.0); glVertex3f(5.0, 5.0, -5.0); glVertex3f(-5.0, 5.0, -5.0); // Draw bottom side: glNormal3f(0.0, -1.0, 0.0); glVertex3f(-5.0, -5.0, -5.0); glVertex3f(5.0, -5.0, -5.0); glVertex3f(5.0, -5.0, 5.0); glVertex3f(-5.0, -5.0, 5.0); glEnd(); } //What I'm Adding for Lab 2 else if (mode == 2) { Matrix4 glmatrix; glmatrix = Globals::camera.getGLMatrix1(); glmatrix.transpose(); glLoadMatrixd(glmatrix.getPointer()); glBegin(GL_TRIANGLES); int index; float color; float vertex; float vertex2; float vertex3; for (int i = 0; i <= 59; i++) { index = indices[i]; color = colors[index * 3]; float color2 = colors[(index * 3) + 1]; float color3 = colors[(index * 3) + 2]; glColor3f(color, color2, color3); vertex = vertices[index * 3]; vertex2 = vertices[(index * 3) + 1]; vertex3 = vertices[(index * 3) + 2]; glVertex3f(vertex, vertex2, vertex3); } glEnd(); } else if (mode == 3) { Matrix4 glmatrix; glmatrix = Globals::camera.getGLMatrix2(); glmatrix.transpose(); glLoadMatrixd(glmatrix.getPointer()); glBegin(GL_TRIANGLES); int index; float color; float vertex; float vertex2; float vertex3; for (int i = 0; i <= 59; i++) { index = indices[i]; color = colors[index * 3]; float color2 = colors[(index * 3) + 1]; float color3 = colors[(index * 3) + 2]; glColor3f(color, color2, color3); vertex = vertices[index * 3]; vertex2 = vertices[(index * 3) + 1]; vertex3 = vertices[(index * 3) + 2]; glVertex3f(vertex, vertex2, vertex3); } glEnd(); } if (mode == 4) { Matrix4 glmatrix; glmatrix = Globals::cube.getMatrix(); glmatrix.transpose(); glLoadMatrixd(glmatrix.getPointer()); glBegin(GL_POINTS); glPointSize(5.0); for (int i = 0; i < 36000; i++) { Vector4 bunny = Vector4(bunnyCoor[i][0], bunnyCoor[i][1], bunnyCoor[i][2], 1); Vector3 normBunny = Vector3(bunnyCoor[i][3], bunnyCoor[i][4], bunnyCoor[i][5]); bunny = bunnyTranslate*bunny; bunny = bunnyScaling*bunny; glNormal3d(normBunny.x, normBunny.y, normBunny.z); glVertex3d(bunny.x, bunny.y, bunny.z); } glEnd(); } if (mode == 5) { Matrix4 glmatrix; glmatrix = Globals::cube.getMatrix(); glmatrix.transpose(); glLoadMatrixd(glmatrix.getPointer()); glBegin(GL_POINTS); glPointSize(5.0); for (int i = 0; i < 110000; i++) { Vector4 dragon = Vector4(dragonCoor[i][0], dragonCoor[i][1], dragonCoor[i][2], 1); Vector3 normDragon = Vector3(dragonCoor[i][3], dragonCoor[i][4], dragonCoor[i][5]); dragon = dragTranslate*dragon; dragon = dragScale*dragon; glNormal3d(normDragon.x, normDragon.y, normDragon.z); glVertex3d(dragon.x, dragon.y, dragon.z); } glEnd(); } //End of What I added for Lab 2 */ glFlush(); glutSwapBuffers(); //End of what I addd for lab 3 }
MatrixTransform* LevelManager::LoadTrees(int amount) { MatrixTransform *forestMT = new MatrixTransform(); LTree TreeGenerator = LTree(0, LSysParam()); treeGrammars = new LSysParam[3]; std::string fernTree = "Tree::Fern Grammar"; std::string niceTree = "Tree::Nice Grammar"; std::string bushTree = "Tree::Bush Grammar"; std::string stalkyTree = "Tree::StalkyTree"; treeGrammars[0].iterations = 5; treeGrammars[0].length = 0.12f; treeGrammars[0].rules['X'] = "F[+X]F[-X]+X"; treeGrammars[0].rules['F'] = "FF"; treeGrammars[0].startRule = 'X'; LSysParam bushTreeProp; treeGrammars[1].iterations = 3; treeGrammars[1].length = 0.5f; treeGrammars[1].radius = 0.009f; treeGrammars[1].rules['F'] = "FF-[-F+F+F]+[+F-F-F]"; treeGrammars[1].startRule = 'F'; treeGrammars[2].iterations = 4; treeGrammars[2].length = 0.3f; treeGrammars[2].radius = 0.08f; treeGrammars[2].rules['F'] = "F[+F]F[-F]F";; treeGrammars[2].startRule = 'F'; // Default LSysParam is a fern grammar EntityNode *fernTreeNode = TreeGenerator.generate(); fernTreeNode->name = fernTree; refES->insert(fernTreeNode); TreeGenerator.setProperties(treeGrammars[0]); EntityNode *niceTreeNode = TreeGenerator.generate(); niceTreeNode->name = niceTree; refES->insert(niceTreeNode); TreeGenerator.setProperties(treeGrammars[1]); EntityNode *bushTreeNode = TreeGenerator.generate(); bushTreeNode->name = bushTree; refES->insert(bushTreeNode); TreeGenerator.setProperties(treeGrammars[2]); EntityNode *stalkyTreeNode = TreeGenerator.generate(); stalkyTreeNode->name = stalkyTree; refES->insert(stalkyTreeNode); int type = 0; MatrixTransform **treeMT = new MatrixTransform*[amount]; Tree **tree = new Tree*[amount]; for(int i = 0; i < amount; ++i) { treeMT[i] = new MatrixTransform(); if( i % 4 == 0 ){ tree[i] = new Tree(fernTree, 0, 0); }else if(i % 4 == 1){ tree[i] = new Tree(niceTree, 0, 0); }else if(i % 4 == 2){ tree[i] = new Tree(bushTree, 0, 0); }else{ tree[i] = new Tree(stalkyTree, 0, 0); } int rx = 0, height = 0, rz = 0; if( refSG->terrain != nullptr ) { rx = (rand() % refSG->terrain->terrainGridWidth) - refSG->terrain->terrainGridWidth/2; rz = (rand() % refSG->terrain->terrainGridLength) - refSG->terrain->terrainGridLength/2; height = refSG->terrain->terrainGetHeight(rx,rz); } treeMT[i]->setMatrix( mat4().translate( vec3(rx,(float)(height) - 0.3f, rz)) ); treeMT[i]->setMatrix( mat4().translate( vec3(rx,(float)(height) - 0.3f, rz)) ); treeMT[i]->addChild(tree[i]); forestMT->addChild(treeMT[i]); } return forestMT; }
/*************************************************************** * Function: ANIMLoadGeometryCreator() * * xformScaleFwd: Root transform node for inflating geometries * xformScaleBwd: Root transform node for shrinking geometries * sphereExteriorSwitch: Switch control for single exterior sphere * ***************************************************************/ void ANIMLoadGeometryCreator(PositionAttitudeTransform** xformScaleFwd, PositionAttitudeTransform** xformScaleBwd, Switch **sphereExteriorSwitch, Geode **sphereExteriorGeode, int &numTypes, ANIMShapeSwitchEntry ***shapeSwitchEntryArray) { *xformScaleFwd = new PositionAttitudeTransform; *xformScaleBwd = new PositionAttitudeTransform; MatrixTransform *geomCreatorTrans = new MatrixTransform; MatrixTransform *sphereExteriorTrans = new MatrixTransform; *sphereExteriorSwitch = new Switch; Switch *createBoxSwitch = new Switch; Switch *createCylinderSwitch = new Switch; (*xformScaleFwd)->addChild(geomCreatorTrans); (*xformScaleBwd)->addChild(geomCreatorTrans); geomCreatorTrans->addChild(*sphereExteriorSwitch); geomCreatorTrans->addChild(createBoxSwitch); geomCreatorTrans->addChild(createCylinderSwitch); osg::Vec3 pos(-1, 0, 0); // create drawables, geodes and attach them to animation switches *sphereExteriorGeode = new Geode(); Sphere *sphere = new Sphere(osg::Vec3(), ANIM_VIRTUAL_SPHERE_RADIUS); ShapeDrawable *sphereDrawable = new ShapeDrawable(sphere); (*sphereExteriorGeode)->addDrawable(sphereDrawable); Box *box = new Box(osg::Vec3(0.1, 0, 0), ANIM_VIRTUAL_SPHERE_RADIUS / 1.9); (*sphereExteriorGeode)->addDrawable(new ShapeDrawable(box)); float r = ANIM_VIRTUAL_SPHERE_RADIUS / 3.0; Cylinder *cylinder = new Cylinder(osg::Vec3(-0.05, 0, -0.05), r, r * 2); (*sphereExteriorGeode)->addDrawable(new ShapeDrawable(cylinder)); Cone *cone = new osg::Cone(osg::Vec3(0, -0.1, 0.05), r, r * 2); (*sphereExteriorGeode)->addDrawable(new ShapeDrawable(cone)); Material *transmaterial = new Material; transmaterial->setDiffuse(Material::FRONT_AND_BACK, Vec4(1, 1, 1, 1)); transmaterial->setAlpha(Material::FRONT_AND_BACK, 0.6f); Image* envMap = osgDB::readImageFile(ANIMDataDir() + "Textures/ShapeContainer.JPG"); Texture2D* envTex = new Texture2D(envMap); StateSet *sphereStateSet = (sphereDrawable)->getOrCreateStateSet(); sphereStateSet->setMode(GL_BLEND, StateAttribute::OVERRIDE | StateAttribute::ON ); sphereStateSet->setRenderingHint(StateSet::TRANSPARENT_BIN); sphereStateSet->setAttributeAndModes(transmaterial, StateAttribute::OVERRIDE | StateAttribute::ON); sphereStateSet->setTextureAttributeAndModes(0, envTex, StateAttribute::ON); sphereStateSet->setMode(GL_CULL_FACE, StateAttribute::ON); sphereExteriorTrans->addChild(*sphereExteriorGeode); (*sphereExteriorSwitch)->addChild(sphereExteriorTrans); (*sphereExteriorSwitch)->setAllChildrenOn(); // write into shape switch entry array record numTypes = 2; *shapeSwitchEntryArray = new ANIMShapeSwitchEntry*[numTypes]; (*shapeSwitchEntryArray)[0] = new ANIMShapeSwitchEntry; (*shapeSwitchEntryArray)[1] = new ANIMShapeSwitchEntry; (*shapeSwitchEntryArray)[0]->mSwitch = createBoxSwitch; (*shapeSwitchEntryArray)[1]->mSwitch = createCylinderSwitch; ANIMCreateSingleShapeSwitchAnimation(&((*shapeSwitchEntryArray)[0]), CAVEGeodeShape::BOX); ANIMCreateSingleShapeSwitchAnimation(&((*shapeSwitchEntryArray)[1]), CAVEGeodeShape::CYLINDER); /* set up the forward / backward scale animation paths for geometry creator */ AnimationPath* animationPathScaleFwd = new AnimationPath; AnimationPath* animationPathScaleBwd = new AnimationPath; animationPathScaleFwd->setLoopMode(AnimationPath::NO_LOOPING); animationPathScaleBwd->setLoopMode(AnimationPath::NO_LOOPING); Vec3 scaleFwd, scaleBwd; float step = 1.f / ANIM_VIRTUAL_SPHERE_NUM_SAMPS; for (int i = 0; i < ANIM_VIRTUAL_SPHERE_NUM_SAMPS + 1; i++) { float val = i * step; scaleFwd = Vec3(val, val, val); scaleBwd = Vec3(1.f-val, 1.f-val, 1.f-val); animationPathScaleFwd->insert(val, AnimationPath::ControlPoint(pos, Quat(), scaleFwd)); animationPathScaleBwd->insert(val, AnimationPath::ControlPoint(pos, Quat(), scaleBwd)); } AnimationPathCallback *animCallbackFwd = new AnimationPathCallback(animationPathScaleFwd, 0.0, 1.f / ANIM_VIRTUAL_SPHERE_LAPSE_TIME); AnimationPathCallback *animCallbackBwd = new AnimationPathCallback(animationPathScaleBwd, 0.0, 1.f / ANIM_VIRTUAL_SPHERE_LAPSE_TIME); (*xformScaleFwd)->setUpdateCallback(animCallbackFwd); (*xformScaleBwd)->setUpdateCallback(animCallbackBwd); }