//-------------------------------------------------------------- void ofxBulletWorldSoft::mouseDragged( ofMouseEventArgs &a ) { if (_pickConstraint != NULL) { //move the constraint pivot btGeneric6DofConstraint* pickCon = static_cast<btGeneric6DofConstraint*>(_pickConstraint); if (pickCon) { //cout << "ofxBulletWorldSoft :: mouseMoved : moving the mouse! with constraint" << endl; //keep it at the same picking distance ofVec3f mouseRay = _camera->screenToWorld( ofVec3f((float)a.x, (float)a.y, 0) ); mouseRay = mouseRay - _camera->getPosition(); mouseRay.normalize(); mouseRay *= 300.f; btVector3 newRayTo(mouseRay.x, mouseRay.y, mouseRay.z); btVector3 oldPivotInB = pickCon->getFrameOffsetA().getOrigin(); btVector3 m_cameraPosition( _camera->getPosition().x, _camera->getPosition().y, _camera->getPosition().z ); btVector3 rayFrom = m_cameraPosition; btVector3 dir = newRayTo-rayFrom; dir.normalize(); dir *= gOldPickingDist; btVector3 newPivotB = rayFrom + dir; pickCon->getFrameOffsetA().setOrigin(newPivotB); } } }
//-------------------------------------------------------------- // pulled from DemoApplication in the AllBulletDemos project included in the Bullet physics download // void ofxBulletWorldSoft::checkMousePicking(float a_mousex, float a_mousey) { ofxBulletRaycastData data = raycastTest(a_mousex, a_mousey, _mouseFilterMask); if(data.bHasHit) { ofxBulletMousePickEvent cdata; cdata.setRaycastData(data); if (cdata.body != NULL) { btVector3 m_cameraPosition( _camera->getPosition().x, _camera->getPosition().y, _camera->getPosition().z ); //other exclusions? if (!(cdata.body->isStaticObject() || cdata.body->isKinematicObject()) && bRegisterGrabbing) { _pickedBody = cdata.body; //btRigidBody // _pickedBody->setActivationState( DISABLE_DEACTIVATION ); btTransform tr; tr.setIdentity(); tr.setOrigin(btVector3(cdata.localPivotPos.x, cdata.localPivotPos.y, cdata.localPivotPos.z)); btGeneric6DofConstraint* dof6 = new btGeneric6DofConstraint(*cdata.body, tr, false); dof6->setLinearLowerLimit(btVector3(0,0,0)); dof6->setLinearUpperLimit(btVector3(0,0,0)); dof6->setAngularLowerLimit(btVector3(0,0,0)); dof6->setAngularUpperLimit(btVector3(0,0,0)); world->addConstraint(dof6); _pickConstraint = dof6; dof6->setParam(BT_CONSTRAINT_STOP_CFM,0.8,0); dof6->setParam(BT_CONSTRAINT_STOP_CFM,0.8,1); dof6->setParam(BT_CONSTRAINT_STOP_CFM,0.8,2); dof6->setParam(BT_CONSTRAINT_STOP_CFM,0.8,3); dof6->setParam(BT_CONSTRAINT_STOP_CFM,0.8,4); dof6->setParam(BT_CONSTRAINT_STOP_CFM,0.8,5); dof6->setParam(BT_CONSTRAINT_STOP_ERP,0.1,0); dof6->setParam(BT_CONSTRAINT_STOP_ERP,0.1,1); dof6->setParam(BT_CONSTRAINT_STOP_ERP,0.1,2); dof6->setParam(BT_CONSTRAINT_STOP_ERP,0.1,3); dof6->setParam(BT_CONSTRAINT_STOP_ERP,0.1,4); dof6->setParam(BT_CONSTRAINT_STOP_ERP,0.1,5); gOldPickingDist = ( btVector3(cdata.pickPosWorld.x, cdata.pickPosWorld.y, cdata.pickPosWorld.z) - m_cameraPosition).length(); //cout << "ofxBulletWorldSoft :: checkMousePicking : adding a mouse constraint" << endl; } //cout << "ofxBulletWorldSoft :: checkMousePicking : selected a body!!!" << endl; ofNotifyEvent( MOUSE_PICK_EVENT, cdata, this ); } } }
void updateCamera() { float m_ele = 0; float m_azi=0; btVector3 m_cameraPosition(12,20,12); btVector3 m_cameraTargetPosition(0,10,0); btVector3 m_cameraUp(0,1,0); int m_forwardAxis=2; btScalar m_cameraDistance = 130; glMatrixMode(GL_PROJECTION); glLoadIdentity(); float m_frustumZNear=1; float m_frustumZFar=1000; if (m_glutScreenWidth == 0 && m_glutScreenHeight == 0) return; float aspect; btVector3 extents; if (m_glutScreenWidth > m_glutScreenHeight) { aspect = m_glutScreenWidth / (float)m_glutScreenHeight; extents.setValue(aspect * 1.0f, 1.0f,0); } else { aspect = m_glutScreenHeight / (float)m_glutScreenWidth; extents.setValue(1.0f, aspect*1.f,0); } if (m_ortho) { // reset matrix glLoadIdentity(); extents *= m_cameraDistance; btVector3 lower = m_cameraTargetPosition - extents; btVector3 upper = m_cameraTargetPosition + extents; glOrtho(lower.getX(), upper.getX(), lower.getY(), upper.getY(),-1000,1000); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); } else { if (m_glutScreenWidth > m_glutScreenHeight) { glFrustum (-aspect * m_frustumZNear, aspect * m_frustumZNear, -m_frustumZNear, m_frustumZNear, m_frustumZNear, m_frustumZFar); } else { glFrustum (-aspect * m_frustumZNear, aspect * m_frustumZNear, -m_frustumZNear, m_frustumZNear, m_frustumZNear, m_frustumZFar); } glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluLookAt(m_cameraPosition[0], m_cameraPosition[1], m_cameraPosition[2], m_cameraTargetPosition[0], m_cameraTargetPosition[1], m_cameraTargetPosition[2], m_cameraUp.getX(),m_cameraUp.getY(),m_cameraUp.getZ()); } }
/// // Draw a triangle using the shader pair created in Init() // void renderFrame() { GLfloat vVertices[] = { -0.5f, 0.5f, 0.0f, // Position 0 0.0f, 0.0f, // TexCoord 0 -0.5f, -0.5f, 0.0f, // Position 1 0.0f, 1.0f, // TexCoord 1 0.5f, -0.5f, 0.0f, // Position 2 1.0f, 1.0f, // TexCoord 2 0.5f, 0.5f, 0.0f, // Position 3 1.0f, 0.0f // TexCoord 3 }; GLushort indices[] = { 0, 1, 2, 2,1,0};//0, 2, 3 }; // Clear the color and depth buffer glClear ( GL_COLOR_BUFFER_BIT |GL_DEPTH_BUFFER_BIT); // Use the program object glUseProgram ( programObject ); // Load the vertex position glVertexAttribPointer ( positionLoc, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), vVertices ); // Load the texture coordinate glVertexAttribPointer ( texCoordLoc, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), &vVertices[3] ); glEnableVertexAttribArray ( positionLoc ); glEnableVertexAttribArray ( texCoordLoc ); // // Bind the texture glActiveTexture ( GL_TEXTURE0 ); glBindTexture ( GL_TEXTURE_2D, textureId ); // Set the sampler texture unit to 0 glUniform1i ( samplerLoc, 0 ); btVector3 m_cameraPosition(0,0,-2); btVector3 m_cameraTargetPosition(0,0,0); btVector3 m_cameraUp(0,1,0);//1,0); static float mat2[16]; #define USE_CAM_FROM_FILE 1 #ifdef USE_CAM_FROM_FILE if (fileLoaded && reader) { reader->m_cameraTrans.inverse().getOpenGLMatrix(mat2); } else #endif { btCreateLookAt(m_cameraPosition,m_cameraTargetPosition,m_cameraUp,mat2); } glUniformMatrix4fv(viewMatrix,1,GL_FALSE,mat2); glUniformMatrix4fv(projectionMatrix,1,GL_FALSE,projMat); { btTransform tr; tr.setIdentity(); static float mat1[16]; tr.getOpenGLMatrix(mat1); glUniformMatrix4fv(modelMatrix,1,GL_FALSE,mat1); glDrawElements ( GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, indices ); if (fileLoaded) { tr.setOrigin(btVector3(0,1,0)); tr.getOpenGLMatrix(mat1); glUniformMatrix4fv(modelMatrix,1,GL_FALSE,mat1); glDrawElements ( GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, indices ); } } if (fileLoaded && reader) { for (int i=0;i<reader->m_graphicsObjects.size();i++) { reader->m_graphicsObjects[i].render(positionLoc,texCoordLoc,samplerLoc,modelMatrix); } dynWorld->setGravity(btVector3(0,0,-1));//-1,0)); dynWorld->stepSimulation(0.016f); } }