OGRE.EAT(shader);

Or

Exporting to OGRE

and

Alternatives


Down to business

So it comes down to this: we want that shader in our OGRE application. How do we do that? Luckily, its not a very difficult procedure.

First thing we have to do is put our shader in its own file. But before we do that, we have to make some changes.

  1. You cannot use global variables - everything has to be sent from OGRE.
  2. We cannot use the technique we created inside the file.


First, we’ll make the necessary changed to the file:

float4 mainVS(		float4 		pos : POSITION,
		uniform float4x4 	worldViewProj_m
		) : POSITION
{
	return mul(pos,worldViewProj_m);
}

float4 mainPS() : COLOR 
{
	return float4(1, 0, 0, 1);
}



This is how our new vertex and pixel programs look. You will notice that while the pixel program had not changed, the vertex program had.
In order to use the world-view-projection matrix, we will have to add it to the programs argument list.

You’re probably thinking “why not declare the WVP matrix with the same semantic we used when it was a global?”

Well, both vertex programs and pixel programs have a list of acceptable input and output semantics. Anything not part of this list must be declared as either a uniform or a varying argument.

We declare an input as uniform when we know its value will not change during the executions of the shader. For instance: the WVP matrix will not change while running the program, it will only change after the frame has been rendered, by then the shader is being executed again, with different values.
In practice, the global variables we defined are actually uniform.

Varying input is all input that’s not uniform. In practice, all input flagged with semantics are of varying type. For instance: the position of the vertex can be manipulated while the shader is executed – if it wasn’t, shaders wouldn’t really be useful.

Once you have written this code to file (you can use notepad, I save them with an .hlsl extension, but it doesn’t really matter to OGRE) Remember to put it somewhere your application can find it.

 the interfering
Not satisfied?

Feeling smart?
Read what I just explained with more detail here:
http://msdn.microsoft.com/en-us/library/bb944006%28VS.85%29.aspx#Uniform_Shader_Inputs

The material

One of the best parts about OGRE is the material scripts system. It allow you to quickly change things without recompiling your application.

So how is the material script arranged?
Actually, it’s very much like we’ve done in FXC, with a bit of difference.

First of, we declare the vertex and pixel programs:

vertex_program [name] hlsl
{
	source [your-file-name-here+extension]
	entry_point [function name]
	target vs_1_1
	default_params
	{
		param_named_auto worldViewProj_m worldviewproj_matrix
	}
}

fragment_program [name] hlsl
{
	source [your-file-name-here+extension]
	entry_point [function name]
target ps_2_0
}

Closer look : Creating a program profile within the material

  1. Declaration: Vertex/fragment_program |name| hlsl (The hlsl in the end defines in which language the code is written. can be glsl, cg, asm….)
  2. Source |file name| : the file in which you put the code.
  3. entry_point |program name| : the name of the function this reference will run.
  4. target |name| : same as what comes after compile back in FXC.
  5. default_params : This one is important. Using this part, you can send data from your OGRE application to your shaders. There are four types of default params, two manual, and two automatic.
    1. param_named |param name| |type| |value| |extra params|
    2. param_indexed |param index| |type| |value| |extra params|
    3. param_named_auto |param name| |automatic value| |extra params|
    4. param_indexed_auto |param index| |automatic value| |extra params|


The parameters using name require the name of the input argument as it is written in the function definition.
The parameters using index require the index of the input argument in the function argument list (for instance, wvp in mainVS is the first and only argument).

Special parameters (such as the primary matrices) have an automatic value you can send (very much like using their semantic in FXC). Their name may be somewhat different, make sure you check.

You can look for the corresponding string in the ogre manual ( http://www.ogre3d.org/docs/manual/manual_23.html#SEC125 ) or use an OGRE script editor such as YAOSE that can offer possible candidates.

The technique

Much like the technique we wrote in FXC, we need to create a rendering technique. The syntax is similar to how it written in pure hlsl file (such as in FXC)

material [name]
{
	technique
	{
		pass
		{
			vertex_program_ref [vertex program name]
			{
			}
			fragment_program_ref [pixel program name]
			{
			}
		}
	}
}


The material definition has a few part, some you can easily recognize:

  1. material |name| – you use this name when loading the material within OGRE.
  2. technique
  3. pass
    1. Inside the pass we create references to the vertex and pixel programs we defined earlier. Syntax:
      1. Vertex/fragment_program_ref |name|
    2. For now, we have no need to add anything inside the reference. The reference has an option to send parameters to the program, much like the default param part in the definition. If you use the same program in different passes / techniques / materials and want to send a different value depending on the specific case, you can define that in this part.

The final result

When we combine the two parts, your resulting file will look like this:

vertex_program [give me a name] hlsl
{
	source [your file name here+extension]
	entry_point [function name]
	target vs_1_1
	default_params
	{
		param_named_auto worldViewProj_m worldviewproj_matrix
	}
}
fragment_program [give me a name] hlsl
{
	source [your-file-name-here+extension]
	entry_point [function name]
	target ps_2_0
}

material [give me a name]
{
	technique 
	{
		pass
		{
			vertex_program_ref [vertex program name]
			{
			}
			fragment_program_ref [pixel program name]
			{
			}
		}
	}
}


Now you can load the material to inside your OGRE application using the name you provided. Remember to fill all the missing parts (names) that I have left blank inside [].

Load the material, and run your application.
You will now witness darkness (probably).
If you move around, you will eventually see something like this:
Image

Why?! What is that!?

Remember what your second grade teacher said about mortifications? Ill remind you:
2 * 3 is the same as 3 * 2

Well, when matrices are in play this law no longer applies. We need to know how our system organizes matrices to know in what order to multiply.

I’ll give you a shortcut; remember this:

 REMEMBER
In FXC you multiply the VECTOR by the MATRIX


In OGRE you multiply the MATRIX by the VECTOR


In order to fix our shader to work with OGRE, we need to change

output.Position = mul( input.Position , worldViewProj_m );

To this

output.Position = mul( worldViewProj_m, input.Position );


Now the result will be that useless red material we made in FXC.

 Another interfering tip box
Feeling smart?

Want to know why matrices make your life hard?
http://www.math.umn.edu/~nykamp/m2374/readings/matvecmult/
This page will show you how matrices multiplication works, and why order of the multiplication is important.
Also: read this entry in the OGRE manual:
http://www.ogre3d.org/docs/manual/manual_20.html#SEC116

Using NVIDIA CG as alternative

If you try to run your OGRE application using OpenGL your material will not be loaded, since it’s written in HLSL which is D3D specific.

CG is another shading language developed by NVIDIA capable of compiling against OpenGL and D3D.

The main advantage of using CG for us is that its syntax is identical to HLSL, allowing us to write as we are used to, and have a shader that runs on both systems.

There are a two changes required:

  1. vertex/fragment_program |name| cg – we need to specify that the program is cg format. (switch hlsl appendix to cg)
  2. profiles |first profile| |second profile|… : instead of the keyword target we use the keyword profiles. You can add a list of profiles to use, both D3D and OpenGL.
    1. Note that I use arbvp1 / arbfp1 for OpenGL because I’m an ATI user. If you have an NVIDIA card, you can use one of the NVIDIA specific profiles. They are called vp** or fp** ( ** being the version, such as vp20 or fp30, check which ones you support )


Well that’s it, now you will be able to run the same shader with both OpenGL and D3D. But remember, cg is not HLSL (even if they share the same syntax) and neither is it GLSL. Each one (including CG) has its specifics, though at some cases, CG provides a valid and recommended alternative .

This is how the file looks on my side of the fence.

vertex_program v cg
{
	source bbb.hlsl
	entry_point VertexShaderFunction
	profiles  vs_2_0 arbvp1
	default_params
	{
		param_named_auto   	worldViewProj_m 	worldviewproj_matrix
	}
}
fragment_program p cg
{
	source bbb.hlsl
	entry_point PixelShaderFunction
	profiles ps_2_0 arbfp1
}

material bs
{
	technique
	{
		pass
		{
			vertex_program_ref v
			{	
			}
			fragment_program_ref p
			{
			}
		}
	}
}

A purer alternative

While some people argue that CG = pow(OpenGL + D3D, easy), some will claim that CG = NONE. If you don’t want to use CG as an alternative to HLSL and GLSL, you can use OGRE’s ‘unified’ type program in your script (provided you have another version of your shader in a different language)

Unified allow you to use two or more different programs in one reference, instead of creating an alternative technique just to allow both to run in the same material.

Here’s how it works strait out of OGRE manual:

vertex_program myVertexProgramHLSL hlsl
{
	source prog.hlsl
	entry_point main_vp
	target vs_2_0
}

vertex_program myVertexProgramGLSL glsl
{
	source prog.vert
}

// unified definition
vertex_program myVertexProgram unified
{
	delegate myVertexProgramGLSL
	delegate myVertexProgramHLSL
}

……
// reference works the same, but points to the unified program. OGRE rocks doesn't it?
		pass
		{
			vertex_program_ref myVertexProgram
			{}
……



You can learn more about it in the OGRE manual if you’d like. (Though this is pretty much all there is to it)

<HR>
Creative Commons Copyright -- Some rights reserved.


THE WORK (AS DEFINED BELOW) IS PROVIDED UNDER THE TERMS OF THIS CREATIVE COMMONS PUBLIC LICENSE ("CCPL" OR "LICENSE"). THE WORK IS PROTECTED BY COPYRIGHT AND/OR OTHER APPLICABLE LAW. ANY USE OF THE WORK OTHER THAN AS AUTHORIZED UNDER THIS LICENSE OR COPYRIGHT LAW IS PROHIBITED.

BY EXERCISING ANY RIGHTS TO THE WORK PROVIDED HERE, YOU ACCEPT AND AGREE TO BE BOUND BY THE TERMS OF THIS LICENSE. THE LICENSOR GRANTS YOU THE RIGHTS CONTAINED HERE IN CONSIDERATION OF YOUR ACCEPTANCE OF SUCH TERMS AND CONDITIONS.

1. Definitions

  • "Collective Work" means a work, such as a periodical issue, anthology or encyclopedia, in which the Work in its entirety in unmodified form, along with a number of other contributions, constituting separate and independent works in themselves, are assembled into a collective whole. A work that constitutes a Collective Work will not be considered a Derivative Work (as defined below) for the purposes of this License.
  • "Derivative Work" means a work based upon the Work or upon the Work and other pre-existing works, such as a translation, musical arrangement, dramatization, fictionalization, motion picture version, sound recording, art reproduction, abridgment, condensation, or any other form in which the Work may be recast, transformed, or adapted, except that a work that constitutes a Collective Work will not be considered a Derivative Work for the purpose of this License. For the avoidance of doubt, where the Work is a musical composition or sound recording, the synchronization of the Work in timed-relation with a moving image ("synching") will be considered a Derivative Work for the purpose of this License.
  • "Licensor" means the individual or entity that offers the Work under the terms of this License.
  • "Original Author" means the individual or entity who created the Work.
  • "Work" means the copyrightable work of authorship offered under the terms of this License.
  • "You" means an individual or entity exercising rights under this License who has not previously violated the terms of this License with respect to the Work, or who has received express permission from the Licensor to exercise rights under this License despite a previous violation.
  • "License Elements" means the following high-level license attributes as selected by Licensor and indicated in the title of this License: Attribution, ShareAlike.

2. Fair Use Rights

Nothing in this license is intended to reduce, limit, or restrict any rights arising from fair use, first sale or other limitations on the exclusive rights of the copyright owner under copyright law or other applicable laws.

3. License Grant

Subject to the terms and conditions of this License, Licensor hereby grants You a worldwide, royalty-free, non-exclusive, perpetual (for the duration of the applicable copyright) license to exercise the rights in the Work as stated below:

  • to reproduce the Work, to incorporate the Work into one or more Collective Works, and to reproduce the Work as incorporated in the Collective Works;
  • to create and reproduce Derivative Works;
  • to distribute copies or phonorecords of, display publicly, perform publicly, and perform publicly by means of a digital audio transmission the Work including as incorporated in Collective Works;
  • to distribute copies or phonorecords of, display publicly, perform publicly, and perform publicly by means of a digital audio transmission Derivative Works.
  • For the avoidance of doubt, where the work is a musical composition:
    • Performance Royalties Under Blanket Licenses. Licensor waives the exclusive right to collect, whether individually or via a performance rights society (e.g. ASCAP, BMI, SESAC), royalties for the public performance or public digital performance (e.g. webcast) of the Work.
    • Mechanical Rights and Statutory Royalties. Licensor waives the exclusive right to collect, whether individually or via a music rights society or designated agent (e.g. Harry Fox Agency), royalties for any phonorecord You create from the Work ("cover version") and distribute, subject to the compulsory license created by 17 USC Section 115 of the US Copyright Act (or the equivalent in other jurisdictions).
    • Webcasting Rights and Statutory Royalties. For the avoidance of doubt, where the Work is a sound recording, Licensor waives the exclusive right to collect, whether individually or via a performance-rights society (e.g. SoundExchange), royalties for the public digital performance (e.g. webcast) of the Work, subject to the compulsory license created by 17 USC Section 114 of the US Copyright Act (or the equivalent in other jurisdictions).


The above rights may be exercised in all media and formats whether now known or hereafter devised. The above rights include the right to make such modifications as are technically necessary to exercise the rights in other media and formats. All rights not expressly granted by Licensor are hereby reserved.

4. Restrictions

The license granted in Section 3 above is expressly made subject to and limited by the following restrictions:

  • You may distribute, publicly display, publicly perform, or publicly digitally perform the Work only under the terms of this License, and You must include a copy of, or the Uniform Resource Identifier for, this License with every copy or phonorecord of the Work You distribute, publicly display, publicly perform, or publicly digitally perform. You may not offer or impose any terms on the Work that alter or restrict the terms of this License or the recipients' exercise of the rights granted hereunder. You may not sublicense the Work. You must keep intact all notices that refer to this License and to the disclaimer of warranties. You may not distribute, publicly display, publicly perform, or publicly digitally perform the Work with any technological measures that control access or use of the Work in a manner inconsistent with the terms of this License Agreement. The above applies to the Work as incorporated in a Collective Work, but this does not require the Collective Work apart from the Work itself to be made subject to the terms of this License. If You create a Collective Work, upon notice from any Licensor You must, to the extent practicable, remove from the Collective Work any credit as required by clause 4(c), as requested. If You create a Derivative Work, upon notice from any Licensor You must, to the extent practicable, remove from the Derivative Work any credit as required by clause 4(c), as requested.
  • You may distribute, publicly display, publicly perform, or publicly digitally perform a Derivative Work only under the terms of this License, a later version of this License with the same License Elements as this License, or a Creative Commons iCommons license that contains the same License Elements as this License (e.g. Attribution-ShareAlike 2.5 Japan). You must include a copy of, or the Uniform Resource Identifier for, this License or other license specified in the previous sentence with every copy or phonorecord of each Derivative Work You distribute, publicly display, publicly perform, or publicly digitally perform. You may not offer or impose any terms on the Derivative Works that alter or restrict the terms of this License or the recipients' exercise of the rights granted hereunder, and You must keep intact all notices that refer to this License and to the disclaimer of warranties. You may not distribute, publicly display, publicly perform, or publicly digitally perform the Derivative Work with any technological measures that control access or use of the Work in a manner inconsistent with the terms of this License Agreement. The above applies to the Derivative Work as incorporated in a Collective Work, but this does not require the Collective Work apart from the Derivative Work itself to be made subject to the terms of this License.
  • If you distribute, publicly display, publicly perform, or publicly digitally perform the Work or any Derivative Works or Collective Works, You must keep intact all copyright notices for the Work and provide, reasonable to the medium or means You are utilizing: (i) the name of the Original Author (or pseudonym, if applicable) if supplied, and/or (ii) if the Original Author and/or Licensor designate another party or parties (e.g. a sponsor institute, publishing entity, journal) for attribution in Licensor's copyright notice, terms of service or by other reasonable means, the name of such party or parties; the title of the Work if supplied; to the extent reasonably practicable, the Uniform Resource Identifier, if any, that Licensor specifies to be associated with the Work, unless such URI does not refer to the copyright notice or licensing information for the Work; and in the case of a Derivative Work, a credit identifying the use of the Work in the Derivative Work (e.g., "French translation of the Work by Original Author," or "Screenplay based on original Work by Original Author"). Such credit may be implemented in any reasonable manner; provided, however, that in the case of a Derivative Work or Collective Work, at a minimum such credit will appear where any other comparable authorship credit appears and in a manner at least as prominent as such other comparable authorship credit.

5. Representations, Warranties and Disclaimer

UNLESS OTHERWISE AGREED TO BY THE PARTIES IN WRITING, LICENSOR OFFERS THE WORK AS-IS AND MAKES NO REPRESENTATIONS OR WARRANTIES OF ANY KIND CONCERNING THE MATERIALS, EXPRESS, IMPLIED, STATUTORY OR OTHERWISE, INCLUDING, WITHOUT LIMITATION, WARRANTIES OF TITLE, MERCHANTIBILITY, FITNESS FOR A PARTICULAR PURPOSE, NONINFRINGEMENT, OR THE ABSENCE OF LATENT OR OTHER DEFECTS, ACCURACY, OR THE PRESENCE OF ABSENCE OF ERRORS, WHETHER OR NOT DISCOVERABLE. SOME JURISDICTIONS DO NOT ALLOW THE EXCLUSION OF IMPLIED WARRANTIES, SO SUCH EXCLUSION MAY NOT APPLY TO YOU.

6. Limitation on Liability.

EXCEPT TO THE EXTENT REQUIRED BY APPLICABLE LAW, IN NO EVENT WILL LICENSOR BE LIABLE TO YOU ON ANY LEGAL THEORY FOR ANY SPECIAL, INCIDENTAL, CONSEQUENTIAL, PUNITIVE OR EXEMPLARY DAMAGES ARISING OUT OF THIS LICENSE OR THE USE OF THE WORK, EVEN IF LICENSOR HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.

7. Termination

  • This License and the rights granted hereunder will terminate automatically upon any breach by You of the terms of this License. Individuals or entities who have received Derivative Works or Collective Works from You under this License, however, will not have their licenses terminated provided such individuals or entities remain in full compliance with those licenses. Sections 1, 2, 5, 6, 7, and 8 will survive any termination of this License.
  • Subject to the above terms and conditions, the license granted here is perpetual (for the duration of the applicable copyright in the Work). Notwithstanding the above, Licensor reserves the right to release the Work under different license terms or to stop distributing the Work at any time; provided, however that any such election will not serve to withdraw this License (or any other license that has been, or is required to be, granted under the terms of this License), and this License will continue in full force and effect unless terminated as stated above.

8. Miscellaneous

  • Each time You distribute or publicly digitally perform the Work or a Collective Work, the Licensor offers to the recipient a license to the Work on the same terms and conditions as the license granted to You under this License.
  • Each time You distribute or publicly digitally perform a Derivative Work, Licensor offers to the recipient a license to the original Work on the same terms and conditions as the license granted to You under this License.
  • If any provision of this License is invalid or unenforceable under applicable law, it shall not affect the validity or enforceability of the remainder of the terms of this License, and without further action by the parties to this agreement, such provision shall be reformed to the minimum extent necessary to make such provision valid and enforceable.
  • No term or provision of this License shall be deemed waived and no breach consented to unless such waiver or consent shall be in writing and signed by the party to be charged with such waiver or consent.
  • This License constitutes the entire agreement between the parties with respect to the Work licensed here. There are no understandings, agreements or representations with respect to the Work not specified here. Licensor shall not be bound by any additional provisions that may appear in any communication from You. This License may not be modified without the mutual written agreement of the Licensor and You.