Skip to content

Instantly share code, notes, and snippets.

@danoli3
Created July 28, 2013 02:27
Show Gist options
  • Save danoli3/6097091 to your computer and use it in GitHub Desktop.
Save danoli3/6097091 to your computer and use it in GitHub Desktop.
openframeworks 0.8 develop ofBoxPrimitive efficiency vs 0.7.4
// TEST 0.8
#include "testApp.h"
struct Box {
ofVec3f pos;
float size;
float xScale;
float yScale;
int x;
int y;
//ofMesh * mesh;
// ofBoxPrimitive* box;
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)
{
// box = new ofBoxPrimitive(1, 1, 1, 1, 1, 1);
// mesh = box->getMeshPtr();
}
// ~Box() {
//
// if(box) {
// delete box;
// box = NULL;
// }
//// if(mesh) {
//// delete mesh;
//// mesh = NULL;
//// }
// }
//
// void draw() {
// if(mesh)
// mesh->draw();
// }
};
testApp::testApp()
{
}
//--------------------------------------------------------------
void testApp::setup() {
ofBackground(0, 0, 0);
bricksHigh = 0;
bricksWide = 0;
totalBricks = 0;
boxer = new ofBoxPrimitive();
// ofSetVerticalSync(true);
ofSetFrameRate(60);
ofSetupScreenPerspective(ofGetWidth(), ofGetHeight(), 65, 0.2f, 1000.0f);
createRandomObjects();
}
//--------------------------------------------------------------
void testApp::update() {
ofSetWindowTitle( "FPS: " + ofToString( ofGetFrameRate() ) + " Primitivies: " + ofToString(m_boxes.size()));
}
void testApp::createRandomObjects() {
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 = 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();
}
}
}
void testApp::drawObjects() {
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
if(m_boxes.size() != 0) {
boxer->setPosition(0, 0, 0);
boxer->set(1);
for (int i=0; i<m_boxes.size(); i++) {
Box * box = m_boxes[i];
if(box) {
boxer->setPosition(box->pos);
boxer->setScale(box->xScale*20, box->yScale*20, 40.0);
float posZ = box->pos.z;
posZ *= -1;
ofSetColor(sinf(ofGetElapsedTimef() * i / 10 * posZ)*128*(posZ+1)/100, sinf(ofGetElapsedTimef() * i / 10 * posZ)*196*(posZ+1)/100, sinf(ofGetElapsedTimef() * i / 10 * posZ)*195 *(posZ+1)/10);
boxer->drawFaces();
}
}
}
}
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();
}
}
if(boxer) {
delete boxer;
boxer = NULL;
}
}
//--------------------------------------------------------------
void testApp::draw() {
ofSetColor(255, 255, 255);
ofEnableDepthTest();
drawObjects();
}
//--------------------------------------------------------------
void testApp::keyPressed(int key){
}
//--------------------------------------------------------------
void testApp::keyReleased(int 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){
}
// Daniel Rosser
// this ofBoxPrimitive Test is to be run against openframeworks 0.8+
#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;
ofBoxPrimitive * boxer;
int zRows;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment