public
Created

  • Download Gist
testApp.cpp
C++
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
#include "testApp.h"
void addFace(vector<ofVec3f> &pos,vector<ofFloatColor> &color,vector<ofVec3f> &normal, vector<ofVec2f>&texcoord, ofVec3f a, ofVec3f b, ofVec3f c) {
ofVec3f _normal = ((b - a).cross(c - a)).normalize();
ofFloatColor _color = ofFloatColor::fromHsb(ofRandom(0.5,0.9),1.0,1.0);
pos.push_back(a);
pos.push_back(b);
pos.push_back(c);
color.push_back(_color);
color.push_back(_color);
color.push_back(_color);
normal.push_back(_normal);
normal.push_back(_normal);
normal.push_back(_normal);
ofVec2f offSet = ofVec2f (ofRandom(-50,50),ofRandom(-50,50));
texcoord.push_back(a+offSet);
texcoord.push_back(b+offSet);
texcoord.push_back(c+offSet);
 
// mesh.addNormal(normal);
// mesh.addColor(color);
// mesh.addTexCoord(a);
// mesh.addVertex(a);
//
// mesh.addNormal(normal);
// mesh.addColor(color);
// mesh.addTexCoord(b);
// mesh.addVertex(b);
//
// mesh.addNormal(normal);
// mesh.addColor(color);
// mesh.addTexCoord(c);
// mesh.addVertex(c);
}
 
//--------------------------------------------------------------
void addFace(vector<ofVec3f> &pos,vector<ofFloatColor> &color,vector<ofVec3f> &normal, vector<ofVec2f>&texcoord, ofVec3f a, ofVec3f b, ofVec3f c, ofVec3f d) {
addFace(pos,color,normal, texcoord , a, b, c);
addFace(pos,color,normal, texcoord , a, c, d);
}
void testApp::setupVBO(int step)
{
total = 0;
int width = ofGetWidth();
int height = ofGetHeight();
 
WIDTH = width*1.0/step;
HEIGHT = height*1.0/step;
pos.clear();
vbo.clear();
color.clear();
normal.clear();
vbo.clear();
int row = 0;
int col = 0;
for(int y = 0 ; y < height ;y+=step)
{
row++;
for (int x = 0; x < width; x+=step)
{
ofVec3f nw = ofVec3f( x, y , 0);
ofVec3f ne = ofVec3f( x + step, y, 0);
ofVec3f sw = ofVec3f( x, y + step, 0);
ofVec3f se = ofVec3f( x + step, y + step, 0);
if(col%2==0 && row%2==1 || col%2==1 && row%2==0)
{
nw = nw.getRotated(90,ofVec3f( x+step*0.5, y + step*0.5, 0), ofVec3f(0,0,1));
ne = ne.getRotated(90,ofVec3f( x+step*0.5, y + step*0.5, 0), ofVec3f(0,0,1));
sw = sw.getRotated(90,ofVec3f( x+step*0.5, y + step*0.5, 0), ofVec3f(0,0,1));
se = se.getRotated(90,ofVec3f( x+step*0.5, y + step*0.5, 0), ofVec3f(0,0,1));
}
addFace(pos,color,normal, texcoord ,nw,ne,se,sw);
total+=6;
col++ ;
}
}
vbo.setVertexData(pos.data(), total, GL_DYNAMIC_DRAW);
vbo.setTexCoordData(texcoord.data(), total, GL_DYNAMIC_DRAW);
// vbo.setColorData(color.data(), total, GL_DYNAMIC_DRAW);
vbo.setNormalData(normal.data(), total, GL_DYNAMIC_DRAW);
}
//--------------------------------------------------------------
void testApp::setup(){
ofEnableAlphaBlending();
setupVBO(20);
grabber.initGrabber(1280,720);
}
 
//--------------------------------------------------------------
void testApp::update(){
grabber.update();
float count = 0;
float count2 = 0;
for(int i = 0 ; i< total ;i+=3)
{
float a = sin((count-=1.1)+ofGetElapsedTimef());
float a2 = cos((count2+=0.07));//+ofGetElapsedTimef());
color[i].a = a;//*a2;
color[i+1].a = a*a2;
color[i+2].a = a*a2;
}
}
 
//--------------------------------------------------------------
void testApp::draw(){
ofBackground(0);
ofPushMatrix();
ofEnableAlphaBlending();
ofSetColor(255, 255, 255);
vbo.bind();
vbo.updateTexCoordData(texcoord.data(), total);
vbo.updateVertexData(pos.data(), total);
// vbo.updateColorData(color.data(), total);
vbo.updateNormalData(normal.data(), total);
grabber.getTextureReference().bind();
for(int i = 0 ; i< total ;i+=3)
{
vbo.draw(GL_TRIANGLE_FAN, i,3);
}
grabber.getTextureReference().unbind();
vbo.unbind();
ofPopMatrix();
}
 
//--------------------------------------------------------------
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){
setupVBO(x);
}
 
//--------------------------------------------------------------
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){
}
testApp.h
C++
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
#pragma once
 
#include "ofMain.h"
 
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);
ofVbo vbo;
vector<ofVec3f> pos;//[GRID_WIDTH*GRID_HEIGHT*LENGTH];
vector<ofVec3f> normal;//[GRID_WIDTH*GRID_HEIGHT*LENGTH];
vector<ofFloatColor> color;//[GRID_WIDTH*GRID_HEIGHT*LENGTH];
vector<ofVec2f> texcoord;//[GRID_WIDTH*GRID_HEIGHT*LENGTH];
ofVec3f center;
int total;
int WIDTH;
int HEIGHT;
int space;
ofVideoGrabber grabber;
void setupVBO(int step);
};

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.