Created
July 28, 2013 02:30
-
-
Save danoli3/6097104 to your computer and use it in GitHub Desktop.
openframeworks 0.8 develop ofBoxPrimitive efficiency vs 0.7.4 (this one built for 0.7.4)
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
// TEST | |
#include "testApp.h" | |
struct Box { | |
ofVec3f pos; | |
float size; | |
float xScale; | |
float yScale; | |
int x; | |
int y; | |
Box(ofVec3f pos=ofVec3f(0.0f, 0.0f, 0.0f), float size=2.0f, int ax = 1, int ay = 1, float aXscale = 1.0f, float aYscale = 1.0f) : | |
pos(pos), | |
size(size), | |
xScale(aXscale), | |
yScale(aYscale), | |
x(ax), | |
y(ay) | |
{ | |
} | |
}; | |
testApp::testApp() | |
{ | |
}; | |
//-------------------------------------------------------------- | |
void testApp::setup() { | |
ofBackground(0, 0, 0); | |
bricksHigh = 0; | |
bricksWide = 0; | |
totalBricks = 0; | |
// ofSetVerticalSync(true); | |
ofSetFrameRate(60); | |
ofSetupScreenPerspective(ofGetWidth(), ofGetHeight(), ofOrientation(65), false, 0.2f, 1000.0f); | |
createRandomObjects(); | |
} | |
//-------------------------------------------------------------- | |
void testApp::update() { | |
ofSetWindowTitle( "FPS: " + ofToString( ofGetFrameRate() ) + " Primitivies: " + ofToString(m_boxes.size())); | |
} | |
void testApp::createRandomObjects() { | |
float brickHeight = ofGetHeight()/10; | |
float brickWidth = ofGetWidth()/10; | |
float brickDepth = brickHeight*10; | |
int brickCountX = 10; | |
int brickCountY = 10; | |
int boxCountZ = 10; | |
totalBricks = brickCountX * brickCountY; | |
bricksWide = brickCountX; | |
bricksHigh = brickCountX; | |
float xScale = brickWidth/brickHeight; | |
float yScale = brickHeight/brickWidth; | |
if(xScale < 1.0f) { | |
yScale = 1.0f; | |
} else if(yScale < 1.0f) { | |
xScale = 1.0f; | |
} | |
zRows = 1; | |
for(int z = 0; z < boxCountZ; z++) { | |
for(int y = 0; y < brickCountY; y++) { | |
for(int x = 0; x < brickCountX; x++) { | |
ofPushMatrix(); | |
float posZ = 0.0f; | |
float posX = 0.0f; | |
float posY = 0.0f; | |
float scaling = 1.0f; | |
posX = x * brickWidth + brickWidth/2 ; | |
posY = y * brickHeight - brickHeight/2 ; | |
scaling = 1.0; | |
ofVec3f position(posX * scaling, posY * scaling, -(zRows * 60) - 60); | |
m_boxes.push_back(new Box(position, (brickWidth-2) * scaling, x, y, xScale, yScale)); | |
ofPopMatrix(); | |
} // end x | |
} // end y | |
zRows++; | |
} // end z | |
} | |
void testApp::removeZBoxes() { | |
if(m_boxes.size() > 0 && zRows >= 0) { | |
int amountDelete = 10*10; | |
int size = m_boxes.size()-1; | |
int maxIndex = size-amountDelete; | |
if(maxIndex <= 0) | |
maxIndex = 0; | |
for(int i = size; i>=maxIndex; i--) { | |
Box* dbox = m_boxes[i]; | |
if(dbox != NULL) { | |
delete dbox; | |
dbox = NULL; | |
} | |
m_boxes.pop_back(); | |
} | |
zRows -= 1; | |
} | |
} | |
void testApp::addZBoxes() { | |
int boxCount = (ofGetWidth()/30) * (ofGetHeight()/30); | |
float brickHeight = ofGetHeight()/10; | |
float brickWidth = ofGetWidth()/10; | |
float brickDepth = brickHeight*10; | |
int brickCountX = 10; | |
int brickCountY = 10; | |
int boxCountZ = 10; | |
totalBricks = brickCountX * brickCountY; | |
bricksWide = brickCountX; | |
bricksHigh = brickCountX; | |
float xScale = brickWidth/brickHeight; | |
float yScale = brickHeight/brickWidth; | |
if(xScale < 1.0f) { | |
yScale = 1.0f; | |
} else if(yScale < 1.0f) { | |
xScale = 1.0f; | |
} | |
zRows++; | |
if(m_boxes.size() == 0) { | |
zRows = 0; | |
} | |
for(int y = 0; y < brickCountY; y++) { | |
for(int x = 0; x < brickCountX; x++) { | |
ofPushMatrix(); | |
float posZ = 0.0f; | |
float posX = 0.0f; | |
float posY = 0.0f; | |
float scaling = 1.0f; | |
posX = x * brickWidth + brickWidth/2 ; | |
posY = y * brickHeight - brickHeight/2 ; | |
scaling = 1.0; // perspective | |
ofVec3f position(posX * scaling, posY * scaling, -(zRows * 60) - 60); | |
m_boxes.push_back(new Box(position, (brickWidth-2) * scaling, x, y, xScale, yScale)); | |
ofPopMatrix(); | |
} // end x | |
} // end y | |
} | |
void testApp::drawObjects() { | |
glEnable(GL_CULL_FACE); | |
glCullFace(GL_BACK); | |
if(m_boxes.size() != 0) { | |
for (int i=0; i<m_boxes.size(); i++) { | |
Box * box = m_boxes[i]; | |
if(box) { | |
ofPushMatrix(); | |
ofTranslate(box->pos); | |
ofScale(box->xScale*20, box->yScale*20, 40.0); | |
float posZ = box->pos.z; | |
posZ *= -1; | |
ofSetColor(sinf(ofGetElapsedTimef() * i / 10 * posZ)*10*(posZ+1)/100, sinf(ofGetElapsedTimef() * i / 10 * posZ)*196*(posZ+1)/100, sinf(ofGetElapsedTimef() * i / 10 * posZ)*195 *(posZ+1)/10); | |
ofBox(1); | |
ofPopMatrix(); | |
} | |
} | |
} | |
} | |
testApp::~testApp() { | |
if(m_boxes.size() != 0) { | |
for(int i=m_boxes.size()-1; i>=0; i--) { | |
Box * del= m_boxes[i]; | |
if(del != NULL) { | |
delete del; | |
del = NULL; | |
} | |
m_boxes.pop_back(); | |
} | |
} | |
} | |
//-------------------------------------------------------------- | |
void testApp::draw() { | |
ofSetColor(255, 255, 255); | |
glEnable(GL_DEPTH_TEST); | |
drawObjects(); | |
} | |
//-------------------------------------------------------------- | |
void testApp::keyPressed(int key){ | |
} | |
//-------------------------------------------------------------- | |
void testApp::keyReleased(int key){ | |
//ofLogNotice(ofToString(key)); | |
if( key == 43 ) { // + | |
addZBoxes(); | |
} else if( key == 95 ) { // - | |
removeZBoxes(); | |
} | |
} | |
//-------------------------------------------------------------- | |
void testApp::mouseMoved(int x, int y){ | |
} | |
//-------------------------------------------------------------- | |
void testApp::mouseDragged(int x, int y, int button){ | |
} | |
//-------------------------------------------------------------- | |
void testApp::mousePressed(int x, int y, int button){ | |
} | |
//-------------------------------------------------------------- | |
void testApp::mouseReleased(int x, int y, int button){ | |
} | |
//-------------------------------------------------------------- | |
void testApp::windowResized(int w, int h){ | |
} | |
//-------------------------------------------------------------- | |
void testApp::gotMessage(ofMessage msg){ | |
} | |
//-------------------------------------------------------------- | |
void testApp::dragEvent(ofDragInfo dragInfo){ | |
} |
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
// 27/07/2013 - Daniel Rosser <[email protected]> | |
#pragma once | |
#include "ofMain.h" | |
struct Box; | |
class testApp : public ofBaseApp { | |
public: | |
testApp(); | |
~testApp(); | |
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); | |
void mouseReleased(int x, int y, int button); | |
void windowResized(int w, int h); | |
void dragEvent(ofDragInfo dragInfo); | |
void gotMessage(ofMessage msg); | |
void createRandomObjects(); | |
void removeZBoxes(); | |
void addZBoxes(); | |
void drawObjects(); | |
vector<Box*> m_boxes; | |
int bricksWide; | |
int bricksHigh; | |
int totalBricks; | |
int zRows; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment