Created
December 19, 2013 12:22
-
-
Save ishikawash/8038343 to your computer and use it in GitHub Desktop.
Instanced rendering demo using openFrameworks.
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 | |
uniform vec3 light_direction; | |
varying vec3 vertex_normal; | |
varying vec3 vertex_color; | |
void main(void) { | |
vec3 N = normalize(vertex_normal); | |
vec3 L = normalize(light_direction); | |
float kd = clamp(dot(N, L), 0.0f, 1.0f); | |
vec3 color = kd * vertex_color; | |
gl_FragColor = vec4(color, 1.0f); | |
} |
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 | |
#define INSTANCE_MAX_COUNT 125 | |
uniform mat4 model_matrix[INSTANCE_MAX_COUNT]; | |
uniform mat3 normal_matrix[INSTANCE_MAX_COUNT]; | |
uniform vec3 colors[INSTANCE_MAX_COUNT]; | |
varying vec3 vertex_normal; | |
varying vec3 vertex_color; | |
void main(void) { | |
int i = gl_InstanceID; | |
vertex_normal = normal_matrix[i] * gl_Normal; | |
vertex_color = colors[i]; | |
gl_Position = gl_ProjectionMatrix * gl_ModelViewMatrix * model_matrix[i] * gl_Vertex; | |
} |
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
#include "ofMain.h" | |
#include "testApp.h" | |
//======================================================================== | |
int main( ){ | |
ofSetupOpenGL(1024,768,OF_WINDOW); // <-------- setup the GL context | |
// this kicks off the running of my app | |
// can be OF_WINDOW or OF_FULLSCREEN | |
// pass in width and height too: | |
ofRunApp(new testApp()); | |
} |
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
#include "testApp.h" | |
using namespace std; | |
using namespace glm; | |
const int GRID_LENGTH = 5; | |
const float GRID_OFFSET = 20.0f; | |
const float BOX_SIZE = 30.0f; | |
void printMatrix4fv(const float *m) { | |
for (int i = 0; i < 4; i++) { | |
for (int j = 0; j < 4; j++) { | |
printf("%8.2f\t", m[4*i + j]); | |
} | |
printf("\n"); | |
} | |
printf("\n"); | |
} | |
void testApp::uniformMatrix4fv(const char *name, const float *m) { | |
int location = glGetUniformLocation(shader.getProgram(), name); | |
if (location != -1) | |
glUniformMatrix4fv(location, 1, GL_FALSE, m); | |
} | |
void testApp::uniformMatrix3fv(const char *name, const float *m) { | |
int location = glGetUniformLocation(shader.getProgram(), name); | |
if (location != -1) | |
glUniformMatrix3fv(location, 1, GL_FALSE, m); | |
} | |
//-------------------------------------------------------------- | |
void testApp::setup(){ | |
ofEnableDepthTest(); | |
bg_colors[0].set(255.0f); | |
bg_colors[1].set(96.0f); | |
box.set(BOX_SIZE); | |
vbo = box.getMesh(); | |
const ivec3 grid_size(GRID_LENGTH); | |
nodes.resize(grid_size.x*grid_size.y*grid_size.z); | |
const vec3 grid_box_size(box.getWidth() + GRID_OFFSET, box.getHeight() + GRID_OFFSET, box.getDepth() + GRID_OFFSET); | |
const vec3 grid_center = 0.5f*vec3(grid_size.x*box.getWidth() + (grid_size.x - 1)*GRID_OFFSET, | |
grid_size.y*box.getHeight() + (grid_size.y - 1)*GRID_OFFSET, | |
grid_size.z*box.getDepth() + (grid_size.z - 1)*GRID_OFFSET); | |
for (int k = 0; k < grid_size.z; k++) { | |
for (int j = 0; j < grid_size.y; j++) { | |
for (int i = 0; i < grid_size.x; i++) { | |
ofNode &node = nodes[grid_size.x*grid_size.y*k + grid_size.x*j + i]; | |
node.move(-grid_center.x, -grid_center.y, -grid_center.z); | |
node.move(grid_box_size.x*i, grid_box_size.y*j, grid_box_size.z*k); | |
node.setParent(parent); | |
} | |
} | |
} | |
camera.move(0.0f, 0.0f, 800.0f); | |
shader.load("box.vs", "box.fs"); | |
shader.begin(); | |
{ | |
vec3 light_direction(0.0f, 1.0f, 1.0f); | |
shader.setUniform3fv("light_direction", value_ptr(light_direction)); | |
vec3 box_colors[2]; | |
box_colors[0] = vec3(0.0f, 0.0f, 0.0f); | |
box_colors[1] = vec3(1.0f, 1.0f, 1.0f); | |
char name[64]; | |
for (int i = 0; i < nodes.size(); i++) { | |
snprintf(name, sizeof(name), "colors[%d]", i); | |
shader.setUniform3fv(name, value_ptr(box_colors[i % 2])); | |
} | |
} | |
shader.end(); | |
} | |
//-------------------------------------------------------------- | |
void testApp::update(){ | |
float t = ofGetElapsedTimef(); | |
float theta = 180.0f*ofGetLastFrameTime(); | |
parent.rotate(theta, cosf(t), sinf(t), 0.0f); | |
shader.begin(); | |
{ | |
glGetFloatv(GL_MODELVIEW_MATRIX, buf); | |
mat4 view_matrix; | |
memcpy(&view_matrix[0][0], buf, sizeof(buf)); | |
char name[64]; | |
for (int i = 0; i < nodes.size(); i++) { | |
ofNode &node = nodes[i]; | |
mat4 model_matrix; | |
memcpy(&model_matrix[0][0], node.getGlobalTransformMatrix().getPtr(), 16*sizeof(float)); | |
snprintf(name, sizeof(name), "model_matrix[%d]", i); | |
uniformMatrix4fv(name, value_ptr(model_matrix)); | |
mat4 model_view_matrix = view_matrix*model_matrix; | |
mat3 normal_matrix = mat3(transpose(inverse(model_view_matrix))); | |
snprintf(name, sizeof(name), "normal_matrix[%d]", i); | |
uniformMatrix3fv(name, value_ptr(normal_matrix)); | |
} | |
} | |
shader.end(); | |
} | |
//-------------------------------------------------------------- | |
void testApp::draw(){ | |
ofBackgroundGradient(bg_colors[0], bg_colors[1], OF_GRADIENT_BAR); | |
camera.begin(); | |
{ | |
shader.begin(); | |
{ | |
vbo.drawInstanced(OF_MESH_FILL, nodes.size()); | |
} | |
shader.end(); | |
} | |
camera.end(); | |
} | |
//-------------------------------------------------------------- | |
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){ | |
} | |
//-------------------------------------------------------------- | |
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){ | |
} |
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
#pragma once | |
#include <glm/glm.hpp> | |
#include <glm/gtc/type_ptr.hpp> | |
#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); | |
ofCamera camera; | |
ofNode parent; | |
std::vector<ofNode> nodes; | |
ofBoxPrimitive box; | |
ofShader shader; | |
ofVboMesh vbo; | |
ofColor bg_colors[2]; | |
float buf[16]; | |
private: | |
void uniformMatrix3fv(const char *name, const float *m); | |
void uniformMatrix4fv(const char *name, const float *m); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment