void Intrinsics::loadProjectionMatrix(float nearDist, float farDist, cv::Point2d viewportOffset) const { ofViewport(viewportOffset.x, viewportOffset.y, imageSize.width, imageSize.height); ofSetMatrixMode(OF_MATRIX_PROJECTION); ofLoadIdentityMatrix(); float w = imageSize.width; float h = imageSize.height; float fx = cameraMatrix.at<double>(0, 0); float fy = cameraMatrix.at<double>(1, 1); float cx = principalPoint.x; float cy = principalPoint.y * aspectRatio; ofMatrix4x4 frustum; frustum.makeFrustumMatrix( nearDist * (-cx) / fx, nearDist * (w - cx) / fx, nearDist * (cy) / fy, nearDist * (cy - h) / fy, nearDist, farDist); ofMultMatrix(frustum); ofSetMatrixMode(OF_MATRIX_MODELVIEW); ofLoadIdentityMatrix(); ofMatrix4x4 lookAt; lookAt.makeLookAtViewMatrix(ofVec3f(0, 0, 0), ofVec3f(0, 0, 1), ofVec3f(0, -1, 0)); ofMultMatrix(lookAt); }
//---------------------------------------------------------- void ofTexture::bind(int textureLocation){ //we could check if it has been allocated - but we don't do that in draw() if(texData.alphaMask){ ofGetGLRenderer()->setAlphaMaskTex(*texData.alphaMask); } enableTextureTarget(textureLocation); if(ofGetUsingNormalizedTexCoords()) { ofSetMatrixMode(OF_MATRIX_TEXTURE); ofPushMatrix(); ofMatrix4x4 m; #ifndef TARGET_OPENGLES if(texData.textureTarget == GL_TEXTURE_RECTANGLE_ARB) m.makeScaleMatrix(texData.width, texData.height, 1.0f); else #endif m.makeScaleMatrix(texData.width / texData.tex_w, texData.height / texData.tex_h, 1.0f); ofLoadMatrix(m); ofSetMatrixMode(OF_MATRIX_MODELVIEW); } if(texData.useTextureMatrix){ ofSetMatrixMode(OF_MATRIX_TEXTURE); if(!ofGetUsingNormalizedTexCoords()) ofPushMatrix(); ofMultMatrix(texData.textureMatrix); ofSetMatrixMode(OF_MATRIX_MODELVIEW); } texData.isBound = true; }
//---------- void ofxSplashScreen::begin(float minimumDuration) { if (!this->image.isAllocated()) { ofLogError("ofxSplashScreen") << "Cannot show splash screen since no image has been loaded"; return; } this->endTime = ofGetElapsedTimef() + minimumDuration; this->appWindow = glfwGetCurrentContext(); glfwHideWindow(this->appWindow); glfwWindowHint(GLFW_DECORATED, GL_FALSE); this->splashScreenWindow = glfwCreateWindow(this->image.getWidth(), this->image.getHeight(), "ofxSplashScreen", NULL, this->appWindow); glfwSetWindowPos(this->splashScreenWindow, (ofGetScreenWidth() - this->image.getWidth()) / 2.0f, (ofGetScreenHeight() - this->image.getHeight()) / 2.0f); glfwWindowHint(GLFW_DECORATED, GL_TRUE); glfwMakeContextCurrent(this->splashScreenWindow); //set the drawing matrices to normalised coordinates ofSetMatrixMode(OF_MATRIX_PROJECTION); ofLoadIdentityMatrix(); ofSetMatrixMode(OF_MATRIX_MODELVIEW); ofLoadIdentityMatrix(); //draw the images ofClear(0,0,0); this->image.update(); this->image.draw(-1,-1,2,2); glfwSwapBuffers(this->splashScreenWindow); glFlush(); //set the context back to main for rest of setup glfwMakeContextCurrent(this->appWindow); ofSetupScreen(); }
// --------------------------------------------------------------------------------------------------------------------------------------------------- // void ofxOculusRift::draw( ofVec2f pos, ofVec2f size ) { // Todo: rewrite this ofPushView(); // draw into the new fbo we have made ofSetColor( 255 ); glBindFramebuffer(GL_FRAMEBUFFER, framebufferID); ofClear(0,0,0); eyeFboLeft.draw( 0.0f, 0.0f ); eyeFboRight.draw( eyeFboLeft.getWidth(), 0.0f ); // is this being drawn correctly? glBindFramebuffer(GL_FRAMEBUFFER, 0); ofSetMatrixMode(OF_MATRIX_PROJECTION); ofLoadIdentityMatrix(); ofSetMatrixMode(OF_MATRIX_MODELVIEW); ofLoadIdentityMatrix(); ofSetColor( 255 ); if( doWarping ) { glEnable( GL_TEXTURE_2D ); glBindTexture(GL_TEXTURE_2D, colorTextureID); renderDistortedEyeNew( true, 0.0f, 0.0f, 0.5f, 1.0f); renderDistortedEyeNew( false, 0.5f, 0.0f, 0.5f, 1.0f); glDisable( GL_TEXTURE_2D ); } ofPopView(); /* glBegin(GL_TRIANGLE_STRIP); glTexCoord2f(pos.x, pos.y ); glVertex2f(-1.0f, -1.0f); glTexCoord2f(pos.x+size.x, pos.y ); glVertex2f(0.0f, -1.0f); glTexCoord2f(pos.x, pos.y+size.y); glVertex2f(-1.0f, 1.0f); glTexCoord2f(pos.x+size.x, pos.y+size.y); glVertex2f(0.0f, 1.0f); glEnd(); */ if( !doWarping ) { ofSetColor(255); eyeFboLeft.draw( 0.0f, 0.0f ); eyeFboRight.draw( eyeFboLeft.getWidth(), 0.0f ); } needSensorReadingThisFrame = true; }
//---------------------------------------- void ofCamera::begin(ofRectangle viewport) { if(!isActive) ofPushView(); isActive = true; ofViewport(viewport.x,viewport.y,viewport.width,viewport.height); ofSetOrientation(ofGetOrientation(),vFlip); ofSetMatrixMode(OF_MATRIX_PROJECTION); ofLoadMatrix( getProjectionMatrix(viewport) ); ofSetMatrixMode(OF_MATRIX_MODELVIEW); ofLoadViewMatrix( getModelViewMatrix() ); }
//-------------------------------------------------------------- void ofApp::draw(){ // // draw background // ofSetMatrixMode(OF_MATRIX_TEXTURE); ofPushMatrix(); float scale = ofGetWidth()/m_background_tile.getWidth(); ofScale(scale, scale); m_background_tile.getTextureReference().draw(0,0,ofGetWidth(),ofGetWidth()); ofPopMatrix(); ofSetMatrixMode(OF_MATRIX_MODELVIEW); ofPushStyle(); ofRectangle safe_rect = m_server.getSafeZone(); ofSetColor(253, 185, 0); ofRect(safe_rect); ofPopStyle(); // // // m_server.draw(); // // // for ( int i = 0; i < k_nbuttons; i++ ) { ofPushMatrix(); ofTranslate(m_button_bounds[ i ].getCenter()); ofRotate(m_button_bounds[i].getCenter().y < ofGetHeight() / 2.f ? 180.f : 0); ofTranslate(-m_button_bounds[ i ].width/2,-m_button_bounds[ i ].height/2.0); int selector = i % k_nbuttonimages; m_buttons[selector].draw(0, 0, m_button_bounds[i].width, m_button_bounds[i].height); /* if ( i == 0 || i == 2 ) { m_buttons[ 0 ].draw(0,0,m_button_bounds[ i ].width,m_button_bounds[ i ].height); } else { m_buttons[ 1 ].draw(0,0,m_button_bounds[ i ].width,m_button_bounds[ i ].height); } */ ofPopMatrix(); } // // // if ( m_current_session ) { m_current_session->draw(); } /* if ( m_current_document ) { m_current_document->draw(); } */ }
//---------------------------------------------------------- void ofTexture::unbind(int textureLocation){ disableTextureTarget(textureLocation); if(texData.alphaMask){ ofGetGLRenderer()->disableAlphaMask(); } if(texData.useTextureMatrix || ofGetUsingNormalizedTexCoords()) { ofSetMatrixMode(OF_MATRIX_TEXTURE); ofPopMatrix(); ofSetMatrixMode(OF_MATRIX_MODELVIEW); } texData.isBound = false; }
//---------------------------------------- void ofCamera::begin(ofRectangle viewport) { if(!isActive) ofPushView(); isActive = true; ofSetCoordHandedness(OF_RIGHT_HANDED); // autocalculate near/far clip planes if not set by user calcClipPlanes(viewport); ofSetMatrixMode(OF_MATRIX_PROJECTION); ofLoadIdentityMatrix(); ofLoadMatrix( this->getProjectionMatrix(viewport) ); ofSetMatrixMode(OF_MATRIX_MODELVIEW); ofLoadMatrix( ofMatrix4x4::getInverseOf(getGlobalTransformMatrix()) ); ofViewport(viewport); }
void ofxMapamok::end() { if (!calibrationReady) { return; } if (useDistortionShader) { distortionBuffer.end(); } // restore default viewport ofViewport(0, 0, ofGetWidth(), ofGetHeight()); ofPopMatrix(); ofSetMatrixMode(OF_MATRIX_PROJECTION); ofPopMatrix(); ofSetMatrixMode(OF_MATRIX_MODELVIEW); if (useDistortionShader) { distortionShader.begin(); distortionBuffer.draw(viewport.getLeft(), viewport.getBottom(), viewport.width, -viewport.height); // draw FBO upside-down distortionShader.end(); } }
void ofxMapamok::begin() { if (!calibrationReady) { return; } ofPushMatrix(); ofSetMatrixMode(OF_MATRIX_PROJECTION); ofPushMatrix(); ofSetMatrixMode(OF_MATRIX_MODELVIEW); intrinsics.loadProjectionMatrix(nearDist, farDist); ofMultMatrix(modelMatrix); if (!useDistortionShader) { ofViewport(viewport); } else { if (distortionBuffer.getWidth() != viewport.width || distortionBuffer.getHeight() != viewport.height) { distortionBuffer.allocate(viewport.width, viewport.height, GL_RGBA); } distortionBuffer.begin(false); ofViewport(ofRectangle(0, 0, viewport.width, viewport.height)); ofClear(0, 0); } }
void MagicMirrorApp::draw(){ // draw the background view->drawBackground(backgroundTexture); infoAnimation->draw(); // set the projection Matrix once ofSetMatrixMode(OF_MATRIX_PROJECTION); ofPushMatrix(); view->setProjection(tracker->getProjectionMatrix()); // call the view object draw functions, which will handle the drawing set<ContentElement*>::iterator elemIt; for (elemIt = visibleElements.begin(); elemIt != visibleElements.end(); ++elemIt) { ContentElement* elem = *elemIt; view->drawElement(elem); } ofPopMatrix(); }
// --------------------------------------------------------------------------------------------------------------------------------------------------- // void ofxOculusRift::beginRender( float _interOcularShift, ofFbo* _fbo ) { ofPushView(); _fbo->begin(); ofClear(0,0,0); // Todo: get the proper clear color setupScreenPerspective( _interOcularShift, ofGetWidth(), ofGetHeight(), ofGetOrientation(), false, getFov(), getNearClip(), getFarClip() ); ofSetMatrixMode(OF_MATRIX_MODELVIEW); ofLoadIdentityMatrix(); ofPushMatrix(); // flip for FBO ofScale(1,-1,1); ofMultMatrix( getHeadsetViewOrientationMat() ); ofTranslate( getPosition() ); }
// --------------------------------------------------------------------------------------------------------------------------------------------------- // void ofxOculusRift::setupScreenPerspective(float _interOcularDistance, float width, float height, ofOrientation orientation, bool vFlip, float fov, float nearDist, float farDist) { if(width == 0) width = ofGetWidth(); if(height == 0) height = ofGetHeight(); float viewW = ofGetViewportWidth(); float viewH = ofGetViewportHeight(); float eyeX = viewW / 2; float eyeY = viewH / 2; float halfFov = PI * fov / 360; float theTan = tanf(halfFov); float dist = eyeY / theTan; float aspect = (float) viewW / viewH; if(nearDist == 0) nearDist = dist / 10.0f; if(farDist == 0) farDist = dist * 10.0f; ofSetMatrixMode(OF_MATRIX_PROJECTION); ofLoadIdentityMatrix(); ofTranslate( ofPoint(_interOcularDistance,0,0) ); ofMatrix4x4 persp; persp.makePerspectiveMatrix(fov, aspect, nearDist, farDist); ofMultMatrix( persp ); //gluPerspective(fov, aspect, nearDist, farDist); ofSetMatrixMode(OF_MATRIX_MODELVIEW); ofLoadIdentityMatrix(); ofMatrix4x4 lookAt; lookAt.makeLookAtViewMatrix( ofVec3f(eyeX, eyeY, dist), ofVec3f(eyeX, eyeY, 0), ofVec3f(0, 1, 0) ); ofLoadMatrix( lookAt ); //note - theo checked this on iPhone and Desktop for both vFlip = false and true if(ofDoesHWOrientation()){ if(vFlip){ ofScale(1, -1, 1); ofTranslate(0, -height, 0); } }else{ if( orientation == OF_ORIENTATION_UNKNOWN ) orientation = ofGetOrientation(); switch(orientation) { case OF_ORIENTATION_180: ofRotate(-180, 0, 0, 1); if(vFlip){ ofScale(1, -1, 1); ofTranslate(-width, 0, 0); }else{ ofTranslate(-width, -height, 0); } break; case OF_ORIENTATION_90_RIGHT: ofRotate(-90, 0, 0, 1); if(vFlip){ ofScale(-1, 1, 1); }else{ ofScale(-1, -1, 1); ofTranslate(0, -height, 0); } break; case OF_ORIENTATION_90_LEFT: ofRotate(90, 0, 0, 1); if(vFlip){ ofScale(-1, 1, 1); ofTranslate(-width, -height, 0); }else{ ofScale(-1, -1, 1); ofTranslate(-width, 0, 0); } break; case OF_ORIENTATION_DEFAULT: default: if(vFlip){ ofScale(1, -1, 1); ofTranslate(0, -height, 0); } break; } } }