Last active
July 27, 2016 19:25
-
-
Save jvcleave/9c001f4952a22534dd2fccb52861b02e to your computer and use it in GitHub Desktop.
modern geometryShaderExample
This file contains hidden or 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" | |
| #define STRINGIFY(x) #x | |
| class ofApp : public ofBaseApp | |
| { | |
| public: | |
| ofShader shader; | |
| ofEasyCam camera; | |
| ofVboMesh mesh; | |
| ofPlanePrimitive plane; | |
| ofSpherePrimitive sphere; | |
| void setup() | |
| { | |
| ofBackground(ofColor::black); | |
| ofSetVerticalSync(false); | |
| string shaderHeader = "#version 330\n"; | |
| string vertShaderSource = shaderHeader; | |
| string fragShaderSource = shaderHeader; | |
| string geomShaderSource = shaderHeader; | |
| vertShaderSource += STRINGIFY( | |
| uniform mat4 modelViewProjectionMatrix; | |
| in vec4 position; | |
| in vec2 texcoord; | |
| out vec2 texCoordVarying; | |
| void main() | |
| { | |
| gl_Position = position; | |
| } | |
| ); | |
| fragShaderSource += STRINGIFY( | |
| out vec4 fragColor; | |
| in vec4 outputColor; | |
| void main (void) | |
| { | |
| fragColor = outputColor; | |
| } | |
| ); | |
| geomShaderSource += STRINGIFY( | |
| layout (points) in; | |
| layout (line_strip, max_vertices = 16) out; | |
| uniform mat4 modelViewProjectionMatrix; | |
| uniform vec4 globalColor; | |
| out vec4 outputColor; | |
| void main() { | |
| vec4 position = gl_in[0].gl_Position; | |
| float size = 10.f; | |
| vec4 NEU = position + vec4( size, size, size, 0.0); | |
| vec4 NED = position + vec4( size, -size, size, 0.0); | |
| vec4 NWU = position + vec4( size, size, -size, 0.0); | |
| vec4 NWD = position + vec4( size, -size, -size, 0.0); | |
| vec4 SEU = position + vec4(-size, size, size, 0.0); | |
| vec4 SED = position + vec4(-size, -size, size, 0.0); | |
| vec4 SWU = position + vec4(-size, size, -size, 0.0); | |
| vec4 SWD = position + vec4(-size, -size, -size, 0.0); | |
| gl_Position = modelViewProjectionMatrix * NED; | |
| outputColor = globalColor; EmitVertex(); | |
| gl_Position = modelViewProjectionMatrix * NWD; | |
| outputColor = globalColor; EmitVertex(); | |
| gl_Position = modelViewProjectionMatrix * SWD; | |
| outputColor = globalColor; EmitVertex(); | |
| gl_Position = modelViewProjectionMatrix * SED; | |
| outputColor = globalColor; EmitVertex(); | |
| gl_Position = modelViewProjectionMatrix * SEU; | |
| outputColor = globalColor; EmitVertex(); | |
| gl_Position = modelViewProjectionMatrix * SWU; | |
| outputColor = globalColor; EmitVertex(); | |
| gl_Position = modelViewProjectionMatrix * NWU; | |
| outputColor = globalColor; EmitVertex(); | |
| gl_Position = modelViewProjectionMatrix * NEU; | |
| outputColor = globalColor; EmitVertex(); | |
| gl_Position = modelViewProjectionMatrix * NED; | |
| outputColor = globalColor; EmitVertex(); | |
| gl_Position = modelViewProjectionMatrix * SED; | |
| outputColor = globalColor; EmitVertex(); | |
| gl_Position = modelViewProjectionMatrix * SEU; | |
| outputColor = globalColor; EmitVertex(); | |
| gl_Position = modelViewProjectionMatrix * NEU; | |
| outputColor = globalColor; EmitVertex(); | |
| gl_Position = modelViewProjectionMatrix * NWU; | |
| outputColor = globalColor; EmitVertex(); | |
| gl_Position = modelViewProjectionMatrix * NWD; | |
| outputColor = globalColor; EmitVertex(); | |
| gl_Position = modelViewProjectionMatrix * SWD; | |
| outputColor = globalColor; EmitVertex(); | |
| gl_Position = modelViewProjectionMatrix * SWU; | |
| outputColor = globalColor; EmitVertex(); | |
| EndPrimitive(); | |
| } | |
| ); | |
| shader.setupShaderFromSource(GL_VERTEX_SHADER, vertShaderSource); | |
| shader.setupShaderFromSource(GL_FRAGMENT_SHADER, fragShaderSource); | |
| shader.setupShaderFromSource(GL_GEOMETRY_SHADER, geomShaderSource); | |
| shader.linkProgram(); | |
| ofLog() << "Maximum number of output vertices support is: " << shader.getGeometryMaxOutputCount(); | |
| plane.set(1000, 1000, 30, 30); | |
| sphere.set(100, 10); | |
| mesh = ofVboMesh(sphere.getMesh()); | |
| mesh.setMode(OF_PRIMITIVE_POINTS); | |
| } | |
| void update() | |
| { | |
| } | |
| void draw() | |
| { | |
| camera.begin(); | |
| shader.begin(); | |
| mesh.draw(); | |
| shader.end(); | |
| //mesh.drawWireframe(); | |
| camera.end(); | |
| ofDrawBitmapString("FPS: " + ofToString(ofGetFrameRate(), 2), 20, 20); | |
| } | |
| void keyPressed (int key) | |
| { | |
| } | |
| }; | |
| int main( ){ | |
| ofSetLogLevel(OF_LOG_VERBOSE); | |
| ofGLFWWindowSettings settings; | |
| settings.setGLVersion(4, 1); | |
| settings.width = 1280; | |
| settings.height = 720; | |
| ofCreateWindow(settings); | |
| ofRunApp(new ofApp()); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment