Catégories
Liens
Ceci est une ancienne révision du document !
Pour commencer, il faut trois fichiers : main.cpp, ofApp.h et ofApp.cpp. Ces fichiers sont générés par le ProjectGenerator.
#include "ofMain.h" #include "ofApp.h" int main( ){ ofSetupOpenGL(700,700,OF_WINDOW); ofRunApp(new ofApp()); }
#pragma once #include "ofMain.h" class ofApp : public ofBaseApp{ public: void setup(); void update(); void draw(); void keyPressed(int key); void keyReleased(int key); void mouseMoved(int x, int y ); void mouseDragged(int x, int y, int button); void mousePressed(int x, int y, int button); voi mouseReleased(int x, int y, int button); void windowResized(int w, int h); void dragEvent(ofDragInfo dragInfo); void gotMessage(ofMessage msg); };
#include "ofApp.h" int myCircleX = 250; int myCircleY = 100; int myCircleRadius = 50; int red = 255; bool bg = true; void ofApp::setup(){ ofSetRectMode(OF_RECTMODE_CENTER); } void ofApp::update(){ } void ofApp::draw(){ if (bg) ofBackground(0); ofSetColor(255); ofNoFill(); ofRect(100, 100, 100, 100); ofFill(); ofSetColor(red, 255, 255); ofCircle(myCircleX, myCircleY, myCircleRadius); ofSetColor(255); ofNoFill(); ofEllipse(400, 100, 80, 100); ofTriangle(500, 150, 550, 50, 600, 150); ofLine(700, 50, 700, 150); if (ofGetMousePressed(OF_MOUSE_BUTTON_LEFT)) { ofFill(); float randomColor = ofRandom(50, 255); ofSetColor(randomColor); ofRect(ofGetMouseX(), ofGetMouseY(), 50, 50); } if (ofGetMousePressed(OF_MOUSE_BUTTON_RIGHT)) { bg = !bg; ofSetBackgroundAuto(bg); } } void ofApp::keyPressed(int key){ if (key == 's') { glReadBuffer(GL_FRONT); // HACK: only needed on windows ofSetAutoBackground(false) ofSaveScreen("savedScreenshot_"+ofGetTimestampString()+".png"); } } void ofApp::mouseMoved(int x, int y ){ float distance = ofDist(myCircleX, myCircleY, x, y); if(distance < myCircleRadius){ red=0; } else { red=255; } } void ofApp::mousePressed(int x, int y, int button){ float distance = ofDist(myCircleX, myCircleY, x, y); if(distance < myCircleRadius){ myCircleRadius=myCircleRadius*1.1; } }
// From Fleeing Triangle Brush : ofBook (https://github.com/openframeworks/ofBook). // Copyright (c) 2014 Michael Hadley, mikewesthad.com #include "ofApp.h" void ofApp::setup(){ ofSetFrameRate(60); ofSetBackgroundAuto(false); ofBackground(0); } void ofApp::update(){ } void ofApp::draw(){ if (ofGetMousePressed(OF_MOUSE_BUTTON_LEFT)) { int numTriangles = 10; int minOffset = 5; int maxOffset = 70; int alpha = 150; for (int t=0; t<numTriangles; ++t) { float offsetDistance = ofRandom(minOffset, maxOffset); // Distance ofVec2f mousePos(ofGetMouseX(), ofGetMouseY()); // Mouse //Triangle at the origin (0,0) that points to the right ofVec2f p1(0, 6.25); ofVec2f p2(25, 0); ofVec2f p3(0, -6.25); // Rotation degrees float rotation = ofRandom(360); p1.rotate(rotation); p2.rotate(rotation); p3.rotate(rotation); ofVec2f triangleOffset(offsetDistance, 0.0); triangleOffset.rotate(rotation); p1 += mousePos + triangleOffset; p2 += mousePos + triangleOffset; p3 += mousePos + triangleOffset; ofColor aqua(0, 252, 255, alpha); ofColor purple(198, 0, 205, alpha); ofColor inbetween = aqua.getLerped(purple, ofRandom(1.0)); ofSetColor(inbetween); ofTriangle(p1, p2, p3); } } if (ofGetMousePressed(OF_MOUSE_BUTTON_RIGHT)) { ofBackground(0); // Erase the screen with a black background } } void ofApp::keyPressed(int key){ if (key == 's') { glReadBuffer(GL_FRONT); // HACK for windows ofSaveScreen("savedScreenshot_"+ofGetTimestampString()+".png"); } }
#include "ofApp.h" int ROWS; int COLS; int posX; int posY; float percent; float GRIDX; float GRIDY; float dx, dy; float angle; ofVec2f p1; ofVec2f p2; void ofApp::setup(){ ofBackground(0); ofSetFrameRate(30); ROWS = 80; COLS = ROWS; GRIDX = float(ofGetScreenWidth() / COLS); GRIDY = float(ofGetScreenHeight() / ROWS); ofSetLineWidth(2); percent=1; p1.set(0, -GRIDY*percent); p2.set(0, GRIDY*percent); } void ofApp::update(){ } void ofApp::draw(){ for (int rows=0; rows<ROWS; rows++) { for (int cols=0; cols<COLS; cols++) { ofPushMatrix(); posX = cols * GRIDX - GRIDX/2; posY = rows * GRIDY + GRIDY/2; ofTranslate(posX, posY); dx = posX - mouseX; dy = posY - mouseY; angle = atan2(dy, dx); ofRotateZ(angle * RAD_TO_DEG + 90); //ofRotateZ(ofGetFrameNum()); ofLine(p1,p2); ofPopMatrix(); } } } void ofApp::keyPressed(int key){ if (key == 's') { glReadBuffer(GL_FRONT); // HACK for windows ofSaveScreen("savedScreenshot_"+ofGetTimestampString()+".png"); } }
#include "ofApp.h" #define NUM_CIRCLES 500 float circleX[NUM_CIRCLES]; float circleY[NUM_CIRCLES]; float circleRad[NUM_CIRCLES]; int circleR[NUM_CIRCLES]; int circleG[NUM_CIRCLES]; int circleB[NUM_CIRCLES]; void ofApp::setup(){ ofSetFrameRate(24); for(int i=0; i<NUM_CIRCLES; i++) { circleX[i] = ofRandom(0, ofGetWidth()); circleY[i] = ofRandom(0, ofGetHeight()); circleRad[i] = ofRandom(10, 40); circleR[i] = ofRandom(0, 255); circleG[i] = ofRandom(0, 255); circleB[i] = ofRandom(0, 255); } } void ofApp::update(){ for(int i=0; i<NUM_CIRCLES; i++) { circleX[i] += ofRandom(-1,1); circleY[i] += ofRandom(-1,1); } } void ofApp::draw(){ for(int i=0; i<NUM_CIRCLES; i++) { ofSetColor(circleR[i], circleG[i], circleB[i]); ofCircle(circleX[i], circleY[i], circleRad[i]); } } void ofApp::keyPressed(int key){ if (key == 's') { glReadBuffer(GL_FRONT); // HACK for windows ofSaveScreen("savedScreenshot_"+ofGetTimestampString()+".png"); } }
#pragma once #include "ofMain.h" #include "ofBall.h" class ofApp : public ofBaseApp{ public: void setup(); void update(); void draw(); void keyPressed(int key); void mousePressed(int x, int y, int button); vector<ofBall> balls; };
#include "ofApp.h" void ofApp::setup(){ for( int i = 0; i < 10; i=i+1){ balls.push_back(ofBall()); } ofBackground(255); } void ofApp::update(){ for( int i = 0; i < balls.size(); i=i+1){ balls[i].update(); } } void ofApp::draw(){ for( int i = 0; i < balls.size(); i=i+1){ balls[i].draw(); } } void ofApp::keyPressed(int key){ if (key == 's') { glReadBuffer(GL_FRONT); // HACK for windows ofSaveScreen("savedScreenshot_"+ofGetTimestampString()+".png"); } } void ofApp::mousePressed(int x, int y, int button){ balls.push_back(ofBall()); }
// ofBall.h // Created by rux on 3/31/14. #ifndef _OF_BALL #define _OF_BALL #include "ofMain.h" class ofBall { // place public functions or variables declarations here public: ofBall(); // constructor // methods, equivalent to specific functions of your class objects void update(); // update method, used to refresh your objects properties void draw(); // draw method, this where you'll do the object's drawing // variables float x; // position float y; float speedY; // speed and direction float speedX; int dim; // size ofColor color; private: // place private functions or variables declarations here }; // don't forget the semicolon!! #endif
#include "ofBall.h" ofBall::ofBall(){ x = ofRandom(0, ofGetWidth()); // give some random positioning y = ofRandom(0, ofGetHeight()); speedX = ofRandom(-10, 10); // and random speed and direction speedY = ofRandom(-10, 10); dim = ofRandom(5, 30);; color.set(ofRandom(255), ofRandom(255), ofRandom(255)); } void ofBall::update(){ if(x < 0 ){ x = 0; speedX *= -1; } else if(x > ofGetWidth()){ x = ofGetWidth(); speedX *= -1; } if(y < 0 ){ y = 0; speedY *= -1; } else if(y > ofGetHeight()){ y = ofGetHeight(); speedY *= -1; } x+=speedX; y+=speedY; } void ofBall::draw(){ ofSetColor(color); ofCircle(x, y, dim); }