Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
154 views
in Technique[技术] by (71.8m points)

ios - How to get a "Glow" shader effect in OpenGL ES 2.0?

I'm writing a 3D app for iOS. I'm new to OpenGL ES 2.0, so I'm still getting myself around writing basic shaders. I really need to implement a "Glow" effect on some of my models, based on the texturing.

Here's a sample:

Bloom/Glow from www.skillmanmedia.com/realbasic/bloomsnapshot.jpg.

I'm looking for code examples for OpenGL ES 2.0. Most code I find on the internet is either for desktop OpenGL or D3D.

Any ideas?

question from:https://stackoverflow.com/questions/8166384/how-to-get-a-glow-shader-effect-in-opengl-es-2-0

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

First of all there are tons of algorithms and techniques to generate a glow effect. I just want to present one possibility.

Create a Material that is luminescent. For this I use a modified Blinn-Phong light model, where the direction to the light source is always the inverse direction of the normal vector of the fragment.

varying vec3 vertPos;
varying vec3 vertNV;
varying vec3 vertCol;

uniform float u_glow;

void main()
{
    vec3 color = vertCol;

    float shininess = 10.0;
    vec3  normalV = normalize( vertNV );
    vec3  eyeV    = normalize( -vertPos );
    vec3  halfV   = normalize( eyeV + normalV );
    float NdotH   = max( 0.0, dot( normalV, halfV ) );
    float glowFac = ( shininess + 2.0 ) * pow( NdotH, shininess ) / ( 2.0 * 3.14159265 );

    gl_FragColor = vec4( u_glow * (0.1 + color.rgb * glowFac * 0.5), 1.0 );
}

In a second step a gaussian blur algorithm is performed on the output. The scene is written to frame buffer with a texture bound to the color plane. A screen space pass uses the texture as the input to blur the output.
For performance reasons, the blur algorithm is first performed along the X-axis of the viewport and in a further step along the Y-axis of the viewport.

varying vec2 vertPos;
uniform sampler2D u_textureCol;
uniform vec2 u_textureSize;
uniform float u_sigma;
uniform int u_width;

float CalcGauss( float x, float sigma ) 
{
  float coeff = 1.0 / (2.0 * 3.14157 * sigma);
  float expon = -(x*x) / (2.0 * sigma);
  return (coeff*exp(expon));
}

void main()
{
    vec2 texC = vertPos.st * 0.5 + 0.5;
    vec4 texCol = texture( u_textureCol, texC );
    vec4 gaussCol = vec4( texCol.rgb, 1.0 );
    vec2 step = 1.0 / u_textureSize;
    for ( int i = 1; i <= u_width; ++ i )
    {
        vec2 actStep = vec2( float(i) * step.x, 0.0 );   // this is for the X-axis
        // vec2 actStep = vec2( 0.0, float(i) * step.y );   this would be for the Y-axis

        float weight = CalcGauss( float(i) / float(u_width), u_sigma );
        texCol = texture2D( u_textureCol, texC + actStep );    
        gaussCol += vec4( texCol.rgb * weight, weight );
        texCol = texture2D( u_textureCol, texC - actStep );
        gaussCol += vec4( texCol.rgb * weight, weight );
    }
    gaussCol.rgb /= gaussCol.w;
    gl_FragColor = vec4( gaussCol.rgb, 1.0 );
}

For the implementation of a blur algorithm see also the answer to the questions:

See the following similar WebGL example which puts all together:

var readInput = true;
function changeEventHandler(event){
    readInput = true;
}
  
(function loadscene() {
  
  var resize, gl, progDraw, progBlurX, progPost, vp_size, blurFB;
  var bufCube = {};
  var bufQuad = {};
  var shininess = 10.0;
  var glow = 10.0;
  var sigma = 0.8;
  
  function render(delteMS){

      //if ( readInput ) {
          readInput = false;
          var sliderScale = 100;
          shininess = document.getElementById( "shine" ).value;
          glow      = document.getElementById( "glow" ).value / sliderScale;
          sigma     = document.getElementById( "sigma" ).value / sliderScale;
      //}

      Camera.create();
      Camera.vp = vp_size;
          
      gl.enable( gl.DEPTH_TEST );
      gl.clearColor( 0.0, 0.0, 0.0, 1.0 );
      gl.clear( gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT );

      // set up framebuffer
      gl.bindFramebuffer( gl.FRAMEBUFFER, blurFB[0] );
      gl.viewport( 0, 0, blurFB[0].width, blurFB[0].height );
      gl.clear( gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT );
  
      // set up draw shader
      ShaderProgram.Use( progDraw.prog );
      ShaderProgram.SetUniformM44( progDraw.prog, "u_projectionMat44", Camera.Perspective() );
      ShaderProgram.SetUniformM44( progDraw.prog, "u_viewMat44", Camera.LookAt() );
      var modelMat = IdentityMat44()
      modelMat = RotateAxis( modelMat, CalcAng( delteMS, 13.0 ), 0 );
      modelMat = RotateAxis( modelMat, CalcAng( delteMS, 17.0 ), 1 );
      ShaderProgram.SetUniformM44( progDraw.prog, "u_modelMat44", modelMat );
      ShaderProgram.SetUniformF1( progDraw.prog, "u_shininess", shininess );
      ShaderProgram.SetUniformF1( progDraw.prog, "u_glow", glow );
      
      // draw scene
      VertexBuffer.Draw( bufCube );

      // set blur-X framebuffer and bind frambuffer texture
      gl.bindFramebuffer( gl.FRAMEBUFFER, blurFB[1] );
      gl.viewport( 0, 0, blurFB[1].width, blurFB[1].height );
      gl.clear( gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT );
      var texUnit = 1;
      gl.activeTexture( gl.TEXTURE0 + texUnit );
      gl.bindTexture( gl.TEXTURE_2D, blurFB[0].color0_texture );

      // set up blur-X shader
      ShaderProgram.Use( progBlurX.prog );
      ShaderProgram.SetUniformI1( progBlurX.prog , "u_texture", texUnit )
      ShaderProgram.SetUniformF2( progBlurX.prog , "u_textureSize", vp_size );
      ShaderProgram.SetUniformF1( progBlurX.prog , "u_sigma", sigma )

      // draw full screen space
      gl.enableVertexAttribArray( progBlurX.inPos );
      gl.bindBuffer( gl.ARRAY_BUFFER, bufQuad.pos );
      gl.vertexAttribPointer( progBlurX.inPos, 2, gl.FLOAT, false, 0, 0 ); 
      gl.bindBuffer( gl.ELEMENT_ARRAY_BUFFER, bufQuad.inx );
      gl.drawElements( gl.TRIANGLES, 6, gl.UNSIGNED_SHORT, 0 );
      gl.disableVertexAttribArray( progBlurX.inPos );

      // reset framebuffer and bind frambuffer texture
      gl.bindFramebuffer( gl.FRAMEBUFFER, null );
      gl.viewport( 0, 0, vp_size[0], vp_size[1] );
      gl.clear( gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT );
      texUnit = 2;
      gl.activeTexture( gl.TEXTURE0 + texUnit );
      gl.bindTexture( gl.TEXTURE_2D, blurFB[1].color0_texture );

      // set up pst process shader
      ShaderProgram.Use( progPost.prog );
      ShaderProgram.SetUniformI1( progPost.prog, "u_texture", texUnit )
      ShaderProgram.SetUniformF2( progPost.prog, "u_textureSize", vp_size );
      ShaderProgram.SetUniformF1( progPost.prog, "u_sigma", sigma );

      // draw full screen space
      gl.enableVertexAttribArray( progPost.inPos );
      gl.bindBuffer( gl.ARRAY_BUFFER, bufQuad.pos );
      gl.vertexAttribPointer( progPost.inPos, 2, gl.FLOAT, false, 0, 0 ); 
      gl.bindBuffer( gl.ELEMENT_ARRAY_BUFFER, bufQuad.inx );
      gl.drawElements( gl.TRIANGLES, 6, gl.UNSIGNED_SHORT, 0 );
      gl.disableVertexAttribArray( progPost.inPos );

      requestAnimationFrame(render);
  }
  
  function resize() {
      //vp_size = [gl.drawingBufferWidth, gl.drawingBufferHeight];
      vp_size = [window.innerWidth, window.innerHeight]
      canvas.width = vp_size[0];
      canvas.height = vp_size[1];

      var fbsize = Math.max(vp_size[0], vp_size[1])-1;
      fbsize = 1 << 31 - Math.clz32(fbsize); // nearest power of 2
      fbsize = fbsize * 2

      blurFB = [];
      for ( var i = 0; i < 2; ++ i ) {
          fb = gl.createFramebuffer();
          fb.width = fbsize;
          fb.height = fbsize;
          gl.bindFramebuffer( gl.FRAMEBUFFER, fb );
          fb.color0_texture = gl.createTexture();
          gl.bindTexture( gl.TEXTURE_2D, fb.color0_texture );
          gl.texParameteri( gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST );
          gl.texParameteri( gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST );
          gl.texImage2D( gl.TEXTURE_2D, 0, gl.RGBA, fb.width, fb.height, 0, gl.RGBA, gl.UNSIGNED_BYTE, null );
          fb.renderbuffer = gl.createRenderbuffer();
          gl.bindRenderbuffer( gl.RENDERBUFFER, fb.renderbuffer );
          gl.renderbufferStorage( gl.RENDERBUFFER, gl.DEPTH_COMPONENT16, fb.width, fb.height );
          gl.framebufferTexture2D( gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, fb.color0_texture, 0 );
          gl.framebufferRenderbuffer( gl.FRAMEBUFFER, gl.DEPTH_ATTACHMENT, gl.RENDERBUFFER, fb.renderbuffer );
          gl.bindTexture( gl.TEXTURE_2D, null );
          gl.bindRenderbuffer( gl.RENDERBUFFER, null );
          gl.bindFramebuffer( gl.FRAMEBUFFER, null );
          blurFB.push( fb );
      }
  }
  
  function initScene() {
  
      canvas = document.getElementById( "canvas");
      gl = canvas.getContext( "experimental-webgl" );
      if ( !gl )
        return null;
  
      progDraw = {}
      progDraw.prog = ShaderProgram.Create( 
        [ { source : "draw-shader-vs", stage : gl.VERTEX_SHADER },
          { source : "draw-shader-fs", stage : gl.FRAGMENT_SHADER }
        ] );
      if ( !progDraw.prog )
          return null;
      progDraw.inPos = gl.getAttribLocation( progDraw.prog, "inPos" );
      progDraw.inNV  = gl.getAttribLocation( progDraw.prog, "inNV" );
      progDraw.inCol = gl.getAttribLocation( progDraw.prog, "inCol" );

      progBlurX = {}
      progBlurX.prog = ShaderProgram.Create( 
        [ { source : "post-shader-vs", stage : gl.VERTEX_SHADER },
          { source : "blurX-shader-fs", stage : gl.FRAGMENT_SHADER }
        ] );
      progBlurX.inPos = gl.getAttribLocation( progBlurX.prog, "inPos" );
      if ( !progBlurX.prog )
          return;    

      progPost = {}
      progPost.prog = ShaderProgram.Create( 
        [ { source : "post-shader-vs", stage : gl.VERTEX_SHADER },
          { source : "blurY-shader-fs", stage : gl.FRAGMENT_SHADER }
        ] );
      progPost.inPos = gl.getAttribLocation( progPost.prog, "inPos" );
      if ( !progPost.prog )
          return;
      
      // create cube
      var cubePos = [
        -1.0, -1.0,  1.0,  1.0, -1.0,  1.0,  1.0,  1.0,  1.0, -1.0,  1.0,  1.0,
        -1.0, -1.0, -1.0,  1.0, -1.0, -1.0,  1.0,  1.0, -1.0, -1.0,  1.0, -1.0 ];
      var cubeCol = [ 1.0, 0.0, 0.0, 1.0, 0.5, 0.0, 1.0, 0.0, 1.0, 1.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0 ];
      var cubeHlpInx = [ 0, 1, 2, 3, 1, 5, 6, 2, 5, 4, 7, 6, 4, 0, 3, 7, 3, 2, 6, 7, 1, 0, 4, 5 ];  
      var cubePosData = [];
      for ( var i = 0; i < cubeHlpInx.length; ++ i ) {
        cubePosData.push( cubePos[cubeHlpInx[i]*3], cubePos[cubeHlpInx[i]*3+1], cubePos[cubeHlpInx[i]*3+2] );
      }
      var cubeNVData = [];
      for ( var i1 = 0; i1 < cubeHlpInx.length; i1 += 4 ) {
      var nv = [0, 0, 0];
      for ( i2 = 0; i2 < 4; ++ i2 ) {
          var i = i1 + i2;
          nv[0] += cubePosData[i*3]; nv[1] += cubePosData[i*3+1]; nv[2] += cubePosData[i*3+2];
      }
      for ( i2 = 0; i2 < 4; ++ i2 )
        cubeNVData.push( nv[0], nv[1], nv[2] );
      }
      var cubeColData = [];
      for ( var is = 0; is < 6; ++ is ) {
        for ( var ip = 0; ip < 4; ++ ip ) {
         cubeColData.push( cubeCol[is*3], cubeCol[is*3+1], cubeCol[is*3+2] ); 
        }
      }
      var cubeInxData = [];
      for ( var i = 0; i < cubeHlpInx.length; i += 4 ) {
        cubeInxData.push( i, i+1, i+2, i, i+2, i+3 );   
      }
      bufCube = VertexBuffer.Create(
      [ { data : cubePosData, attrSize : 3, attrLoc : progDraw.inPos },
        { data : cubeNVData,  attrSize : 3, attrLoc : progDraw.inNV },
        { data : cubeColData, attrSize : 3, attrLoc : progDraw.inCol } ],
        cubeInxDat

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...