many shaders Questions

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.

many shaders Questions

Postby Granyte » Wed Dec 14, 2011 8:33 pm

first i have a very simple issue my custom written shader is drawn over a transparent cloud layer that should be around it why is that Should i output depth with my shader or is it even simpler
Image
PS i use my custom shader on a sphere wich is inside an other sphere on wich i aplied the default irrlicht transparent material


also is there a way with irricht to apply a pixel shader to an image and save the result? cause rightnow i'm running many operations that could have been done only once in real time
would something like drawing on a screen aligned quad and rendering in a depth buffer work?
Granyte
 
Posts: 520
Joined: Tue Jan 25, 2011 11:07 pm

Re: many shaders Questions

Postby Granyte » Thu Dec 15, 2011 3:10 am

the first issue is fixed ijust had to change the base material now is it possible to some how use pixel shader on a texture and save the result?


and why does irrlicht react wierd when you have a shader on a transparent material and an other shader behind it

the only blend mode that seem to work properly is alpha ref for all the others there is no transparency

Image

Uploaded with ImageShack.us

this is the result i get when using EMT_TRANSPARENT_ALPHA_CHANNEL

using EMT_TRANSPARENT_ALPHA_CHANNEL_REF put a hole in the texture

i can manipulate the alpha output of the shader code as i wish but no mather what i put in there there is no blending happening
Granyte
 
Posts: 520
Joined: Tue Jan 25, 2011 11:07 pm

Re: many shaders Questions

Postby Granyte » Thu Dec 15, 2011 9:31 am

fix in progress i have no idea what was really wrong but when messing around i aparently fixed it and could not see it because some tweaking i did to my shader were over saturating the channels


EDIT actualy what "fixed my issue was enabling the blend operation flag but i have no idea why
Last edited by Granyte on Thu Dec 15, 2011 10:16 am, edited 1 time in total.
Granyte
 
Posts: 520
Joined: Tue Jan 25, 2011 11:07 pm

Re: many shaders Questions

Postby Klunk » Thu Dec 15, 2011 9:55 am

i asked the same question a couple of weeks ago, seems noone had an answer, hey ho.

http://irrlicht.sourceforge.net/forum/viewtopic.php?f=1&t=45238

you could try setting material.ZWriteEnable =false; i don't know if it works in all situations though.
Klunk
 
Posts: 232
Joined: Mon Jan 10, 2011 5:21 pm

Re: many shaders Questions

Postby Granyte » Thu Dec 15, 2011 10:47 am

aparently in HLSL that fix is not working i had to use the blend operation flag and that makes it give wierd result
Granyte
 
Posts: 520
Joined: Tue Jan 25, 2011 11:07 pm

Re: many shaders Questions

Postby hybrid » Thu Dec 15, 2011 1:25 pm

So it seems you're using SVN/trunk? There's some bug with the blend modes in d3d, indeed. Seems like some states are not properly set/reset. Will be fixed in the final release.
hybrid
Admin
 
Posts: 13972
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany

Re: many shaders Questions

Postby Granyte » Thu Dec 15, 2011 8:18 pm

thanks good to know ill use the blend operation until it's fixed i doubt i would have my shader fixed soon enough for the diference in blend modes to mather


any idea on how to use shaders on a texture and save the result?
something like rendering a shader to a screen quad and saving the result to a RT Texture?
how would that even bee done i found no example on how to use a screen quad (except some cleaverly hidden withing post processing frameworks)
Granyte
 
Posts: 520
Joined: Tue Jan 25, 2011 11:07 pm

Re: many shaders Questions

Postby hendu » Fri Dec 16, 2011 11:09 am

Well that's exactly it. Copy the screen quad from such a framework, or do a search, some of those have been posted to the forum too.
hendu
 
Posts: 1619
Joined: Sat Dec 18, 2010 12:53 pm

Re: many shaders Questions

Postby Granyte » Sat Dec 17, 2011 7:36 pm

Image

Image
Image

now that you saw the result you know there is no need to be a genius to understand there is an issue ethier with my shader code ethier with the screen quad code

i used the screen quad from mel post processing exemple with a little methode added for convinient acces to shader material

cpp Code: Select all
class CScreenQuadSceneNode : public scene::ISceneNode{
      core::aabbox3df aabb;               //An axis aligned bounding box. Actually not needed.
      video::SMaterial material;            //The material used to render the Scene Node
       video::S3DVertex2TCoords vertices[4];    //The vertices of the Scene Node.
                                    //Normally we wouldn't need more
                                    //than one set of UV coordinates.
                                    //But if we are to use the builtin materials, this is necesary
 
public:
       CScreenQuadSceneNode::CScreenQuadSceneNode(scene::ISceneNode* parent, scene::ISceneManager* mgr, s32 id)
       :ISceneNode(parent,mgr,id)
       {
               
          f32 shiftX,shiftY;
          core::dimension2d<u32> currentResolution;
 
/**Here we initialize the vertices of the screen Aligned quad*/
 
          currentResolution = mgr->getVideoDriver()->getScreenSize();
 
          aabb.reset(0,0,0);
 
          shiftX = 0.5/currentResolution.Width;   //This small shift is necesary to compensate the texture sampling bias
          shiftY = 0.5/currentResolution.Height;   //It avoids that our effect becomes too blurry.
 
         vertices[0] = video::S3DVertex2TCoords(
                  -1.0f,-1.0f,0.0f,
                  0.0f,0.0f,-1.0f,
                  video::SColor(255,255,255,255),
                  shiftX,1+shiftY,
                  shiftX,1+shiftY);
 
         vertices[1] = video::S3DVertex2TCoords(
                  1.0f,-1.0,0.0f,
                  0.0f,0.0f,-1.0f,
                  video::SColor(255,255,255,255),
                  1.0f+shiftX,1+shiftY,
                  1.0f+shiftX,1+shiftY);
 
         vertices[2] = video::S3DVertex2TCoords(
                  -1.0f,1.0,0.0f,
                  0.0f,0.0f,-1.0f,
                  video::SColor(255,255,255,255),
                  shiftX,shiftY,
                  shiftX,shiftY);
 
         vertices[3] = video::S3DVertex2TCoords(
                  1.0f,1.0f,0.0f,
                  0.0f,0.0f,-1.0f,
                  video::SColor(255,255,255,255),
                  1.0f+shiftX,shiftY,
                  1.0f+shiftX,shiftY);
 
/**Now we proceed to initialize the appropriate settings for the material we are going to use
We can alter these later, but for the time being, initializing then here will do no harm*/

 
         material.Lighting = false;                     //No need for lighting.
         material.MaterialType = video::EMT_SOLID;   //This will add both first and second textures :)
         material.BackfaceCulling=false;                  //not needed, but simplifies things
         setAutomaticCulling(scene::EAC_OFF);            //We don't need this scene
                                                //node to be culled because we render it in screen space.
      }
 
       CScreenQuadSceneNode::~CScreenQuadSceneNode()
       {
      }
 
      const core::aabbox3df& CScreenQuadSceneNode::getBoundingBox() const
      {
         return aabb;
      }
 
       void CScreenQuadSceneNode::OnRegisterSceneNode()
       {
         //This method is empty because it is best for us to render this scene node manually.
         //So, it is never really rendered on its own, if we don't tell it to do so.
      }
           void CScreenQuadSceneNode::ChangeMaterialType(video::E_MATERIAL_TYPE NewMat)
           {
                   material.MaterialType= NewMat;
           }
       void CScreenQuadSceneNode::render()
       {
         video::IVideoDriver* drv = getSceneManager()->getVideoDriver();
         core::matrix4 proj;
          u16 indices[] = {0,1,2,3,1,2};
          //A triangle list
 
         drv->setMaterial(material);
 
         drv->setTransform(video::ETS_PROJECTION, core::IdentityMatrix);
         drv->setTransform(video::ETS_VIEW, core::IdentityMatrix);
         drv->setTransform(video::ETS_WORLD, core::IdentityMatrix);
 
         drv->drawIndexedTriangleList(&vertices[0],4,&indices[0],2);
 
      }
 
       u32 CScreenQuadSceneNode::getMaterialCount()
       {
         return 1;   //There is only one material
      }
 
      video::SMaterial& CScreenQuadSceneNode::getMaterial(irr::u32 i)
      {
         return material;//We always return the same material, so there is no need for more.
      }
 
};


and here is the shader code

cpp Code: Select all
// part of the Irrlicht Engine Shader example.
// These simple Direct3D9 pixel and vertex shaders will be loaded by the shaders
// example. Please note that these example shaders don't do anything really useful.
// They only demonstrate that shaders can be used in Irrlicht.
 
//-----------------------------------------------------------------------------
// Global variables
//-----------------------------------------------------------------------------
float4x4 mWorldViewProj;  // World * View * Projection transformation
float4x4 mInvWorld;       // Inverted world matrix
float4x4 mTransWorld;     // Transposed world matrix
float3 mLightPos;         // Light position
float4 mLightColor;       // Light color
float3 mNodecenter;
float mTimer;
 
static int permutation[] ={ 151,160,137,91,90,15,
   131,13,201,95,96,53,194,233,7,225,140,36,103,30,69,142,8,99,37,240,21,10,23,
   190, 6,148,247,120,234,75,0,26,197,62,94,252,219,203,117,35,11,32,57,177,33,
   88,237,149,56,87,174,20,125,136,171,168, 68,175,74,165,71,134,139,48,27,166,
   77,146,158,231,83,111,229,122,60,211,133,230,220,105,92,41,55,46,245,40,244,
   102,143,54, 65,25,63,161, 1,216,80,73,209,76,132,187,208, 89,18,169,200,196,
   135,130,116,188,159,86,164,100,109,198,173,186, 3,64,52,217,226,250,124,123,
   5,202,38,147,118,126,255,82,85,212,207,206,59,227,47,16,58,17,182,189,28,42,
   223,183,170,213,119,248,152, 2,44,154,163, 70,221,153,101,155,167, 43,172,9,
   129,22,39,253, 19,98,108,110,79,113,224,232,178,185, 112,104,218,246,97,228,
   251,34,242,193,238,210,144,12,191,179,162,241, 81,51,145,235,249,14,239,107,
   49,192,214, 31,181,199,106,157,184, 84,204,176,115,121,50,45,127, 4,150,254,
   138,236,205,93,222,114,67,29,24,72,243,141,128,195,78,66,215,61,156,180};
 
   // Generate permutation and gradient textures using CPU runtime
 
texture permTexture
 
<
 
  string texturetype = "2D";
 
  string format = "BBAAABBA";
 
  string function = "GeneratePermTexture";
 
  int width = 256, height = 1;
>;
 
float4 GeneratePermTexture(float p : POSITION) : COLOR
 
{
 
  return permutation[p * 256.0f] / 255.0f;
 
}
 
sampler permSampler = sampler_state
 
{
 
  texture = <permTexture>;
 
  AddressU  = Wrap;
 
  AddressV  = Wrap;//= Clamp;
 
  MAGFILTER = LINEAR;
 
  MINFILTER = LINEAR;
 
  MIPFILTER = LINEAR;
 
};
 
 
 
// gradients for 3D noise
 
static float3 g[] = {
 
  1,1,0,    -1,1,0,    1,-1,0,    -1,-1,0,
 
  1,0,1,    -1,0,1,    1,0,-1,    -1,0,-1,
 
  0,1,1,    0,-1,1,    0,1,-1,    0,-1,-1,
 
  1,1,0,    0,-1,1,    -1,1,0,    0,-1,-1,
 
};
 
 
 
texture gradTexture
 
<
 
  string texturetype = "2D";
 
  string format = "u8v8w8q8";
 
  string function = "GenerateGradTexture";
 
  int width = 16, height = 1;
 
 
 
>;
 
 
 
float3 GenerateGradTexture(float p : POSITION) : COLOR
 
{
 
  return g[p * 16];
 
}
 
 
 
sampler gradSampler = sampler_state
 
{
 
  texture = <gradTexture>;
 
  AddressU  = Wrap;
 
  AddressV  = Wrap;// Clamp;
 
  MAGFILTER = LINEAR;
 
  MINFILTER = LINEAR;
 
  MIPFILTER = LINEAR;
 
};
 
 
float3 fade(float3 t)
 
{
 
  return t * t * t * (t * (t * 6 - 15) + 10); // new curve
 
 // return t * t * (3 - 2 * t); // old curve
 
}
 
 
 
float perm(float x)
 
{
 
  return tex1D(permSampler, x / 256.0) * 256;
 
}
 
 
 
float grad(float x, float3 p)
 
{
 
  return dot(tex1D(gradSampler, x), p);
 
}
 
 
 
// 3D version
 
float inoise(float3 p)
 
{
 
  float3 P =float3(p.x,p.y,p.z);
  P= fmod(floor(P), 256.0);
 
  p -= floor(p);
 
  float3 f = fade(p);
 
 
 
  // HASH COORDINATES FOR 6 OF THE 8 CUBE CORNERS
 
 
 
  float A = perm(P.x) + P.y;
 
  float AA = perm(A) + P.z;
 
  float AB = perm(A + 1) + P.z;
 
  float B =  perm(P.x + 1) + P.y;
 
  float BA = perm(B) + P.z;
 
  float BB = perm(B + 1) + P.z;
 
 
 
  // AND ADD BLENDED RESULTS FROM 8 CORNERS OF CUBE
 
 
 
  return lerp(
 
    lerp(lerp(grad(perm(AA), p),
 
              grad(perm(BA), p + float3(-1, 0, 0)), f.x),
 
         lerp(grad(perm(AB), p + float3(0, -1, 0)),
 
              grad(perm(BB), p + float3(-1, -1, 0)), f.x), f.y),
 
    lerp(lerp(grad(perm(AA + 1), p + float3(0, 0, -1)),
 
              grad(perm(BA + 1), p + float3(-1, 0, -1)), f.x),
 
         lerp(grad(perm(AB + 1), p + float3(0, -1, -1)),
 
              grad(perm(BB + 1), p + float3(-1, -1, -1)), f.x), f.y),
 
    f.z);
 
}
 
// Vertex shader output structure
struct VS_OUTPUT
{
        float4 Position   : POSITION;   // vertex position
        //float4 Diffuse    : COLOR0;     // vertex diffuse color
        float3 TexCoord   : TEXCOORD0;  // tex coords
        //float3 normal   : TEXCOORD2;
};
 
 
VS_OUTPUT vertexMain( in float4 vPosition : POSITION,
                      in float3 vNormal   : NORMAL,
                      float2 texCoord     : TEXCOORD0
                                         )
{
        VS_OUTPUT Output;
       
       
        VS_OUTPUT output;
        float3 offset = float3(0.5/512.0f, 0.5/512.0f,0.5/512.0f);
        Output.Position = float4(vPosition.xy, 0.0, 1.0);
        Output.TexCoord = float3(vPosition.wxy);//+offset;
        return Output;
}
 
float AbsNoise(float3 pos)
{
        //return (inoise(pos);
    return abs(inoise(pos));
}
 
float FBMNoise(float3 pos)
{    
        float value = 0.0f;    
        float frequency = 20.0f;    
        float amplitude = 2.020f;
        int Octaves = 5;    
        // Classic FBM loop    
        for ( int i=0; i < Octaves; i++ )    
        {
                 float noiseValue = AbsNoise(pos*frequency);        
                         value += amplitude * noiseValue;
                         
                         frequency *= 0.072f;        
                         amplitude *= 0.5f;    
        }    
        return value;
}
 
 
 
// Pixel shader output structure
struct PS_OUTPUT
{
    float4 RGBColor : COLOR0;  // Pixel color    
};
 
 
PS_OUTPUT pixelMain( float4 Position   : POSITION,   // vertex position
                                        // float4 Diffuse    : COLOR0,     // vertex diffuse color
                                         float3 TexCoord   : TEXCOORD0  // tex coords
                                         )
{
        float scale = 10.0f;
 
    float2 c[9] = {
            float2(-0.0078125, 0.0078125),
            float2( 0.00 ,     0.0078125),
            float2( 0.0078125, 0.0078125),
            float2(-0.0078125, 0.00 ),
            float2( 0.0,       0.0),
            float2( 0.0078125, 0.007 ),
            float2(-0.0078125,-0.0078125),
            float2( 0.00 ,    -0.0078125),
            float2( 0.0078125,-0.0078125),
    };
        PS_OUTPUT Output;/*
        float4 col,col2,map,colfinal;
       
        float3 acol[9];    
    int i;
        for (i=0; i < 9; i++) {
      acol[i] = inoise(TexCoord.xyz);
    }
        float3 rgb2lum = float3(0.30, 0.59, 0.11);
   
    float lum[9];
    for (i = 0; i < 9; i++) {
      lum[i] = dot(acol[i].xyz, rgb2lum);
    }
       
 
        float x = lum[2]+  lum[8]+2*lum[5]-lum[0]-2*lum[3]-lum[6];
        float y = lum[6]+2*lum[7]+  lum[8]-lum[0]-2*lum[1]-lum[2];
        float edge =(x*x + y*y);*/

         
        // multiply with diffuse and do other senseless operations
        /*float height = 0;
        int i;
        for (i = 0; i < 8; i++)
        {
                        float frequency = pow(0.8f,i);//This increases the frequency with every loop of the octave.
            float amplitude = pow(0.75f,i);//This decreases the amplitude with every loop of the octave.
 
        height =height+ inoise( float3(TexCoord.x*frequency*i,TexCoord.y*frequency*i,TexCoord.z*frequency*i)*256.0f)* amplitude;
        }*/

        float height = FBMNoise(float3(TexCoord.x,TexCoord.y,TexCoord.z));
        Output.RGBColor =float4(height,height,height,height); // abs(float4(height,height,height,height));
       
 
        return Output ;
}


now after spending the night googling i found that it could be MAGFILTER = LINEAR; MINFILTER = LINEAR; MIPFILTER = LINEAR; wich was originaly at some other value
or the screen quad that's wrong wich is why i asked in the first place i knew that copying a screen quad without knowing what is going on would not do me any good
Granyte
 
Posts: 520
Joined: Tue Jan 25, 2011 11:07 pm

Re: many shaders Questions

Postby Granyte » Sun Dec 18, 2011 7:12 am

Additional set of questions is there a way in irrlicht to have the child of a node not follow the rotation but only the movement of it's parent?


and an other good one how would one do to coppy the content of a Render target to a normal texture and push that texture update back to the gpu and doing so every frame? unless there is a way to have the RTT not erased every frame wich i doubt
EDIT: i found the answer to the second one


the perlin shader issue stil stands how eve
Granyte
 
Posts: 520
Joined: Tue Jan 25, 2011 11:07 pm


Return to Beginners Help

Who is online

Users browsing this forum: No registered users and 1 guest