void StarsApp::update() { double elapsed = getElapsedSeconds() - mTime; mTime += elapsed; // animate camera mCamera.update(elapsed); // update background and user interface mBackground.setCameraDistance( mCamera.getCamera().getEyePoint().length() ); mUserInterface.setCameraDistance( mCamera.getCamera().getEyePoint().length() ); }
void StarsApp::draw() { gl::clear( Color::black() ); gl::pushMatrices(); gl::setMatrices( mCamera.getCamera() ); { // draw background mBackground.draw(); // draw grid if(mIsGridVisible) mGrid.draw(); // draw stars mStars.draw(); } gl::popMatrices(); // draw user interface mUserInterface.draw(); // fade in at start of application gl::enableAlphaBlending(); double t = math<double>::clamp( mTimer.getSeconds() / 3.0, 0.0, 1.0 ); float a = ci::lerp<float>(1.0f, 0.0f, (float) t); if( a > 0.0f ) { gl::color( ColorA(0,0,0,a) ); gl::drawSolidRect( getWindowBounds() ); } gl::disableAlphaBlending(); }
void StarsApp::update() { double elapsed = getElapsedSeconds() - mTime; mTime += elapsed; double time = getElapsedSeconds() / 200.0; if(mSoundEngine && mMusic && mPlayMusic) time = mMusic->getPlayPosition() / (double) mMusic->getPlayLength(); // animate camera mCamera.setDistanceTime(time); mCamera.update(elapsed); // adjust content based on camera distance float distance = mCamera.getCamera().getEyePoint().length(); mBackground.setCameraDistance( distance ); mLabels.setCameraDistance( distance ); mConstellations.setCameraDistance( distance ); mConstellationLabels.setCameraDistance( distance ); mUserInterface.setCameraDistance( distance ); // if(mSoundEngine) { // send camera position to sound engine (for 3D sounds) Vec3f pos = mCamera.getPosition(); mSoundEngine->setListenerPosition( vec3df(pos.x, pos.y, pos.z), vec3df(-pos.x, -pos.y, -pos.z), vec3df(0,0,0), vec3df(0,1,0) ); // if music has finished, play next track if( mPlayMusic && mMusic && mMusic->isFinished() ) { playMusic( getNextFile(mMusicPath) ); } } }
void StarsApp::draw() { int w = getWindowWidth(); int h = getWindowHeight(); gl::clear( Color::black() ); if(mIsStereoscopic) { glPushAttrib( GL_VIEWPORT_BIT ); gl::pushMatrices(); // render left eye mCamera.enableStereoLeft(); gl::setViewport( Area(0, 0, w / 2, h) ); gl::setMatrices( mCamera.getCamera() ); render(); // draw user interface mUserInterface.draw("Stereoscopic Projection"); // render right eye mCamera.enableStereoRight(); gl::setViewport( Area(w / 2, 0, w, h) ); gl::setMatrices( mCamera.getCamera() ); render(); // draw user interface mUserInterface.draw("Stereoscopic Projection"); gl::popMatrices(); glPopAttrib(); } else if(mIsCylindrical) { // make sure we have a frame buffer to render to createFbo(); // determine correct aspect ratio and vertical field of view for each of the 3 views w = mFbo.getWidth() / 3; h = mFbo.getHeight(); const float aspect = float(w) / float(h); const float hFoV = 60.0f; const float vFoV = toDegrees( 2.0f * math<float>::atan( math<float>::tan( toRadians(hFoV) * 0.5f ) / aspect ) ); // for values smaller than 1.0, this will cause each view to overlap the other ones const float overlap = 1.0f; // bind the frame buffer object mFbo.bindFramebuffer(); // store viewport, camera and matrices, so we can restore later glPushAttrib( GL_VIEWPORT_BIT ); CameraStereo original = mCamera.getCamera(); gl::pushMatrices(); // setup camera CameraStereo cam = mCamera.getCamera(); cam.disableStereo(); cam.setAspectRatio(aspect); cam.setFov( vFoV ); Vec3f right, up; cam.getBillboardVectors(&right, &up); Vec3f forward = up.cross(right); // render left side gl::setViewport( Area(0, 0, w, h) ); cam.setViewDirection( Quatf(up, overlap * toRadians(hFoV)) * forward ); cam.setWorldUp( up ); gl::setMatrices( cam ); render(); // render front side gl::setViewport( Area(w, 0, w*2, h) ); cam.setViewDirection( forward ); cam.setWorldUp( up ); gl::setMatrices( cam ); render(); // draw user interface mUserInterface.draw( (boost::format("Cylindrical Projection (%d degrees)") % int( (1.0f + 2.0f * overlap) * hFoV ) ).str() ); // render right side gl::setViewport( Area(w*2, 0, w*3, h) ); cam.setViewDirection( Quatf(up, -overlap * toRadians(hFoV)) * forward ); cam.setWorldUp( up ); gl::setMatrices( cam ); render(); // unbind the frame buffer object mFbo.unbindFramebuffer(); // restore states gl::popMatrices(); mCamera.setCurrentCam(original); glPopAttrib(); // draw frame buffer and perform cylindrical projection using a fragment shader if(mShader) { float sides = 3; float radians = sides * toRadians( hFoV ); float reciprocal = 0.5f / sides; mShader.bind(); mShader.uniform("texture", 0); mShader.uniform("sides", sides); mShader.uniform("radians", radians ); mShader.uniform("reciprocal", reciprocal ); } Rectf centered = Rectf(mFbo.getBounds()).getCenteredFit( getWindowBounds(), false ); gl::draw( mFbo.getTexture(), centered ); if(mShader) mShader.unbind(); } else { mCamera.disableStereo(); gl::pushMatrices(); gl::setMatrices( mCamera.getCamera() ); render(); gl::popMatrices(); // draw user interface mUserInterface.draw("Perspective Projection"); } // fade in at start of application gl::enableAlphaBlending(); double t = math<double>::clamp( mTimer.getSeconds() / 3.0, 0.0, 1.0 ); float a = ci::lerp<float>(1.0f, 0.0f, (float) t); if( a > 0.0f ) { gl::color( ColorA(0,0,0,a) ); gl::drawSolidRect( getWindowBounds() ); } gl::disableAlphaBlending(); }
void StarsApp::setup() { // create the spherical grid mesh mGrid.setup(); // load the star database and create the VBO mesh if( fs::exists( getAssetPath("") / "stars.cdb" ) ) mStars.read( loadFile( getAssetPath("") / "stars.cdb" ) ); if( fs::exists( getAssetPath("") / "labels.cdb" ) ) mLabels.read( loadFile( getAssetPath("") / "labels.cdb" ) ); if( fs::exists( getAssetPath("") / "constellations.cdb" ) ) mConstellations.read( loadFile( getAssetPath("") / "constellations.cdb" ) ); if( fs::exists( getAssetPath("") / "constellationlabels.cdb" ) ) mConstellationLabels.read( loadFile( getAssetPath("") / "constellationlabels.cdb" ) ); // create user interface mUserInterface.setup(); // initialize background image mBackground.setup(); // initialize camera mCamera.setup(); CameraPersp cam( mCamera.getCamera() ); cam.setNearClip( 0.01f ); cam.setFarClip( 5000.0f ); // mIsGridVisible = true; mIsLabelsVisible = true; mIsConstellationsVisible = true; mIsStereoscopic = false; mIsCylindrical = false; // create stars mStars.setup(); mStars.setAspectRatio( mIsStereoscopic ? 0.5f : 1.0f ); // create labels mLabels.setup(); mConstellationLabels.setup(); // mMusicExtensions.push_back( ".flac" ); mMusicExtensions.push_back( ".ogg" ); mMusicExtensions.push_back( ".wav" ); mMusicExtensions.push_back( ".mp3" ); mPlayMusic = true; // initialize the IrrKlang Sound Engine in a very safe way mSoundEngine = shared_ptr<ISoundEngine>( createIrrKlangDevice(), std::mem_fun(&ISoundEngine::drop) ); if(mSoundEngine) { // play 3D Sun rumble mSound = createSound( getAssetPath("") / "sound/low_rumble_loop.mp3" ); if(mSound) { mSound->setIsLooped(true); mSound->setMinDistance(2.5f); mSound->setMaxDistance(12.5f); mSound->setIsPaused(false); } // play background music (the first .mp3 file found in ./assets/music) fs::path path = getFirstFile( getAssetPath("") / "music" ); playMusic(path); } // createShader(); // mTimer.start(); #if (defined WIN32 && defined NDEBUG) forceHideCursor(); #else forceShowCursor(); #endif mTime = getElapsedSeconds(); }
void StarsApp::draw() { float w = 0.5f * getWindowWidth(); float h = 1.0f * getWindowHeight(); gl::clear( Color::black() ); if(mIsStereoscopic) { glPushAttrib( GL_VIEWPORT_BIT ); // render left eye gl::setViewport( Area(0, 0, w, h) ); mCamera.enableStereoLeft(); gl::pushMatrices(); gl::setMatrices( mCamera.getCamera() ); { // draw background mBackground.draw(); // draw grid if(mIsGridVisible) mGrid.draw(); // draw stars mStars.draw(); // draw constellations if(mIsConstellationsVisible) { mConstellations.draw(); mConstellationLabels.draw(); } // draw labels if(mIsLabelsVisible) mLabels.draw(); } gl::popMatrices(); // draw user interface mUserInterface.draw(); // render right eye gl::setViewport( Area(w, 0, w * 2.0f, h) ); mCamera.enableStereoRight(); gl::pushMatrices(); gl::setMatrices( mCamera.getCamera() ); { // draw background mBackground.draw(); // draw grid if(mIsGridVisible) mGrid.draw(); // draw stars mStars.draw(); // draw constellations if(mIsConstellationsVisible) { mConstellations.draw(); mConstellationLabels.draw(); } // draw labels if(mIsLabelsVisible) mLabels.draw(); } gl::popMatrices(); // draw user interface mUserInterface.draw(); glPopAttrib(); } else { mCamera.disableStereo(); gl::pushMatrices(); gl::setMatrices( mCamera.getCamera() ); { // draw background mBackground.draw(); // draw grid if(mIsGridVisible) mGrid.draw(); // draw stars mStars.draw(); // draw constellations if(mIsConstellationsVisible) { mConstellations.draw(); mConstellationLabels.draw(); } // draw labels if(mIsLabelsVisible) mLabels.draw(); } gl::popMatrices(); // draw user interface mUserInterface.draw(); } // fade in at start of application gl::enableAlphaBlending(); double t = math<double>::clamp( mTimer.getSeconds() / 3.0, 0.0, 1.0 ); float a = ci::lerp<float>(1.0f, 0.0f, (float) t); if( a > 0.0f ) { gl::color( ColorA(0,0,0,a) ); gl::drawSolidRect( getWindowBounds() ); } gl::disableAlphaBlending(); }