Package org.sunflow.core
Class ShadingState
- java.lang.Object
-
- org.sunflow.core.ShadingState
-
- All Implemented Interfaces:
java.lang.Iterable<LightSample>
public final class ShadingState extends java.lang.Object implements java.lang.Iterable<LightSample>
Represents a point to be shaded and provides various options for the shading of this point, including spawning of new rays.
-
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddSample(LightSample sample)Add the specified light sample to the list of lights to be usedColordiffuse(Color diff)Computes a plain diffuse response to the current light samples and global illumination.voidfaceforward()Flip the surface normals to ensure they are facing the current ray.OrthoNormalBasisgetBasis()Gets the local orthonormal basis for the current hit point.Matrix4getCameraToWorld()Get a transformation matrix that will transform camera space points into world space.floatgetCosND()Cosine between the shading normal and the ray.intgetDepth()Get the current total tracing depth.intgetDiffuseDepth()Get the current diffuse tracing depth.Vector3getGeoNormal()Gets the geometric normal of the current hit point.ColorgetGlobalRadiance()Returns the glboal diffuse radiance estimate given by the currentGIEngineif present.InstancegetInstance()Get the instance which was intersectedColorgetIrradiance(Color diffuseReflectance)Gets the total irradiance reaching the current point from diffuse surfaces.Vector3getNormal()Get shading normal at the hit point.Point3getPoint()Get hit point.intgetPrimitiveID()Get the primitive ID which was intersecteddoublegetRandom(int j, int dim)Get a QMC sample from an infinite sequence.doublegetRandom(int j, int dim, int n)Get a QMC sample from a finite sequence of n elements.floatgetRasterX()Get x coordinate of the pixel being shaded.floatgetRasterY()Get y coordinate of the pixel being shaded.RaygetRay()Gets the ray that is associated with this state.intgetReflectionDepth()Get the current reflection tracing depth.intgetRefractionDepth()Get the current refraction tracing depth.ColorgetResult()Get the result of shading this pointShadergetShader()Get the shader to be used to shade this surface.intgetShadowDepth()booleangetTrianglePoints(Point3[] p)Get the three triangle corners in object space if the hit object is a mesh, returns false otherwise.floatgetU()Get u barycentric coordinate of the intersection point.Point2getUV()Get texture coordinates at the hit point.floatgetV()Get v barycentric coordinate of the intersection point.floatgetW()Get w barycentric coordinate of the intersection point.Matrix4getWorldToCamera()Get a transformation matrix that will transform world space points into camera space.booleanincludeLights()Checks to see if the shader should include emitted light.booleanincludeSpecular()Checks to see if the shader should include specular terms.voidinit()Create objects needed for surface shading: point, normal, texture coordinates and basis.voidinitCausticSamples()Add caustic samples to the current light sample set.voidinitLightSamples()Initialize the use of light samples.booleanisBehind()Returns true if the ray hit the surface from behind.java.util.Iterator<LightSample>iterator()Allows iteration over current light samples.Colorocclusion(int samples, float maxDist)Simple black and white ambient occlusion.Colorocclusion(int samples, float maxDist, Color bright, Color dark)Ambient occlusion routine, returns a value between bright and dark depending on the amount of geometric occlusion in the scene.voidsetBasis(OrthoNormalBasis basis)Define the orthonormal basis for the current hit point.voidsetModifier(Modifier modifier)Record which modifier should be applied to the intersected surfacevoidsetShader(Shader shader)Record which shader should be executed for the intersected surface.Colorshade()Run the shader at this surface point.ColorspecularPhong(Color spec, float power, int numRays)Computes a phong specular response to the current light samples and global illumination.voidstorePhoton(Vector3 dir, Color power, Color diffuse)Records a photon at the specified location.voidtraceDiffusePhoton(Ray r, Color power)Trace a new photon from the current location.ShadingStatetraceFinalGather(Ray r, int i)Trace a final gather ray and return the intersection result as a new render stateColortraceGlossy(Ray r, int i)Returns the color obtained by recursively tracing the specified ray.ColortraceReflection(Ray r, int i)Returns the color obtained by recursively tracing the specified ray.voidtraceReflectionPhoton(Ray r, Color power)Trace a new photon from the current location.ColortraceRefraction(Ray r, int i)Returns the color obtained by recursively tracing the specified ray.voidtraceRefractionPhoton(Ray r, Color power)Trace a new photon from the current location.ColortraceShadow(Ray r)Trace a shadow ray against the scene, and computes the accumulated opacity along the ray.ColortraceTransparency()Trace transparency, this is equivalent to tracing a refraction ray in the incoming ray direction.ColortraceTransparentShadow(float oldMaxT)Vector3transformNormalObjectToWorld(Vector3 n)Transform the given normal from object space to world space.Vector3transformNormalWorldToObject(Vector3 n)Transform the given normal from world space to object space.Point3transformObjectToWorld(Point3 p)Transform the given point from object space to world space.Vector3transformVectorObjectToWorld(Vector3 v)Transform the given vector from object space to world space.Vector3transformVectorWorldToObject(Vector3 v)Transform the given vector from world space to object space.Point3transformWorldToObject(Point3 p)Transform the given point from world space to object space.
-
-
-
Method Detail
-
init
public final void init()
Create objects needed for surface shading: point, normal, texture coordinates and basis.
-
shade
public final Color shade()
Run the shader at this surface point.- Returns:
- shaded result
-
faceforward
public final void faceforward()
Flip the surface normals to ensure they are facing the current ray. This method also offsets the shading point away from the surface so that new rays will not intersect the same surface again by mistake.
-
getRasterX
public final float getRasterX()
Get x coordinate of the pixel being shaded.- Returns:
- pixel x coordinate
-
getRasterY
public final float getRasterY()
Get y coordinate of the pixel being shaded.- Returns:
- pixel y coordinate
-
getCosND
public final float getCosND()
Cosine between the shading normal and the ray. This is set byfaceforward().- Returns:
- cosine between shading normal and the ray
-
isBehind
public final boolean isBehind()
Returns true if the ray hit the surface from behind. This is set byfaceforward().- Returns:
trueif the surface was hit from behind.
-
getU
public final float getU()
Get u barycentric coordinate of the intersection point.- Returns:
- u barycentric coordinate
-
getV
public final float getV()
Get v barycentric coordinate of the intersection point.- Returns:
- v barycentric coordinate
-
getW
public final float getW()
Get w barycentric coordinate of the intersection point.- Returns:
- w barycentric coordinate
-
getInstance
public final Instance getInstance()
Get the instance which was intersected- Returns:
- intersected instance object
-
getPrimitiveID
public final int getPrimitiveID()
Get the primitive ID which was intersected- Returns:
- intersected primitive ID
-
transformObjectToWorld
public Point3 transformObjectToWorld(Point3 p)
Transform the given point from object space to world space. A newPoint3object is returned.- Parameters:
p- object space position to transform- Returns:
- transformed position
-
transformWorldToObject
public Point3 transformWorldToObject(Point3 p)
Transform the given point from world space to object space. A newPoint3object is returned.- Parameters:
p- world space position to transform- Returns:
- transformed position
-
transformNormalObjectToWorld
public Vector3 transformNormalObjectToWorld(Vector3 n)
Transform the given normal from object space to world space. A newVector3object is returned.- Parameters:
n- object space normal to transform- Returns:
- transformed normal
-
transformNormalWorldToObject
public Vector3 transformNormalWorldToObject(Vector3 n)
Transform the given normal from world space to object space. A newVector3object is returned.- Parameters:
n- world space normal to transform- Returns:
- transformed normal
-
transformVectorObjectToWorld
public Vector3 transformVectorObjectToWorld(Vector3 v)
Transform the given vector from object space to world space. A newVector3object is returned.- Parameters:
v- object space vector to transform- Returns:
- transformed vector
-
transformVectorWorldToObject
public Vector3 transformVectorWorldToObject(Vector3 v)
Transform the given vector from world space to object space. A newVector3object is returned.- Parameters:
v- world space vector to transform- Returns:
- transformed vector
-
getResult
public final Color getResult()
Get the result of shading this point- Returns:
- shaded result
-
addSample
public final void addSample(LightSample sample)
Add the specified light sample to the list of lights to be used- Parameters:
sample- a valid light sample
-
getRandom
public final double getRandom(int j, int dim)Get a QMC sample from an infinite sequence.- Parameters:
j- sample number (starts from 0)dim- dimension to sample- Returns:
- pseudo-random value in [0,1)
-
getRandom
public final double getRandom(int j, int dim, int n)Get a QMC sample from a finite sequence of n elements. This provides better stratification than the infinite version, but does not allow for adaptive sampling.- Parameters:
j- sample number (starts from 0)dim- dimension to samplen- number of samples- Returns:
- pseudo-random value in [0,1)
-
includeLights
public final boolean includeLights()
Checks to see if the shader should include emitted light.- Returns:
trueif emitted light should be included,falseotherwise
-
includeSpecular
public final boolean includeSpecular()
Checks to see if the shader should include specular terms.- Returns:
trueif specular terms should be included,falseotherwise
-
getShader
public final Shader getShader()
Get the shader to be used to shade this surface.- Returns:
- shader to be used
-
setShader
public final void setShader(Shader shader)
Record which shader should be executed for the intersected surface.- Parameters:
shader- surface shader to use to shade the current intersection point
-
setModifier
public final void setModifier(Modifier modifier)
Record which modifier should be applied to the intersected surface- Parameters:
modifier- modifier to use the change this shading state
-
getDepth
public final int getDepth()
Get the current total tracing depth. First generation rays have a depth of 0.- Returns:
- current tracing depth
-
getDiffuseDepth
public final int getDiffuseDepth()
Get the current diffuse tracing depth. This is the number of diffuse surfaces reflected from.- Returns:
- current diffuse tracing depth
-
getReflectionDepth
public final int getReflectionDepth()
Get the current reflection tracing depth. This is the number of specular surfaces reflected from.- Returns:
- current reflection tracing depth
-
getRefractionDepth
public final int getRefractionDepth()
Get the current refraction tracing depth. This is the number of specular surfaces refracted from.- Returns:
- current refraction tracing depth
-
getPoint
public final Point3 getPoint()
Get hit point.- Returns:
- hit point
-
getNormal
public final Vector3 getNormal()
Get shading normal at the hit point. This may differ from the geometric normal- Returns:
- shading normal
-
getUV
public final Point2 getUV()
Get texture coordinates at the hit point.- Returns:
- texture coordinate
-
getGeoNormal
public final Vector3 getGeoNormal()
Gets the geometric normal of the current hit point.- Returns:
- geometric normal of the current hit point
-
getBasis
public final OrthoNormalBasis getBasis()
Gets the local orthonormal basis for the current hit point.- Returns:
- local basis or
nullif undefined
-
setBasis
public final void setBasis(OrthoNormalBasis basis)
Define the orthonormal basis for the current hit point.- Parameters:
basis-
-
getRay
public final Ray getRay()
Gets the ray that is associated with this state.- Returns:
- ray associated with this state.
-
getCameraToWorld
public final Matrix4 getCameraToWorld()
Get a transformation matrix that will transform camera space points into world space.- Returns:
- camera to world transform
-
getWorldToCamera
public final Matrix4 getWorldToCamera()
Get a transformation matrix that will transform world space points into camera space.- Returns:
- world to camera transform
-
getTrianglePoints
public final boolean getTrianglePoints(Point3[] p)
Get the three triangle corners in object space if the hit object is a mesh, returns false otherwise.- Parameters:
p- array of 3 points- Returns:
trueif the points were read succesfully,falseotherwise
-
initLightSamples
public final void initLightSamples()
Initialize the use of light samples. Prepares a list of visible lights from the current point.
-
initCausticSamples
public final void initCausticSamples()
Add caustic samples to the current light sample set. This method does nothing if caustics are not enabled.
-
traceGlossy
public final Color traceGlossy(Ray r, int i)
Returns the color obtained by recursively tracing the specified ray. The reflection is assumed to be glossy.- Parameters:
r- ray to tracei- instance number of this sample- Returns:
- color observed along specified ray.
-
traceReflection
public final Color traceReflection(Ray r, int i)
Returns the color obtained by recursively tracing the specified ray. The reflection is assumed to be specular.- Parameters:
r- ray to tracei- instance number of this sample- Returns:
- color observed along specified ray.
-
traceRefraction
public final Color traceRefraction(Ray r, int i)
Returns the color obtained by recursively tracing the specified ray.- Parameters:
r- ray to tracei- instance number of this sample- Returns:
- color observed along specified ray.
-
traceTransparency
public final Color traceTransparency()
Trace transparency, this is equivalent to tracing a refraction ray in the incoming ray direction.- Returns:
- color observed behind the current shading point
-
traceShadow
public final Color traceShadow(Ray r)
Trace a shadow ray against the scene, and computes the accumulated opacity along the ray.- Parameters:
r- ray to trace- Returns:
- opacity along the shadow ray
-
storePhoton
public final void storePhoton(Vector3 dir, Color power, Color diffuse)
Records a photon at the specified location.- Parameters:
dir- incoming direction of the photonpower- photon powerdiffuse- diffuse reflectance at the given point
-
traceReflectionPhoton
public final void traceReflectionPhoton(Ray r, Color power)
Trace a new photon from the current location. This assumes that the photon was reflected by a specular surface.- Parameters:
r- ray to trace photon alongpower- power of the new photon
-
traceRefractionPhoton
public final void traceRefractionPhoton(Ray r, Color power)
Trace a new photon from the current location. This assumes that the photon was refracted by a specular surface.- Parameters:
r- ray to trace photon alongpower- power of the new photon
-
traceDiffusePhoton
public final void traceDiffusePhoton(Ray r, Color power)
Trace a new photon from the current location. This assumes that the photon was reflected by a diffuse surface.- Parameters:
r- ray to trace photon alongpower- power of the new photon
-
getGlobalRadiance
public final Color getGlobalRadiance()
Returns the glboal diffuse radiance estimate given by the currentGIEngineif present.- Returns:
- global diffuse radiance estimate
-
getIrradiance
public final Color getIrradiance(Color diffuseReflectance)
Gets the total irradiance reaching the current point from diffuse surfaces.- Parameters:
diffuseReflectance- diffuse reflectance at the current point, can be used for importance tracking- Returns:
- indirect diffuse irradiance reaching the point
-
traceFinalGather
public final ShadingState traceFinalGather(Ray r, int i)
Trace a final gather ray and return the intersection result as a new render state- Parameters:
r- ray to shooti- instance of the ray- Returns:
- new render state object corresponding to the intersection result
-
occlusion
public final Color occlusion(int samples, float maxDist)
Simple black and white ambient occlusion.- Parameters:
samples- number of sample raysmaxDist- maximum length of the rays- Returns:
- occlusion color
-
occlusion
public final Color occlusion(int samples, float maxDist, Color bright, Color dark)
Ambient occlusion routine, returns a value between bright and dark depending on the amount of geometric occlusion in the scene.- Parameters:
samples- number of sample raysmaxDist- maximum length of the raysbright- color when nothing is occludeddark- color when fully occluded- Returns:
- occlusion color
-
diffuse
public final Color diffuse(Color diff)
Computes a plain diffuse response to the current light samples and global illumination.- Parameters:
diff- diffuse color- Returns:
- shaded result
-
specularPhong
public final Color specularPhong(Color spec, float power, int numRays)
Computes a phong specular response to the current light samples and global illumination.- Parameters:
spec- specular colorpower- phong exponentnumRays- number of glossy rays to trace- Returns:
- shaded color
-
iterator
public java.util.Iterator<LightSample> iterator()
Allows iteration over current light samples.- Specified by:
iteratorin interfacejava.lang.Iterable<LightSample>
-
getShadowDepth
public final int getShadowDepth()
-
traceTransparentShadow
public Color traceTransparentShadow(float oldMaxT)
-
-