Last active
August 9, 2018 02:51
-
-
Save mantissa/8220909 to your computer and use it in GitHub Desktop.
Demonstrates how to use perform rotations on instances drawn with ofVBOMesh::drawInstances().
Also uses per pixel lighting and color.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#version 120 | |
#extension GL_EXT_gpu_shader4 : require | |
varying vec3 vertex_light_position; | |
varying vec3 vertex_normal; | |
void main(){ | |
// calculate the shading based on the normal | |
float diffuse_value = max(dot(vertex_normal, vertex_light_position), 0.0); | |
diffuse_value = 0.1 + diffuse_value * 0.9; | |
// set the output color of our current pixel | |
gl_FragColor = vec4( vec4(gl_Color * diffuse_value).rgb, 1.0 ); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#version 120 | |
#extension GL_EXT_gpu_shader4 : require | |
uniform mat4 transformMatrix[200]; | |
uniform vec3 position[200]; | |
varying vec3 vertex_light_position; | |
varying vec3 vertex_normal; | |
void main(){ | |
// Calculate the normal value for this vertex, in world coordinates | |
// (multiply by gl_NormalMatrix and transformation matrix) | |
vertex_normal = normalize( mat3(transformMatrix[gl_InstanceID] ) * gl_NormalMatrix * gl_Normal); | |
// Calculate the light position for this vertex | |
vertex_light_position = normalize( gl_LightSource[0].position.xyz ); | |
// Set the front color to the color passed through with glColor | |
gl_FrontColor = gl_Color; | |
gl_BackColor = gl_Color; | |
// Multiply the shape coordinates by the transformation matrix | |
// Offset by the position | |
vec4 vPos = vec4( position[gl_InstanceID], 0 ) + ( transformMatrix[gl_InstanceID] * gl_Vertex ); | |
// Multiply by the model view and projection matrix | |
gl_Position = gl_ProjectionMatrix * gl_ModelViewMatrix * vPos; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
GLuint matInstanceIndex; | |
//-------------------------------------------------------------- | |
void testApp::setup(){ | |
// initialize screen, lock framerate to vsync | |
ofEnableDepthTest(); | |
ofSetFrameRate(0); | |
ofSetVerticalSync(true); | |
// generate a box vboMesh from a primitive. | |
ofBoxPrimitive tmpBox; | |
// set the size to be 50 units. | |
tmpBox.set(50); | |
mVboBox = tmpBox.getMesh(); | |
// load the shader | |
mShdInstanced = ofPtr<ofShader>(new ofShader()); | |
mShdInstanced->load("shaders/instanced.vert", "shaders/instanced.frag"); | |
} | |
//-------------------------------------------------------------- | |
void testApp::draw(){ | |
ofBackground(33); | |
// orientation of instances | |
ofMatrix4x4 mats[4]; | |
mats[0].makeRotationMatrix( ofGetFrameNum()+66, ofVec3f(0, 1, 0)); | |
mats[1].makeRotationMatrix( ofGetFrameNum(), ofVec3f(1, 0, 0)); | |
mats[2].makeRotationMatrix( ofGetFrameNum()+265, ofVec3f(0, 0, 1)); | |
mats[3].makeRotationMatrix( ofGetFrameNum()+220, ofVec3f(1, 1, 0)); | |
// position of instances | |
ofVec3f translations[4]; | |
translations[0].set(100, ofGetHeight()/5, 0); | |
translations[1].set(200, ofGetHeight()*2/5, 0); | |
translations[2].set(300, ofGetHeight()*3/5, 0); | |
translations[3].set(400, ofGetHeight()*4/5, 0); | |
// copy positions to float array | |
float positions[4*3]; | |
for(int i=0; i<4; i++){ | |
positions[i*3] = translations[i].x; | |
positions[i*3+1] = translations[i].y; | |
positions[i*3+2] = translations[i].z; | |
} | |
/* | |
// draw for preview | |
for(int i=0; i<4; i++){ | |
ofSetColor(0, 0, 255); | |
ofCircle(translations[i].x, translations[i].y, translations[i].z, 4); | |
} | |
*/ | |
// setup lighting | |
ofLight light0; | |
ofColor ambientColor(130.0f); | |
ofColor diffuseColor(130.0f); | |
ofColor specularColor(25.0f); | |
light0.setPosition(ofGetWidth(), ofGetHeight()/2, -100); | |
light0.setAmbientColor(ambientColor); | |
light0.setDiffuseColor(diffuseColor); | |
light0.setSpecularColor(specularColor); | |
light0.setPointLight(); | |
light0.enable(); | |
// set draw color | |
ofSetColor(255, 0, 127); | |
mShdInstanced->begin(); | |
// set positions (vec3) | |
mShdInstanced->setUniform3fv("position", (float*)positions, 4); | |
// set transformations (mat4) | |
GLuint loc = glGetUniformLocation(mShdInstanced->getProgram(), "transformMatrix"); | |
if( loc != -1 ) glUniformMatrix4fv(loc, 4, GL_FALSE, mats[0].getPtr()); | |
// draw instances | |
mVboBox.drawInstanced(OF_MESH_FILL, 4); | |
mShdInstanced->end(); | |
ofSetColor(255); | |
ofDrawBitmapString(ofToString(ofGetFrameRate(), 1), ofPoint(10, 20)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment