Skip to content

Commit

Permalink
updated to run with programmable renderer
Browse files Browse the repository at this point in the history
  • Loading branch information
neilmendoza committed Jul 9, 2016
1 parent 1e1f036 commit 3850e47
Show file tree
Hide file tree
Showing 14 changed files with 188 additions and 55 deletions.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
8 changes: 8 additions & 0 deletions example/bin/data/shaders330/draw.frag
@@ -0,0 +1,8 @@
#version 330

out vec4 fragColor;

void main()
{
fragColor = vec4(1.0, 1.0, 1.0, 0.1);
}
16 changes: 16 additions & 0 deletions example/bin/data/shaders330/draw.vert
@@ -0,0 +1,16 @@
#version 330

uniform mat4 modelViewProjectionMatrix;
uniform sampler2DRect particles0;
uniform sampler2DRect particles1;

in vec4 position;
in vec2 texcoord;

out vec2 texCoordVarying;

void main()
{
texCoordVarying = texcoord;
gl_Position = modelViewProjectionMatrix * vec4(texture(particles0, texCoordVarying).xyz, 1.0);
}
45 changes: 45 additions & 0 deletions example/bin/data/shaders330/update.frag
@@ -0,0 +1,45 @@
#version 330

// ping pong inputs
uniform sampler2DRect particles0;
uniform sampler2DRect particles1;

uniform vec3 mouse;
uniform float radiusSquared;
uniform float elapsed;

in vec2 texCoordVarying;

layout(location = 0) out vec4 posOut;
layout(location = 1) out vec4 velOut;

void main()
{
vec3 pos = texture(particles0, texCoordVarying.st).xyz;
vec3 vel = texture(particles1, texCoordVarying.st).xyz;

// mouse attraction
vec3 direction = mouse - pos.xyz;
float distSquared = dot(direction, direction);
float magnitude = 500.0 * (1.0 - distSquared / radiusSquared);
vec3 force = step(distSquared, radiusSquared) * magnitude * normalize(direction);

// gravity
force += vec3(0.0, -0.5, 0.0);

// accelerate
vel += elapsed * force;

// bounce off the sides
vel.x *= step(abs(pos.x), 512.0) * 2.0 - 1.0;
vel.y *= step(abs(pos.y), 384.0) * 2.0 - 1.0;

// damping
vel *= 0.995;

// move
pos += elapsed * vel;

posOut = vec4(pos, 1.0);
velOut = vec4(vel, 0.0);
}
12 changes: 12 additions & 0 deletions example/bin/data/shaders330/update.vert
@@ -0,0 +1,12 @@
#version 330

in vec4 position;
in vec2 texcoord;

out vec2 texCoordVarying;

void main()
{
texCoordVarying = texcoord;
gl_Position = position;
}
46 changes: 40 additions & 6 deletions example/example.xcodeproj/project.pbxproj
Expand Up @@ -7,7 +7,7 @@
objects = {

/* Begin PBXBuildFile section */
29D734F0DDECC8C5978185D7 /* testApp.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 00A1BFCBB38848AD0E0C8539 /* testApp.cpp */; };
29D734F0DDECC8C5978185D7 /* ofApp.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 00A1BFCBB38848AD0E0C8539 /* ofApp.cpp */; };
900DDEDCFE3FAF10C3318D7C /* GpuParticles.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F023988BFE9B8D4B44299A5E /* GpuParticles.cpp */; };
E4328149138ABC9F0047C5CB /* openFrameworksDebug.a in Frameworks */ = {isa = PBXBuildFile; fileRef = E4328148138ABC890047C5CB /* openFrameworksDebug.a */; };
E4B69E200A3A1BDC003C02F2 /* main.cpp in Sources */ = {isa = PBXBuildFile; fileRef = E4B69E1D0A3A1BDC003C02F2 /* main.cpp */; };
Expand Down Expand Up @@ -43,9 +43,17 @@
/* End PBXCopyFilesBuildPhase section */

/* Begin PBXFileReference section */
00A1BFCBB38848AD0E0C8539 /* testApp.cpp */ = {isa = PBXFileReference; explicitFileType = sourcecode.cpp.cpp; fileEncoding = 30; name = testApp.cpp; path = src/testApp.cpp; sourceTree = SOURCE_ROOT; };
00A1BFCBB38848AD0E0C8539 /* ofApp.cpp */ = {isa = PBXFileReference; explicitFileType = sourcecode.cpp.cpp; fileEncoding = 30; name = ofApp.cpp; path = src/ofApp.cpp; sourceTree = SOURCE_ROOT; };
0D69749DC16A63170FE23709 /* GpuParticles.h */ = {isa = PBXFileReference; explicitFileType = sourcecode.c.h; fileEncoding = 30; name = GpuParticles.h; path = ../../../addons/ofxGpuParticles/src/GpuParticles.h; sourceTree = SOURCE_ROOT; };
2D129AB0F7D46B8DFCEADEDB /* testApp.h */ = {isa = PBXFileReference; explicitFileType = sourcecode.c.h; fileEncoding = 30; name = testApp.h; path = src/testApp.h; sourceTree = SOURCE_ROOT; };
28EF34951D317BE000012D57 /* draw.frag */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.glsl; path = draw.frag; sourceTree = "<group>"; };
28EF34961D317BE000012D57 /* draw.vert */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.glsl; path = draw.vert; sourceTree = "<group>"; };
28EF34971D317BE000012D57 /* update.frag */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.glsl; path = update.frag; sourceTree = "<group>"; };
28EF34981D317BE000012D57 /* update.vert */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.glsl; path = update.vert; sourceTree = "<group>"; };
28EF349A1D317BE000012D57 /* draw.frag */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.glsl; path = draw.frag; sourceTree = "<group>"; };
28EF349B1D317BE000012D57 /* draw.vert */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.glsl; path = draw.vert; sourceTree = "<group>"; };
28EF349C1D317BE000012D57 /* update.frag */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.glsl; path = update.frag; sourceTree = "<group>"; };
28EF349D1D317BE000012D57 /* update.vert */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.glsl; path = update.vert; sourceTree = "<group>"; };
2D129AB0F7D46B8DFCEADEDB /* ofApp.h */ = {isa = PBXFileReference; explicitFileType = sourcecode.c.h; fileEncoding = 30; name = ofApp.h; path = src/ofApp.h; sourceTree = SOURCE_ROOT; };
40562A1AC8141EEAE8DE9FD6 /* ofxGpuParticles.h */ = {isa = PBXFileReference; explicitFileType = sourcecode.c.h; fileEncoding = 30; name = ofxGpuParticles.h; path = ../../../addons/ofxGpuParticles/src/ofxGpuParticles.h; sourceTree = SOURCE_ROOT; };
E4328143138ABC890047C5CB /* openFrameworksLib.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = openFrameworksLib.xcodeproj; path = ../../../libs/openFrameworksCompiled/project/osx/openFrameworksLib.xcodeproj; sourceTree = SOURCE_ROOT; };
E4B69B5B0A3A1756003C02F2 /* exampleDebug.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = exampleDebug.app; sourceTree = BUILT_PRODUCTS_DIR; };
Expand All @@ -68,6 +76,30 @@
/* End PBXFrameworksBuildPhase section */

/* Begin PBXGroup section */
28EF34941D317BE000012D57 /* shaders120 */ = {
isa = PBXGroup;
children = (
28EF34951D317BE000012D57 /* draw.frag */,
28EF34961D317BE000012D57 /* draw.vert */,
28EF34971D317BE000012D57 /* update.frag */,
28EF34981D317BE000012D57 /* update.vert */,
);
name = shaders120;
path = bin/data/shaders120;
sourceTree = SOURCE_ROOT;
};
28EF34991D317BE000012D57 /* shaders330 */ = {
isa = PBXGroup;
children = (
28EF349A1D317BE000012D57 /* draw.frag */,
28EF349B1D317BE000012D57 /* draw.vert */,
28EF349C1D317BE000012D57 /* update.frag */,
28EF349D1D317BE000012D57 /* update.vert */,
);
name = shaders330;
path = bin/data/shaders330;
sourceTree = SOURCE_ROOT;
};
6948EE371B920CB800B5AC1A /* local_addons */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -125,9 +157,11 @@
E4B69E1C0A3A1BDC003C02F2 /* src */ = {
isa = PBXGroup;
children = (
28EF34941D317BE000012D57 /* shaders120 */,
28EF34991D317BE000012D57 /* shaders330 */,
E4B69E1D0A3A1BDC003C02F2 /* main.cpp */,
00A1BFCBB38848AD0E0C8539 /* testApp.cpp */,
2D129AB0F7D46B8DFCEADEDB /* testApp.h */,
00A1BFCBB38848AD0E0C8539 /* ofApp.cpp */,
2D129AB0F7D46B8DFCEADEDB /* ofApp.h */,
);
path = src;
sourceTree = SOURCE_ROOT;
Expand Down Expand Up @@ -229,7 +263,7 @@
buildActionMask = 2147483647;
files = (
E4B69E200A3A1BDC003C02F2 /* main.cpp in Sources */,
29D734F0DDECC8C5978185D7 /* testApp.cpp in Sources */,
29D734F0DDECC8C5978185D7 /* ofApp.cpp in Sources */,
900DDEDCFE3FAF10C3318D7C /* GpuParticles.cpp in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
23 changes: 13 additions & 10 deletions example/src/main.cpp
@@ -1,16 +1,19 @@
#include "ofMain.h"
#include "testApp.h"
#include "ofApp.h"
#include "ofAppGlutWindow.h"

//========================================================================
int main( ){

int main()
{
#ifdef _PROGRAMMABLE_RENDERER
ofGLFWWindowSettings settings;
settings.setGLVersion(4, 1);
settings.width = 1024;
settings.height = 768;
ofCreateWindow(settings);
#else
ofAppGlutWindow window;
ofSetupOpenGL(&window, 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());

ofSetupOpenGL(&window, 1024,768, OF_WINDOW);
#endif
ofRunApp(new ofApp());
}
33 changes: 18 additions & 15 deletions example/src/testApp.cpp → example/src/ofApp.cpp
@@ -1,7 +1,7 @@
#include "testApp.h"
#include "ofApp.h"

//--------------------------------------------------------------
void testApp::setup()
void ofApp::setup()
{
ofBackground(0);
ofSetFrameRate(60);
Expand All @@ -12,6 +12,9 @@ void testApp::setup()

particles.init(w, h);

if (ofIsGLProgrammableRenderer()) particles.loadShaders("shaders330/update", "shaders330/draw");
else particles.loadShaders("shaders120/update", "shaders120/draw");

// initial positions
// use new to allocate 4,000,000 floats on the heap rather than
// the stack
Expand All @@ -34,18 +37,18 @@ void testApp::setup()
particles.zeroDataTexture(ofxGpuParticles::VELOCITY);

// listen for update event to set additonal update uniforms
ofAddListener(particles.updateEvent, this, &testApp::onParticlesUpdate);
ofAddListener(particles.updateEvent, this, &ofApp::onParticlesUpdate);
}

//--------------------------------------------------------------
void testApp::update()
void ofApp::update()
{
ofSetWindowTitle(ofToString(ofGetFrameRate(), 2));
particles.update();
}

// set any update uniforms in this function
void testApp::onParticlesUpdate(ofShader& shader)
void ofApp::onParticlesUpdate(ofShader& shader)
{
ofVec3f mouse(ofGetMouseX() - .5f * ofGetWidth(), .5f * ofGetHeight() - ofGetMouseY() , 0.f);
shader.setUniform3fv("mouse", mouse.getPtr());
Expand All @@ -54,7 +57,7 @@ void testApp::onParticlesUpdate(ofShader& shader)
}

//--------------------------------------------------------------
void testApp::draw()
void ofApp::draw()
{
cam.begin();
ofEnableBlendMode(OF_BLENDMODE_ADD);
Expand All @@ -64,46 +67,46 @@ void testApp::draw()
}

//--------------------------------------------------------------
void testApp::keyPressed(int key){
void ofApp::keyPressed(int key){

}

//--------------------------------------------------------------
void testApp::keyReleased(int key){
void ofApp::keyReleased(int key){

}

//--------------------------------------------------------------
void testApp::mouseMoved(int x, int y ){
void ofApp::mouseMoved(int x, int y ){

}

//--------------------------------------------------------------
void testApp::mouseDragged(int x, int y, int button){
void ofApp::mouseDragged(int x, int y, int button){

}

//--------------------------------------------------------------
void testApp::mousePressed(int x, int y, int button){
void ofApp::mousePressed(int x, int y, int button){

}

//--------------------------------------------------------------
void testApp::mouseReleased(int x, int y, int button){
void ofApp::mouseReleased(int x, int y, int button){

}

//--------------------------------------------------------------
void testApp::windowResized(int w, int h){
void ofApp::windowResized(int w, int h){

}

//--------------------------------------------------------------
void testApp::gotMessage(ofMessage msg){
void ofApp::gotMessage(ofMessage msg){

}

//--------------------------------------------------------------
void testApp::dragEvent(ofDragInfo dragInfo){
void ofApp::dragEvent(ofDragInfo dragInfo){

}
4 changes: 3 additions & 1 deletion example/src/testApp.h → example/src/ofApp.h
Expand Up @@ -3,7 +3,9 @@
#include "ofMain.h"
#include "ofxGpuParticles.h"

class testApp : public ofBaseApp
#define _PROGRAMMABLE_RENDERER

class ofApp : public ofBaseApp
{
public:
void setup();
Expand Down

0 comments on commit 3850e47

Please sign in to comment.