Skip to content

Instantly share code, notes, and snippets.

@rettuce
Last active April 7, 2016 01:54
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 rettuce/2e69e0df319b57e72ddf9c2bcbdc24bb to your computer and use it in GitHub Desktop.
Save rettuce/2e69e0df319b57e72ddf9c2bcbdc24bb to your computer and use it in GitHub Desktop.
VBO + InterleavedArrays Drawing Test. use openFrameworks.
#include "ofMain.h"
#include "BuildSetting.h"
#define BUFFER_OFFSET(bytes) ((GLubyte *)NULL + (bytes))
#define countof(array) (sizeof(array)/sizeof((array)[0]))
class ofApp : public ofBaseApp
{
GLuint buffers[2]; // vert+normal+texcord, index
struct CUSTOM_VERTEX
{
GLfloat tx, ty;
GLfloat r, g, b, a;
GLfloat nx, ny, nz;
GLfloat x, y, z;
};
vector<CUSTOM_VERTEX> cverts;
int psize = 600000;
float radius = 500.;
float trisize = 20;
vector<ofVec3f> verts;
vector<ofVec3f> norms;
vector<ofVec2f> texs;
vector<ofFloatColor> colors;
vector<int> indexs;
int mode = -1;
ofEasyCam cam;
public:
void keyPressed(int key)
{
if(key=='0') mode=0;
if(key=='1') mode=1;
if(key=='2') mode=2;
if(key=='3') mode=3;
}
void setup()
{
ofSetVerticalSync(false);
ofBackground(0);
dataset();
setups3();
}
void dataset()
{
for (int i=0; i<psize; i+=3)
{
ofVec3f pos = ofVec3f(ofRandomf(), ofRandomf(), ofRandomf()).normalize() * radius;
verts.push_back( pos + ofVec3f(ofRandomf(), ofRandomf(), ofRandomf())*trisize );
verts.push_back( pos + ofVec3f(ofRandomf(), ofRandomf(), ofRandomf())*trisize );
verts.push_back( pos + ofVec3f(ofRandomf(), ofRandomf(), ofRandomf())*trisize );
norms.push_back( ofVec3f(0,1,0) );
norms.push_back( ofVec3f(0,1,0) );
norms.push_back( ofVec3f(0,1,0) );
texs.push_back( ofVec3f(0,0) );
texs.push_back( ofVec3f(1,0) );
texs.push_back( ofVec3f(1,1) );
indexs.push_back( i+0 );
indexs.push_back( i+1 );
indexs.push_back( i+2 );
ofFloatColor c = ofFloatColor(ofRandom(0.3,1), ofRandom(0.3,1), ofRandom(0.3,1), 1);
colors.push_back( c );
colors.push_back( c );
colors.push_back( c );
}
for (int i=0; i<verts.size(); i++)
{
CUSTOM_VERTEX cv = {
texs[i].x, texs[i].y,
colors[i].r, colors[i].g, colors[i].b, colors[i].a,
norms[i].x, norms[i].y, norms[i].z,
verts[i].x, verts[i].y, verts[i].z
};
cverts.push_back( cv );
}
}
void setups3()
{
glGenBuffers(2, buffers);
glBindBuffer(GL_ARRAY_BUFFER, buffers[0]);
glBufferData(GL_ARRAY_BUFFER, cverts.size() * sizeof(CUSTOM_VERTEX), &cverts[0], GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffers[1]);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indexs.size()*sizeof(int), &indexs[0], GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
}
void update(){}
void draw()
{
glPushClientAttrib(GL_ALL_ATTRIB_BITS);
{
cam.begin();
{
ofEnableDepthTest();
if(mode==0) draws0();
if(mode==1) draws1();
if(mode==2) draws2();
if(mode==3) draws3();
}
cam.end();
}
glPopClientAttrib();
ofSetColor(255);
ofScale(2,2);
ofSetDrawBitmapMode( OF_BITMAPMODE_SIMPLE );
ofDrawBitmapString(ofToString(ofGetFrameRate()), 5, 15 );
ofDrawBitmapString( "Now PolygonsNum : " +ofToString(psize/3), 5, 30 );
string sm;
if(mode==0) sm = "Default oF ofDrawTriangle";
if(mode==1) sm = "glBegin( GL_TRIANGLES )";
if(mode==2) sm = "glDrawArrays( GL_TRIANGLES )";
if(mode==3) sm = "glInterleavedArrays + glDrawElements";
ofDrawBitmapString( "Now : " +sm, 5, 45 );
}
void draws0()
{
for (int i=0; i<psize; i+=3) {
ofSetColor(colors[i]);
ofDrawTriangle(verts[i+0], verts[i+1], verts[i+2]);
}
}
void draws1()
{
glBegin( GL_TRIANGLES );
for (int i=0; i<psize; i+=3) {
ofSetColor(colors[i]);
glVertex3fv( verts[i+0].getPtr() );
glVertex3fv( verts[i+1].getPtr() );
glVertex3fv( verts[i+2].getPtr() );
}
glEnd();
}
void draws2()
{
glEnableClientState( GL_VERTEX_ARRAY );
glEnableClientState( GL_NORMAL_ARRAY );
glEnableClientState( GL_COLOR_ARRAY );
glEnableClientState( GL_TEXTURE_COORD_ARRAY );
{
glTexCoordPointer( 2, GL_FLOAT, 0, &texs[0] );
glColorPointer( 4, GL_FLOAT, 0, &colors[0] );
glNormalPointer( GL_FLOAT, 0, &norms[0] );
glVertexPointer( 3, GL_FLOAT, 0, &verts[0] );
}
glDrawArrays( GL_TRIANGLES, 0, psize );
glDisableClientState( GL_VERTEX_ARRAY );
glDisableClientState( GL_NORMAL_ARRAY );
glDisableClientState( GL_COLOR_ARRAY );
glDisableClientState( GL_TEXTURE_COORD_ARRAY );
}
// InterleavedArrays
void draws3()
{
glBindBuffer(GL_ARRAY_BUFFER, buffers[0]);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffers[1]);
glInterleavedArrays(GL_T2F_C4F_N3F_V3F, 0, BUFFER_OFFSET(0));
glDrawElements(GL_TRIANGLES, indexs.size(), GL_UNSIGNED_INT, BUFFER_OFFSET(0));
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
}
};
#include "ofAppGLFWWindow.h"
int main(int argc, const char** argv)
{
ofGLFWWindowSettings settings;
settings.width = PREVIEW_WIDTH;
settings.height = PREVIEW_HEIGHT;
shared_ptr<ofAppBaseWindow> mainWindow = ofCreateWindow(settings);
shared_ptr<ofApp> mainApp(new ofApp);
ofRunApp(mainWindow, mainApp);
ofRunMainLoop();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment