#include "ofApp.h"
void ofApp::addFace(ofMesh& mesh, const glm::vec3& a, const glm::vec3& b, const glm::vec3& c) {
mesh.addVertex(a);
mesh.addVertex(b);
mesh.addVertex(c);
}
void ofApp::addFace(ofMesh& mesh, const glm::vec3& a, const glm::vec3& b, const glm::vec3& c, const glm::vec3& d) {
addFace(mesh, a, b, c);
addFace(mesh, a, c, d);
}
void ofApp::addTexCoords(ofMesh& mesh, const glm::vec2& a, const glm::vec2& b, const glm::vec2& c) {
mesh.addTexCoord(a);
mesh.addTexCoord(b);
mesh.addTexCoord(c);
}
void ofApp::addTexCoords(ofMesh& mesh, const glm::vec2& a, const glm::vec2& b, const glm::vec2& c, const glm::vec2& d) {
addTexCoords(mesh, a, b, c);
addTexCoords(mesh, a, c, d);
}
glm::vec3 ofApp::getVertexFromImg(ofImage& img, int x, int y) {
ofColor color = img.getColor(x, y);
if(color.a > 0) {
float z = ofMap(color.a, 0, 255, -480, 480);
return glm::vec3(x - img.getWidth() / 2, y - img.getHeight() / 2, z);
} else {
return glm::vec3(0, 0, 0);
}
}
void ofApp::setup() {
#ifdef TARGET_OPENGLES
ofEnableNormalizedTexCoords();
#endif
img.load("linzer.png");
mesh.setMode(OF_PRIMITIVE_TRIANGLES);
int skip = 10;
int width = img.getWidth();
int height = img.getHeight();
glm::vec2 imageSize(width,height);
glm::vec3 zero(0, 0, 0);
for(int y = 0; y < height - skip; y += skip) {
for(int x = 0; x < width - skip; x += skip) {
glm::vec3 nw = getVertexFromImg(img, x, y);
glm::vec3 ne = getVertexFromImg(img, x + skip, y);
glm::vec3 sw = getVertexFromImg(img, x, y + skip);
glm::vec3 se = getVertexFromImg(img, x + skip, y + skip);
glm::vec2 nwi(x, y);
glm::vec2 nei(x + skip, y);
glm::vec2 swi(x, y + skip);
glm::vec2 sei(x + skip, y + skip);
if(nw != zero && ne != zero && sw != zero && se != zero) {
addFace(mesh, nw, ne, se, sw);
if(ofGetUsingNormalizedTexCoords()) {
nwi /= imageSize;
nei /= imageSize;
sei /= imageSize;
swi /= imageSize;
}
addTexCoords(mesh, nwi, nei, sei, swi);
}
}
}
vboMesh = mesh;
}
void ofApp::update() {
}
void ofApp::draw() {
ofBackgroundGradient(ofColor(64), ofColor(0));
cam.begin();
ofEnableDepthTest();
ofRotateYDeg(ofGetElapsedTimef() * 30);
ofScale(1, -1, 1);
ofScale(.5, .5, .5);
img.bind();
int n = 5;
glm::vec2 spacing(img.getWidth(), img.getHeight());
ofTranslate(-spacing.x * n / 2, -spacing.y * n / 2, 0);
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
ofPushMatrix();
ofTranslate(i * spacing.x, j * spacing.y);
ofTranslate(spacing.x / 2, spacing.y / 2);
if(ofGetKeyPressed()) {
vboMesh.draw();
} else {
mesh.draw();
}
ofPopMatrix();
}
}
img.unbind();
ofDisableDepthTest();
cam.end();
ofDrawBitmapString(ofToString((int) ofGetFrameRate()) + " fps", 10, 20);
ofDrawBitmapString("Hold any key for ofVboMesh mode.", 10, 40);
string mode = (ofGetKeyPressed() ? "ofVboMesh" : "ofMesh");
ofDrawBitmapString("Current mode: " + mode, 10, 60);
}
void ofApp::keyPressed(int key){}
void ofApp::keyReleased(int key){
if(key == ' ') {
ofToggleFullscreen();
}
}
void ofApp::mouseMoved(int x, int y){
}
void ofApp::mouseDragged(int x, int y, int button){
}
void ofApp::mousePressed(int x, int y, int button){
}
void ofApp::mouseReleased(int x, int y, int button){
}
void ofApp::mouseEntered(int x, int y){
}
void ofApp::mouseExited(int x, int y){
}
void ofApp::windowResized(int w, int h){
}
void ofApp::gotMessage(ofMessage msg){
}
void ofApp::dragEvent(ofDragInfo dragInfo){
}
Comments