Ok now I have working GPU ripples in OSX but I can’t port from opengl / glsl to gles. My shader’s seem to compile ok but they don’t seem to get used when I try and put textures through them and render that into an fbo for drawing later. The image that is eventually displayed is just the circle drawn under the mouse pointer, which is the same bug I had in OSX when the faulty vertex shader was causing problems at this stage.
#include "ofApp.h"
//--------------------------------------------------------------
void ofApp::setup(){
bool didLoadShader = shader.load("Empty_GLES.vert", "ripple.frag", "");
if (!didLoadShader)
{
ofLogError() << "Load Shader FAIL";
}
texture1.allocate(ofGetWidth(), ofGetHeight());
texture2.allocate(ofGetWidth(), ofGetHeight());
texture3.allocate(ofGetWidth(), ofGetHeight());
texture1.begin();
ofClear(0, 0, 0, 0);
texture1.end();
texture2.begin();
ofClear(0,0,0,0);
texture2.end();
texture3.begin();
ofClear(0,0,0,0);
texture3.end();
ofEnableAlphaBlending();
damping = 0.995;
even = true;
}
//--------------------------------------------------------------
void ofApp::update(){
//add new waves from mouse pos
if(even)
{
ofPushStyle();
ofPushMatrix();
texture1.begin();
ofFill();
ofSetColor(ofNoise( ofGetFrameNum() ) * 255 * 5, 255);
ofEllipse(mouseX,mouseY, 10,10);
texture1.end();
ofPopMatrix();
ofPopStyle();
texture3.begin();
texture3.bind();
shader.begin();
shader.setUniformTexture("backbuffer", texture2.getTextureReference(), 0);
shader.setUniformTexture("tex0", texture1.getTextureReference(), 1);
shader.setUniform1f("damping", (float)damping );
//render frame
ofSetColor(255,255);
/*
glBegin(GL_QUADS);
glTexCoord2f(0, 0); glVertex3f(0, 0, 0);
glTexCoord2f(ofGetWidth(), 0); glVertex3f(ofGetWidth(), 0, 0);
glTexCoord2f(ofGetWidth(), ofGetHeight()); glVertex3f(ofGetWidth(), ofGetHeight(), 0);
glTexCoord2f(0,ofGetHeight()); glVertex3f(0,ofGetHeight(), 0);
glEnd();
*/
GLfloat vtx1[] = {
0, 0, 0,
ofGetWidth(), 0, 0,
ofGetWidth(), ofGetHeight(), 0,
0, ofGetHeight(), 0
};
GLfloat tex1[] = {
0,0,
1,0,
1,1,
0,1
};
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glVertexPointer(3, GL_FLOAT, 0, vtx1);
glTexCoordPointer(2, GL_FLOAT, 0, tex1);
glDrawArrays(GL_TRIANGLE_FAN,0,4);
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
//ofRect(0, 0, ofGetWidth(), ofGetHeight() );
shader.end();
texture3.unbind();
texture3.end();
texture2.begin();
texture3.draw(0,0);
texture2.end();
//
even = false;
} else
{
ofPushStyle();
ofPushMatrix();
texture2.begin();
ofFill();
ofSetColor(ofNoise( ofGetFrameNum() ) * 255 * 5, 255);
ofEllipse(mouseX,mouseY, 10,10);
texture2.end();
ofPopMatrix();
ofPopStyle();
texture3.begin();
texture3.bind();
shader.begin();
shader.setUniformTexture("backbuffer", texture1.getTextureReference(), 0);
shader.setUniformTexture("tex0", texture2.getTextureReference(), 1);
shader.setUniform1f("damping", (float)damping );
//render frame
ofSetColor(255,255);
/*
glBegin(GL_QUADS);
glTexCoord2f(0, 0); glVertex3f(0, 0, 0);
glTexCoord2f(ofGetWidth(), 0); glVertex3f(ofGetWidth(), 0, 0);
glTexCoord2f(ofGetWidth(), ofGetHeight()); glVertex3f(ofGetWidth(), ofGetHeight(), 0);
glTexCoord2f(0,ofGetHeight()); glVertex3f(0,ofGetHeight(), 0);
glEnd();
*/
GLfloat vtx1[] = {
0, 0, 0,
ofGetWidth(), 0, 0,
ofGetWidth(), ofGetHeight(), 0,
0, ofGetHeight(), 0
};
GLfloat tex1[] = {
0,0,
1,0,
1,1,
0,1
};
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glVertexPointer(3, GL_FLOAT, 0, vtx1);
glTexCoordPointer(2, GL_FLOAT, 0, tex1);
glDrawArrays(GL_TRIANGLE_FAN,0,4);
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
//ofRect(0, 0, ofGetWidth(), ofGetHeight());
shader.end();
texture3.unbind();
texture3.end();
texture1.begin();
texture3.draw(0,0);
texture1.end();
//
even = true;
}
}
//--------------------------------------------------------------
void ofApp::draw(){
ofBackground(0);
ofSetColor(255,255);
ofPushStyle();
ofEnableAlphaBlending();
if ( even ) {
texture2.draw(0,0);
} else
{
texture1.draw(0,0);
}
ofPopStyle();
}
relevant bit of header
ofShader shader;
ofFbo texture1;
ofFbo texture2;
ofFbo texture3;
bool even;
float damping;
vertex shader code
attribute vec4 position;
attribute vec4 color;
attribute vec4 normal;
attribute vec2 texcoord;
uniform mat4 modelViewMatrix;
uniform mat4 projectionMatrix;
varying vec4 colorVarying;
varying vec2 texCoordVarying;
void main()
{
vec4 pos = projectionMatrix * modelViewMatrix * position;
gl_Position = pos;
colorVarying = color;
texCoordVarying = texcoord;
}
fragment shader code
precision highp float;
uniform sampler2D backbuffer; // previus buffer
uniform sampler2D tex0; // actual buffer
uniform float damping;
vec2 offset[4];
varying vec2 texCoordVarying;
void main(){
vec2 st = texCoordVarying;
offset[0] = vec2(-1.0, 0.0);
offset[1] = vec2(1.0, 0.0);
offset[2] = vec2(0.0, 1.0);
offset[3] = vec2(0.0, -1.0);
// Grab the information arround the active pixel
//
// [3]
//
// [0] st [1]
//
// [2]
vec3 sum = vec3(0.0, 0.0, 0.0);
for (int i = 0; i < 4 ; i++){
sum += texture2D(tex0, st + offset[i]).rgb;
}
// make an average and substract the center value
//
sum = (sum / 2.0) - texture2D(backbuffer, st).rgb;
sum *= damping;
gl_FragColor = vec4(sum, 1.0);
}
I have set the renderer to GLES in the main.cpp as per all the examples I’ve seen of pi code. The bit where I think I’m going wrong is where I’ve changed the code from ofxFX that renders the texture, the original is
glBegin(GL_QUADS);
glTexCoord2f(0, 0); glVertex3f(0, 0, 0);
glTexCoord2f(ofGetWidth(), 0); glVertex3f(ofGetWidth(), 0, 0);
glTexCoord2f(ofGetWidth(), ofGetHeight()); glVertex3f(ofGetWidth(), ofGetHeight(), 0);
glTexCoord2f(0,ofGetHeight()); glVertex3f(0,ofGetHeight(), 0);
glEnd();
I changed this to something a bit more GLES friendly but I don’t think it’s worked.
GLfloat vtx1[] = {
0, 0, 0,
ofGetWidth(), 0, 0,
ofGetWidth(), ofGetHeight(), 0,
0, ofGetHeight(), 0
};
GLfloat tex1[] = {
0,0,
1,0,
1,1,
0,1
};
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glVertexPointer(3, GL_FLOAT, 0, vtx1);
glTexCoordPointer(2, GL_FLOAT, 0, tex1);
glDrawArrays(GL_TRIANGLE_FAN,0,4);
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);