Skip to content

Instantly share code, notes, and snippets.

@joshuajnoble
Created December 25, 2013 20:13
Show Gist options
  • Save joshuajnoble/8126521 to your computer and use it in GitHub Desktop.
Save joshuajnoble/8126521 to your computer and use it in GitHub Desktop.
mat for transforms for instancing - can't test on OSX so may not work :/
#version 150
in mat4 transformMatrix;
in vec4 position;
uniform mat4 modelViewProjectionMatrix;
void main()
{
mat4 mvp = modelViewProjectionMatrix * transformMatrix;
gl_Position = mvp * position;
}
GLuint matInstanceIndex;
//--------------------------------------------------------------
void testApp::setup(){
// initialize variables:
isShaderDirty = true; // this flag will tell us whether to reload our shader from disk.
// this allows you to change your shaders without having to restart
// your app. we'll set it up so that pressing the SPACE key on your
// keyboard will reload the shader.
// initialize screen, lock framerate to vsync:
ofSetFrameRate(0);
ofSetVerticalSync(true);
// generate a box vboMesh from a primitive.
ofBoxPrimitive tmpBox;
// set the size to be 2 units.
tmpBox.set(2);
mVboBox = tmpBox.getMesh();
mShdInstanced = ofPtr<ofShader>(new ofShader());
mShdInstanced->load("shaders/instanced.vert", "shaders/instanced.frag");
int pos = mShdInstanced->getAttributeLocation("transformMatrix");
glGenBuffers(1, &matInstanceIndex);
glBindBuffer(GL_ARRAY_BUFFER, matInstanceIndex);
for (unsigned int i = 0; i < 4 ; i++) {
glEnableVertexAttribArray(pos + i);
glVertexAttribPointer(pos + i, 4, GL_FLOAT, GL_FALSE, sizeof(GLfloat) * 4 * 4, (const GLvoid*)(sizeof(GLfloat) * i * 4));
glVertexAttribDivisor(pos + i, 1);
}
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
//--------------------------------------------------------------
void testApp::update(){
}
//--------------------------------------------------------------
void testApp::draw(){
ofMatrix4x4 mats[4];
mats[0].makeRotationMatrix( ofVec3f(0, 0, 0), ofVec3f(0, 20, 0));
mats[0].translate(0, 50, 50);
mats[1].makeRotationMatrix( ofVec3f(0, 0, 0), ofVec3f(0, 60, 0));
mats[0].translate(0, 100, 100);
mats[2].makeRotationMatrix( ofVec3f(0, 0, 0), ofVec3f(0, 100, 0));
mats[0].translate(0, 150, 150);
mats[3].makeRotationMatrix( ofVec3f(0, 0, 0), ofVec3f(0, 140, 0));
mats[0].translate(0, 200, 200);
float matdata[16 * 4];
for( int i = 0; i < 3; i++)
{
memcpy( (void*) &matdata[i * 16], (void*) mats[i].getPtr(), 16 * sizeof(float));
}
mShdInstanced->begin();
mVboBox.getVbo().setAttributeData(matInstanceIndex, &matdata[0], 0, 16 * 4 * sizeof(float), GL_DYNAMIC_DRAW, 4);
mVboBox.drawInstanced(OF_MESH_FILL, 4);
mShdInstanced->end();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment