Introduction
openFrameworks introduction:
– http://www.openframeworks.cc/
– open-source creative coding toolkit for writing software
– provides many high-level classes and abstractions which are cross-platform for a collection of libraries including: OpenCV, Vector/Matrix Math, Graphics using OpenGL, Mesh, OBJ, VBO, Shaders, Audio/Video input/output/saving, Kinect, File/Directory access/manipulation, TCP/IP/UDP/OSC, Threading, Physics engine, Synthesizers, iPhone/Android integration (GPS/Compass/UI/OpenGL/Maps/Audio/Video), …
– project generator (v0072+) gets you started with a new openFrameworks project quickly.
Instructions
First use the project generator to create a new openFrameworks project that we’ll open using XCode. The project generator is an application which creates an XCode project and sets up all the openFrameworks libraries and path settings within the project so we don’t have to. You can find it inside your openFrameworks download inside the directory, “projectGenerator”. Give your sketch any name, and click the “GENERATE” button. We won’t need to use any other features of the project generator at this time.
I’ve included the source code for a few different programs below. To try these, copy and paste the testApp.h and testApp.cpp files below into the testApp.h and testApp.cpp files inside of your newly created XCode project. Try running each of the individual programs one at a time. To get your program running inside of XCode, after you have copied the code so your testApp.h and testApp.cpp files look exactly as below, click the large Play button in XCode. If you have any problems at this point, first make sure the “Scheme” is set to the name of your project, and also have a look at the images above to make sure the SDK has been set (see images above with black arrows indicating where to click!). Have a read through the code line by line, changing anything you wish to play with, and come ready with questions for next week.
basic program:
testApp.h
#pragma once
#include "ofMain.h"
class testApp : public ofBaseApp{
public:
// declaration of functions
void setup();
void update();
void draw();
};
testApp.cpp
#include "testApp.h"
// here we "define" the methods we "declared" in the "testApp.h" file
// i get called once
void testApp::setup(){
// do some initialization
// set the size of the window
ofSetWindowShape(250, 250);
// the rate at which the program runs (FPS)
ofSetFrameRate(30);
}
// i get called in a loop that runs until the program ends
void testApp::update(){
}
// i also get called in a loop that runs until the program ends
void testApp::draw(){
}
mouse input:
testApp.h
#pragma once
#include "ofMain.h"
class testApp : public ofBaseApp{
public:
// declaration of functions
void setup();
void update();
void draw();
};
testApp.cpp
#include "testApp.h"
// here we "define" the methods we "declared" in the "testApp.h" file
// i get called once
void testApp::setup(){
// do some initialization
// set the size of the window
ofSetWindowShape(250, 250);
// the rate at which the program runs (FPS)
ofSetFrameRate(30);
// don't clear the background on each update/draw loop
// ofSetBackgroundAuto(false);
}
// i get called in a loop that runs until the program ends
void testApp::update(){
}
// i also get called in a loop that runs until the program ends
void testApp::draw(){
// set the size of the circle
float radius = 3.0f;
// draw a circle at the mouse position
ofCircle(mouseX, mouseY, radius);
// note:
// the two variables, mouseX and mouseY are declared
// in the base (parent) class
}
drawing a line:
testApp.h
#pragma once
#include "ofMain.h"
class testApp : public ofBaseApp{
public:
// declaration of functions
void setup();
void update();
void draw();
int counter;
};
testApp.cpp
#include "testApp.h"
// here we "define" the methods we "declared" in the "testApp.h" file
// i get called once
void testApp::setup(){
// do some initialization
// set the size of the window
ofSetWindowShape(250, 250);
// the rate at which the program runs (FPS)
ofSetFrameRate(30);
// we are going to increment this variable each "frame"
counter = 0;
}
// i get called in a loop that runs until the program ends
void testApp::update(){
// update some variables
counter = counter + 1;
}
// i also get called in a loop that runs until the program ends
void testApp::draw(){
ofBackground(0);
float amplitude = 100.0f;
ofSetColor(100, 100, 100);
ofLine(0, 125, 250, 125);
ofSetColor(200, 200, 200);
for (int i = 1; i < 250; i++) {
float x1 = (float)i / 250.0;
float y1 = amplitude * sin( x1 * 2.0*PI );
float x2 = (float)(i - 1) / 250.0;
float y2 = amplitude * sin( x2 * 2.0*PI );
ofLine(x1 * 250.0f, -y1 + 125, x2 * 250.0f, -y2 + 125);
}
}
audio input:
testApp.h
#pragma once
#include "ofMain.h"
class testApp : public ofBaseApp{
public:
// redeclaration of functions (declared in base class)
void setup();
void update();
void draw();
// redeclaration of two special functions which handle audio i/o (declared in base class)
void audioRequested(float *buf, int size, int ch);
void audioReceived(float *buf, int size, int ch);
// we will keep a pointer to our audio
float *audioInput;
int sampleRate, bufferSize;
};
testApp.cpp
#include "testApp.h"
// here we "define" the methods we "declared" in the "testApp.h" file
// i get called once
void testApp::setup(){
// do some initialization
// set the size of the window
ofSetWindowShape(250, 250);
// the rate at which the program runs (FPS)
ofSetFrameRate(30);
// setup the sound
sampleRate = 44100;
bufferSize = 250;
ofSoundStreamSetup(2, // output channels
1, // input channels
sampleRate, // how many samples (readings) per second
bufferSize, // size of each copy of audio
4); // latency of audio
// a variable to store our audio
audioInput = new float[bufferSize];
}
// i get called in a loop that runs until the program ends
void testApp::update(){
}
// i also get called in a loop that runs until the program ends
void testApp::draw(){
ofBackground(0);
float amplitude = 100.0f;
ofSetColor(100, 100, 100);
ofLine(0, 125, 250, 125);
ofSetColor(200, 200, 200);
for (int i = 1; i < 250; i++) {
float x1 = (float)i / 250.0;
float y1 = amplitude * audioInput[i]; //amplitude * sin( x1 * 2.0*PI );
float x2 = (float)(i - 1) / 250.0;
float y2 = amplitude * audioInput[i-1]; //amplitude * sin( x2 * 2.0*PI );
ofLine(x1 * 250.0f, -y1 + 125, x2 * 250.0f, -y2 + 125);
}
}
void testApp::audioRequested(float *buf, int size, int ch)
{
}
void testApp::audioReceived(float *buf, int size, int ch)
{
// copy the data into our variable, audioInput
memcpy(audioInput, buf, sizeof(float) * size);
}
camera input:
testApp.h
#pragma once
#include "ofMain.h"
class testApp : public ofBaseApp{
public:
// redeclaration of functions (declared in base class)
void setup();
void update();
void draw();
void keyPressed(int key);
ofVideoGrabber camera;
};
testApp.cpp
#include "testApp.h"
// here we "define" the methods we "declared" in the "testApp.h" file
// i get called once
void testApp::setup(){
// do some initialization
// set the size of the window
ofSetWindowShape(320, 240);
// the rate at which the program runs (FPS)
ofSetFrameRate(30);
// setup the camera
camera.initGrabber(320, 240);
}
// i get called in a loop that runs until the program ends
void testApp::update(){
camera.update();
}
// i also get called in a loop that runs until the program ends
void testApp::draw(){
ofBackground(0);
// draw the camera
camera.draw(0,0);
}
void testApp::keyPressed(int key)
{
switch (key) {
case 's':
camera.videoSettings();
break;
default:
break;
}
}

