Skip to content

Instantly share code, notes, and snippets.

@roymacdonald
Created June 28, 2015 04:27
Show Gist options
  • Save roymacdonald/6bcc930cb88282a5412e to your computer and use it in GitHub Desktop.
Save roymacdonald/6bcc930cb88282a5412e to your computer and use it in GitHub Desktop.
#include "ofMain.h"
class ofApp : public ofBaseApp{
public:
ofRectangle viewport;
bool bUseOrtho, bUseCamera;
ofCamera cam;
//--------------------------------------------------------------
void setup(){
randomizeViewport();
cam.setVFlip(true);
bUseOrtho = true;
bUseCamera = true;
setCamOrtho();
}
//--------------------------------------------------------------
void randomizeViewport(){
viewport.x = ofRandom(ofGetWidth()*0.5f);
viewport.y = ofRandom(ofGetHeight()*0.5f);
viewport.width = ofRandom(ofGetWidth()-viewport.x);
viewport.height = ofRandom(ofGetHeight()-viewport.y);
cam.setPosition(0, 0, cam.getImagePlaneDistance(viewport));
}
//--------------------------------------------------------------
void draw(){
ofBackground(0);
ofSetColor(255);
ofNoFill();
ofDrawRectangle(viewport);
if (bUseCamera) {
ofPushMatrix();
cam.begin(viewport);
ofTranslate(-viewport.width/2, -viewport.height/2);
}else{
ofPushView();
ofViewport(viewport);
if(bUseOrtho){
ofSetupScreenOrtho();
}else{
ofSetupScreenPerspective();
}
}
ofFill();
int r = 20;
ofSetColor(ofColor::red);
ofDrawCircle(r,r,r);
ofSetColor(ofColor::cyan);
ofDrawCircle(viewport.width-r, viewport.height -r, r);
ofSetColor(ofColor::yellow);
ofDrawCircle(viewport.width-r, r, r);
ofSetColor(ofColor::darkGreen);
ofDrawCircle(r, viewport.height -r, r);
ofSetColor(ofColor::white);
ofDrawCircle(viewport.width/2, viewport.height/2, r);
if (bUseCamera) {
cam.end();
ofPopMatrix();
}else{
ofPopView();
}
string s = (string)(bUseCamera?"USING CAMERA":"NOT USING CAMERA") + "\n"+ (string)(bUseOrtho?"ORTHO":"PERSPECTIVE");
if (bUseCamera) {
s += "\nCam Projection Matrix:\n"+ofToString(cam.getProjectionMatrix());
s += "\n\nCam Model View Matrix:\n"+ofToString(cam.getModelViewMatrix());
}
ofDrawBitmapString(s, 20, 20);
}
//--------------------------------------------------------------
void setCamOrtho(){
if (bUseCamera) {
if (bUseOrtho) {
cam.enableOrtho();
}else{
cam.disableOrtho();
}
}
}
//--------------------------------------------------------------
void keyReleased(int key){
if(key == ' '){
bUseOrtho ^= true;
setCamOrtho();
}else if(key == 'c'){
bUseCamera ^=true;
setCamOrtho();
}else if(key == 'r'){
randomizeViewport();
}
}
};
//========================================================================
int main( ){
ofSetupOpenGL(1024,768, OF_WINDOW); // <-------- setup the GL context
// this kicks off the running of my app
// can be OF_WINDOW or OF_FULLSCREEN
// pass in width and height too:
ofRunApp( new ofApp());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment