Skip to main content
History: HLSL
View published page
Source of version: 7
(current)
__HLSL__ - High Level Shader Language. This is a shader language developed by Microsoft for use with ((-DirectX|DirectX)) and is very similar to ((-Cg|Cg)). {WIKIPEDIA()/} !!Using HLSL in OGRE HLSL shaders are used inside Ogre much like the other shading languages as Cg and ((-GLSL|GLSL)), but there are some little differences that I will try to explain here. First, the material definition will be much like this: {CODE(wrap="1", colors="c++")}material Test13/RockWall { technique { pass { vertex_program_ref Deferred_nm_vs { } fragment_program_ref Deferred_nm_ps { } texture_unit { // sampler s0 // ... } texture_unit { // sampler s1 // ... } } } }{CODE} __vertex_program_ref__ refers to a vertex program definition and __fragment_program_ref__ refers to a fragment program definition. Those can be defined in __.program__ scripts or inside the __.material__ script itself. These define the name of the program text file, the destination profile (vs/ps version) and default parameters. {CODE(wrap="1", colors="c++")}vertex_program Deferred_vs hlsl { source Deferred_vs.hlsl target vs_1_1 entry_point main default_params { param_named_auto worldView worldview_matrix param_named_auto worldViewProj worldviewproj_matrix } } fragment_program Deferred_ps hlsl { source Deferred_ps.hlsl target ps_2_0 entry_point main default_params { param_named specularity float 0.0 } }{CODE} * __source__ defines the program source code name. * __entry_point__ is the name of the shader function that is called for each vertex/fragment. This is usually __main__. * __target__ signifies the Vertex Shaders or Pixel Shaders version to compile for. For fragment_program this can be ps_1_1, ps_1_4, ps_2_0, ps_2_x, ps_3_0 or ps_3_x (for more information have a look at [http://www.ogre3d.org/docs/manual/manual_18.html#SEC89|Declaring Vertex and Fragment Programs]. For vertex_program it can be vs_1_1, vs_2_0, vs_2_x or vs_3_0. Always try to use the lowest version that can do what you need, as this is usually faster and supports more cards. * __default_params__ define default parameters for this shader. These can be overidden in the __vertex_program_ref__ and __fragment_program_ref__ blocks. !!Parameters Named parameters can be defined at global scope in the HLSL program: {CODE(wrap="1", colors="c++")}float4x4 worldViewProj; float4x4 world; float4x4 worldView;{CODE} Ogre will make sure these parameters will contain the values assigned to them via __param_named__ or __param_named_auto__ in the program or material script. !!Samplers and texture units Inside .hlsl fragment programs, you can define samplers using a special syntax, so that Ogre knows which texture unit the sampler refers to: {CODE(wrap="1", colors="c++")}sampler Tex0: register(s0); sampler Tex1: register(s1);{CODE} __register(s0)__ refers to the first __texture_unit__ block, __register(s1)__ to the second one and so on. !!Vertex shaders A minimal vertex shader that receives the POSITION, NORMAL and TEXCOORD0 vertex attributes and outputs POSITION and TEXCOORD0 to the fragment shader would look like this: {CODE(wrap="1", colors="c++")}struct VS_OUTPUT { float4 pos: POSITION; float2 texCoord0: TEXCOORD0; }; float4x4 worldViewProj; VS_OUTPUT main( float4 Pos: POSITION, float3 normal: NORMAL, float2 texCoord0: TEXCOORD0 ){ VS_OUTPUT Out; Out.pos = mul(worldViewProj, Pos); Out.texCoord0 = texCoord0; return Out; }{CODE} !!Fragment shaders A minimal fragment shader that samples a texture at the incoming texture coordinate and outputs the sampled COLOR: {CODE(wrap="1", colors="c++")}sampler Tex0: register(s0); float4 main(float4 texCoord0: TEXCOORD0): COLOR0 { return tex2D(Tex0, texCoord0); }{CODE} !!Tangent vectors If you need tangent vectors in your HLSL shaders, you can ask OGRE to provide them. However, you will have to use the semantic TEXCOORD for that. So the input structure for your vertex shader may look like this: {CODE(wrap="1", colors="c++")}struct a2v { float4 position : POSITION0; float3 normal : NORMAL; float2 tex : TEXCOORD0; float3 tangent : TEXCOORD1; };{CODE} To make sure OGRE can give you these tangents, either tell the exporter you use for your models to generate them, or tell OGRE to generate them by using the following code: {CODE(wrap="1", colors="c++")}unsigned short src, dest; if (!pMesh->suggestTangentVectorBuildParams(Ogre::VES_TANGENT, src, dest)) { pMesh->buildTangentVectors(Ogre::VES_TANGENT, src, dest); }{CODE} __Note:__ * Since OGRE [http://1.6.0|1.6.0] this is done automatically. * OGRE does not generate any binormals. If you need them, you can calculate them yourself by simply taking the cross product of the tangent and the normal in the vertex shader. !!Links [http://www.ogre3d.org/docs/manual/manual_20.html#SEC76|OGRE manual: Declaring Vertex and Fragment programs] [http://msdn.microsoft.com/library/default.asp?url=/library/en-us/directx9_c/directx/graphics/reference/hlslreference/hlslreference.asp|Microsoft HLSL reference]
Search by Tags
Search Wiki by Freetags
Latest Changes
Compiled API Reference
Overlay Editor
Introduction - JaJDoo Shader Guide - Basics
RT Shader System
RapidXML Dotscene Loader
One Function Ogre
One Function Ogre
...more
Search
Find
Online Users
118 online users
OGRE Wiki
Support and community documentation for Ogre3D
Ogre Forums
ogre3d.org
Log in
Username
Password
CapsLock is on.
Remember me (for 1 year)
Log in