void RCViewableTransform::moveToFit(const Eks::BoundingBox &bnds) { Eks::Vector3D pos = bnds.centre(); Eks::AxisAlignedBoundingBox bbox(Eks::Frame::XYZ(), bnds); Eks::Vector3D across = acrossDirection().normalized(); Eks::Vector3D up = upDirection().normalized(); Eks::Vector3D look = lookDirection().normalized(); float minX, maxX; bbox.maximumExtents(across, minX, maxX); float minY, maxY; bbox.maximumExtents(up, minY, maxY); float minZ, maxZ; bbox.maximumExtents(look, minZ, maxZ); float centreX = pos.dot(across); float centreY = pos.dot(up); float centreZ = pos.dot(look); float axisX = std::max(centreX - minX, maxX - centreX) / aspectRatio(); float axisY = std::max(centreY - minY, maxY - centreY); float minDist = centreZ - minZ; moveToFit(pos, lookDirection(), std::max(axisX, axisY), minDist); }
void TriangleWidget::paintGL() { RenderWidget::paintGL(); // Setup the modelview matrix m_modelMatrix.setToIdentity(); m_modelMatrix.rotate(45,1,1,0); QVector3D eyePosition( 0.0, 0.0, 5.0 ); QVector3D targetPosition( 0.0, 0.0, 0.0 ); QVector3D upDirection( 0.0, 1.0, 0.0 ); m_viewMatrix.setToIdentity(); m_viewMatrix.lookAt( eyePosition, targetPosition, upDirection ); m_shader->bind(); m_shader->setUniformValue( "modelMatrix", m_modelMatrix ); m_shader->setUniformValue( "viewMatrix", m_viewMatrix ); m_vertexBuffer.bind(); setAttributeObject( "vertex", GL_FLOAT, 0, 4 ); m_indexBuffer.bind(); // Draw stuff glDrawElements( GL_TRIANGLES, // Type of primitive to draw 3, // The number of indices in our index buffer we wish to draw GL_UNSIGNED_INT, // The element type of the index buffer 0 ); // Offset from the start of our index buffer of where to begin m_shader->release(); }
osg::Camera* createRttCamera( int texWidth, int texHeight, const osg::BoundingSphere& bs ) { osg::ref_ptr<osg::Camera> rttCamera = new osg::Camera; rttCamera->setClearMask( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); rttCamera->setReferenceFrame( osg::Transform::ABSOLUTE_RF ); rttCamera->setViewport( 0, 0, texWidth, texHeight ); rttCamera->setRenderOrder( osg::Camera::PRE_RENDER ); rttCamera->setRenderTargetImplementation( osg::Camera::FRAME_BUFFER_OBJECT ); double viewDistance = 2.0 * bs.radius(); double znear = viewDistance - bs.radius(); double zfar = viewDistance + bs.radius(); float top = 0.6 * znear; float right = 0.8 * znear; rttCamera->setProjectionMatrixAsFrustum( -right, right, -top, top, znear, zfar ); osg::Vec3d upDirection( 0.0,0.0,1.0 ); osg::Vec3d viewDirection( 0.0,-1.0,0.0 ); osg::Vec3d center = bs.center(); osg::Vec3d eyePoint = center + viewDirection * viewDistance; rttCamera->setViewMatrixAsLookAt( eyePoint, center, upDirection ); return rttCamera.release(); }
glm::mat4 Renderer::createWorldToCameraMatrix() const { auto phi = degToRad(activeCamera_.getPhi()); auto theta = degToRad(activeCamera_.getTheta() + 90.0f); auto sinTheta = sinf(theta); auto cosTheta = cosf(theta); auto sinPhi = sinf(phi); auto cosPhi = cosf(phi); glm::vec3 directionToCamera(sinTheta * cosPhi, cosTheta, sinTheta * sinPhi); directionToCamera = directionToCamera * activeCamera_.getRadius() + activeCamera_.lookAtPosition_; auto lookDirection = glm::normalize(activeCamera_.lookAtPosition_ - directionToCamera); glm::vec3 upDirection(0.0f, 1.0f, 0.0f); auto rightDirection = glm::normalize(glm::cross(lookDirection, upDirection)); auto perpUpDirection = glm::cross(rightDirection, lookDirection); glm::mat4 rotation(1.0f); rotation[0] = glm::vec4(rightDirection, 0.0f); rotation[1] = glm::vec4(perpUpDirection, 0.0f); rotation[2] = glm::vec4(-lookDirection, 0.0f); rotation = glm::transpose(rotation); glm::mat4 translation(1.0f); translation[3] = glm::vec4(-directionToCamera, 1.0f); return rotation * translation; }
bool Renderer::Initialize() { m_renderTarget = new GDIRenderTarget(m_hwnd,m_frameWidth,m_frameHeight); if(m_renderTarget == nullptr) { return false; } m_renderTarget->Initialize(); Color* renderTargetBuffer = m_renderTarget->GetColorBuffer(); int stride = m_renderTarget->GetWidth(); m_rasterizer = new Rasterizer(renderTargetBuffer,stride); if(m_rasterizer == nullptr) { return false; } //camera data and initialization Vector3D viewTarget(0.0f,0.0f,0.0f); Vector3D upDirection(0.0f,1.0f,0.0f); Vector3D cameraPosition(0.5f,0.5f,1.5f); float fov = PI / 2.0f; float aspectRatio = 16.0f / 9.0f; float nearPlane = -0.1f; float farPlane = -1000.0f; m_camera = new Camera(cameraPosition,viewTarget,upDirection,fov,aspectRatio,nearPlane,farPlane,m_frameWidth,m_frameHeight); if(m_camera == nullptr) { return false; } m_camera->Initialize(); m_viewTransMatrix = m_camera->ComputeViewTransformMatrix(); m_importer = new ImporterOBJ("testScene.obj"); if(m_importer == nullptr) { return false; } if(!m_importer->ProcessFile()) return false; m_scene = m_importer->GetScene(); m_HRTimer = new HRTimer(); if(m_HRTimer == nullptr) return false; return true; }
void ColladaNode::handleLookAt(domLookat *lookat) { if(lookat == NULL) return; domNodeRef node = getDOMElementAs<domNode>(); Pnt3f eyePosition(lookat->getValue()[0],lookat->getValue()[1],lookat->getValue()[2]), lookAtPoint(lookat->getValue()[3],lookat->getValue()[4],lookat->getValue()[5]); Vec3f upDirection(lookat->getValue()[7],lookat->getValue()[8],lookat->getValue()[9]); if(getGlobal()->getOptions()->getFlattenNodeXForms()) { LookAtTransformationElementUnrecPtr LookAtElement = LookAtTransformationElement::create(); LookAtElement->setEyePosition(eyePosition); LookAtElement->setLookAtPosition(lookAtPoint); LookAtElement->setUpDirection(upDirection); setName(LookAtElement, lookat->getSid()); appendStackedXForm(LookAtElement, node); } else { TransformUnrecPtr xform = Transform::create(); NodeUnrecPtr xformN = makeNodeFor(xform); Matrix lookAtMatrix; MatrixLookAt(lookAtMatrix,eyePosition, lookAtPoint, upDirection); xform->setMatrix(lookAtMatrix); if(getGlobal()->getOptions()->getCreateNameAttachments() == true && node->getName() != NULL ) { std::string nodeName = node->getName(); if(lookat->getSid() != NULL && getGlobal()->getOptions()->getFlattenNodeXForms() == false) { nodeName.append("." ); nodeName.append(lookat->getSid()); } setName(xformN, nodeName); } appendXForm(xformN); } }
//-------------------------------- osg::Camera* test::createBirdsEye(const osg::BoundingSphere& bs) { osg::ref_ptr<osg::Camera> camera=new osg::Camera; camera->setClearMask(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); camera->setReferenceFrame(osg::Transform::ABSOLUTE_RF); double viewDistance=2.0*bs.radius(); double znear=viewDistance-bs.radius(); double zfar=viewDistance+bs.radius(); float top=bs.radius(); float right=bs.radius(); camera->setProjectionMatrixAsOrtho(-right,right,-top,top,znear,zfar); osg::Vec3d upDirection(0,1,0); osg::Vec3d viewDirection(0,0,1); osg::Vec3d center=bs.center(); osg::Vec3d eyePoint=center+viewDirection*viewDistance; camera->setViewMatrixAsLookAt(eyePoint,center,upDirection); return camera.release(); }
void CFlag::Unserialize( RakNet::BitStream& bt ) { int stringLength = 0; char* inString = NULL; vector3df vScale; f32 fMass; int iBody; UNLOAD_STRING String modelname = inString; delete[] inString; inString = NULL; UNLOAD_STRING String texname = inString; delete[] inString; inString = NULL; bt.ReadVector( vScale.X, vScale.Y, vScale.Z ); bt.Read( fMass ); bt.Read( iBody ); bt.Read( team ); assemblePhysics( modelname.c_str(), ( BodyType )iBody, vScale, vector3df( 0, 0, 0 ), fMass, false ); if ( ( !node ) || ( !body ) ) { bInvalidEntity = true; return; } //bFix2DPos = bFix2DRot = false; node->setMaterialTexture( 0, IRR.video->getTexture( texname.c_str() ) ); // upvector joint vector3df upDirection( -1.0f, 0.0f, 0.0f ); m_upVector = NewtonConstraintCreateUpVector( WORLD.GetPhysics()->nWorld, &upDirection.X, body ); // continuous collision NewtonBodySetContinuousCollisionMode( body, 1 ); //setRotation( vector3df(0, 0, 90 ) ); }