int main(int argc, char* argv[]) { //----------------------------------------------------------------------- // INITIALIZATION //----------------------------------------------------------------------- printf ("\n"); printf ("-----------------------------------\n"); printf ("CHAI3D\n"); printf ("Demo: 04-shapes\n"); printf ("Copyright 2003-2012\n"); printf ("-----------------------------------\n"); printf ("\n\n"); printf ("Keyboard Options:\n\n"); printf ("[x] - Exit application\n"); printf ("\n\n>\r"); // parse first arg to try and locate resources resourceRoot = string(argv[0]).substr(0,string(argv[0]).find_last_of("/\\")+1); //----------------------------------------------------------------------- // WORLD - CAMERA - LIGHTING //----------------------------------------------------------------------- // create a new world. world = new cWorld(); // set the background color of the environment world->m_backgroundColor.setBlack(); // create a camera and insert it into the virtual world camera = new cCamera(world); world->addChild(camera); // position and oriente the camera camera->set( cVector3d (3.0, 0.0, 0.0), // camera position (eye) cVector3d (0.0, 0.0, 0.0), // lookat position (target) cVector3d (0.0, 0.0, 1.0)); // direction of the (up) vector /* camera->set( cVector3d (0.0, 0.0, -3.0), // camera position (eye) cVector3d (0.0, 0.0, 0.0), // lookat position (target) cVector3d (-1.0, 0.0, 0.0)); // direction of the (up) vector*/ // set the near and far clipping planes of the camera // anything in front/behind these clipping planes will not be rendered camera->setClippingPlanes(0.01, 10.0); // enable multi-pass rendering to handle transparent objects camera->setUseMultipassTransparency(true); // create a light source light = new cDirectionalLight(world); // add light to world world->addChild(light); // enable light source light->setEnabled(true); // define the direction of the light beam light->setDir(-1.0, 0.0, 0.0); //----------------------------------------------------------------------- // HAPTIC DEVICES / TOOLS //----------------------------------------------------------------------- // create a haptic device handler handler = new cHapticDeviceHandler(); // get access to the first available haptic device handler->getDevice(hapticDevice, 0); // retrieve information about the current haptic device cHapticDeviceInfo hapticDeviceInfo = hapticDevice->getSpecifications(); // create a 3D tool and add it to the world tool = new cToolCursor(world); world->addChild(tool); // connect the haptic device to the tool tool->setHapticDevice(hapticDevice); // initialize tool by connecting to haptic device tool->start(); // map the physical workspace of the haptic device to a larger virtual workspace. tool->setWorkspaceRadius(2.0); // define a radius for the tool tool->setRadius(0.03); // read the scale factor between the physical workspace of the haptic // device and the virtual workspace defined for the tool double workspaceScaleFactor = tool->getWorkspaceScaleFactor(); // my device open **********************MINE deltaCtrl.ConnectDevice(); //double R[3][3] = {0,0,-1,0,-1,0,-1,0,0}; //deltaCtrl.SetRotation(R); //----------------------------------------------------------------------- // CREATING OBJECTS //----------------------------------------------------------------------- // temp variable cGenericEffect* newEffect; // stiffness properties double maxStiffness = hapticDeviceInfo.m_maxLinearStiffness / workspaceScaleFactor; double maxLinearForce = hapticDeviceInfo.m_maxLinearForce; // pull up stiffness maxStiffness =300; // create a sphere 0 sphere0 = new cShapeSphere(0.1); world->addChild(sphere0); sphere0->setLocalPos(0.0,-0.7, 0.0); sphere0->m_material->setRedFireBrick(); newEffect = new cEffectSurface(sphere0); sphere0->addEffect(newEffect); sphere0->m_material->setStiffness(0.4 * maxStiffness); // create a sphere 1 sphere1 = new cShapeSphere(0.1); world->addChild(sphere1); sphere1->setLocalPos(0.0, 0.7, 0.0); sphere1->m_material->setRedFireBrick(); newEffect = new cEffectSurface(sphere1); sphere1->addEffect(newEffect); sphere1->m_material->setStiffness(0.4 * maxStiffness); // create a line line = new cShapeLine(sphere0->getLocalPos(), sphere1->getLocalPos()); world->addChild(line); line->m_material->setWhite(); newEffect = new cEffectMagnet(line); line->addEffect(newEffect); line->m_material->setMagnetMaxDistance(0.05); line->m_material->setMagnetMaxForce(0.1 * maxLinearForce); line->m_material->setStiffness(0.1 * maxStiffness); // create a cylinder cylinder = new cShapeCylinder(0.25, 0.25, 0.2); world->addChild(cylinder); cylinder->setLocalPos(0.0, 0.0, 0.0); cylinder->rotateAboutGlobalAxisDeg(cVector3d(1.0, 0.0, 0.0), 90); cylinder->m_material->setBlueCornflower(); newEffect = new cEffectSurface(cylinder); cylinder->addEffect(newEffect); cylinder->m_material->setStiffness(0.8 * maxStiffness); //----------------------------------------------------------------------- // WIDGETS //----------------------------------------------------------------------- // create a font cFont *font = NEW_CFONTCALIBRI20(); // create a label to display the haptic rate of the simulation labelHapticRate = new cLabel(font); camera->m_frontLayer->addChild(labelHapticRate); //----------------------------------------------------------------------- // OPEN GL - WINDOW DISPLAY //----------------------------------------------------------------------- // simulation in now running! simulationRunning = true; // initialize GLUT glutInit(&argc, argv); // retrieve the resolution of the computer display and estimate the position // of the GLUT window so that it is located at the center of the screen int screenW = glutGet(GLUT_SCREEN_WIDTH); int screenH = glutGet(GLUT_SCREEN_HEIGHT); int windowPosX = (screenW - WINDOW_SIZE_W) / 2; int windowPosY = (screenH - WINDOW_SIZE_H) / 2; // initialize the OpenGL GLUT window glutInitWindowPosition(windowPosX, windowPosY); glutInitWindowSize(WINDOW_SIZE_W, WINDOW_SIZE_H); if (USE_STEREO_DISPLAY) { glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE | GLUT_STEREO); camera->setUseStereo(true); } else { glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE); camera->setUseStereo(false); } glutCreateWindow(argv[0]); glutDisplayFunc(updateGraphics); glutKeyboardFunc(keySelect); glutReshapeFunc(resizeWindow); glutSetWindowTitle("CHAI3D"); // create a mouse menu (right button) glutCreateMenu(menuSelect); glutAddMenuEntry("full screen", OPTION_FULLSCREEN); glutAddMenuEntry("window display", OPTION_WINDOWDISPLAY); glutAttachMenu(GLUT_RIGHT_BUTTON); //----------------------------------------------------------------------- // START SIMULATION //----------------------------------------------------------------------- // create a thread which starts the main haptics rendering loop cThread* hapticsThread = new cThread(); hapticsThread->start(updateHaptics, CTHREAD_PRIORITY_HAPTICS); // start the main graphics rendering loop glutTimerFunc(30, graphicsTimer, 0); glutMainLoop(); // close everything close(); // exit return (0); }
int main(int argc, char* argv[]) { //----------------------------------------------------------------------- // INITIALIZATION //----------------------------------------------------------------------- printf ("\n"); printf ("-----------------------------------\n"); printf ("CHAI3D\n"); printf ("Demo: 10-oring\n"); printf ("Copyright 2003-2012\n"); printf ("-----------------------------------\n"); printf ("\n\n"); printf ("Keyboard Options:\n\n"); printf ("[1] - Texture (ON/OFF)\n"); printf ("[2] - Wireframe (ON/OFF)\n"); printf ("[x] - Exit application\n"); printf ("\n\n>\r"); // parse first arg to try and locate resources string resourceRoot = string(argv[0]).substr(0,string(argv[0]).find_last_of("/\\")+1); //----------------------------------------------------------------------- // WORLD - CAMERA - LIGHTING //----------------------------------------------------------------------- // create a new world. world = new cWorld(); // set the background color of the environment world->m_backgroundColor.setBlack(); // create a camera and insert it into the virtual world camera = new cCamera(world); world->addChild(camera); // position and oriente the camera camera->set( cVector3d (3.0, 0.0, 0.0), // camera position (eye) cVector3d (0.0, 0.0, 0.0), // lookat position (target) cVector3d (0.0, 0.0, 1.0)); // direction of the (up) vector // set the near and far clipping planes of the camera // anything in front/behind these clipping planes will not be rendered camera->setClippingPlanes(0.01, 10.0); // enable shadow casting camera->setUseShadowCasting(true); // create a light source light = new cSpotLight(world); // attach light to camera camera->addChild(light); // enable light source light->setEnabled(true); // position the light source light->setLocalPos( 0.0, 0.5, 0.0); // define the direction of the light beam light->setDir(-3.0,-0.5, 0.0); // enable this light source to generate shadows light->setShadowMapEnabled(true); //----------------------------------------------------------------------- // HAPTIC DEVICES / TOOLS //----------------------------------------------------------------------- // create a haptic device handler handler = new cHapticDeviceHandler(); // get access to the first available haptic device handler->getDevice(hapticDevice, 0); // retrieve information about the current haptic device cHapticDeviceInfo info = hapticDevice->getSpecifications(); // create a 3D tool and add it to the world tool = new cToolCursor(world); world->addChild(tool); // connect the haptic device to the tool tool->setHapticDevice(hapticDevice); // initialize tool by connecting to haptic device tool->start(); // map the physical workspace of the haptic device to a larger virtual workspace. tool->setWorkspaceRadius(1.0); // define a radius for the tool tool->setRadius(0.03); // read the scale factor between the physical workspace of the haptic // device and the virtual workspace defined for the tool double workspaceScaleFactor = tool->getWorkspaceScaleFactor(); // define a maximum stiffness that can be handled by the current // haptic device. The value is scaled to take into account the // workspace scale factor double stiffnessMax = info.m_maxLinearStiffness / workspaceScaleFactor; //----------------------------------------------------------------------- // CREATING OBJECTS //----------------------------------------------------------------------- // create a torus object (o-ring) object = new cShapeTorus(0.24, 0.50); // add object to world world->addChild(object); // set the position of the object at the center of the world object->setLocalPos(0.0, 0.0, 0.0); // rotate object of 90 degrees around its y-axis so that it // stands vertically object->rotateAboutGlobalAxisDeg( cVector3d(0, 1, 0), 90); // set the material stiffness to 100% of the maximum permitted stiffness // of the haptic device object->m_material->setStiffness(1.00 * stiffnessMax); // set some environmental texture and material properties object->m_texture = new cTexture2d(); // load texture file bool fileload = object->m_texture->loadFromFile(RESOURCE_PATH("resources/images/spheremap-6.jpg")); if (!fileload) { //fileload = object->m_texture->loadFromFile("../../../bin/resources/images/spheremap-6.jpg"); fileload = object->m_texture->loadFromFile("resources/images/spheremap-6.jpg"); } if (!fileload) { printf("Error - Texture image failed to load correctly.\n"); close(); return (-1); } // setup texture and material properties object->setUseTexture(true); object->m_texture->setSphericalMappingEnabled(true); object->m_material->m_ambient.set(0.9, 0.9, 0.9); object->m_material->m_diffuse.set(0.9, 0.9, 0.9); object->m_material->m_specular.set(1.0, 1.0, 1.0); // create a haptic effect for this object so that the operator can // feel its surface cEffectSurface* newEffect = new cEffectSurface(object); object->addEffect(newEffect); //----------------------------------------------------------------------- // WIDGETS //----------------------------------------------------------------------- // create a font cFont *font = NEW_CFONTCALIBRI20(); // create a label to display the haptic rate of the simulation labelHapticRate = new cLabel(font); camera->m_frontLayer->addChild(labelHapticRate); level = new cLevel(); camera->m_frontLayer->addChild(level); level->setLocalPos(100,100,0); level->setRange(0.0, 25.0); dial = new cDial(); camera->m_frontLayer->addChild(dial); dial->setSingleIncrementDisplay(false); dial->setLocalPos(600,100,0); dial->setRange(0.0, 5.0); dial->setSize(60); dial->setValue0(0.0); //----------------------------------------------------------------------- // OPEN GL - WINDOW DISPLAY //----------------------------------------------------------------------- // simulation in now running! simulationRunning = true; // initialize GLUT glutInit(&argc, argv); // retrieve the resolution of the computer display and estimate the position // of the GLUT window so that it is located at the center of the screen int screenW = glutGet(GLUT_SCREEN_WIDTH); int screenH = glutGet(GLUT_SCREEN_HEIGHT); int windowPosX = (screenW - WINDOW_SIZE_W) / 2; int windowPosY = (screenH - WINDOW_SIZE_H) / 2; // initialize the OpenGL GLUT window glutInitWindowPosition(windowPosX, windowPosY); glutInitWindowSize(WINDOW_SIZE_W, WINDOW_SIZE_H); if (USE_STEREO_DISPLAY) { glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE | GLUT_STEREO); camera->setUseStereo(true); } else { glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE); camera->setUseStereo(false); } glutCreateWindow(argv[0]); glutDisplayFunc(updateGraphics); glutKeyboardFunc(keySelect); glutReshapeFunc(resizeWindow); glutSetWindowTitle("CHAI3D"); // create a mouse menu (right button) glutCreateMenu(menuSelect); glutAddMenuEntry("full screen", OPTION_FULLSCREEN); glutAddMenuEntry("window display", OPTION_WINDOWDISPLAY); glutAttachMenu(GLUT_RIGHT_BUTTON); //----------------------------------------------------------------------- // START SIMULATION //----------------------------------------------------------------------- // create a thread which starts the main haptics rendering loop cThread* hapticsThread = new cThread(); hapticsThread->start(updateHaptics, CTHREAD_PRIORITY_HAPTICS); // start the main graphics rendering loop glutTimerFunc(30, graphicsTimer, 0); glutMainLoop(); // close everything close(); // exit return (0); }
int main(int argc, char* argv[]) { //----------------------------------------------------------------------- // INITIALIZATION //----------------------------------------------------------------------- printf ("\n"); printf ("-----------------------------------\n"); printf ("CHAI3D\n"); printf ("Demo: 13-primitives\n"); printf ("Copyright 2003-2012\n"); printf ("-----------------------------------\n"); printf ("\n\n"); printf ("Keyboard Options:\n\n"); printf ("[1] - Wireframe (ON/OFF)\n"); printf ("[x] - Exit application\n"); printf ("\n\n>\r"); //----------------------------------------------------------------------- // WORLD - CAMERA - LIGHTING //----------------------------------------------------------------------- // create a new world. world = new cWorld(); // set the background color of the environment world->m_backgroundColor.setBlack(); // create a camera and insert it into the virtual world camera = new cCamera(world); world->addChild(camera); // position and oriente the camera camera->set( cVector3d (1.4, 0.0, 0.6), // camera position (eye) cVector3d (0.0, 0.0, 0.0), // lookat position (target) cVector3d (0.0, 0.0, 1.0)); // direction of the (up) vector // set the near and far clipping planes of the camera // anything in front/behind these clipping planes will not be rendered camera->setClippingPlanes(0.01, 10.0); // Enable shadow casting camera->setUseShadowCasting(true); // create a light source light = new cSpotLight(world); // attach light to camera camera->addChild(light); // enable light source light->setEnabled(true); // position the light source light->setLocalPos( 0.0, 0.5, 0.0); // define the direction of the light beam light->setDir(-3.0,-0.5, 0.0); // enable this light source to generate shadows light->setShadowMapEnabled(true); //----------------------------------------------------------------------- // HAPTIC DEVICES / TOOLS //----------------------------------------------------------------------- // create a haptic device handler handler = new cHapticDeviceHandler(); // get access to the first available haptic device handler->getDevice(hapticDevice, 0); // retrieve information about the current haptic device cHapticDeviceInfo info = hapticDevice->getSpecifications(); // if the haptic devices carries a gripper, enable it to behave like a user switch hapticDevice->setEnableGripperUserSwitch(true); // create a 3D tool and add it to the world tool = new cToolCursor(world); world->addChild(tool); // connect the haptic device to the tool tool->setHapticDevice(hapticDevice); // initialize tool by connecting to haptic device tool->start(); // map the physical workspace of the haptic device to a larger virtual workspace. tool->setWorkspaceRadius(1.0); // define the radius of the tool (sphere) double toolRadius = 0.05; // define a radius for the tool tool->setRadius(toolRadius); // hide the device sphere. only show proxy. tool->setShowContactPoints(true, false); // enable if objects in the scene are going to rotate of translate // or possibly collide against the tool. If the environment // is entirely static, you can set this parameter to "false" tool->enableDynamicObjects(true); // read the scale factor between the physical workspace of the haptic // device and the virtual workspace defined for the tool double workspaceScaleFactor = tool->getWorkspaceScaleFactor(); // define a maximum stiffness that can be handled by the current // haptic device. The value is scaled to take into account the // workspace scale factor double stiffnessMax = info.m_maxLinearStiffness / workspaceScaleFactor; //----------------------------------------------------------------------- // CREATING OBJECTS //----------------------------------------------------------------------- ///////////////////////////////////////////////////////////////////////// // BASE ///////////////////////////////////////////////////////////////////////// // create a virtual mesh cMesh* base = new cMesh(); // add object to world world->addChild(base); // build mesh using a cylinder primitive cCreateCylinder(base, 0.01, 0.5, 64, 1, true, true, cVector3d(0.0, 0.0, 0.0), cMatrix3d(cDegToRad(0), cDegToRad(0), cDegToRad(0), C_EULER_ORDER_XYZ) ); // set material color base->m_material->setGrayGainsboro(); base->m_material->setStiffness(0.5 * stiffnessMax); // build collision detection tree base->createAABBCollisionDetector(toolRadius); ///////////////////////////////////////////////////////////////////////// // TEA POT ///////////////////////////////////////////////////////////////////////// // create a virtual mesh cMesh* teaPot = new cMesh(); // add object to world base->addChild(teaPot); // build mesh using a cylinder primitive cCreateTeaPot(teaPot, 0.5, cVector3d(0.0, 0.2, 0.12), cMatrix3d(cDegToRad(0), cDegToRad(0), cDegToRad(170), C_EULER_ORDER_XYZ) ); // set material properties teaPot->m_material->setRedDark(); teaPot->m_material->setStiffness(0.5 * stiffnessMax); // build collision detection tree teaPot->createAABBCollisionDetector(toolRadius); ///////////////////////////////////////////////////////////////////////// // GLASS ///////////////////////////////////////////////////////////////////////// // create a virtual mesh cMesh* glass = new cMesh(); // add object to world base->addChild(glass); // build mesh using a cylinder primitive cCreatePipe(glass, 0.15, 0.05, 0.06, 32, 1, cVector3d(0.0,-0.2, 0.0), cMatrix3d(cDegToRad(0), cDegToRad(0), cDegToRad(170), C_EULER_ORDER_XYZ) ); // set material color glass->m_material->setBlueCornflower(); glass->m_material->setStiffness(0.5 * stiffnessMax); // build collision detection tree glass->createAABBCollisionDetector(toolRadius); ///////////////////////////////////////////////////////////////////////// // CONE ///////////////////////////////////////////////////////////////////////// // create a virtual mesh cMesh* cone = new cMesh(); // add object to world base->addChild(cone); // build mesh using a cylinder primitive cCreateCone(cone, 0.15, 0.05, 0.01, 32, 1, false, true, cVector3d(0.25, 0.0, 0.0), cMatrix3d(cDegToRad(0), cDegToRad(0), cDegToRad(0), C_EULER_ORDER_XYZ) ); // set material color cone->m_material->setGreenForest(); cone->m_material->setStiffness(0.5 * stiffnessMax); // build collision detection tree cone->createAABBCollisionDetector(toolRadius); //----------------------------------------------------------------------- // WIDGETS //----------------------------------------------------------------------- // create a font cFont *font = NEW_CFONTCALIBRI20(); // create a label to display the haptic rate of the simulation labelHapticRate = new cLabel(font); labelHapticRate->m_fontColor.setBlack(); camera->m_frontLayer->addChild(labelHapticRate); // create a background cBackground* background = new cBackground(); camera->m_backLayer->addChild(background); // set background properties background->setCornerColors(cColorf(1.0, 1.0, 1.0), cColorf(1.0, 1.0, 1.0), cColorf(0.9, 0.9, 0.9), cColorf(0.9, 0.9, 0.9)); //----------------------------------------------------------------------- // OPEN GL - WINDOW DISPLAY //----------------------------------------------------------------------- // simulation in now running! simulationRunning = true; // initialize GLUT glutInit(&argc, argv); // retrieve the resolution of the computer display and estimate the position // of the GLUT window so that it is located at the center of the screen int screenW = glutGet(GLUT_SCREEN_WIDTH); int screenH = glutGet(GLUT_SCREEN_HEIGHT); int windowW = 0.7 * screenH; int windowH = 0.7 * screenH; int windowPosX = (screenW - windowH) / 2; int windowPosY = (screenH - windowW) / 2; // initialize the OpenGL GLUT window glutInitWindowPosition(windowPosX, windowPosY); glutInitWindowSize(windowW, windowH); if (USE_STEREO_DISPLAY) { glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE | GLUT_STEREO); camera->setUseStereo(true); } else { glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE); camera->setUseStereo(false); } glutCreateWindow(argv[0]); glutDisplayFunc(updateGraphics); glutKeyboardFunc(keySelect); glutReshapeFunc(resizeWindow); glutSetWindowTitle("CHAI3D"); // create a mouse menu (right button) glutCreateMenu(menuSelect); glutAddMenuEntry("full screen", OPTION_FULLSCREEN); glutAddMenuEntry("window display", OPTION_WINDOWDISPLAY); glutAttachMenu(GLUT_RIGHT_BUTTON); //----------------------------------------------------------------------- // START SIMULATION //----------------------------------------------------------------------- // create a thread which starts the main haptics rendering loop cThread* hapticsThread = new cThread(); hapticsThread->start(updateHaptics, CTHREAD_PRIORITY_HAPTICS); // start the main graphics rendering loop glutTimerFunc(30, graphicsTimer, 0); glutMainLoop(); // close everything close(); // exit return (0); }
int main(int argc, char* argv[]) { //----------------------------------------------------------------------- // INITIALIZATION //----------------------------------------------------------------------- printf ("\n"); printf ("-----------------------------------\n"); printf ("CHAI3D\n"); printf ("Demo: 22-chrome\n"); printf ("Copyright 2003-2012\n"); printf ("-----------------------------------\n"); printf ("\n\n"); printf ("Keyboard Options:\n\n"); printf ("[1] - Texture ON\n"); printf ("[2] - Texture OFF\n"); printf ("[3] - Wireframe ON\n"); printf ("[4] - Wireframe OFF\n"); printf ("[5] - Normals ON\n"); printf ("[6] - Normals OFF\n"); printf ("[x] - Exit application\n"); printf ("\n\n>\r"); // parse first arg to try and locate resources resourceRoot = string(argv[0]).substr(0,string(argv[0]).find_last_of("/\\")+1); //----------------------------------------------------------------------- // WORLD - CAMERA - LIGHTING //----------------------------------------------------------------------- // create a new world. world = new cWorld(); // set the background color of the environment world->m_backgroundColor.setBlack(); // create a camera and insert it into the virtual world camera = new cCamera(world); world->addChild(camera); // position and oriente the camera camera->set( cVector3d (3.0, 0.0, 0.6), // camera position (eye) cVector3d (0.0, 0.0, 0.0), // lookat position (target) cVector3d (0.0, 0.0, 1.0)); // direction of the (up) vector // set the near and far clipping planes of the camera // anything in front/behind these clipping planes will not be rendered camera->setClippingPlanes(0.01, 10.0); // Enable shadow casting camera->setUseShadowCasting(true); // create a light source light = new cSpotLight(world); // attach light to camera camera->addChild(light); // enable light source light->setEnabled(true); // position the light source light->setLocalPos( 0.0, 0.5, 0.0); // define the direction of the light beam light->setDir(-3.0,-0.5, 0.0); // enable this light source to generate shadows light->setShadowMapEnabled(true); //----------------------------------------------------------------------- // HAPTIC DEVICES / TOOLS //----------------------------------------------------------------------- // create a haptic device handler handler = new cHapticDeviceHandler(); // get access to the first available haptic device handler->getDevice(hapticDevice, 0); // retrieve information about the current haptic device cHapticDeviceInfo info = hapticDevice->getSpecifications(); // if the haptic devices carries a gripper, enable it to behave like a user switch hapticDevice->setEnableGripperUserSwitch(true); // create a 3D tool and add it to the world tool = new cToolCursor(world); world->addChild(tool); // connect the haptic device to the tool tool->setHapticDevice(hapticDevice); // initialize tool by connecting to haptic device tool->start(); // map the physical workspace of the haptic device to a larger virtual workspace. tool->setWorkspaceRadius(1.0); // define the radius of the tool (sphere) double toolRadius = 0.04; // define a radius for the tool tool->setRadius(toolRadius); // hide the device sphere. only show proxy. tool->setShowContactPoints(true, false); // enable if objects in the scene are going to rotate of translate // or possibly collide against the tool. If the environment // is entirely static, you can set this parameter to "false" tool->enableDynamicObjects(true); // read the scale factor between the physical workspace of the haptic // device and the virtual workspace defined for the tool double workspaceScaleFactor = tool->getWorkspaceScaleFactor(); // define a maximum stiffness that can be handled by the current // haptic device. The value is scaled to take into account the // workspace scale factor double stiffnessMax = info.m_maxLinearStiffness / workspaceScaleFactor; //----------------------------------------------------------------------- // CREATING OBJECT //----------------------------------------------------------------------- // create a virtual mesh object = new cMultiMesh(); // add object to world world->addChild(object); // set the position of the object at the center of the world object->setLocalPos(0.0, 0.0, 0.0); // rotate the object 90 degrees object->rotateAboutGlobalAxisDeg(cVector3d(0,0,1), 90); // load an object file bool fileload; fileload = object->loadFromFile(RESOURCE_PATH("resources/models/face/face.obj")); if (!fileload) { #if defined(_MSVC) fileload = object->loadFromFile("../../../bin/resources/models/face/face.obj"); #endif } if (!fileload) { printf("Error - 3D Model failed to load correctly.\n"); close(); return (-1); } // compute a boundary box object->computeBoundaryBox(true); // get dimensions of object double size = cSub(object->getBoundaryMax(), object->getBoundaryMin()).length(); // resize object to screen if (size > 0) { object->scale( 2.0 * tool->getWorkspaceRadius() / size); } // compute collision detection algorithm object->createAABBCollisionDetector(toolRadius); cMaterial mat; mat.setRenderTriangles(true, true); object->setMaterial(mat); // define some environmental texture mapping cTexture2d* texture = new cTexture2d(); // load texture file fileload = texture->loadFromFile(RESOURCE_PATH("resources/images/chrome.bmp")); if (!fileload) { #if defined(_MSVC) fileload = texture->loadFromFile("../../../bin/resources/images/chrome.bmp"); #endif } if (!fileload) { printf("Error - Texture image failed to load correctly.\n"); close(); return (-1); } // enable spherical mapping texture->setSphericalMappingEnabled(true); // assign texture to object object->setTexture(texture, true); // enable texture mapping object->setUseTexture(true, true); // disable culling object->setUseCulling(false, true); // define a default stiffness for the object object->setStiffness(stiffnessMax, true); // define some haptic friction properties object->setFriction(0.1, 0.2, true); //----------------------------------------------------------------------- // WIDGETS //----------------------------------------------------------------------- // create a font cFont *font = NEW_CFONTCALIBRI20(); // create a label to display the haptic rate of the simulation labelHapticRate = new cLabel(font); labelHapticRate->m_fontColor.setWhite(); camera->m_frontLayer->addChild(labelHapticRate); //----------------------------------------------------------------------- // OPEN GL - WINDOW DISPLAY //----------------------------------------------------------------------- // simulation in now running! simulationRunning = true; // initialize GLUT glutInit(&argc, argv); // retrieve the resolution of the computer display and estimate the position // of the GLUT window so that it is located at the center of the screen int screenW = glutGet(GLUT_SCREEN_WIDTH); int screenH = glutGet(GLUT_SCREEN_HEIGHT); int windowW = 0.7 * screenH; int windowH = 0.7 * screenH; int windowPosX = (screenW - windowH) / 2; int windowPosY = (screenH - windowW) / 2; // initialize the OpenGL GLUT window glutInitWindowPosition(windowPosX, windowPosY); glutInitWindowSize(windowW, windowH); if (USE_STEREO_DISPLAY) { glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE | GLUT_STEREO); camera->setUseStereo(true); } else { glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE); camera->setUseStereo(false); } glutCreateWindow(argv[0]); glutDisplayFunc(updateGraphics); glutKeyboardFunc(keySelect); glutReshapeFunc(resizeWindow); glutSetWindowTitle("CHAI3D"); // create a mouse menu (right button) glutCreateMenu(menuSelect); glutAddMenuEntry("full screen", OPTION_FULLSCREEN); glutAddMenuEntry("window display", OPTION_WINDOWDISPLAY); glutAttachMenu(GLUT_RIGHT_BUTTON); //----------------------------------------------------------------------- // START SIMULATION //----------------------------------------------------------------------- // create a thread which starts the main haptics rendering loop cThread* hapticsThread = new cThread(); hapticsThread->start(updateHaptics, CTHREAD_PRIORITY_HAPTICS); // start the main graphics rendering loop glutTimerFunc(30, graphicsTimer, 0); glutMainLoop(); // close everything close(); // exit return (0); }
int main(int argc, char* argv[]) { //----------------------------------------------------------------------- // INITIALIZATION //----------------------------------------------------------------------- printf ("\n"); printf ("-----------------------------------\n"); printf ("CHAI3D\n"); printf ("Demo: 01-mydevice\n"); printf ("Copyright 2003-2012\n"); printf ("-----------------------------------\n"); printf ("\n\n"); printf ("Keyboard Options:\n\n"); printf ("[1] - Enable/Disable potential field\n"); printf ("[2] - Enable/Disable damping\n"); printf ("[x] - Exit application\n"); printf ("\n\n>\r"); //----------------------------------------------------------------------- // WORLD - CAMERA - LIGHTING //----------------------------------------------------------------------- // create a new world. world = new cWorld(); // set the background color of the environment world->m_backgroundColor.setBlack(); // create a camera and insert it into the virtual world camera = new cCamera(world); world->addChild(camera); // position and orient the camera camera->set( cVector3d (0.5, 0.0, 0.0), // camera position (eye) cVector3d (0.0, 0.0, 0.0), // look at position (target) cVector3d (0.0, 0.0, 1.0)); // direction of the (up) vector // set the near and far clipping planes of the camera // anything in front/behind these clipping planes will not be rendered camera->setClippingPlanes(0.01, 10.0); // create a light source light = new cDirectionalLight(world); // add light to world world->addChild(light); // enable light source light->setEnabled(true); // define the direction of the light beam light->setDir(-1.0, 0.0, 0.0); // create a sphere (cursor) to represent the haptic device cursor = new cShapeSphere(0.01); // add cursor to the world world->addChild(cursor); // create a small line to illustrate the velocity of the haptic device velocity = new cShapeLine(cVector3d(0,0,0), cVector3d(0,0,0)); // add line to the world world->addChild(velocity); //----------------------------------------------------------------------- // HAPTIC DEVICE //----------------------------------------------------------------------- // create a haptic device handler handler = new cHapticDeviceHandler(); // get a handle to the first haptic device handler->getDevice(hapticDevice, 0); // open a connection with the haptic device hapticDevice->open(); // retrieve information about the current haptic device cHapticDeviceInfo info = hapticDevice->getSpecifications(); // if the haptic device provides orientation sensing (stylus), // a reference frame is displayed if (info.m_sensedRotation == true) { // display a reference frame cursor->setShowFrame(true); // set the size of the reference frame cursor->setFrameSize(0.05, 0.05); } // if the device has a gripper, enable the gripper to behave like a user switch hapticDevice->setEnableGripperUserSwitch(true); //----------------------------------------------------------------------- // WIDGETS //----------------------------------------------------------------------- // create a font cFont *font = NEW_CFONTCALIBRI20(); // create a label to display the haptic device model labelHapticDeviceModel = new cLabel(font); camera->m_frontLayer->addChild(labelHapticDeviceModel); labelHapticDeviceModel->setString("haptic device: "+info.m_modelName); // create a label to display the position of haptic device labelHapticDevicePosition = new cLabel(font); camera->m_frontLayer->addChild(labelHapticDevicePosition); // create a label to display the haptic rate of the simulation labelHapticRate = new cLabel(font); camera->m_frontLayer->addChild(labelHapticRate); //----------------------------------------------------------------------- // OPEN GL - WINDOW DISPLAY //----------------------------------------------------------------------- // simulation in now running! simulationRunning = true; // initialize GLUT glutInit(&argc, argv); // retrieve the resolution of the computer display and estimate the position // of the GLUT window so that it is located at the center of the screen int screenW = glutGet(GLUT_SCREEN_WIDTH); int screenH = glutGet(GLUT_SCREEN_HEIGHT); int windowW = 0.7 * screenH; int windowH = 0.7 * screenH; int windowPosX = (screenW - windowH) / 2; int windowPosY = (screenH - windowW) / 2; // initialize the OpenGL GLUT window glutInitWindowPosition(windowPosX, windowPosY); glutInitWindowSize(windowW, windowH); if (USE_STEREO_DISPLAY) { glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE | GLUT_STEREO); camera->setUseStereo(true); } else { glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE); camera->setUseStereo(false); } glutCreateWindow(argv[0]); glutDisplayFunc(updateGraphics); glutKeyboardFunc(keySelect); glutReshapeFunc(resizeWindow); glutSetWindowTitle("CHAI3D"); // create a mouse menu (right button) glutCreateMenu(menuSelect); glutAddMenuEntry("full screen", OPTION_FULLSCREEN); glutAddMenuEntry("window display", OPTION_WINDOWDISPLAY); glutAttachMenu(GLUT_RIGHT_BUTTON); //----------------------------------------------------------------------- // START SIMULATION //----------------------------------------------------------------------- // create a thread which starts the main haptics rendering loop cThread* hapticsThread = new cThread(); hapticsThread->start(updateHaptics, CTHREAD_PRIORITY_HAPTICS); // start the main graphics rendering loop glutTimerFunc(30, graphicsTimer, 0); glutMainLoop(); // close everything close(); // exit return (0); }