Skip to content

Instantly share code, notes, and snippets.

@arturoc
Last active December 20, 2015 08:19
Show Gist options
  • Save arturoc/6100152 to your computer and use it in GitHub Desktop.
Save arturoc/6100152 to your computer and use it in GitHub Desktop.
// TEST 0.8
#include "testApp.h"
//--------------------------------------------------------------
void testApp::setup() {
ofBackground(0, 0, 0);
bricksHigh = 0;
bricksWide = 0;
totalBricks = 0;
ofSetVerticalSync(false);
//ofSetFrameRate(60);
//ofSetVerticalSync(false);
ofSetupScreenPerspective(ofGetWidth(), ofGetHeight(), 65, 0.2f, 1000.0f);
createRandomObjects();
hugeVbo.setUsage(GL_STREAM_DRAW);
}
//--------------------------------------------------------------
void testApp::update() {
float now = ofGetElapsedTimef();
int boxVertices = box.getNumVertices();
vector<ofFloatColor> & colors = hugeVbo.getColors();
for (int i=0; i<m_boxes.size(); i++) {
Box & box = m_boxes[i];
float posZ = box.pos.z;
posZ *= -1;
ofFloatColor c(sinf(now * i / 10 * posZ)*10*(posZ+1)/100/255.f, sinf(now * i / 10 * posZ)*196*(posZ+1)/100/255.f, sinf(now * i / 10 * posZ)*195 *(posZ+1)/10/255.f);
for(int j=0;j<boxVertices;j++){
colors[i*boxVertices+j].set(c.r,c.g,c.b);
}
}
}
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++) {
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(Box(position, (brickWidth-2) * scaling, x, y, xScale, yScale));
box = ofMesh::box(xScale*20,yScale*20,40,2,2,2);
for(int i=0;i<box.getNumVertices();i++){
box.getVertices()[i] += position;
}
hugeVbo.append(box);
} // end x
} // end y
zRows++;
} // end z
hugeVbo.getColors().resize(hugeVbo.getNumVertices());
}
void testApp::drawObjects() {
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
hugeVbo.draw();
/*for (int i=0; i<m_boxes.size(); i++) {
Box & box = m_boxes[i];
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);
//ofDrawBox(box->pos,box->xScale*20, box->yScale*20, 40.0);
ofDrawBox(box.pos,box.xScale*20, box.yScale*20, 40.0);
}*/
glDisable(GL_CULL_FACE);
}
//--------------------------------------------------------------
void testApp::draw() {
ofSetColor(255, 255, 255);
ofEnableDepthTest();
drawObjects();
ofDisableDepthTest();
ofSetColor(255);
ofDrawBitmapString( "FPS: " + ofToString( ofGetFrameRate() ) + " Primitivies: " + ofToString(m_boxes.size()),20,20);
}
//--------------------------------------------------------------
void testApp::keyPressed(int key){
}
//--------------------------------------------------------------
void testApp::keyReleased(int key){
}
//--------------------------------------------------------------
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 {
ofVec3f pos;
float size;
float xScale;
float yScale;
int x;
int y;
//ofVboMesh 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)
{
}
};
class testApp : 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);
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 drawObjects();
vector<Box> m_boxes;
int bricksWide;
int bricksHigh;
int totalBricks;
ofVboMesh hugeVbo;
int zRows;
ofMesh box;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment