Skip to content

Instantly share code, notes, and snippets.

@roymacdonald
Created July 8, 2015 22:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save roymacdonald/e6fb4c391bd687337abd to your computer and use it in GitHub Desktop.
Save roymacdonald/e6fb4c391bd687337abd to your computer and use it in GitHub Desktop.
Level of detail mesh max vertices test for openFrameworks. All the code is inside the main.cpp file
#include "ofMain.h"
class LODMeshInstanced{
public:
ofVboMesh tile;
ofShader* shader;
int levels, totalWidth, resolution, numTiles;
vector<ofVec3f>tilePositions;
vector<float>tileScale;
//--------------------------------------------------------------
LODMeshInstanced(){
shader = NULL;
}
//--------------------------------------------------------------
~LODMeshInstanced(){
clear();
}
//--------------------------------------------------------------
void clear(){
numTiles = 0;
tilePositions.clear();
tileScale.clear();
levels = 0;
totalWidth = 0;
resolution = 0;
shader = NULL;
tile.clear();
}
//--------------------------------------------------------------
void setup( ofShader * shader,int levels, int totalWidth, int resolution){
clear();
this->levels = levels;
this->totalWidth = totalWidth;
this->resolution = resolution;
this->shader = shader;
ofPlanePrimitive plane(1, 1, resolution, resolution);
tile = plane.getMesh();
tile.setUsage(GL_STATIC_DRAW);
int size = totalWidth/ pow( 2, levels);
addTileAttributes(-size, -size, size);
addTileAttributes( 0, -size, size);
addTileAttributes( 0, 0, size);
addTileAttributes(-size, 0, size);
//*
for (int i = 0; i< levels ; i ++, size *= 2 ) {
addTileAttributes( -2 * size, -2 * size, size);
addTileAttributes( -2 * size, -size, size);
addTileAttributes( -2 * size, 0, size);
addTileAttributes( -2 * size, size, size);
addTileAttributes( -size, -2 * size, size);
addTileAttributes( -size, size, size);
addTileAttributes( 0, -2 * size, size);
addTileAttributes( 0, size, size);
addTileAttributes( size, -2 * size, size);
addTileAttributes( size, -size, size);
addTileAttributes( size, 0, size);
addTileAttributes( size, size, size);
}
shader->begin();
tile.getVbo().setAttributeData(shader->getAttributeLocation("tilePositions"), &tilePositions[0].x, 3, tilePositions.size(), GL_STATIC_DRAW, sizeof(ofVec3f));
tile.getVbo().setAttributeData(shader->getAttributeLocation("tileScale"), &tileScale[0], 1, tileScale.size(), GL_STATIC_DRAW, sizeof(float));
tile.getVbo().setAttributeDivisor(shader->getAttributeLocation("tilePositions"), 1);
tile.getVbo().setAttributeDivisor(shader->getAttributeLocation("tileScale"), 1);
shader->end();
}
//--------------------------------------------------------------
void addTileAttributes(int x, int y, float scale){
tilePositions.push_back(ofVec3f(x,y));
tileScale.push_back(scale);
numTiles++;
}
//--------------------------------------------------------------
void draw( bool bDrawWireframe = true, bool bUseShader = true ){
if (shader) {
if (bUseShader) {
shader->begin();
}
if (bDrawWireframe) {
tile.drawInstanced(OF_MESH_WIREFRAME, numTiles);
}else{
tile.drawInstanced(OF_MESH_FILL,numTiles);
}
if (bUseShader) {
shader->end();
}
}
}
};
class ofApp : public ofBaseApp{
public:
LODMeshInstanced lod;
ofEasyCam cam;
ofShader shader;
ofTexture mTexDepth;
bool bUseShader, bDrawWireframe;
//--------------------------------------------------------------
void setup(){
ofSetVerticalSync(true);
bUseShader = true;
bDrawWireframe = true;
shader.load("shaders/instanced_120");
lod.setup(&shader, 4, 1024, 64);
}
//--------------------------------------------------------------
void update(){
ofSetWindowTitle(ofToString(ofGetFrameRate(), 2));
}
//--------------------------------------------------------------
void draw(){
ofEnableDepthTest();
ofBackground(0);//Gradient(ofColor(18,33,54), ofColor(18,22,28));
ofSetColor(ofColor::white);
cam.begin();
lod.draw( bDrawWireframe, bUseShader);
cam.end();
ofDisableDepthTest();
string str="Draw Wireframe: " + (string)(bDrawWireframe?"TRUE":"FALSE")+"\n";
str +="Use Shader: " + (string)(bUseShader?"TRUE":"FALSE")+"\n";
str +="Tile resolution: " + ofToString(lod.resolution) + "\n";
str +="Tile Num Vertices: " + ofToString(lod.tile.getNumVertices()) + "\n";
str +="Tile instances: " + ofToString(lod.numTiles) + "\n";
ofDrawBitmapStringHighlight(str, 20, 20);
}
//--------------------------------------------------------------
void keyReleased(int key){
switch (key) {
case 's':
bUseShader ^= true;
break;
case 'w':
bDrawWireframe ^= true;
break;
default:
break;
}
}
};
//========================================================================
int main( ){
ofGLWindowSettings glWindowSettings;
glWindowSettings.setGLVersion(2, 1);
ofCreateWindow(glWindowSettings); // <-------- setup the GL context
ostringstream extStr;
extStr << (char*)glGetString(GL_EXTENSIONS); // first get all available extensions.
string extensionsAvailable = extStr.str();
if (ofStringTimesInString(extensionsAvailable, "GL_ARB_draw_instanced") == 0)
{
ofLogFatalError("App") << " GL_ARB_draw_instanced is needed for this example but it is not supported by your graphics card. Exiting App.";
return -1;
} else {
// GL_ARB_draw_instanced is available... so far so good.
// either one of these is needed, too:
if (ofStringTimesInString(extensionsAvailable, "GL_EXT_gpu_shader4") == 0 &&
ofStringTimesInString(extensionsAvailable, "NV_vertex_program4") == 0 )
{
ofLogFatalError("App") << " GL_EXT_gpu_shader4 or NV_vertex_program4 is needed for this example but it is not supported by your graphics card. Exiting App.";
return -1;
}
}
ofRunApp(new ofApp());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment