Example #1
0
        public Color shadeHit(ShadingState state)
        {
            state.getInstance().prepareShadingState(state);
            IShader shader = getShader(state);

            return((shader != null) ? shader.getRadiance(state) : Color.BLACK);
        }
Example #2
0
 public Color lookup(ShadingState state, IShader shader)
 {
     if (state.getNormal() == null)
     {
         return(null);
     }
     // search further
     for (Sample s = first; s != null; s = s.next)
     {
         if (s.i != state.getInstance())
         {
             continue;
         }
         if (s.s != shader)
         {
             continue;
         }
         if (state.getRay().dot(s.dx, s.dy, s.dz) < 0.999f)
         {
             continue;
         }
         if (state.getNormal().dot(s.nx, s.ny, s.nz) < 0.99f)
         {
             continue;
         }
         // we have a match
         hits++;
         return(s.c);
     }
     misses++;
     return(null);
 }
Example #3
0
        void shadePhoton(ShadingState state, Color power)
        {
            state.getInstance().prepareShadingState(state);
            IShader shader = getPhotonShader(state);

            // scatter photon
            if (shader != null)
            {
                shader.scatterPhoton(state, power);
            }
        }
Example #4
0
 private Color lookupShadingCache(ShadingState state, IShader shader)
 {
     lock (lockObj)
     {
         if (state.getNormal() == null)
         {
             return(null);
         }
         cacheLookups++;
         int        cx   = (int)(state.getRasterX() * shadingCacheResolution);
         int        cy   = (int)(state.getRasterY() * shadingCacheResolution);
         int        hash = hashfunc(cx, cy);
         CacheEntry e    = _shadingCache[hash & (_shadingCache.Length - 1)];
         if (e == null)
         {
             cacheEmptyEntryMisses++;
             return(null);
         }
         // entry maps to correct pixel
         if (e.cx == cx && e.cy == cy)
         {
             // search further
             for (Sample s = e.first; s != null; s = s.next)
             {
                 if (s.i != state.getInstance())
                 {
                     continue;
                 }
                 // if (s.prim != state.getPrimitiveID())
                 // continue;
                 if (s.s != shader)
                 {
                     continue;
                 }
                 if (state.getNormal().dot(s.nx, s.ny, s.nz) < 0.95f)
                 {
                     continue;
                 }
                 // we have a match
                 cacheHits++;
                 return(s.c);
             }
         }
         else
         {
             cacheWrongEntryMisses++;
         }
         return(null);
     }
 }
Example #5
0
 private void addShadingCache(ShadingState state, IShader shader, Color c)
 {
     lock (lockObj)
     {
         // don't cache samples with null normals
         if (state.getNormal() == null)
         {
             return;
         }
         cacheEntryAdditions++;
         int        cx = (int)(state.getRasterX() * shadingCacheResolution);
         int        cy = (int)(state.getRasterY() * shadingCacheResolution);
         int        h  = hashfunc(cx, cy) & (_shadingCache.Length - 1);
         CacheEntry e  = _shadingCache[h];
         // new entry ?
         if (e == null)
         {
             e = _shadingCache[h] = new CacheEntry();
         }
         Sample s = new Sample();
         s.i = state.getInstance();
         // s.prim = state.getPrimitiveID();
         s.s  = shader;
         s.c  = c;
         s.nx = state.getNormal().x;
         s.ny = state.getNormal().y;
         s.nz = state.getNormal().z;
         if (e.cx == cx && e.cy == cy)
         {
             // same pixel - just add to the front of the list
             s.next  = e.first;
             e.first = s;
         }
         else
         {
             // different pixel - new list
             e.cx    = cx;
             e.cy    = cy;
             s.next  = null;
             e.first = s;
         }
     }
 }
Example #6
0
        public void add(ShadingState state, IShader shader, Color c)
        {
            if (state.getNormal() == null)
            {
                return;
            }
            depth++;
            Sample s = new Sample();

            s.i    = state.getInstance();
            s.s    = shader;
            s.c    = c;
            s.dx   = state.getRay().dx;
            s.dy   = state.getRay().dy;
            s.dz   = state.getRay().dz;
            s.nx   = state.getNormal().x;
            s.ny   = state.getNormal().y;
            s.nz   = state.getNormal().z;
            s.next = first;
            first  = s;
        }
Example #7
0
 public ShadingState getRadiance(float rx, float ry, int i, Ray r, IntersectionState istate)
 {
     lock (lockObj)
     {
         scene.trace(r, istate);
         if (istate.hit())
         {
             ShadingState state = ShadingState.createState(istate, rx, ry, r, i, this);
             state.getInstance().prepareShadingState(state);
             IShader shader = getShader(state);
             if (shader == null)
             {
                 state.setResult(Color.BLACK);
                 return(state);
             }
             if (_shadingCache != null)
             {
                 Color c = lookupShadingCache(state, shader);
                 if (c != null)
                 {
                     state.setResult(c);
                     return(state);
                 }
             }
             state.setResult(shader.getRadiance(state));
             if (_shadingCache != null)
             {
                 addShadingCache(state, shader, state.getResult());
             }
             return(state);
         }
         else
         {
             return(null);
         }
     }
 }
Example #8
0
 public ShadingState getRadiance(float rx, float ry, float time, int i, int d, Ray r, IntersectionState istate, ShadingCache cache)
 {
     istate.time = time;
     scene.trace(r, istate);
     if (istate.hit())
     {
         ShadingState state = ShadingState.createState(istate, rx, ry, time, r, i, d, this);
         state.getInstance().prepareShadingState(state);
         IShader shader = getShader(state);
         if (shader == null)
         {
             state.setResult(Color.BLACK);
             return(state);
         }
         if (cache != null)
         {
             Color c = cache.lookup(state, shader);
             if (c != null)
             {
                 state.setResult(c);
                 return(state);
             }
         }
         state.setResult(shader.GetRadiance(state));
         if (cache != null)
         {
             cache.add(state, shader, state.getResult());
         }
         checkNanInf(state.getResult());
         return(state);
     }
     else
     {
         return(null);
     }
 }
Example #9
0
 public void prepareShadingState(ShadingState state)
 {
     state.getInstance().prepareShadingState(state);
 }