Writing, compiling and linking a plain shader for Maya Mental Ray.
Intro
Note - I am referring to paths where Maya is installed or where the user's Maya directory is located. Those are the specific paths for my case. In your case, they may be different so you will have to adjust them.
Get Started
Note: you can avoid coping the files, but then you will have to tell to the compile to go and find them.
Shader Declaration
1 . # A plain shader
2 . declare shader
3 . color "plain"
4 . (
5 . color "color",
6 . scalar "brightness", #: default 1.0 min 0.0 softmax 1.0
7 . )
8 . version 1
9 . apply material
10. end declare
1 Use a single # to comment out the line.
2,10 Every shader declaration starts and ends like this.
3 The name and the output of the shader
5,6 All the input attributes of the shader. Need to have the same parameters with the shader structure which we will make later on. For all the types available, refer to the help file. After #: you can specify the default value and the slider range for the attribute editor. Soft min and max mean that a greater or lower values can be typed it and the slider will be adjusted.
8 This is there for mental ray to check that this .mi file match the current version of your source code.
9 There you specify what type of shader it is. If apply is not used, the shader will be a simple texture. With apply material the shader can be assigned to objects, and the node will be under Materials in the HyperShade. For all the types available check the help file.
Shader Source
1 . #include "shader.h"
2 .
3 .
4 . struct plain
5 . {
6 . miColor color;
7 . miScalar brightness;
8 . };
9 .
10. DLLEXPORT int plain_version(void) {return 1;}
11.
12.
13.
14. DLLEXPORT miBoolean plain
15. (
16. miColor *result,
17. miState *state,
18. struct plain *paras
19. )
20. {
21. miColor *color = mi_eval_color(¶s->color);
22. miScalar *brightness = mi_eval_scalar(¶s->brightness);
23.
24.
25. result->r = color->r * (*brightness);
26. result->g = color->g * (*brightness);
27. result->b = color->b * (*brightness);
28.
29. return miTRUE;
30. }
1 shader.h provides all the functionality you will need to write the shader.
4 You need to declare a data structure from each shader. This structure should have the attributes that the shader will have as inputs from Maya's Attribute Editor and must be the same with those that you declared in the .mi file. You can also put the structure declaration in its own .h file and include it in this source.
10 This is the version of your source code which must match the version in the .mi. The DLLEXPORT can be ignored when compiling under Linux.
14 The shader main function, which always takes those three parameters and returns a boolean whether the function call was successful or not. Again ignore the DLLEXPORT if it is Linux.
16 Reference for the output result of the shader.
17 Reference for the current state of the renderer. Out of this structure, you can get all the information you need for the point which is sampled and many more.
18 Reference to a copy of the structure you declared previously, which holds the values of the shader's attributes.
21 This is how you take the attribute values from the shader structure. There is a different function call for each type. Make this call when and only it is needed to save computation time.
25 That's the point where the shader actually does something and modifies the result colour of the sample which is being calculated.
29 The return statement.
Compiling
cl /c /MD /nologo plain.c
link /nologo /DLL /nodefaultlib:LIBC.LIB plain.obj shader.lib
Note: For other compilers or systems, refer to the mentalRay help file)
* Make sure you have added the path of the Visual Studio compiler in your Windows enviroment variables. Then just move the .dll in C:\Program Files\Maya6.0\mentalray\lib and the .mi in C:\Program Files\Maya6.0\mentalray\include directory.
link "{MAYABASE}/lib/plain.{DSO}"
mi "{MAYABASE}/include/plain.mi"
gcc -c -O3 -fPIC -Bsymbolic plain.c
ld -export-dynamic -shared -o plain.so plain.o
Note: For other compilers or systems, refer to the mentalRay help file)
* To load the shader into Maya, there are two approaches depending if you have permissions into the root directory /usr/aw/maya6.0/ of Maya or not.
link "{MAYABASE}/lib/plain.{DSO}"
mi "{MAYABASE}/include/plain.mi"
link "/(full path from root)/mentalShaders/plain.{DSO}"
mi "/(full path from root)/mentalShaders/plain.mi"
MI_CUSTOM_SHADER_PATH = /(full path from root)/mentalshader
MAYA_MRFM_SHOW_CUSTOM_SHADERS = /(full path from root)/mentalShaders
Rendering
gcc -c -O3 -fPIC -Bsymbolic $1.c
ld -export-dynamic -shared -o $1.so $1.o
rm $1.o
End
Note: For any queries or correction, please email me.
Thanks to Sander Van Der Steen for his help.