Author Topic: Shaders and Shiny Objects  (Read 3916 times)

0 Members and 2 Guests are viewing this topic.

Charles Pegge

  • Guest
Shaders and Shiny Objects
« on: March 10, 2015, 03:53:12 PM »
My first attempt at a glass Structure, using a cubic reflection shader :)

SkyBox images and 32 bit Binary of viewer included.

This demo is based on examples/Opengl/GLSLWIP.o2bas, which is a jumble of experiments.

.

Mike Lobanovsky

  • Guest
Re: Shaders and Shiny Objects
« Reply #1 on: March 10, 2015, 04:27:25 PM »



I think you're past the point of no return, Charles.

Now that you've seen and mastered the beauty of shaders and harmony of GLSL, you will never come back to OpenGL's immediate mode or draw lists again.

I had been able to (intentionally) refrain from this for two and a half years before I finally surrendered. :D

Charles Pegge

  • Guest
Re: Shaders and Shiny Objects
« Reply #2 on: March 11, 2015, 07:37:38 AM »

Mike,

The shader and a few more specimens:

Code: [Select]
  'LIGHT MATERIAL AND CUBIC REFLECTION ALPHA SHADER
  '================================================
  '
  vs[7]=
  "
  // User-specified uniforms
  uniform vec3 CameraPosition;
 
  varying vec3 vN;
  varying vec3 v;
  varying vec2 texCoord;
  varying vec3 NormalDirection;
  varying vec3 ViewDirection;
  void main(void) 
  {     
    texCoord = gl_MultiTexCoord0;
    v  = vec3(gl_ModelViewMatrix * gl_Vertex);       
    vN = normalize(gl_NormalMatrix * gl_Normal);
    NormalDirection = normalize(vec3(vec4(gl_Normal, 0.0) * gl_ModelViewMatrixInverse));
    ViewDirection = vec3(gl_ModelViewMatrix * gl_Vertex
    - vec4(CameraPosition, 1.0));
    gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
  }
  "
  fs[7]=
  "
  uniform vec4 Tint;
  uniform sampler2D TextureUnit;
  uniform samplerCube CubeMap;   

  varying vec3 vN;
  varying vec3 v;
  varying vec2 texCoord;
  //for cubic reflection
  varying vec3 NormalDirection;
  varying vec3 ViewDirection;
  #define MAX_LIGHTS 3
  void main (void)
  {
    vec3  N = normalize(vN);
    vec4  finalColor = Tint;
    float Alpha = Tint.a;
    vec3 reflectedDirection = reflect(ViewDirection, NormalDirection);
   
    for (int i=0;i<MAX_LIGHTS;i++)
    {
      vec3 L = normalize(gl_LightSource[i].position.xyz - v);
      vec3 E = normalize(-v); // we are in Eye Coordinates, so EyePos is (0,0,0)
      vec3 R = normalize(-reflect(L,N));
   
      //calculate Ambient Term:
      vec4 Iamb = gl_FrontLightProduct[i].ambient;
      //calculate Diffuse Term:
      vec4 Idiff = gl_FrontLightProduct[i].diffuse * max(dot(N,L), 0.0);
      Idiff = clamp(Idiff, 0.0, 1.0);
   
      // calculate Specular Term:
      vec4 Ispec = gl_FrontLightProduct[i].specular
      * pow(max(dot(R,E),0.0),0.3*gl_FrontMaterial.shininess);
      Ispec = clamp(Ispec, 0.0, 1.0);
   
      finalColor +=
      Iamb
      +Idiff
      +Ispec;
    }
    finalColor *=
    ///texture2D(TextureUnit, texCoord);
    textureCube(CubeMap, reflectedDirection);
    gl_FragColor = vec4(vec3(finalColor),Alpha);
  }
  "





.

Mike Lobanovsky

  • Guest
Re: Shaders and Shiny Objects
« Reply #3 on: March 11, 2015, 09:26:38 AM »
Looks very attractive, Charles. :)

Can you make a little experiment? Take two transparent glassy cubes (like in your glassy Sierpinsky pyramid further above) of slightly different tint, make them rotate slowly around their axes and also around each other. We can see if their transparent surfaces are rendered correctly on the z axis when overlapping one another as the cubes rotate, i.e. if there are no depth artifacts as in Patrice's renderer with simple transparency illumination model.

Charles Pegge

  • Guest
Re: Shaders and Shiny Objects
« Reply #4 on: March 11, 2015, 12:55:15 PM »
Hi Mike,

The Glass Sierpinski pyramid does not stand up to close scrutiny. It has no z ordering. Sometimes the nearest struts will occlude those behind them, and sometimes you can see the far struts through them correctly, but the reflections are a good disguise. :)

When the central sphere is made transparent the z-order artefacts become immediately apparent.

Since resolving multiple transparent surfaces correctly is quite intensive, one could have a fine rendering mode for stills, to take as long as is necessary to render a high quality image, whenever the scene stops moving.
 

Mike Lobanovsky

  • Guest
Re: Shaders and Shiny Objects
« Reply #5 on: March 11, 2015, 01:38:04 PM »
I suspected it when I saw the pyramid but just wanted to make sure. Such a simple alpha shader shouldn't have done the job correctly. It's indeed rather difficult to render glass against glass. :)