Author Topic: Animated Backgrounds?  (Read 36507 times)

Michael Lobko-Lobanovsky

  • Administrator
  • *****
  • Posts: 1481
Re: Animated Backgrounds?
« Reply #60 on: December 12, 2019, 05:59:04 am »
Good morning, my friend,

Here are a few animated backgrounds to your morning coffee. :)

Just put the files in the \Animated resources folder.


P.S. Haldin looks very good for spaceship flies-by.
Mike
(3.6GHz Intel Core i5 Quad w/ 16GB RAM, nVidia GTX 1060Ti w/ 6GB VRAM, Windows 7 Ultimate Sp1)

Patrice Terrier

  • Administrator
  • *****
  • Posts: 1903
    • zapsolution
Re: Animated Backgrounds?
« Reply #61 on: December 12, 2019, 10:34:48 am »
Thank you my friend!

I shall probably make a little change to OR, to be able to use @ to load a .fs file from a specific folder (just like for static wallpaper).

I would like to convert some of the glsl code to plain OpenGL to turn them into BB plugins.

Do you think this is feasible without using shaders?
I would like to start first with a simple one, like
https://www.shadertoy.com/view/ls3BDH

Added:
#wallpaper @shadertoy.fs
is working, i shall post the sync files in a couple of hours (have to go outside first)

Sync
Here is the sync.zip file, search for: // PAT: 12-12-2019
« Last Edit: December 12, 2019, 02:32:55 pm by Patrice Terrier »
Patrice
(Always working with the latest Windows version available...)

Michael Lobko-Lobanovsky

  • Administrator
  • *****
  • Posts: 1481
Re: Animated Backgrounds?
« Reply #62 on: December 12, 2019, 02:48:22 pm »
Re. plugins, I'm afraid it's infeasible except in a very few simplest cases. Shaders work several hundred times faster than immediate OpenGL in both maths and graphics. Besides, you'll have to recreate the entire framework of GLSL language in C to mimic its heavily overloaded maths for 2-, 3- and 4-dimensional vectors and matrices, interpolation, texture access, etc.

For all intents and purposes, it will be more practical to add to the BB three functions to load, compile and link shaders, and a dedicated render procedure to output them to the existing OpenGL window without VBOs or post-processing.

Re: mods, thanks for the zip, I'll merge them tonight.
Mike
(3.6GHz Intel Core i5 Quad w/ 16GB RAM, nVidia GTX 1060Ti w/ 6GB VRAM, Windows 7 Ultimate Sp1)

Patrice Terrier

  • Administrator
  • *****
  • Posts: 1903
    • zapsolution
Re: Animated Backgrounds?
« Reply #63 on: December 13, 2019, 01:48:27 pm »
To download the default shadertoy textures
https://shadertoyunofficial.wordpress.com/

I did try to convert shadertoy scripts to .fs, but i failed miserably, i have no real clue on what to do to make them work in OR  :-[

Added

I was able to make this one work (combustible.fs)

Code: [Select]
/*
    Combustible Voronoi Layers
--------------------------

    The effect itself is nothing new or exciting, just some moving 3D Voronoi layering.
    However, the fire palette might prove useful to some.

*/

uniform vec4 iDate; // (year, month, day, time in seconds) MLL: year/month/day not used
uniform vec3 iMouse; // mouse pixel coords. xy: current (if MLB down); if vec3 then z: click
uniform vec2 iResolution; // viewport resolution (in pixels)
uniform float iTime; // shader playback time (in seconds)


// This is my favorite fire palette. It's trimmed down for shader usage, and is based on an
// article I read at Hugo Elias's site years ago. I'm sure most old people, like me, have
// visited his site at one time or another:
//
// http://freespace.virgin.net/hugo.elias/models/m_ffire.htm
//
vec3 firePalette(float i){

    float T = 1400. + 1300.*i; // Temperature range (in Kelvin).
    vec3 L = vec3(7.4, 5.6, 4.4); // Red, green, blue wavelengths (in hundreds of nanometers).
    L = pow(L,vec3(5.0)) * (exp(1.43876719683e5/(T*L))-1.0);
    return 1.0-exp(-5e8/L); // Exposure level. Set to "50." For "70," change the "5" to a "7," etc.
}

/*
vec3 firePalette(float i){

    float T = 1400. + 1300.*i; // Temperature range (in Kelvin).
    // Hardcode red, green and blue wavelengths (in hundreds of nanometers).
    vec3 L = (exp(vec3(19442.7999572, 25692.271372, 32699.2544734)/T)-1.0);
    // Exposure level. Set to "50" For "70," change the ".5" to a ".7," etc.
    return 1.0-exp(-vec3(22532.6051122, 90788.296915, 303184.239775)*2.*.5/L);
}
*/

// Hash function. This particular one probably doesn't disperse things quite as nicely as some
// of the others around, but it's compact, and seems to work.
//
vec3 hash33(vec3 p){
   
    float n = sin(dot(p, vec3(7, 157, 113)));   
    return fract(vec3(2097152, 262144, 32768)*n);
}

// 3D Voronoi: Obviously, this is just a rehash of IQ's original.
//
float voronoi(vec3 p){

vec3 b, r, g = floor(p);
p = fract(p); // "p -= g;" works on some GPUs, but not all, for some annoying reason.

// Maximum value: I think outliers could get as high as "3," the squared diagonal length
// of the unit cube, with the mid point being "0.75." Is that right? Either way, for this
// example, the maximum is set to one, which would cover a good part of the range, whilst
// dispensing with the need to clamp the final result.
float d = 1.;
     
    // I've unrolled one of the loops. GPU architecture is a mystery to me, but I'm aware
    // they're not fond of nesting, branching, etc. My laptop GPU seems to hate everything,
    // including multiple loops. If it were a person, we wouldn't hang out.
for(int j = -1; j <= 1; j++) {
    for(int i = -1; i <= 1; i++) {
   
    b = vec3(i, j, -1);
    r = b - p + hash33(g+b);
    d = min(d, dot(r,r));
   
    b.z = 0.0;
    r = b - p + hash33(g+b);
    d = min(d, dot(r,r));
   
    b.z = 1.;
    r = b - p + hash33(g+b);
    d = min(d, dot(r,r));
   
    }
}

return d; // Range: [0, 1]
}

// Standard fBm function with some time dialation to give a parallax
// kind of effect. In other words, the position and time frequencies
// are changed at different rates from layer to layer.
//
float noiseLayers(in vec3 p) {

    // Normally, you'd just add a time vector to "p," and be done with
    // it. However, in this instance, time is added seperately so that
    // its frequency can be changed at a different rate. "p.z" is thrown
    // in there just to distort things a little more.
    vec3 t = vec3(0., 0., p.z+iTime*1.5);

    const int iter = 5; // Just five layers is enough.
    float tot = 0., sum = 0., amp = 1.; // Total, sum, amplitude.

    for (int i = 0; i < iter; i++) {
        tot += voronoi(p + t) * amp; // Add the layer to the total.
        p *= 2.0; // Position multiplied by two.
        t *= 1.5; // Time multiplied by less than two.
        sum += amp; // Sum of amplitudes.
        amp *= 0.5; // Decrease successive layer amplitude, as normal.
    }
   
    return tot/sum; // Range: [0, 1].
}

void main()
{
    // Screen coordinates.
vec2 uv = (gl_FragCoord.xy - iResolution.xy*0.5) / iResolution.y;

// Shifting the central position around, just a little, to simulate a
// moving camera, albeit a pretty lame one.
uv += vec2(sin(iTime*0.5)*0.25, cos(iTime*0.5)*0.125);

    // Constructing the unit ray.
vec3 rd = normalize(vec3(uv.x, uv.y, 3.1415926535898/8.));

    // Rotating the ray about the XY plane, to simulate a rolling camera.
float cs = cos(iTime*0.25), si = sin(iTime*0.25);
    // Apparently "r *= rM" can break in some older browsers.
rd.xy = rd.xy*mat2(cs, -si, si, cs);

// Passing a unit ray multiple into the Voronoi layer function, which
// is nothing more than an fBm setup with some time dialation.
float c = noiseLayers(rd*2.);

// Optional: Adding a bit of random noise for a subtle dust effect.
c = max(c + dot(hash33(rd)*2.-1., vec3(0.015)), 0.);

    // Coloring:
   
    // Nebula.
    c *= sqrt(c)*1.5; // Contrast.
    vec3 col = firePalette(c); // Palettization.
    //col = mix(col, col.zyx*0.1+c*0.9, clamp((1.+rd.x+rd.y)*0.45, 0., 1.)); // Color dispersion.
    col = mix(col, col.zyx*0.15+c*0.85, min(pow(dot(rd.xy, rd.xy)*1.2, 1.5), 1.)); // Color dispersion.
    col = pow(col, vec3(1.5));
   
    // The fire palette on its own. Perhaps a little too much fire color.
    //c = pow(c*1.33, 1.5);
    //vec3 col =  firePalette(c);
   
    // Black and white, just to keep the art students happy. :)
//c *= c*1.5;
//vec3 col = vec3(c);

// Done.
gl_FragColor = vec4(sqrt(clamp(col, 0., 1.)), 1.);
}
« Last Edit: December 13, 2019, 02:30:42 pm by Patrice Terrier »
Patrice
(Always working with the latest Windows version available...)

Michael Lobko-Lobanovsky

  • Administrator
  • *****
  • Posts: 1481
Re: Animated Backgrounds?
« Reply #64 on: December 13, 2019, 02:56:28 pm »
Didn't I send you this one?! :o

The \Resource\Animated subfolder should also contain this README.TXT:

!!! SHADER FILES MUST BE SAVED AS PLAIN ASCII TEXT !!!

0. Each shader should explicitly declare at least 4 uniforms:

-- uniform vec4  iDate: year/month/day/time in seconds as floats (year/month/day not implemented for the time being)
-- uniform vec3  iMouse: mouse coords as floats are current on-screen x and y if LMB down, and z = 1.0f if LMB down; otherwise all 0.0f
-- uniform vec2  iResolution: current viewport x and y sizes in pixels as floats
-- uniform float iTime: (in seconds w/ decimal fractions) system time, or time since session start, or time since app start.

Those uniforms may be used or not used in the shader code as needed.

1. Additional uniforms as needed for a specific shader:

-- uniform sampler2D filename_ext|mip_filename_ext|nomip_filename_ext: for as many textures as the shader needs.
   mip_ prefix (optional) means the respective texture should be mipmapped; nomip_ means no mipmapping is allowed.
   Prefixes, if any, are removed automatically, and underscores, replaced with dots to load the respective textures.
   Consequently, all occurrences of iChannel0 to iChannel3 sampler names throughout the code should be replaced with
   those new sampler names.

2. Shader resources (textures, etc.) should be stored in the same folder as the shader.

3. Shaders should contain attribution blurbs wherever available to respect the original author's copyright.

4. The mainImage(...) sub should be re-declared as void main(void) with arguments omitted.

5. All fragCoord and fragColor built-in variables should be renamed gl_FragCoord and gl_FragColor, respectively, throughout the shader code.


This is a full guide on how to convert ShaderToy ONE-BUFFER shaders for use in ObjReader. (one-buffer means those that run in one render pass and occupy exactly one code tab on their site; multiple code tabs require more than one FBO whereas OR has only one FBO and one render pass ATM)
« Last Edit: December 13, 2019, 03:09:26 pm by Michael Lobko-Lobanovsky »
Mike
(3.6GHz Intel Core i5 Quad w/ 16GB RAM, nVidia GTX 1060Ti w/ 6GB VRAM, Windows 7 Ultimate Sp1)

Patrice Terrier

  • Administrator
  • *****
  • Posts: 1903
    • zapsolution
Re: Animated Backgrounds?
« Reply #65 on: December 13, 2019, 03:04:40 pm »
Quote
Didn't I send you this one?!
NO, you didn't send me any of those listed there:
http://www.objreader.com/index.php?topic=197.msg5718#msg5718

I shall look at an example using textures, to see what to do.
« Last Edit: December 13, 2019, 03:07:09 pm by Patrice Terrier »
Patrice
(Always working with the latest Windows version available...)

Michael Lobko-Lobanovsky

  • Administrator
  • *****
  • Posts: 1481
Re: Animated Backgrounds?
« Reply #66 on: December 13, 2019, 03:13:00 pm »
Please re-read the above README.TXT file carefully. It contains all the information you need on how to fix the code of ST shaders for use in OR. :)
Mike
(3.6GHz Intel Core i5 Quad w/ 16GB RAM, nVidia GTX 1060Ti w/ 6GB VRAM, Windows 7 Ultimate Sp1)

Patrice Terrier

  • Administrator
  • *****
  • Posts: 1903
    • zapsolution
Re: Animated Backgrounds?
« Reply #67 on: December 13, 2019, 03:32:29 pm »
I have re-read the text, but i can't get this one to work

postcard.fs:

// Postcard by nimitz (twitter: @stormoid)
// https://www.shadertoy.com/view/XdBSWd
// License Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
// Contact the author for other licensing options

uniform vec4 iDate; // (year, month, day, time in seconds) MLL: year/month/day not used
uniform vec3 iMouse; // mouse pixel coords. xy: current (if MLB down); if vec3 then z: click
uniform vec2 iResolution; // viewport resolution (in pixels)
uniform float iTime; // shader playback time (in seconds)

uniform sampler2D texture_png; // texture to load

/*
   Implementation of: http://iquilezles.org/www/articles/dynclouds/dynclouds.htm
   
   Added some raymarched mountains and normal mapped water to complete the scene.

   One thing I did differently is modyfying the scale of the fbm based on the distance
   from the shaded clouds allowing for a much less "planar" look to the cloud layer. 
*/

//Compare with simple clouds
//#define BASIC_CLOUDS

#define time iTime*2.
#define FAR 420.

//------------------------------------------------------------------
//----------------------Utility functions---------------------------
//------------------------------------------------------------------
vec3 rotx(vec3 p, float a){
    float s = sin(a), c = cos(a);
    return vec3(p.x, c*p.y - s*p.z, s*p.y + c*p.z);
}
vec3 roty(vec3 p, float a){
    float s = sin(a), c = cos(a);
    return vec3(c*p.x + s*p.z, p.y, -s*p.x + c*p.z);
}
float nmzHash(vec2 q)
{
    uvec2 p = uvec2(ivec2(q));
    p = p*uvec2(374761393U,22695477U) + p.yx;
    p.x = p.x*(p.y^(p.x>>15U));
    return float(p.x^(p.x >> 16U))*(1.0/float(0xffffffffU));
}
float noise(in vec2 p) {
    vec2 ip = floor(p);
    vec2 fp = fract(p);
   vec2 u = fp*fp*(3.0-2.0*fp);
    return -1.0+2.0*mix( mix( nmzHash( ip + vec2(0.0,0.0) ), nmzHash( ip + vec2(1.0,0.0) ), u.x),
                mix(nmzHash( ip + vec2(0.0,1.0) ), nmzHash( ip + vec2(1.0,1.0)), u.x), u.y);
}
//------------------------------------------------------------------
//---------------------------Terrain--------------------------------
//------------------------------------------------------------------
float terrain(in vec2 p)
{
    p*= 0.035;
    float rz = 0.;
    float m = 1.;
    float z = 1.;
    for(int i=0; i<=2; i++)
    {
        rz += (sin(noise(p/m)*1.7)*0.5+0.5)*z;
        m *= -0.25;
        z *= .2;
    }
    rz=exp2(rz-1.5);
    rz -= sin(p.y*.2+sin(p.x*.45));
    return rz*20.-14.;
}

float tmap(in vec3 p){ return p.y-terrain(p.zx);}
//Using "cheap AA" from eiffie (https://www.shadertoy.com/view/XsSXDt)
vec3 tmarch(in vec3 ro, in vec3 rd, in float d)
{
   float precis = 0.01;
    float h=precis*2.0;
    float hm = 100., dhm = 0.;
    for( int i=0; i<15; i++ )
    {   
        d += h = tmap(ro+rd*d)*1.5;
        if (h < hm)
        {
            hm = h;
            dhm = d;
        }
        if( abs(h)<precis||d>FAR ) break;
    }
   return vec3(d, hm, dhm);
}


vec3 normal( in vec3 pos, float t )
{
   float e = 0.001*t;
    vec2  eps = vec2(e,0.0);
    float h = terrain(pos.xz);
    return normalize(vec3( terrain(pos.xz-eps.xy)-h, e, terrain(pos.xz-eps.yx)-h ));
}

float plane( in vec3 ro, in vec3 rd, vec3 c, vec3 u, vec3 v )
{
   vec3 q = ro - c;
   vec3 n = cross(u,v);
    return -dot(n,q)/dot(rd,n);
}
//------------------------------------------------------------------
//-------------------------2d Clouds--------------------------------
//------------------------------------------------------------------
vec3 lgt = normalize(vec3(-1.0,0.1,.0));
vec3 hor = vec3(0);

float nz(in vec2 p){return texture(texture_png, p*.01).x;}
mat2 m2 = mat2( 0.80,  0.60, -0.60,  0.80 );
float fbm(in vec2 p, in float d)
{   
   d = smoothstep(0.,100.,d);
    p *= .3/(d+0.2);
    float z=2.;
   float rz = 0.;
    p  -= time*0.02;
   for (float i= 1.;i <=5.;i++ )
   {
      rz+= (sin(nz(p)*6.5)*0.5+0.5)*1.25/z;
      z *= 2.1;
      p *= 2.15;
        p += time*0.027;
        p *= m2;
   }
    return pow(abs(rz),2.-d);
}

vec4 clouds(in vec3 ro, in vec3 rd, in bool wtr)
{   
   
    //Base sky coloring is from iq's "Canyon" (https://www.shadertoy.com/view/MdBGzG)
    float sun = clamp(dot(lgt,rd),0.0,1.0 );
    hor = mix( 1.*vec3(0.70,1.0,1.0), vec3(1.3,0.55,0.15), 0.25+0.75*sun );
    vec3 col = mix( vec3(0.5,0.75,1.), hor, exp(-(4.+ 2.*(1.-sun))*max(0.0,rd.y-0.05)) );
    col *= 0.4;
   
    if (!wtr)
    {
        col += 0.8*vec3(1.0,0.8,0.7)*pow(sun,512.0);
        col += 0.2*vec3(1.0,0.4,0.2)*pow(sun,32.0);
    }
    else
    {
        col += 1.5*vec3(1.0,0.8,0.7)*pow(sun,512.0);
        col += 0.3*vec3(1.0,0.4,0.2)*pow(sun,32.0);
    }
    col += 0.1*vec3(1.0,0.4,0.2)*pow(sun,4.0);
   
   float pt = (90.0-ro.y)/rd.y;
    vec3 bpos = ro + pt*rd;
    float dist = sqrt(distance(ro,bpos));
    float s2p = distance(bpos,lgt*100.);
   
    const float cls = 0.002;
    float bz = fbm(bpos.xz*cls,dist);
    float tot = bz;
    const float stm = .0;
    const float stx = 1.15;
    tot = smoothstep(stm,stx,tot);
    float ds = 2.;
    for (float i=0.;i<=3.;i++)
    {

        vec3 pp = bpos + ds*lgt;
        float v = fbm(pp.xz*cls,dist);
        v = smoothstep(stm,stx,v);
        tot += v;
        #ifndef BASIC_CLOUDS
        ds *= .14*dist;
        #endif
    }

    col = mix(col,vec3(.5,0.5,0.55)*0.2,pow(bz,1.5));
    tot = smoothstep(-7.5,-0.,1.-tot);
    vec3 sccol = mix(vec3(0.11,0.1,0.2),vec3(.2,0.,0.1),smoothstep(0.,900.,s2p));
    col = mix(col,sccol,1.-tot)*1.6;
    vec3 sncol = mix(vec3(1.4,0.3,0.),vec3(1.5,.65,0.),smoothstep(0.,1200.,s2p));
    float sd = pow(sun,10.)+.7;
    col += sncol*bz*bz*bz*tot*tot*tot*sd;
   
    if (wtr) col = mix(col,vec3(0.5,0.7,1.)*0.3,0.4); //make the water blue-er
    return vec4(col,tot);
}
//------------------------------------------------------------------
//-------------------------------Extras-----------------------------
//------------------------------------------------------------------
float bnoise(in vec2 p)
{
    float d = sin(p.x*1.5+sin(p.y*.2))*0.1;
    return d += texture(texture_png,p.xy*0.01+time*0.001).x*0.04;
}

vec3 bump(in vec2 p, in vec3 n, in float t)
{
    vec2 e = vec2(40.,0)/(t*t);
    float n0 = bnoise(p);
    vec3 d = vec3(bnoise(p+e.xy)-n0,2., bnoise(p+e.yx)-n0)/e.x;
    n = normalize(n-d);
    return n;
}
//------------------------------------------------------------------
//------------------------------------------------------------------
void main()
{   
    vec2 bp = gl_FragCoord.xy/iResolution.xy*2.-1.;

    vec2 p  = bp;
   p.x*=iResolution.x/iResolution.y;
   vec2 mo = iMouse.xy / iResolution.xy-.5;
    mo = (mo==vec2(-.5))?mo=vec2(-0.4,-0.15):mo;
   mo.x *= iResolution.x/iResolution.y;
   vec3 ro = vec3(140.,0.,100.);
    vec3 rd = normalize(vec3(p,-2.7));
    rd = rotx(rd,0.15+mo.y*0.4);rd = roty(rd,1.5+mo.x*0.5);
    vec3 brd = rd;
    vec3 col = vec3(0);
      
   float pln = plane(ro, rd, vec3(0.,-4.,0), vec3(1.,0.,0.), vec3(0.0,.0,1.0));
    vec3 ppos = ro + rd*pln;
    bool wtr = false;
    vec3 bm = vec3(0);
    if (pln < 500. && pln > 0.)
    {
        vec3 n = vec3(0,1,0);
        float d= distance(ro,ppos);
        n = bump(ppos.xz,n,d);
        bm = n;
        rd = reflect(rd,n);
        wtr = true;
    }
    vec4 clo = clouds(ro, rd, wtr);
    col = clo.rgb;
   
    vec3 rz = tmarch(ro,brd,350.);
    float px = 3.5/iResolution.y;
    if (rz.x < FAR && (rz.x < pln || pln < 0.))
    {
        vec3 pos = ro + brd*rz.x;
        float dst = distance(pos, ro);
        vec3 nor = normal(pos,dst);
        float nl = clamp(dot(nor,lgt),0.,1.);
        vec3 mcol = vec3(0.04)+vec3(nl)*0.4*vec3(.5,0.35,0.1);
        mcol = mix(mcol,hor,smoothstep(210.,400.,rz.x-(pos.y+18.)*5.));//fogtains
        col = mix(mcol,col,clamp(rz.y/(px*rz.z),0.,1.));
    }
   
    //smooth water edge
    if (wtr && rz.x > pln)col = mix(col,hor*vec3(0.3,0.4,.6)*0.4,smoothstep(10.,200.,pln));
   
    //post
    col = pow(clamp(col,0.0,1.0), vec3(.9));
    col.g *= 0.93;
    //fancy vignetting
    float vgn1 = pow(smoothstep(0.0,.3,(bp.x + 1.)*(bp.y + 1.)*(bp.x - 1.)*(bp.y - 1.)),.5);
    float vgn2 = 1.-pow(dot(vec2(bp.x*.3, bp.y),bp),3.);
    col *= mix(vgn1,vgn2,.4)*.5+0.5;

   gl_FragColor = vec4( col, 1.0 );

}



And does the texture would work with the @ local folder as long it is available inside of it?
« Last Edit: December 13, 2019, 03:34:40 pm by Patrice Terrier »
Patrice
(Always working with the latest Windows version available...)

Michael Lobko-Lobanovsky

  • Administrator
  • *****
  • Posts: 1481
Re: Animated Backgrounds?
« Reply #68 on: December 13, 2019, 03:46:03 pm »
I suspect "I can't get this one to work" is a wrong statement. I think you rather crash your OR when trying to compile this shader. My OR fails to compile it invariably and I don't know the reason why GLSL doesn't show any error messages.

Some statement in the shader code may be semantically incorrect at a point where nVidia GLSL and WebGL 2.0 differ.

The bug isn't related to the texture you're using.
Mike
(3.6GHz Intel Core i5 Quad w/ 16GB RAM, nVidia GTX 1060Ti w/ 6GB VRAM, Windows 7 Ultimate Sp1)

Michael Lobko-Lobanovsky

  • Administrator
  • *****
  • Posts: 1481
Re: Animated Backgrounds?
« Reply #69 on: December 13, 2019, 03:48:11 pm »
The texture you sent above is already available in the \Animated subfolder under the name BWNoise.png.
Mike
(3.6GHz Intel Core i5 Quad w/ 16GB RAM, nVidia GTX 1060Ti w/ 6GB VRAM, Windows 7 Ultimate Sp1)

Michael Lobko-Lobanovsky

  • Administrator
  • *****
  • Posts: 1481
Re: Animated Backgrounds?
« Reply #70 on: December 13, 2019, 07:19:55 pm »
Hey Patrice,

I've just noticed that the mip_/nomip_ feature for shader samplers isn't yet implemented in OR. Thus, if you run the tinyclouds.fs background shader, you'll notice that the darker cloud areas seem dithered. That's because the noise texture used in the shader gets mipmapped when loaded while in fact it shouldn't.

So in the tinyclouds.fs shader code, change the two occurrences of colornoise_png to nomip_colornoise_png, and in shaders.h, change the glsl_LoadShaderFromFile() code as follows:

GLuint glsl_LoadShaderFromFile(IN WCHAR* fName) {
    // Mobj_createGlTextureFromFileEX OUT parms
    BYTE* lpArray = NULL;
    long X = 0, Y = 0;

    BKSHADER* cs = &gP.tCurAniBkgndShader;
    WCHAR wTmp[MAX_PATH] = { 0 };
    GLuint program = 0, vertShader = 0, fragShader = 0;
    char* p = NULL;
    char samplerMap[MAX_PATH] = { 0 };
    BOOL bNoMip = FALSE;

    if (wcsstr(fName, L"\\")) { // PAT: 12-12-2019
        wcscopy(wTmp, fName);
    } else {
        Path_Combine(wTmp, EXEresource(), L"Animated\\");
        Add_Str(wTmp, fName);
    }

    FILE* pFile = _wfopen(wTmp, L"r");
    if (!pFile)
        return 0;

    DWORD fSize = FileSize(wTmp);
    char* fs = (char*)calloc(1, fSize + 1);

    fread(fs, 1, fSize, pFile);
    fclose(pFile);

    vertShader = glsl_CompileShader(GL_VERTEX_SHADER, vsReusable, lstrlenA(vsReusable));
    fragShader = glsl_CompileShader(GL_FRAGMENT_SHADER, fs, lstrlenA(fs));
    free(fs);

    if (vertShader && fragShader) {
        program = cs->nShaderID = glsl_LinkShaders(vertShader, fragShader);
        glDeleteShader(vertShader); // this only marks shaders for deletion; they'll be
        glDeleteShader(fragShader); // deleted together with program in resetCurAniShader()
    }

    // Store shader file name
    wcscpy(cs->shFileName, wTmp); // don't know if we need it yet

    // Locate sampler uniforms and create textures
    for (int idx = 0; idx <= 3; idx++) { // up to 4 2D samplers
        if (cs->samplerName[idx][0]) {
            strcpy(samplerMap, cs->samplerName[idx]);
            cs->samplerLoc[idx] = glGetUniformLocation(program, samplerMap); // still name, not map!
            if (strstr(samplerMap, "nomip_")) {
                strcpy(samplerMap, &samplerMap[6]);
                bNoMip = TRUE;
            }
            else if (strstr(samplerMap, "mip_"))
                strcpy(samplerMap, &samplerMap[4]);

            if (p = strstr(samplerMap, "_")) // replace '_' with '.' to get valid map file name
                *p = '.';
            Path_Combine(wTmp, EXEresource(), L"Animated\\");
            Add_Str(wTmp, cswconv(samplerMap));
            if (Mobj_createGlTextureFromFileEX(wTmp, X, Y, lpArray, TEX_REPEAT)) {
                glGenTextures(1, &(cs->samplerMapID[idx]));
                glBindTexture(GL_TEXTURE_2D, cs->samplerMapID[idx]);

                glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
                glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
                glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
                if (bNoMip) {
                    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
                    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, X, Y, 0, GL_RGBA, GL_UNSIGNED_BYTE, lpArray);
                }
                else {
                    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
                    gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGBA8, X, Y, GL_RGBA, GL_UNSIGNED_BYTE, lpArray);
                }


                glBindTexture(GL_TEXTURE_2D, 0);
                delete[] lpArray; lpArray = NULL;
            }
        }
    }

    // Locate float and vector uniforms (common for all background shaders)
    cs->nTimeLoc = glGetUniformLocation(program, "iTime");
    cs->nResLoc = glGetUniformLocation(program, "iResolution");
    cs->nMouseLoc = glGetUniformLocation(program, "iMouse");
    cs->nDateLoc = glGetUniformLocation(program, "iDate");

    return program;
}


Now if you run this shader, you'll see the darker clouds are crisp and clear and devoid of dither at any main window sizes.
Mike
(3.6GHz Intel Core i5 Quad w/ 16GB RAM, nVidia GTX 1060Ti w/ 6GB VRAM, Windows 7 Ultimate Sp1)

Patrice Terrier

  • Administrator
  • *****
  • Posts: 1903
    • zapsolution
Re: Animated Backgrounds?
« Reply #71 on: December 13, 2019, 07:33:31 pm »
tinicloud.fs

The changes have been done, and it runs perfectly well now, thank you!

Perhaps we could just use a blue color for the shader background, rather than black.
« Last Edit: December 13, 2019, 07:36:26 pm by Patrice Terrier »
Patrice
(Always working with the latest Windows version available...)

Michael Lobko-Lobanovsky

  • Administrator
  • *****
  • Posts: 1481
Re: Animated Backgrounds?
« Reply #72 on: December 13, 2019, 07:45:14 pm »
Perhaps we could just use a blue color for the shader background, rather than black.

I don't quite get you here. Shaders have no background colors in the sense of OpenGL window background color. Every pixel in the viewport is drawn by the shader code in the color calculated as gl_FragColor.
Mike
(3.6GHz Intel Core i5 Quad w/ 16GB RAM, nVidia GTX 1060Ti w/ 6GB VRAM, Windows 7 Ultimate Sp1)

Patrice Terrier

  • Administrator
  • *****
  • Posts: 1903
    • zapsolution
Re: Animated Backgrounds?
« Reply #73 on: December 13, 2019, 07:48:34 pm »
Patrice
(Always working with the latest Windows version available...)

Michael Lobko-Lobanovsky

  • Administrator
  • *****
  • Posts: 1481
Re: Animated Backgrounds?
« Reply #74 on: December 13, 2019, 08:20:07 pm »
No Patrice,

Our code reproduces 100% of what you see at the link you posted regarding the sky colors.

But!

The original shader uses the same weird 32-bit noise texture originally used in the canyon shader and not available anywhere on the net for downloading. I went through a lot of PITA to design my own that would work satisfactorily in canyon.fs but it wouldn't fit in tinyclouds.fs. So I used another color noise texture available on the net and tweaked the d parameter a little to make the cloud shapes more or less veritable.

I don't think we can make our shader any better than that, color-wise.
Mike
(3.6GHz Intel Core i5 Quad w/ 16GB RAM, nVidia GTX 1060Ti w/ 6GB VRAM, Windows 7 Ultimate Sp1)