Created
July 26, 2015 13:00
-
-
Save Morpheu5/d2c03b92d1340363e5fc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "cinder/app/AppNative.h" | |
#include "cinder/gl/gl.h" | |
#include "cinder/gl/Fbo.h" | |
#include "cinder/Perlin.h" | |
using namespace ci; | |
using namespace ci::app; | |
using namespace std; | |
#define IS_LANDSCAPE (getWindowWidth()/1.6f > getWindowHeight()) | |
#define WIDTH 320 | |
#define HEIGHT 200 | |
#define ASPECT 1.6f | |
class FBOTestApp : public AppNative { | |
gl::Fbo _fbo; | |
gl::Texture _texture; | |
int _x, _y; | |
Perlin noise; | |
public: | |
void prepareSettings(App::Settings *settings); | |
void setup(); | |
void update(); | |
void draw(); | |
void drawToFbo(); | |
}; | |
void FBOTestApp::prepareSettings(App::Settings *settings) { | |
// settings->enableHighDensityDisplay(); | |
} | |
void FBOTestApp::setup() { | |
gl::Fbo::Format format; | |
format.setSamples(0); | |
format.setTarget(GL_TEXTURE_2D); | |
format.enableMipmapping(false); | |
format.enableDepthBuffer(); | |
_fbo = gl::Fbo(WIDTH, HEIGHT, format); | |
setWindowSize(HEIGHT*2, WIDTH*2); | |
noise = Perlin(); | |
setFrameRate(30); | |
} | |
void FBOTestApp::update() { | |
float dx = 100 * noise.fBm((float)_x/WIDTH, 0, getElapsedSeconds()/100.0); | |
float dy = 100 * noise.fBm(0, (float)_y/HEIGHT, getElapsedSeconds()/100.0); | |
_x = round(dx + WIDTH/2); | |
_y = round(dy + HEIGHT/2); | |
} | |
void FBOTestApp::drawToFbo() { | |
gl::SaveFramebufferBinding bindingSaver; | |
_fbo.bindFramebuffer(); | |
gl::setViewport(_fbo.getBounds()); | |
gl::setMatricesWindow(_fbo.getSize()); | |
gl::color(ColorA(0, 0, 0, 0.1f)); | |
gl::drawSolidRect(_fbo.getBounds()); | |
gl::color(ColorA(1.0f, 1.0f, 1.0f)); | |
gl::drawSolidCircle(Vec2f(_x, _y), 20 - 10*cos(2*M_PI * 0.01 * getElapsedSeconds())); | |
} | |
void FBOTestApp::draw() { | |
gl::clear(Color(0,0,0)); | |
drawToFbo(); | |
Area a; | |
if(IS_LANDSCAPE) { | |
int h = getWindowHeight(); | |
int w = floor((float)h * ASPECT); | |
int d = floor((getWindowWidth() - w)/2.0); | |
a = Area(d, 0, d+w, h); | |
} else { | |
int w = getWindowWidth(); | |
int h = floor((float)w / ASPECT); | |
int d = floor((getWindowHeight() - h)/2.0); | |
a = Area(0, d, w, d+h); | |
} | |
gl::setViewport(a); | |
gl::Texture t(_fbo.getTexture()); | |
t.setMagFilter(GL_NEAREST); | |
gl::draw(t); | |
} | |
CINDER_APP_NATIVE( FBOTestApp, RendererGl(0) ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment