We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpOpenGL and 3D Libraries › OpenGL Offscreen buffer for glow effect
Pages: 1 2 3 
OpenGL Offscreen buffer for glow effect (Read 15859 times)
Re: OpenGL Offscreen buffer for glow effect
Reply #15 - Apr 24th, 2008, 8:54pm
 
This example works for me (you should get a red screen):

Code:

import processing.opengl.*;
import javax.media.opengl.*;

PGraphicsOpenGL pgl;
GL gl;

int[] drawTex = { 0 };
int[] drawFBO = { 0 };

int texWidth = 320;
int texHeight = 240;

void setup()
{
size(640, 480, OPENGL);

pgl = (PGraphicsOpenGL) g; // g may change
gl = pgl.gl; // always use the GL object returned by beginGL

// Creating texture.
gl.glGenTextures(1, drawTex, 0);
gl.glBindTexture(GL.GL_TEXTURE_2D, drawTex[0]);
gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MIN_FILTER, GL.GL_NEAREST);
gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MAG_FILTER, GL.GL_NEAREST);
gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_WRAP_S, GL.GL_CLAMP);
gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_WRAP_T, GL.GL_CLAMP);
gl.glTexImage2D(GL.GL_TEXTURE_2D, 0, GL.GL_RGBA, texWidth, texHeight, 0, GL.GL_BGRA, GL.GL_UNSIGNED_BYTE, null);

// Creating FBO.
gl.glGenFramebuffersEXT(1, drawFBO, 0);
gl.glBindFramebufferEXT(GL.GL_FRAMEBUFFER_EXT, drawFBO[0]);
gl.glFramebufferTexture2DEXT(GL.GL_FRAMEBUFFER_EXT, GL.GL_COLOR_ATTACHMENT0_EXT, GL.GL_TEXTURE_2D, drawTex[0], 0);
int stat = gl.glCheckFramebufferStatusEXT(GL.GL_FRAMEBUFFER_EXT);
if (stat != GL.GL_FRAMEBUFFER_COMPLETE_EXT) System.out.println("FBO error");
gl.glBindFramebufferEXT(GL.GL_FRAMEBUFFER_EXT, 0);
}

void draw()
{
PGraphicsOpenGL pgl = (PGraphicsOpenGL) g; // g may change
GL gl = pgl.beginGL(); // always use the GL object returned by beginGL

// Binding FBO.
gl.glBindFramebufferEXT(GL.GL_FRAMEBUFFER_EXT, drawFBO[0]);

// Drawing to the first color attachement of drawFBO (this is where drawTex is attached to).
gl.glDrawBuffer(GL.GL_COLOR_ATTACHMENT0_EXT);

// Setting orthographic projection.
gl.glMatrixMode(GL.GL_PROJECTION);
gl.glPushMatrix();
gl.glLoadIdentity();
gl.glOrtho(0.0, texWidth, 0.0, texHeight, -100.0, +100.0);
gl.glMatrixMode(GL.GL_MODELVIEW);
gl.glPushMatrix();
gl.glLoadIdentity();

gl.glViewport(0, 0, texWidth, texHeight);

// Just rendering a red quad.
gl.glDisable(GL.GL_TEXTURE_2D);
gl.glColor4f(1.0, 0.0, 0.0, 1.0);
gl.glBegin(GL.GL_QUADS);
gl.glVertex2f(0.0, 0.0);
gl.glVertex2f(texWidth, 0.0);
gl.glVertex2f(texWidth, texHeight);
gl.glVertex2f(0.0, texHeight);
gl.glEnd();

gl.glMatrixMode(GL.GL_PROJECTION);
gl.glPopMatrix();
gl.glMatrixMode(GL.GL_MODELVIEW);
gl.glPopMatrix();

// Unbinding drawFBO. Now drawing to screen again.
gl.glBindFramebufferEXT(GL.GL_FRAMEBUFFER_EXT, 0);

// Setting orthographic projection.
gl.glMatrixMode(GL.GL_PROJECTION);
gl.glPushMatrix();
gl.glLoadIdentity();
gl.glOrtho(0.0, width, 0.0, height, -100.0, +100.0);
gl.glMatrixMode(GL.GL_MODELVIEW);
gl.glPushMatrix();
gl.glLoadIdentity();

gl.glViewport(0, 0, width, height);

// Drawing texture to screen.
gl.glEnable(GL.GL_TEXTURE_2D);
gl.glActiveTexture(GL.GL_TEXTURE0);
gl.glBindTexture(GL.GL_TEXTURE_2D, drawTex[0]);
gl.glBegin(GL.GL_QUADS);
gl.glTexCoord2f(0.0, 1.0);
gl.glVertex2f(0.0, 0.0);

gl.glTexCoord2f(1.0, 1.0);
gl.glVertex2f(width, 0.0);

gl.glTexCoord2f(1.0, 0.0);
gl.glVertex2f(width, height);

gl.glTexCoord2f(0.0, 0.0);
gl.glVertex2f(0.0, height);
gl.glEnd();
gl.glBindTexture(GL.GL_TEXTURE_2D, 0);

gl.glMatrixMode(GL.GL_PROJECTION);
gl.glPopMatrix();
gl.glMatrixMode(GL.GL_MODELVIEW);
gl.glPopMatrix();

pgl.endGL();
}
Re: OpenGL Offscreen buffer for glow effect
Reply #16 - Apr 25th, 2008, 10:30am
 
yes it works. I will try to get into that stuff! Thanks!
Re: OpenGL Offscreen buffer for glow effect
Reply #17 - May 3rd, 2008, 3:24pm
 
I was playing around with your example by simply drawing the texture to the mouse coordinates. It appears to me that everything is flipped vertically. It appears to be the framebuffer since the texCoords seem to be correct.
Code:

void draw()
{
background(0);
PGraphicsOpenGL pgl = (PGraphicsOpenGL) g; // g may change
GL gl = pgl.beginGL(); // always use the GL object returned by beginGL

// Binding FBO.
gl.glBindFramebufferEXT(GL.GL_FRAMEBUFFER_EXT, drawFBO[0]);

// Drawing to the first color attachement of drawFBO (this is where drawTex is attached to).
gl.glDrawBuffer(GL.GL_COLOR_ATTACHMENT0_EXT);

// Setting orthographic projection.
gl.glMatrixMode(GL.GL_PROJECTION);
gl.glPushMatrix();
gl.glLoadIdentity();
gl.glOrtho(0.0, texWidth, 0.0, texHeight, -100.0, +100.0);
gl.glMatrixMode(GL.GL_MODELVIEW);
gl.glPushMatrix();
gl.glLoadIdentity();

gl.glViewport(0, 0, texWidth, texHeight);

// Just rendering a red quad.
gl.glDisable(GL.GL_TEXTURE_2D);
gl.glColor4f(1.0, 0.0, 0.0, 1.0);
gl.glBegin(GL.GL_QUADS);
gl.glVertex2f(0.0, 0.0);
gl.glVertex2f(texWidth, 0.0);
gl.glVertex2f(texWidth, texHeight);
gl.glVertex2f(0.0, texHeight);
gl.glEnd();

gl.glMatrixMode(GL.GL_PROJECTION);
gl.glPopMatrix();
gl.glMatrixMode(GL.GL_MODELVIEW);
gl.glPopMatrix();

// Unbinding drawFBO. Now drawing to screen again.
gl.glBindFramebufferEXT(GL.GL_FRAMEBUFFER_EXT, 0);

// Setting orthographic projection.
gl.glMatrixMode(GL.GL_PROJECTION);
gl.glPushMatrix();
gl.glLoadIdentity();
gl.glOrtho(0.0, width, 0.0, height, -100.0, +100.0);
gl.glMatrixMode(GL.GL_MODELVIEW);
gl.glPushMatrix();
gl.glLoadIdentity();

gl.glViewport(0, 0, width, height);

// Drawing texture to screen.
gl.glEnable(GL.GL_TEXTURE_2D);
gl.glActiveTexture(GL.GL_TEXTURE0);
gl.glBindTexture(GL.GL_TEXTURE_2D, drawTex[0]);
gl.glPushMatrix();
gl.glTranslatef(mouseX, -mouseY+height, 0);
gl.glBegin(GL.GL_QUADS);
gl.glTexCoord2f(1.0, 0.0);
gl.glVertex2f(0.0, 0.0);

gl.glTexCoord2f(0.0, 0.0);
gl.glVertex2f(100, 0.0);

gl.glTexCoord2f(0.0, 1.0);
gl.glVertex2f(100, 100);

gl.glTexCoord2f(1.0, 1.0);
gl.glVertex2f(0.0, 100);
gl.glEnd();
gl.glPopMatrix();
gl.glBindTexture(GL.GL_TEXTURE_2D, 0);

gl.glMatrixMode(GL.GL_PROJECTION);
gl.glPopMatrix();
gl.glMatrixMode(GL.GL_MODELVIEW);
gl.glPopMatrix();

pgl.endGL();
}

only if I use the negative value of mouseY everything is displayed How its intended to be. Can someone explain why this happenes?
Code:

void draw()
{
background(0);
PGraphicsOpenGL pgl = (PGraphicsOpenGL) g; // g may change
GL gl = pgl.beginGL(); // always use the GL object returned by beginGL

// Binding FBO.
gl.glBindFramebufferEXT(GL.GL_FRAMEBUFFER_EXT, drawFBO[0]);

// Drawing to the first color attachement of drawFBO (this is where drawTex is attached to).
gl.glDrawBuffer(GL.GL_COLOR_ATTACHMENT0_EXT);

// Setting orthographic projection.
gl.glMatrixMode(GL.GL_PROJECTION);
gl.glPushMatrix();
gl.glLoadIdentity();
gl.glOrtho(0.0, texWidth, 0.0, texHeight, -100.0, +100.0);
gl.glMatrixMode(GL.GL_MODELVIEW);
gl.glPushMatrix();
gl.glLoadIdentity();

gl.glViewport(0, 0, texWidth, texHeight);

// Just rendering a red quad.
gl.glDisable(GL.GL_TEXTURE_2D);
gl.glColor4f(1.0, 0.0, 0.0, 1.0);
gl.glBegin(GL.GL_QUADS);
gl.glVertex2f(0.0, 0.0);
gl.glVertex2f(texWidth, 0.0);
gl.glVertex2f(texWidth, texHeight);
gl.glVertex2f(0.0, texHeight);
gl.glEnd();

gl.glMatrixMode(GL.GL_PROJECTION);
gl.glPopMatrix();
gl.glMatrixMode(GL.GL_MODELVIEW);
gl.glPopMatrix();

// Unbinding drawFBO. Now drawing to screen again.
gl.glBindFramebufferEXT(GL.GL_FRAMEBUFFER_EXT, 0);

// Setting orthographic projection.
gl.glMatrixMode(GL.GL_PROJECTION);
gl.glPushMatrix();
gl.glLoadIdentity();
gl.glOrtho(0.0, width, 0.0, height, -100.0, +100.0);
gl.glMatrixMode(GL.GL_MODELVIEW);
gl.glPushMatrix();
gl.glLoadIdentity();

gl.glViewport(0, 0, width, height);

// Drawing texture to screen.
gl.glEnable(GL.GL_TEXTURE_2D);
gl.glActiveTexture(GL.GL_TEXTURE0);
gl.glBindTexture(GL.GL_TEXTURE_2D, drawTex[0]);
gl.glPushMatrix();
gl.glTranslatef(mouseX, -mouseY+height, 0);
gl.glBegin(GL.GL_QUADS);
gl.glTexCoord2f(1.0, 0.0);
gl.glVertex2f(0.0, 0.0);

gl.glTexCoord2f(0.0, 0.0);
gl.glVertex2f(100, 0.0);

gl.glTexCoord2f(0.0, 1.0);
gl.glVertex2f(100, 100);

gl.glTexCoord2f(1.0, 1.0);
gl.glVertex2f(0.0, 100);
gl.glEnd();
gl.glPopMatrix();
gl.glBindTexture(GL.GL_TEXTURE_2D, 0);

gl.glMatrixMode(GL.GL_PROJECTION);
gl.glPopMatrix();
gl.glMatrixMode(GL.GL_MODELVIEW);
gl.glPopMatrix();

pgl.endGL();
}


I am on a first generation mac book pro if that matters.
Re: OpenGL Offscreen buffer for glow effect
Reply #18 - May 3rd, 2008, 8:23pm
 
This is because the OPENGL coord system has its origin in the middle of the screen (0, 0) . Y goes up and X to the right. The upper left corner is (-1, 1), the upper right (1, 1), down right (1, -1), down left (-1, -1)
Re: OpenGL Offscreen buffer for glow effect
Reply #19 - May 3rd, 2008, 10:54pm
 
If you get the glow to work I'd love to see an example.
Re: OpenGL Offscreen buffer for glow effect
Reply #20 - May 4th, 2008, 3:04pm
 
well  no matter what I change about the texCoords. It moves vertically flipped. weird.
Re: OpenGL Offscreen buffer for glow effect
Reply #21 - May 4th, 2008, 11:04pm
 
Flipping the t-coordinates of the texture should work, i.e.:
gl.glTexCoord2f(s, t); -> gl.glTexCoord2f(s, 1.0 - t);

Anyways, I finished a new version of the gltexture library (but I renamed to glgraphics), which includes a bloom effect in the examples:

http://codeanticode.wordpress.com/2008/05/04/gltexture-renamed-to-glgraphics/

It's quite experimental (requires replacing the default opengl.jar file with one provided in the package), so make a backup before trying it out.
Re: OpenGL Offscreen buffer for glow effect
Reply #22 - May 5th, 2008, 2:51am
 
thanks for sharing that! Thats really helpful allready. Anyway s my mainproblem is as you can propably tell the render to texture thing.

Is there some rtt stuff implemented in your library allready? because using the pixels[] array to create a texture is too slow.
Re: OpenGL Offscreen buffer for glow effect
Reply #23 - May 5th, 2008, 4:02am
 
Look at the OffScreen example included with the library. It implements render-to-texture by means of the GLGraphics object used as an off-sreen buffer.
Re: OpenGL Offscreen buffer for glow effect
Reply #24 - May 5th, 2008, 10:18am
 
I get some OpenGL errors in the example:

GL_ERROR at render_triangles in: 0506  UNKNOWN
GL_ERROR at render_triangles out: 0506  UNKNOWN
GL_ERROR at non-binding 6: 0506  UNKNOWN
GL_ERROR at non-binding 6: 0506  UNKNOWN

anyways I am rather looking for a way to render something from the screen to texture. As far as I can tell I would have to use the pixels array to do that in your example which is what I want to avoid.

PS: I am on a Mac Book pro. Thanks for your patience!
Re: OpenGL Offscreen buffer for glow effect
Reply #25 - May 5th, 2008, 10:28am
 
I have combined "BloomEffect" with "Offscreen", axes are flipped but works very fine!

...


code -> http://www.owimahn.de/processing_offscreenbloom.pde
Re: OpenGL Offscreen buffer for glow effect
Reply #26 - May 5th, 2008, 5:35pm
 
Cool. To flip the textures add the following code at the end of the setup function:

srcTex.setFlippedY(true);
bloomMask.setFlippedY(true);
destTex.setFlippedY(true);
tex0.setFlippedY(true);
tex2.setFlippedY(true);    
tex4.setFlippedY(true);    
tex8.setFlippedY(true);
tex16.setFlippedY(true);
Re: OpenGL Offscreen buffer for glow effect
Reply #27 - May 5th, 2008, 5:39pm
 
Do you get this error in the offscreen example What about the other examples I have tried the lib on a iMac with a ATI card (2600 I think) and works fine.

Did you make sure to replace the original version of opengl.jar with the one provided along with the library

In any case, the offscreen rendering is not using the pixel array at all. It renders directly to an opengl texture inside the GLGraphics object.

moka wrote on May 5th, 2008, 10:18am:
I get some OpenGL errors in the example:

GL_ERROR at render_triangles in: 0506  UNKNOWN
GL_ERROR at render_triangles out: 0506  UNKNOWN
GL_ERROR at non-binding 6: 0506  UNKNOWN
GL_ERROR at non-binding 6: 0506  UNKNOWN

anyways I am rather looking for a way to render something from the screen to texture. As far as I can tell I would have to use the pixels array to do that in your example which is what I want to avoid.

PS: I am on a Mac Book pro. Thanks for your patience!

Re: OpenGL Offscreen buffer for glow effect
Reply #28 - May 5th, 2008, 5:41pm
 
for some reason the offscreen buffer does not work on my mac book pro. The direct code with the red square works but the one in your example folder does not work.

this is the whole error:

Framebuffer Object error!
Framebuffer Object error!

GL_ERROR at render_triangles in: 0506  UNKNOWN
GL_ERROR at render_triangles out: 0506  UNKNOWN
GL_ERROR at render_lines out: 0506  UNKNOWN
GL_ERROR at non-binding 6: 0506  UNKNOWN
GL_ERROR at non-binding 6: 0506  UNKNOWN
GL_ERROR at non-binding 6: 0506  UNKNOWN
GL_ERROR at non-binding 6: 0506  UNKNOWN
GL_ERROR at non-binding 6: 0506  UNKNOWN
GL_ERROR at non-binding 6: 0506  UNKNOWN
GL_ERROR at non-binding 6: 0506  UNKNOWN
GL_ERROR at non-binding 6: 0506  UNKNOWN
GL_ERROR at non-binding 6: 0506  UNKNOWN
GL_ERROR at non-binding 6: 0506  UNKNOWN
GL_ERROR at non-binding 6: 0506  UNKNOWN
GL_ERROR at non-binding 6: 0506  UNKNOWN
GL_ERROR at non-binding 6: 0506  UNKNOWN
GL_ERROR at non-binding 6: 0506  UNKNOWN
GL_ERROR at render_triangles in: 0506  UNKNOWN
GL_ERROR at render_triangles out: 0506  UNKNOWN
GL_ERROR at non-binding 6: 0506  UNKNOWN
GL_ERROR at non-binding 6: 0506  UNKNOWN

can I change anything about it?

//EDIT: Oh I guess we posted at the same time...wait a second

EDIT2:

Well I replaced eversything and the other examples work fine. Only when it comes to the FBO things I get the error messages. I have a ATI Radeon X1600 in my Mac book pro.
Re: OpenGL Offscreen buffer for glow effect
Reply #29 - May 5th, 2008, 6:24pm
 
setFlippedY works. Thank you a lot for this lib! Everything fine on Macbook Pro 2.2 GHz Intel with NVIDIA GeForce 8600M GT

Quote:


import processing.opengl.*;
import codeanticode.glgraphics.*;

GLTexture srcTex, bloomMask, destTex;
GLTexture tex0, tex2, tex4, tex8, tex16;
GLTextureFilter extractBloom, blur, blend4, tonemap;
GLTextureParameters floatTexParams;
GLTextureFilterParameters bloomParam, toneParam;
GLGraphics glg1;

void setup() {
 size(800, 600, GLConstants.GLGRAPHICS);

 // Loading required filters.
 extractBloom = new GLTextureFilter(this, "ExtractBloom.xml");
 blur = new GLTextureFilter(this, "Blur.xml");
 blend4 = new GLTextureFilter(this, "Blend4.xml", 4);  
 tonemap = new GLTextureFilter(this, "ToneMap.xml", 2);

 bloomParam = new GLTextureFilterParameters(this);
 bloomParam.parFlt1 = 0.99; // bright threshold;        
 toneParam = new GLTextureFilterParameters(this);
 toneParam.parFlt1 = 0.86; // exposure;
 toneParam.parFlt2 = 0.5;  // bloom factor;
 toneParam.parFlt3 = 0.9;  // bright threshold;  

 glg1 = new GLGraphics(width, height, this, true);
 srcTex = glg1.getTexture();
 int w = srcTex.width;
 int h = srcTex.height;
 destTex = new GLTexture(this, w, h);

 // Initializing bloom mask and blur textures.
 floatTexParams = new GLTextureParameters();
 floatTexParams.format = GLTexture.FLOAT4;
 floatTexParams.minFilter = GLTexture.LINEAR;
 floatTexParams.magFilter = GLTexture.LINEAR;    

 bloomMask = new GLTexture(this, w, h, floatTexParams);
 tex0 = new GLTexture(this, w, h, floatTexParams);
 tex2 = new GLTexture(this, w / 2, h / 2, floatTexParams);
 tex4 = new GLTexture(this, w / 4, h / 4, floatTexParams);
 tex8 = new GLTexture(this, w / 8, h / 8, floatTexParams);
 tex16 = new GLTexture(this, w / 16, h / 16, floatTexParams);    

 srcTex.setFlippedY(true);
 bloomMask.setFlippedY(true);
 destTex.setFlippedY(true);
 tex0.setFlippedY(true);
 tex2.setFlippedY(true);    
 tex4.setFlippedY(true);    
 tex8.setFlippedY(true);
 tex16.setFlippedY(true);
}

void draw() {
 glg1.beginDraw();
 glg1.stroke(255);
 glg1.background(0);    
 glg1.fill(255, 0, 0);
 glg1.rect(30, 30, 300, 200);
 glg1.stroke(0,255, 0);              
 glg1.line(0, 0, mouseX, mouseY);
 glg1.endDraw();

 float fx = float(mouseX) / width;
 float fy = float(mouseY) / height;

 bloomParam.parFlt1 = fx;
 toneParam.parFlt1 = fy;    
 toneParam.parFlt3 = fx;

 // Extracting the bright regions from input texture.
 srcTex.filter(extractBloom, tex0, bloomParam);

 // Downsampling with blur.
 tex0.filter(blur, tex2);
 tex2.filter(blur, tex4);    
 tex4.filter(blur, tex8);    
 tex8.filter(blur, tex16);    

 // Blending downsampled textures.
 blend4.apply(new GLTexture[]{tex2, tex4, tex8, tex16  }, new GLTexture[]{ bloomMask });

 // Final tone mapping into destination texture.
 tonemap.apply(new GLTexture[]{srcTex, bloomMask  }, new GLTexture[]{destTex  }, toneParam);

 image(destTex, 0, 0, width, height);
}


Pages: 1 2 3