Jump to content

Cook Torrance shader code help pls


kensonuken

Recommended Posts

hey Mario,

I mean to say that when intensity of light increases the specs changes its color so as it gets out of control in certain situations so the specular lobes are very nice but the color of the spec changes. That would be disturbing some times.

I haven't studied the code posted here, but the Cook-Torrance model is usually applied to metallic surfaces, where a color shift in the reflections (or specularity, same thing) is desirable -- I believe it's actually implicit in the Fresnel function. This may or may not be what you're seeing (again, I haven't looked at the code in detail, so I can't say for sure).

If there *is* a parallell-to-perpendicular tint shift going on (either via Fresnel's kr, or any other approximation), then it should be fairly simple to spot and modify -- just trace through all the assignments to all the terms involved in setting the specular/reflected color.

On the other hand, it could be due to some misinterpretation of the model itself (with either the distribution function, the shadowing-and-masking term -- a.k.a "geometry factor", or some stray normalization that was originally meant for the continuous version but stayed around in the discreet versions used in shaders).

Also, if things are simply "blowing up" (as opposed to looking "somewhat wrong"), then first check for division by zero, bad arguments to exp(), null vectors, singular matrices, and so on.

HTH.

Link to comment
Share on other sites

  • 2 weeks later...

On a somewhat related note, you don't need to just use the Beckmann distribution. The distribution function used in the Torrance-Sparrow model, was a Guassian distribution, and in the Cook-Torrance model, the Beckmann distribution, but one of the possible uses for this model is to model the appearance of fibers, via a fiber direction vector (with the Heidrich-Seidel distribution function, meant to be used together with an isotropic specular model, such as Phong, being the anisotropic roughness the Phong exponent).

The RSL code for the Heidrich-Seidel distribution function follows, for those interested (note however that my math knowledge pretty much sucks, so don't blame me if this snippet sets your house on fire or accelerates the heat death of the universe):

float heidrich(
		normal Nf; // surface normal
		vector Hv, Vf, Ln; // half-vector, viewer, and light vector
		vector dir; // anisotropy direction
		float roughness; // anisotropic roughness
				)
{
	vector xdir = normalize( Nf ^ dir );
	float ldott = Ln.xdir;
	float vdott = Vf.xdir;
	float alpha = sqrt( max( 0.0, 1.0 - (ldott * ldott) ) );
	float beta = sqrt( max( 0.0, 1.0 - (vdott * vdott) ) );
	return pow( alpha * beta - (ldott * vdott), roughness);
}

Other possibilities are the Trowbridge-Reitz distribution function, or Bennett-Porteus, and there are some alternatives for the self shadowing functions as well, you might want to check

Microfacet distribution functions

P.S.: anyone implemented the Bennett-Porteus distribution in RSL or VEX? And btw, just starting to learn VEX, and how the hell do you do shader message passing? (i'm thinking about what in RSL would be lightsource("__nondiffuse", nondiff....)

Edited by rendertan
Link to comment
Share on other sites

Hey rendertan,

Thanks for that distribution!

And btw, just starting to learn VEX, and how the hell do you do shader message passing? (i'm thinking about what in RSL would be lightsource("__nondiffuse", nondiff....)

Have a look at the following functions:

limport() -- import from light shaders

simport() -- import from surface shaders

dimport() -- import from displacement shaders

shimport() -- import from shadow shaders

And related:

The rayimport() function together with the "send:<name>" syntax in variadic args for the tracing functions (like gather(), reflectlight(), refractlight(), etc) gives you equivalent functionality to PRMan's "ray labels". Details on this mechanism are available in the on-line help for the gather() construct, but I've found it also works for the other functions (though it's not documented at the moment).

HTH.

Link to comment
Share on other sites

Hi Mario

Thanks a lot for the information on the message passing, it was just what i needed, so back to the docs and the "VEX playground" ;)

I'll post some results here soon.

Thanks once again.

limport() -- import from light shaders

simport() -- import from surface shaders

dimport() -- import from displacement shaders

shimport() -- import from shadow shaders

And related:

The rayimport() function together with the "send:<name>" syntax in variadic args for the tracing functions (like gather(), reflectlight(), refractlight(), etc) gives you equivalent functionality to PRMan's "ray labels". Details on this mechanism are available in the on-line help for the gather() construct, but I've found it also works for the other functions (though it's not documented at the moment).

HTH.

Link to comment
Share on other sites

  • 2 weeks later...

Hi

Can someone explain me a thing? I've been reading some papers (Neumann's "Constructions of BRDFs", as well as Kelemen's and Ashikhmin's papers, all this regarding microfacet based BRDFs).

I noticed that in one of the Cook-Torrance implementations here, the fresnel call is inside the illuminance loop. Which would only make sense if it was light dependent, and indeed, the light vector is passed. On the other implementation (the shading language guide), the fresnel call is still inside the illuminance loop, however it's only viewer and surface normal dependent, which doesn't makes much sense. I mean, if this is the case, you could move the fresnel call outside the illuminance loop, and avoid having it run for each light source, just multiplying it by the cook term outside the illuminance loop. This of course, if the fresnel call is indeed just dependent on surface normal and viewer vector.

However one of the implementations has the light vector and surface normal being passed, wich would justify fresnel inside the illuminance loop.

In Neumann's paper, when he describes the Cook-Torrance model, he says that the fresnel term is between the normal of the microfacet selected with the distribution function, and the incident angle relative to this microfacet normal. In Kelemen's paper, when describing the Cook-Torrance model, he mentions the fresnel term being dependent on the beta angle, to quote "

Edited by rendertan
Link to comment
Share on other sites

In Neumann's paper, when he describes the Cook-Torrance model, he says that the fresnel term is between the normal of the microfacet selected with the distribution function, and the incident angle relative to this microfacet normal. In Kelemen's paper, when describing the Cook-Torrance model, he mentions the fresnel term being dependent on the beta angle, to quote "
Link to comment
Share on other sites

Hi Mario

Yeah, that makes sense, that fresnel call in the illuminance loop, with Nf, Vf, was puzzling me, wasn't making much sense. Back to the papers, playing with distributions & shadowing terms ;)

Thanks a lot once again.

Hey Luis,

I would be inclined to agree with them (Neumann, Kelemen, Ashikhmin) :)

As I understand it, reflectance (I mean the amount of reflected light after absorption etc. i.e: kr) is dependent on the angle of incidence (of the light). The only time when you can, for convenience, replace the light's incidence direction with the viewing direction, is when you're doing mirror reflection/transmission, where both happen to be the same (in terms of their angle with N). So I'd say that in general, Fresnel depends on the angle of incidence (and therefore the direction to the light).

IMHO, that is.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...