programming objectspace-normal mentalray shader


#1

Hello

From an animation i’d like to render a pass that shows the final normals (calculated from object and bumpmapping and so on) in object-space.

As i couldn’t find a way to do this in the software, i tried to write a mentalray shader for this.

the shadercode seems to work in a simple testscene, but not in the one i want to use it with.

This is my shadercode:

struct mrNormalOutputShader_params {
	miBoolean normalize;
	miBoolean mapTo01;
	miBoolean flipYZ;
};

extern "C" DLLEXPORT int mrNormalOutputShader_version(void) {return(2);}

extern "C" DLLEXPORT miBoolean mrNormalOutputShader(
	miColor		*result,
	miState		*state,
	struct mrNormalOutputShader_params *paras)
{

	miVector internalNormal = state->normal;
	miVector objectSpaceNormal;
	mi_normal_to_object(state,&objectSpaceNormal,&internalNormal);

	if(paras->normalize){
		mi_vector_normalize(&objectSpaceNormal);
	}

	if(paras->flipYZ){
		miScalar tmpy = objectSpaceNormal.y;
		objectSpaceNormal.y = objectSpaceNormal.z;
		objectSpaceNormal.z = -tmpy;
	}

	if(paras->mapTo01){
		objectSpaceNormal.x+=1.0;
		objectSpaceNormal.x*=0.5;
		objectSpaceNormal.y+=1.0;
		objectSpaceNormal.y*=0.5;
		objectSpaceNormal.z+=1.0;
		objectSpaceNormal.z*=0.5;
	}

	result->a = 1.0;
	result->r = objectSpaceNormal.x;
	result->g = objectSpaceNormal.y;
	result->b = objectSpaceNormal.z;

	return(miTRUE);
}

I use this shader in a “mr Shader Element” render element to output a objectspace-normals pass. the direction of the output normals seems to be right qualitatively, but theyr ammount is wrong - as if some gammacorrection was applied to the values (but i did try to set different Gamma/LUT values, this only affected the visual appearance on screen, not the actual pixelvalues)

So, this is what I get:


http://neo.cycovery.com/normal_shader_problem.jpg

in the middle top you see the normalpass output, with a pixel of the left wall testet (this wall is aligned to the world coordinate system and has no transformation applied to it). i would expect 0.5 in the green and blue channel, but i get the value 0.217

Even more strange: if i hit the “clone rendered Frame Window” button, then i get the result in the bottom right corner, where the pixelvalues deviate even more (there green and blue of the left wall have the value 0.035)

I don’t see what’s wrong in my shadercode… anyone can spot an error or knows how i could get that rendered correctly?


#2

it must be connected to gamma-correction somehow, as 0.5 ^ 2.2 = 0.217

and when i clone the window, turns out that 0.217 ^ 2.2 = 0.035

why is there a gamma of 2.2 applied to the values in the framebuffer? (and why is it applied again when i clone the window??)

i get exactly the same results (value wise, on the screen it will fairly look different) independant of Gamma/LUT settings

this is confusing me.


#3

I know nothing about c and MR shaders programming but the logic of your shader looks right (for me)).
The only thing i want to mention is your mapto01 func will work right only if normals have been already normalized. But you have separate check boxes for these ops.


#4

And could you please explain why do you expect left wall normal to be [1,0.5,0.5]. For me it should be [1,0,0] ?

EDIT: got it - mapto01 func)


#5

This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.